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) && |
| 482 | (entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || |
| 483 | entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_ERASER); |
| 484 | } |
| 485 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 486 | // Determines if the given window can be targeted as InputTarget::FLAG_FOREGROUND. |
| 487 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 488 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 489 | // be sent to such a window, but it is not a foreground event and doesn't use |
| 490 | // InputTarget::FLAG_FOREGROUND. |
| 491 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 492 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 493 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 494 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 495 | } |
| 496 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 497 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, int32_t pid, int32_t uid) { |
| 498 | if (windowHandle == nullptr) { |
| 499 | return false; |
| 500 | } |
| 501 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 502 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 503 | return true; |
| 504 | } |
| 505 | return false; |
| 506 | } |
| 507 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 508 | // Checks targeted injection using the window's owner's uid. |
| 509 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 510 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 511 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 512 | const EventEntry& entry) { |
| 513 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 514 | // The event was not injected, or the injected event does not target a window. |
| 515 | return {}; |
| 516 | } |
| 517 | const int32_t uid = *entry.injectionState->targetUid; |
| 518 | if (window == nullptr) { |
| 519 | return StringPrintf("No valid window target for injection into uid %d.", uid); |
| 520 | } |
| 521 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
| 522 | return StringPrintf("Injected event targeted at uid %d would be dispatched to window '%s' " |
| 523 | "owned by uid %d.", |
| 524 | uid, window->getName().c_str(), window->getInfo()->ownerUid); |
| 525 | } |
| 526 | return {}; |
| 527 | } |
| 528 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 529 | Point resolveTouchedPosition(const MotionEntry& entry) { |
| 530 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 531 | // Always dispatch mouse events to cursor position. |
| 532 | if (isFromMouse) { |
| 533 | return Point(static_cast<int32_t>(entry.xCursorPosition), |
| 534 | static_cast<int32_t>(entry.yCursorPosition)); |
| 535 | } |
| 536 | |
| 537 | const int32_t pointerIndex = getMotionEventActionPointerIndex(entry.action); |
| 538 | return Point(static_cast<int32_t>( |
| 539 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)), |
| 540 | static_cast<int32_t>( |
| 541 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y))); |
| 542 | } |
| 543 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 544 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 545 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 546 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 547 | return keyEntry.downTime; |
| 548 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 549 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 550 | return motionEntry.downTime; |
| 551 | } |
| 552 | return std::nullopt; |
| 553 | } |
| 554 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 555 | } // namespace |
| 556 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 557 | // --- InputDispatcher --- |
| 558 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 559 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 560 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} |
| 561 | |
| 562 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy, |
| 563 | std::chrono::nanoseconds staleEventTimeout) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 564 | : mPolicy(policy), |
| 565 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 566 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 567 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 568 | mAppSwitchSawKeyDown(false), |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 569 | mAppSwitchDueTime(LLONG_MAX), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 570 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 571 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 572 | mDispatchEnabled(false), |
| 573 | mDispatchFrozen(false), |
| 574 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 575 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 576 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 577 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 578 | mStaleEventTimeout(staleEventTimeout), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 579 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 580 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 581 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 582 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 583 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 584 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 585 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 586 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 587 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 588 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 589 | policy->getDispatcherConfiguration(&mConfig); |
| 590 | } |
| 591 | |
| 592 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 593 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 594 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 595 | resetKeyRepeatLocked(); |
| 596 | releasePendingEventLocked(); |
| 597 | drainInboundQueueLocked(); |
| 598 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 599 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 600 | while (!mConnectionsByToken.empty()) { |
| 601 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 602 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 603 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 607 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 608 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 609 | return ALREADY_EXISTS; |
| 610 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 611 | mThread = std::make_unique<InputThread>( |
| 612 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 613 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 617 | if (mThread && mThread->isCallingThread()) { |
| 618 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 619 | return INVALID_OPERATION; |
| 620 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 621 | mThread.reset(); |
| 622 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 625 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 626 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 627 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 628 | std::scoped_lock _l(mLock); |
| 629 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 630 | |
| 631 | // Run a dispatch loop if there are no pending commands. |
| 632 | // The dispatch loop might enqueue commands to run afterwards. |
| 633 | if (!haveCommandsLocked()) { |
| 634 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 635 | } |
| 636 | |
| 637 | // Run all pending commands if there are any. |
| 638 | // 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] | 639 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 640 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 641 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 642 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 643 | // If we are still waiting for ack on some events, |
| 644 | // we might have to wake up earlier to check if an app is anr'ing. |
| 645 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 646 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 647 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 648 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 649 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 650 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 651 | mDispatcherEnteredIdle.notify_all(); |
| 652 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 653 | } // release lock |
| 654 | |
| 655 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 656 | nsecs_t currentTime = now(); |
| 657 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 658 | mLooper->pollOnce(timeoutMillis); |
| 659 | } |
| 660 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 661 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 662 | * Raise ANR if there is no focused window. |
| 663 | * Before the ANR is raised, do a final state check: |
| 664 | * 1. The currently focused application must be the same one we are waiting for. |
| 665 | * 2. Ensure we still don't have a focused window. |
| 666 | */ |
| 667 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 668 | // Check if the application that we are waiting for is still focused. |
| 669 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 670 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 671 | if (focusedApplication == nullptr || |
| 672 | focusedApplication->getApplicationToken() != |
| 673 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 674 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 675 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 676 | focusedApplication->getName().c_str()); |
| 677 | return; // The focused application has changed. |
| 678 | } |
| 679 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 680 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 681 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 682 | if (focusedWindowHandle != nullptr) { |
| 683 | return; // We now have a focused window. No need for ANR. |
| 684 | } |
| 685 | onAnrLocked(mAwaitedFocusedApplication); |
| 686 | } |
| 687 | |
| 688 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 689 | * Check if any of the connections' wait queues have events that are too old. |
| 690 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 691 | * Return the time at which we should wake up next. |
| 692 | */ |
| 693 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 694 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 695 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 696 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 697 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 698 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 699 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 700 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 701 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 702 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 703 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 704 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 705 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | // Check if any connection ANRs are due |
| 710 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 711 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 712 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 713 | } |
| 714 | |
| 715 | // If we reached here, we have an unresponsive connection. |
| 716 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 717 | if (connection == nullptr) { |
| 718 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 719 | return nextAnrCheck; |
| 720 | } |
| 721 | connection->responsive = false; |
| 722 | // Stop waking up for this unresponsive connection |
| 723 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 724 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 725 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 728 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
| 729 | const sp<Connection>& connection) { |
| 730 | if (connection->monitor) { |
| 731 | return mMonitorDispatchingTimeout; |
| 732 | } |
| 733 | const sp<WindowInfoHandle> window = |
| 734 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 735 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 736 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 737 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 738 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 741 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 742 | nsecs_t currentTime = now(); |
| 743 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 744 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 745 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 746 | // repeat if the device is just coming out of sleep. |
| 747 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 748 | resetKeyRepeatLocked(); |
| 749 | } |
| 750 | |
| 751 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 752 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 753 | if (DEBUG_FOCUS) { |
| 754 | ALOGD("Dispatch frozen. Waiting some more."); |
| 755 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 756 | return; |
| 757 | } |
| 758 | |
| 759 | // Optimize latency of app switches. |
| 760 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 761 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 762 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 763 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 764 | *nextWakeupTime = mAppSwitchDueTime; |
| 765 | } |
| 766 | |
| 767 | // Ready to start a new event. |
| 768 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 769 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 770 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 771 | if (isAppSwitchDue) { |
| 772 | // The inbound queue is empty so the app switch key we were waiting |
| 773 | // for will never arrive. Stop waiting for it. |
| 774 | resetPendingAppSwitchLocked(false); |
| 775 | isAppSwitchDue = false; |
| 776 | } |
| 777 | |
| 778 | // Synthesize a key repeat if appropriate. |
| 779 | if (mKeyRepeatState.lastKeyEntry) { |
| 780 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 781 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 782 | } else { |
| 783 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 784 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | // Nothing to do if there is no pending event. |
| 790 | if (!mPendingEvent) { |
| 791 | return; |
| 792 | } |
| 793 | } else { |
| 794 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 795 | mPendingEvent = mInboundQueue.front(); |
| 796 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 797 | traceInboundQueueLengthLocked(); |
| 798 | } |
| 799 | |
| 800 | // Poke user activity for this event. |
| 801 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 802 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 803 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | // Now we have an event to dispatch. |
| 807 | // 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] | 808 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 809 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 810 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 811 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 812 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 813 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 814 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 818 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 822 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 823 | const ConfigurationChangedEntry& typedEntry = |
| 824 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 825 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 826 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 827 | break; |
| 828 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 829 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 830 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 831 | const DeviceResetEntry& typedEntry = |
| 832 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 833 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 834 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 835 | break; |
| 836 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 837 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 838 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 839 | std::shared_ptr<FocusEntry> typedEntry = |
| 840 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 841 | dispatchFocusLocked(currentTime, typedEntry); |
| 842 | done = true; |
| 843 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 844 | break; |
| 845 | } |
| 846 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 847 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 848 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 849 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 850 | done = true; |
| 851 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 852 | break; |
| 853 | } |
| 854 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 855 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 856 | const auto typedEntry = |
| 857 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 858 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 859 | done = true; |
| 860 | break; |
| 861 | } |
| 862 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 863 | case EventEntry::Type::DRAG: { |
| 864 | std::shared_ptr<DragEntry> typedEntry = |
| 865 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 866 | dispatchDragLocked(currentTime, typedEntry); |
| 867 | done = true; |
| 868 | break; |
| 869 | } |
| 870 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 871 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 872 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 873 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 874 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 875 | resetPendingAppSwitchLocked(true); |
| 876 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 877 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 878 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 879 | } |
| 880 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 881 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 882 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 883 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 884 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 885 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 886 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 887 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 888 | break; |
| 889 | } |
| 890 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 891 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 892 | std::shared_ptr<MotionEntry> motionEntry = |
| 893 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 894 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 895 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 896 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 897 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 898 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 899 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 900 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 901 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 902 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 903 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 904 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 905 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 906 | |
| 907 | case EventEntry::Type::SENSOR: { |
| 908 | std::shared_ptr<SensorEntry> sensorEntry = |
| 909 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 910 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 911 | dropReason = DropReason::APP_SWITCH; |
| 912 | } |
| 913 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 914 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 915 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 916 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 917 | dropReason = DropReason::STALE; |
| 918 | } |
| 919 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 920 | done = true; |
| 921 | break; |
| 922 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 926 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 927 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 928 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 929 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 930 | |
| 931 | releasePendingEventLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 932 | *nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 936 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 937 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 938 | } |
| 939 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 940 | /** |
| 941 | * Return true if the events preceding this incoming motion event should be dropped |
| 942 | * Return false otherwise (the default behaviour) |
| 943 | */ |
| 944 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 945 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 946 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 947 | |
| 948 | // Optimize case where the current application is unresponsive and the user |
| 949 | // decides to touch a window in a different application. |
| 950 | // If the application takes too long to catch up then we drop all events preceding |
| 951 | // the touch into the other window. |
| 952 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 953 | const int32_t displayId = motionEntry.displayId; |
| 954 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 955 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 956 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 957 | sp<WindowInfoHandle> touchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 958 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 959 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 960 | touchedWindowHandle->getApplicationToken() != |
| 961 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 962 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 963 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 964 | "for %s", |
| 965 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 966 | return true; |
| 967 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 968 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 969 | // Alternatively, maybe there's a spy window that could handle this event. |
| 970 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 971 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 972 | for (const auto& windowHandle : touchedSpies) { |
| 973 | const sp<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 974 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 975 | // This spy window could take more input. Drop all events preceding this |
| 976 | // 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] | 977 | 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] | 978 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 979 | mAwaitedFocusedApplication->getName().c_str()); |
| 980 | return true; |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 986 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 987 | // events to be processed before dispatching the key event. This is because these motion events |
| 988 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 989 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 990 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 991 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 992 | "just send the pending key event to the focused window."); |
| 993 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 994 | } |
| 995 | return false; |
| 996 | } |
| 997 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 998 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 999 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1000 | mInboundQueue.push_back(std::move(newEntry)); |
| 1001 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1002 | traceInboundQueueLengthLocked(); |
| 1003 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1004 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1005 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1006 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1007 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1008 | // Optimize app switch latency. |
| 1009 | // If the application takes too long to catch up then we drop all events preceding |
| 1010 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1011 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1012 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1013 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1014 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1015 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1016 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1017 | if (DEBUG_APP_SWITCH) { |
| 1018 | ALOGD("App switch is pending!"); |
| 1019 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1020 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1021 | mAppSwitchSawKeyDown = false; |
| 1022 | needWake = true; |
| 1023 | } |
| 1024 | } |
| 1025 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1026 | |
| 1027 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1028 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1029 | // time for it may have been handled in the policy and could be dropped. |
| 1030 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1031 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1032 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1033 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1034 | pendingKey.interceptKeyResult == |
| 1035 | KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1036 | pendingKey.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1037 | pendingKey.interceptKeyWakeupTime = 0; |
| 1038 | needWake = true; |
| 1039 | } |
| 1040 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1041 | break; |
| 1042 | } |
| 1043 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1044 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1045 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1046 | "Unexpected untrusted event."); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1047 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1048 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1049 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1051 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1052 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1053 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1054 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1055 | break; |
| 1056 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1057 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1058 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1059 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1060 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1061 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1062 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1063 | // nothing to do |
| 1064 | break; |
| 1065 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | return needWake; |
| 1069 | } |
| 1070 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1071 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1072 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1073 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1074 | mRecentQueue.push_back(entry); |
| 1075 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1076 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1077 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1081 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1082 | int32_t y, TouchState* touchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1083 | bool isStylus, |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1084 | bool addOutsideTargets, |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 1085 | bool ignoreDragWindow) const { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1086 | if (addOutsideTargets && touchState == nullptr) { |
| 1087 | 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] | 1088 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1089 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1090 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1091 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1092 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1093 | continue; |
| 1094 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1095 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1096 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1097 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1098 | return windowHandle; |
| 1099 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1100 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1101 | if (addOutsideTargets && |
| 1102 | info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1103 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1104 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1105 | } |
| 1106 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1107 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1110 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1111 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1112 | // Traverse windows from front to back and gather the touched spy windows. |
| 1113 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1114 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1115 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1116 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1117 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1118 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1119 | continue; |
| 1120 | } |
| 1121 | if (!info.isSpy()) { |
| 1122 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1123 | return spyWindows; |
| 1124 | } |
| 1125 | spyWindows.push_back(windowHandle); |
| 1126 | } |
| 1127 | return spyWindows; |
| 1128 | } |
| 1129 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1130 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1131 | const char* reason; |
| 1132 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1133 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1134 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1135 | ALOGD("Dropped event because policy consumed it."); |
| 1136 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1137 | reason = "inbound event was dropped because the policy consumed it"; |
| 1138 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1139 | case DropReason::DISABLED: |
| 1140 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1141 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1142 | } |
| 1143 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1144 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1145 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1146 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1147 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1148 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1149 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1150 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1151 | "has started interacting with a different application."); |
| 1152 | reason = "inbound event was dropped because the current application is not responding " |
| 1153 | "and the user has started interacting with a different application"; |
| 1154 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1155 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1156 | ALOGI("Dropped event because it is stale."); |
| 1157 | reason = "inbound event was dropped because it is stale"; |
| 1158 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1159 | case DropReason::NO_POINTER_CAPTURE: |
| 1160 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1161 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1162 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1163 | case DropReason::NOT_DROPPED: { |
| 1164 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1165 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1166 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1169 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1170 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1171 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1172 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1173 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1174 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1175 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1176 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1177 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1178 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1179 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1180 | } else { |
| 1181 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1182 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1183 | } |
| 1184 | break; |
| 1185 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1186 | case EventEntry::Type::SENSOR: { |
| 1187 | break; |
| 1188 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1189 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1190 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1191 | break; |
| 1192 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1193 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1194 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1195 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1196 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1197 | 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] | 1198 | break; |
| 1199 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1203 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1204 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1205 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1206 | } |
| 1207 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1208 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1209 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1210 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1211 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | bool InputDispatcher::isAppSwitchPendingLocked() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1215 | return mAppSwitchDueTime != LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1219 | mAppSwitchDueTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1220 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1221 | if (DEBUG_APP_SWITCH) { |
| 1222 | if (handled) { |
| 1223 | ALOGD("App switch has arrived."); |
| 1224 | } else { |
| 1225 | ALOGD("App switch was abandoned."); |
| 1226 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1227 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1228 | } |
| 1229 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1231 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1234 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1235 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1236 | return false; |
| 1237 | } |
| 1238 | |
| 1239 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1240 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1241 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1242 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1243 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1244 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1245 | return true; |
| 1246 | } |
| 1247 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1248 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1249 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1253 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1254 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1255 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1256 | releaseInboundEventLocked(entry); |
| 1257 | } |
| 1258 | traceInboundQueueLengthLocked(); |
| 1259 | } |
| 1260 | |
| 1261 | void InputDispatcher::releasePendingEventLocked() { |
| 1262 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1263 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1264 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1268 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1269 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1270 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1271 | if (DEBUG_DISPATCH_CYCLE) { |
| 1272 | ALOGD("Injected inbound event was dropped."); |
| 1273 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1274 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1275 | } |
| 1276 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1277 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1278 | } |
| 1279 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1283 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1284 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1288 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1289 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1290 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1291 | uint32_t policyFlags = entry->policyFlags & |
| 1292 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1293 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1294 | std::shared_ptr<KeyEntry> newEntry = |
| 1295 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1296 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1297 | entry->flags, entry->keyCode, entry->scanCode, |
| 1298 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1299 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1300 | newEntry->syntheticRepeat = true; |
| 1301 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1302 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1303 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1304 | } |
| 1305 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1306 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1307 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1308 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1309 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1310 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1311 | |
| 1312 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1313 | resetKeyRepeatLocked(); |
| 1314 | |
| 1315 | // 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] | 1316 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1317 | scoped_unlock unlock(mLock); |
| 1318 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1319 | }; |
| 1320 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1321 | return true; |
| 1322 | } |
| 1323 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1324 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1325 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1326 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1327 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1328 | entry.deviceId); |
| 1329 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1330 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1331 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1332 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1333 | resetKeyRepeatLocked(); |
| 1334 | } |
| 1335 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1336 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1337 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1338 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1339 | return true; |
| 1340 | } |
| 1341 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1342 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1343 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1344 | if (mPendingEvent != nullptr) { |
| 1345 | // Move the pending event to the front of the queue. This will give the chance |
| 1346 | // for the pending event to get dispatched to the newly focused window |
| 1347 | mInboundQueue.push_front(mPendingEvent); |
| 1348 | mPendingEvent = nullptr; |
| 1349 | } |
| 1350 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1351 | std::unique_ptr<FocusEntry> focusEntry = |
| 1352 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1353 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1354 | |
| 1355 | // This event should go to the front of the queue, but behind all other focus events |
| 1356 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1357 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1358 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1359 | [](const std::shared_ptr<EventEntry>& event) { |
| 1360 | return event->type == EventEntry::Type::FOCUS; |
| 1361 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1362 | |
| 1363 | // 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] | 1364 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1367 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1368 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1369 | if (channel == nullptr) { |
| 1370 | return; // Window has gone away |
| 1371 | } |
| 1372 | InputTarget target; |
| 1373 | target.inputChannel = channel; |
| 1374 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1375 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1376 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1377 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1378 | std::string reason = std::string("reason=").append(entry->reason); |
| 1379 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1380 | dispatchEventLocked(currentTime, entry, {target}); |
| 1381 | } |
| 1382 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1383 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1384 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1385 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1386 | dropReason = DropReason::NOT_DROPPED; |
| 1387 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1388 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1389 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1390 | |
| 1391 | if (entry->pointerCaptureRequest.enable) { |
| 1392 | // Enable Pointer Capture. |
| 1393 | if (haveWindowWithPointerCapture && |
| 1394 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1395 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1396 | // app of the state change, so there is no need to notify the app. |
| 1397 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1398 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1399 | } |
| 1400 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1401 | // This can happen if a window requests capture and immediately releases capture. |
| 1402 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1403 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1404 | return; |
| 1405 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1406 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1407 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1408 | return; |
| 1409 | } |
| 1410 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1411 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1412 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1413 | mWindowTokenWithPointerCapture = token; |
| 1414 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1415 | // Disable Pointer Capture. |
| 1416 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1417 | // for two reasons: |
| 1418 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1419 | // to disable capture with the same sequence number: one generated by |
| 1420 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1421 | // Capture being disabled in InputReader. |
| 1422 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1423 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1424 | // in InputReader. |
| 1425 | if (!haveWindowWithPointerCapture) { |
| 1426 | // Pointer capture was already forcefully disabled because of focus change. |
| 1427 | dropReason = DropReason::NOT_DROPPED; |
| 1428 | return; |
| 1429 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1430 | token = mWindowTokenWithPointerCapture; |
| 1431 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1432 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1433 | setPointerCaptureLocked(false); |
| 1434 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | auto channel = getInputChannelLocked(token); |
| 1438 | if (channel == nullptr) { |
| 1439 | // Window has gone away, clean up Pointer Capture state. |
| 1440 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1441 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1442 | setPointerCaptureLocked(false); |
| 1443 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1444 | return; |
| 1445 | } |
| 1446 | InputTarget target; |
| 1447 | target.inputChannel = channel; |
| 1448 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1449 | entry->dispatchInProgress = true; |
| 1450 | dispatchEventLocked(currentTime, entry, {target}); |
| 1451 | |
| 1452 | dropReason = DropReason::NOT_DROPPED; |
| 1453 | } |
| 1454 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1455 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1456 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1457 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1458 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1459 | if (windowHandles.empty()) { |
| 1460 | return; |
| 1461 | } |
| 1462 | const std::vector<InputTarget> inputTargets = |
| 1463 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1464 | if (inputTargets.empty()) { |
| 1465 | return; |
| 1466 | } |
| 1467 | entry->dispatchInProgress = true; |
| 1468 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1469 | } |
| 1470 | |
| 1471 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1472 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1473 | std::vector<InputTarget> inputTargets; |
| 1474 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1475 | const sp<IBinder>& token = handle->getToken(); |
| 1476 | if (token == nullptr) { |
| 1477 | continue; |
| 1478 | } |
| 1479 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1480 | if (channel == nullptr) { |
| 1481 | continue; // Window has gone away |
| 1482 | } |
| 1483 | InputTarget target; |
| 1484 | target.inputChannel = channel; |
| 1485 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1486 | inputTargets.push_back(target); |
| 1487 | } |
| 1488 | return inputTargets; |
| 1489 | } |
| 1490 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1491 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1492 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1493 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1494 | if (!entry->dispatchInProgress) { |
| 1495 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1496 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1497 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1498 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1499 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1500 | // We have seen two identical key downs in a row which indicates that the device |
| 1501 | // driver is automatically generating key repeats itself. We take note of the |
| 1502 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1503 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1504 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1505 | // Make sure we don't get key down from a different device. If a different |
| 1506 | // device Id has same key pressed down, the new device Id will replace the |
| 1507 | // current one to hold the key repeat with repeat count reset. |
| 1508 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1509 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1510 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1511 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1512 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1513 | } else { |
| 1514 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1515 | resetKeyRepeatLocked(); |
| 1516 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1517 | } |
| 1518 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1519 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1520 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1521 | // 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] | 1522 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1523 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1524 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1525 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1526 | resetKeyRepeatLocked(); |
| 1527 | } |
| 1528 | |
| 1529 | if (entry->repeatCount == 1) { |
| 1530 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1531 | } else { |
| 1532 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1533 | } |
| 1534 | |
| 1535 | entry->dispatchInProgress = true; |
| 1536 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1537 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | // Handle case where the policy asked us to try again later last time. |
| 1541 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1542 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1543 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1544 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1545 | } |
| 1546 | return false; // wait until next wakeup |
| 1547 | } |
| 1548 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1549 | entry->interceptKeyWakeupTime = 0; |
| 1550 | } |
| 1551 | |
| 1552 | // Give the policy a chance to intercept the key. |
| 1553 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1554 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1555 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1556 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1557 | |
| 1558 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1559 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1560 | }; |
| 1561 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1562 | return false; // wait for the command to run |
| 1563 | } else { |
| 1564 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1565 | } |
| 1566 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1567 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1568 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1573 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1574 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1575 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1576 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1577 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1578 | return true; |
| 1579 | } |
| 1580 | |
| 1581 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1582 | InputEventInjectionResult injectionResult; |
| 1583 | sp<WindowInfoHandle> focusedWindow = |
| 1584 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1585 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1586 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1587 | return false; |
| 1588 | } |
| 1589 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1590 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1591 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1592 | return true; |
| 1593 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1594 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1595 | |
| 1596 | std::vector<InputTarget> inputTargets; |
| 1597 | addWindowTargetLocked(focusedWindow, |
| 1598 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1599 | BitSet32(0), getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1600 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1601 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1602 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1603 | |
| 1604 | // Dispatch the key. |
| 1605 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1606 | return true; |
| 1607 | } |
| 1608 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1609 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1610 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1611 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1612 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1613 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1614 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1615 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1616 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1617 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1618 | } |
| 1619 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1620 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1621 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1622 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1623 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1624 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1625 | "source=0x%x, sensorType=%s", |
| 1626 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1627 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1628 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1629 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1630 | scoped_unlock unlock(mLock); |
| 1631 | |
| 1632 | if (entry->accuracyChanged) { |
| 1633 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1634 | } |
| 1635 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1636 | entry->hwTimestamp, entry->values); |
| 1637 | }; |
| 1638 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1642 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1643 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1644 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1645 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1646 | { // acquire lock |
| 1647 | std::scoped_lock _l(mLock); |
| 1648 | |
| 1649 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1650 | std::shared_ptr<EventEntry> entry = *it; |
| 1651 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1652 | it = mInboundQueue.erase(it); |
| 1653 | releaseInboundEventLocked(entry); |
| 1654 | } |
| 1655 | } |
| 1656 | } |
| 1657 | return true; |
| 1658 | } |
| 1659 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1660 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1661 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1662 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1663 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1664 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1665 | entry->dispatchInProgress = true; |
| 1666 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1667 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1671 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1672 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1673 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1674 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1675 | return true; |
| 1676 | } |
| 1677 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1678 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1679 | |
| 1680 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1681 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1682 | |
| 1683 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1684 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1685 | if (isPointerEvent) { |
| 1686 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1687 | |
| 1688 | if (mDragState && |
| 1689 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1690 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1691 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1692 | } |
| 1693 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1694 | std::vector<TouchedWindow> touchedWindows = |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame^] | 1695 | findTouchedWindowTargetsLocked(currentTime, *entry, &conflictingPointerActions, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1696 | /*byref*/ injectionResult); |
| 1697 | for (const TouchedWindow& touchedWindow : touchedWindows) { |
| 1698 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED, |
| 1699 | "Shouldn't be adding window if the injection didn't succeed."); |
| 1700 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 1701 | touchedWindow.pointerIds, touchedWindow.firstDownTimeInTarget, |
| 1702 | inputTargets); |
| 1703 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1704 | } else { |
| 1705 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1706 | sp<WindowInfoHandle> focusedWindow = |
| 1707 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1708 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1709 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1710 | addWindowTargetLocked(focusedWindow, |
| 1711 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1712 | BitSet32(0), getDownTime(*entry), inputTargets); |
| 1713 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1714 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1715 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1716 | return false; |
| 1717 | } |
| 1718 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1719 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1720 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1721 | return true; |
| 1722 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1723 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1724 | CancelationOptions::Mode mode(isPointerEvent |
| 1725 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1726 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1727 | CancelationOptions options(mode, "input event injection failed"); |
| 1728 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1729 | return true; |
| 1730 | } |
| 1731 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1732 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1733 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1734 | |
| 1735 | // Dispatch the motion. |
| 1736 | if (conflictingPointerActions) { |
| 1737 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1738 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1739 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1740 | } |
| 1741 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1742 | return true; |
| 1743 | } |
| 1744 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1745 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1746 | bool isExiting, const int32_t rawX, |
| 1747 | const int32_t rawY) { |
| 1748 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1749 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1750 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 1751 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1752 | |
| 1753 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1754 | } |
| 1755 | |
| 1756 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1757 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1758 | if (channel == nullptr) { |
| 1759 | return; // Window has gone away |
| 1760 | } |
| 1761 | InputTarget target; |
| 1762 | target.inputChannel = channel; |
| 1763 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1764 | entry->dispatchInProgress = true; |
| 1765 | dispatchEventLocked(currentTime, entry, {target}); |
| 1766 | } |
| 1767 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1768 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1769 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1770 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1771 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1772 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1773 | "metaState=0x%x, buttonState=0x%x," |
| 1774 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1775 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1776 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1777 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1778 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1779 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1780 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1781 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1782 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1783 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1784 | "orientation=%f", |
| 1785 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1786 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1787 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1788 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1789 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1790 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1791 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1792 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1793 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1794 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1795 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1796 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1797 | } |
| 1798 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1799 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1800 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1801 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1802 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1803 | if (DEBUG_DISPATCH_CYCLE) { |
| 1804 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1805 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1806 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1807 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1808 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1809 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1810 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1811 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1812 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1813 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1814 | sp<Connection> connection = |
| 1815 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1816 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1817 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1818 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1819 | if (DEBUG_FOCUS) { |
| 1820 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1821 | "is no longer registered with the input dispatcher.", |
| 1822 | inputTarget.inputChannel->getName().c_str()); |
| 1823 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | } |
| 1827 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1828 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1829 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1830 | // If the policy decides to close the app, we will get a channel removal event via |
| 1831 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1832 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1833 | // pile up. |
| 1834 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1835 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1836 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1837 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1838 | "application not responding"); |
| 1839 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1843 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1844 | if (DEBUG_FOCUS) { |
| 1845 | ALOGD("Resetting ANR timeouts."); |
| 1846 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1847 | |
| 1848 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1849 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1850 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1851 | } |
| 1852 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1853 | /** |
| 1854 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1855 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1856 | * Focused display is the display that the user most recently interacted with. |
| 1857 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1858 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1859 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1860 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1861 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1862 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1863 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1864 | break; |
| 1865 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1866 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1867 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1868 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1869 | break; |
| 1870 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1871 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1872 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1873 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1874 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1875 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1876 | case EventEntry::Type::SENSOR: |
| 1877 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1878 | 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] | 1879 | return ADISPLAY_ID_NONE; |
| 1880 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1881 | } |
| 1882 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1883 | } |
| 1884 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1885 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1886 | const char* focusedWindowName) { |
| 1887 | if (mAnrTracker.empty()) { |
| 1888 | // already processed all events that we waited for |
| 1889 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1890 | return false; |
| 1891 | } |
| 1892 | |
| 1893 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1894 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1895 | // 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] | 1896 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1897 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1898 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1899 | return true; |
| 1900 | } |
| 1901 | |
| 1902 | // We still have pending events, and already started the timer |
| 1903 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1904 | return true; // Still waiting |
| 1905 | } |
| 1906 | |
| 1907 | // Waited too long, and some connection still hasn't processed all motions |
| 1908 | // Just send the key to the focused window |
| 1909 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1910 | focusedWindowName); |
| 1911 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1912 | return false; |
| 1913 | } |
| 1914 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1915 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
| 1916 | nsecs_t currentTime, const EventEntry& entry, nsecs_t* nextWakeupTime, |
| 1917 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1918 | std::string reason; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1919 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1920 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1921 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1922 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1923 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1924 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1925 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1926 | // If there is no currently focused window and no focused application |
| 1927 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1928 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1929 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1930 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1931 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1932 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1933 | } |
| 1934 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1935 | // Drop key events if requested by input feature |
| 1936 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1937 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1940 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1941 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1942 | // start interacting with another application via touch (app switch). This code can be removed |
| 1943 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1944 | // an app is expected to have a focused window. |
| 1945 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1946 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1947 | // 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] | 1948 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1949 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1950 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1951 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1952 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1953 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1954 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1955 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1956 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1957 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 1958 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1959 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1960 | // Already raised ANR. Drop the event |
| 1961 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1962 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1963 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1964 | } else { |
| 1965 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1966 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 1967 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | // we have a valid, non-null focused window |
| 1972 | resetNoFocusedWindowTimeoutLocked(); |
| 1973 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1974 | // Verify targeted injection. |
| 1975 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 1976 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1977 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 1978 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1979 | } |
| 1980 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1981 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 1982 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1983 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1984 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 1985 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | // If the event is a key event, then we must wait for all previous events to |
| 1989 | // complete before delivering it because previous events may have the |
| 1990 | // side-effect of transferring focus to a different window and we want to |
| 1991 | // ensure that the following keys are sent to the new window. |
| 1992 | // |
| 1993 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1994 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1995 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1996 | // often anticipate pending UI changes when typing on a keyboard. |
| 1997 | // To obtain this behavior, we must serialize key events with respect to all |
| 1998 | // prior input events. |
| 1999 | if (entry.type == EventEntry::Type::KEY) { |
| 2000 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 2001 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2002 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2003 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2004 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2005 | } |
| 2006 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2007 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2008 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2009 | } |
| 2010 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2011 | /** |
| 2012 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2013 | * that are currently unresponsive. |
| 2014 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2015 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2016 | const std::vector<Monitor>& monitors) const { |
| 2017 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2018 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2019 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 2020 | sp<Connection> connection = |
| 2021 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2022 | if (connection == nullptr) { |
| 2023 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2024 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2025 | return false; |
| 2026 | } |
| 2027 | if (!connection->responsive) { |
| 2028 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2029 | connection->inputChannel->getName().c_str()); |
| 2030 | return false; |
| 2031 | } |
| 2032 | return true; |
| 2033 | }); |
| 2034 | return responsiveMonitors; |
| 2035 | } |
| 2036 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2037 | std::vector<TouchedWindow> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame^] | 2038 | nsecs_t currentTime, const MotionEntry& entry, bool* outConflictingPointerActions, |
| 2039 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2040 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2041 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2042 | std::vector<TouchedWindow> touchedWindows; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2043 | // For security reasons, we defer updating the touch state until we are sure that |
| 2044 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2045 | const int32_t displayId = entry.displayId; |
| 2046 | const int32_t action = entry.action; |
| 2047 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2048 | |
| 2049 | // 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] | 2050 | outInjectionResult = InputEventInjectionResult::PENDING; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2051 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 2052 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2053 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2054 | // Copy current touch state into tempTouchState. |
| 2055 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2056 | // 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] | 2057 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2058 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2059 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2060 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2061 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2062 | } |
| 2063 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2064 | bool isSplit = tempTouchState.split; |
| 2065 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 2066 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 2067 | tempTouchState.displayId != displayId); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2068 | |
| 2069 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2070 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2071 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2072 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2073 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2074 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2075 | bool wrongDevice = false; |
| 2076 | if (newGesture) { |
| 2077 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2078 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2079 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2080 | "in display %" PRId32, |
| 2081 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2082 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2083 | outInjectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2084 | switchedDevice = false; |
| 2085 | wrongDevice = true; |
| 2086 | goto Failed; |
| 2087 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2088 | tempTouchState.reset(); |
| 2089 | tempTouchState.down = down; |
| 2090 | tempTouchState.deviceId = entry.deviceId; |
| 2091 | tempTouchState.source = entry.source; |
| 2092 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2093 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2094 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2095 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2096 | "in display %" PRId32, |
| 2097 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2098 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2099 | outInjectionResult = InputEventInjectionResult::FAILED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2100 | switchedDevice = false; |
| 2101 | wrongDevice = true; |
| 2102 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2106 | /* 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] | 2107 | const auto [x, y] = resolveTouchedPosition(entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2108 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2109 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2110 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2111 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2112 | isStylus, isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2113 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2114 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2115 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2116 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2117 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2118 | // 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] | 2119 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2122 | // Verify targeted injection. |
| 2123 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2124 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2125 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2126 | newTouchedWindowHandle = nullptr; |
| 2127 | goto Failed; |
| 2128 | } |
| 2129 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2130 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2131 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2132 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2133 | // New window supports splitting, but we should never split mouse events. |
| 2134 | isSplit = !isFromMouse; |
| 2135 | } else if (isSplit) { |
| 2136 | // New window does not support splitting but we have already split events. |
| 2137 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2138 | newTouchedWindowHandle = nullptr; |
| 2139 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2140 | } else { |
| 2141 | // 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] | 2142 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2143 | // should never be split. |
| 2144 | tempTouchState.split = isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2147 | // Update hover state. |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2148 | if (newTouchedWindowHandle != nullptr) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2149 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2150 | newHoverWindowHandle = nullptr; |
| 2151 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2152 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2153 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2154 | } |
| 2155 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2156 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2157 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2158 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2159 | // Process the foreground window first so that it is the first to receive the event. |
| 2160 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2161 | } |
| 2162 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2163 | if (newTouchedWindows.empty()) { |
| 2164 | ALOGI("Dropping event because there is no touchable window at (%d, %d) on display %d.", |
| 2165 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2166 | outInjectionResult = InputEventInjectionResult::FAILED; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2167 | goto Failed; |
| 2168 | } |
| 2169 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2170 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2171 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2172 | continue; |
| 2173 | } |
| 2174 | |
| 2175 | // Set target flags. |
| 2176 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2177 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2178 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2179 | // 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] | 2180 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2181 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2182 | |
| 2183 | if (isSplit) { |
| 2184 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2185 | } |
| 2186 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
| 2187 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2188 | } else if (isWindowObscuredLocked(windowHandle)) { |
| 2189 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2190 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2191 | |
| 2192 | // Update the temporary touch state. |
| 2193 | BitSet32 pointerIds; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2194 | pointerIds.markBit(entry.pointerProperties[pointerIndex].id); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2195 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2196 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds, |
| 2197 | entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2198 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2199 | |
| 2200 | // If any existing window is pilfering pointers from newly added window, remove it |
| 2201 | BitSet32 canceledPointers = BitSet32(0); |
| 2202 | for (const TouchedWindow& window : tempTouchState.windows) { |
| 2203 | if (window.isPilferingPointers) { |
| 2204 | canceledPointers |= window.pointerIds; |
| 2205 | } |
| 2206 | } |
| 2207 | tempTouchState.cancelPointersForNonPilferingWindows(canceledPointers); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2208 | } else { |
| 2209 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2210 | |
| 2211 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2212 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2213 | if (DEBUG_FOCUS) { |
| 2214 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2215 | "dropped the pointer down event in display %" PRId32, |
| 2216 | displayId); |
| 2217 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2218 | outInjectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2219 | goto Failed; |
| 2220 | } |
| 2221 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2222 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2223 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2224 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2225 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2226 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2227 | const auto [x, y] = resolveTouchedPosition(entry); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2228 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2229 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2230 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2231 | newTouchedWindowHandle = |
| 2232 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2233 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2234 | // Verify targeted injection. |
| 2235 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2236 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2237 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2238 | newTouchedWindowHandle = nullptr; |
| 2239 | goto Failed; |
| 2240 | } |
| 2241 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2242 | // Drop touch events if requested by input feature |
| 2243 | if (newTouchedWindowHandle != nullptr && |
| 2244 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2245 | newTouchedWindowHandle = nullptr; |
| 2246 | } |
| 2247 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2248 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2249 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2250 | if (DEBUG_FOCUS) { |
| 2251 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2252 | oldTouchedWindowHandle->getName().c_str(), |
| 2253 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2254 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2255 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2256 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2257 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2258 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2259 | |
| 2260 | // Make a slippery entrance into the new window. |
| 2261 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2262 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2263 | } |
| 2264 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2265 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
| 2266 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
| 2267 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2268 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2269 | if (isSplit) { |
| 2270 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2271 | } |
| 2272 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2273 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2274 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2275 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | BitSet32 pointerIds; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2279 | pointerIds.markBit(entry.pointerProperties[0].id); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2280 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds, |
| 2281 | entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2282 | } |
| 2283 | } |
| 2284 | } |
| 2285 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2286 | // Update dispatching for hover enter and exit. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2287 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2288 | // Let the previous window know that the hover sequence is over, unless we already did |
| 2289 | // it when dispatching it as is to newTouchedWindowHandle. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2290 | if (mLastHoverWindowHandle != nullptr && |
| 2291 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2292 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2293 | if (DEBUG_HOVER) { |
| 2294 | ALOGD("Sending hover exit event to window %s.", |
| 2295 | mLastHoverWindowHandle->getName().c_str()); |
| 2296 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2297 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2298 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2299 | } |
| 2300 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2301 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2302 | // when dispatching it as is to newTouchedWindowHandle. |
| 2303 | if (newHoverWindowHandle != nullptr && |
| 2304 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2305 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2306 | if (DEBUG_HOVER) { |
| 2307 | ALOGD("Sending hover enter event to window %s.", |
| 2308 | newHoverWindowHandle->getName().c_str()); |
| 2309 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2310 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2311 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2312 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2313 | } |
| 2314 | } |
| 2315 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2316 | // Ensure that we have at least one foreground window or at least one window that cannot be a |
| 2317 | // foreground target. If we only have windows that are not receiving foreground touches (e.g. we |
| 2318 | // only have windows getting ACTION_OUTSIDE), then drop the event, because there is no window |
| 2319 | // that is actually receiving the entire gesture. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2320 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2321 | [](const TouchedWindow& touchedWindow) { |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2322 | return !canReceiveForegroundTouches( |
| 2323 | *touchedWindow.windowHandle->getInfo()) || |
| 2324 | (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2325 | })) { |
Siarhei Vishniakou | 1fb1891 | 2022-03-08 10:31:39 -0800 | [diff] [blame] | 2326 | ALOGI("Dropping event because there is no touched window on display %d to receive it: %s", |
| 2327 | displayId, entry.getDescription().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2328 | outInjectionResult = InputEventInjectionResult::FAILED; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2329 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2330 | } |
| 2331 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2332 | // Ensure that all touched windows are valid for injection. |
| 2333 | if (entry.injectionState != nullptr) { |
| 2334 | std::string errs; |
| 2335 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
| 2336 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2337 | // Allow ACTION_OUTSIDE events generated by targeted injection to be |
| 2338 | // dispatched to any uid, since the coords will be zeroed out later. |
| 2339 | continue; |
| 2340 | } |
| 2341 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2342 | if (err) errs += "\n - " + *err; |
| 2343 | } |
| 2344 | if (!errs.empty()) { |
| 2345 | ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " |
| 2346 | "%d:%s", |
| 2347 | *entry.injectionState->targetUid, errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2348 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2349 | goto Failed; |
| 2350 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2351 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2352 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2353 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2354 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2355 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2356 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2357 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2358 | if (foregroundWindowHandle) { |
| 2359 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2360 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2361 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2362 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2363 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2364 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2365 | InputTarget::FLAG_ZERO_COORDS, |
| 2366 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2367 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2368 | } |
| 2369 | } |
| 2370 | } |
| 2371 | } |
| 2372 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2373 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2374 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2375 | // of the touch gesture. |
| 2376 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2377 | // engine only supports touch events. We would need to add a mechanism similar |
| 2378 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2379 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2380 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2381 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2382 | if (foregroundWindowHandle && |
| 2383 | foregroundWindowHandle->getInfo()->inputConfig.test( |
| 2384 | WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2385 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2386 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2387 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2388 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2389 | if (info->displayId == displayId && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2390 | windowHandle->getInfo()->inputConfig.test( |
| 2391 | WindowInfo::InputConfig::IS_WALLPAPER)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2392 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2393 | .addOrUpdateWindow(windowHandle, |
| 2394 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2395 | InputTarget:: |
| 2396 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2397 | InputTarget::FLAG_DISPATCH_AS_IS, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2398 | BitSet32(0), entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2399 | } |
| 2400 | } |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | // Success! Output targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2405 | touchedWindows = tempTouchState.windows; |
| 2406 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2407 | |
| 2408 | // Drop the outside or hover touch windows since we will not care about them |
| 2409 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2410 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2411 | |
| 2412 | Failed: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2413 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2414 | if (!wrongDevice) { |
| 2415 | if (switchedDevice) { |
| 2416 | if (DEBUG_FOCUS) { |
| 2417 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2418 | } |
| 2419 | *outConflictingPointerActions = true; |
| 2420 | } |
| 2421 | |
| 2422 | if (isHoverAction) { |
| 2423 | // Started hovering, therefore no longer down. |
| 2424 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2425 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2426 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2427 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2428 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2429 | *outConflictingPointerActions = true; |
| 2430 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2431 | tempTouchState.reset(); |
| 2432 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2433 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2434 | tempTouchState.deviceId = entry.deviceId; |
| 2435 | tempTouchState.source = entry.source; |
| 2436 | tempTouchState.displayId = displayId; |
| 2437 | } |
| 2438 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2439 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2440 | // All pointers up or canceled. |
| 2441 | tempTouchState.reset(); |
| 2442 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2443 | // First pointer went down. |
| 2444 | if (oldState && oldState->down) { |
| 2445 | if (DEBUG_FOCUS) { |
| 2446 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2447 | } |
| 2448 | *outConflictingPointerActions = true; |
| 2449 | } |
| 2450 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2451 | // One pointer went up. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2452 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2453 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2454 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2455 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2456 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2457 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2458 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2459 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2460 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2461 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2462 | i += 1; |
| 2463 | } |
| 2464 | } else if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2465 | // If no split, we suppose all touched windows should receive pointer down. |
| 2466 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2467 | for (size_t i = 0; i < tempTouchState.windows.size(); i++) { |
| 2468 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2469 | // Ignore drag window for it should just track one pointer. |
| 2470 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2471 | continue; |
| 2472 | } |
| 2473 | touchedWindow.pointerIds.markBit(entry.pointerProperties[pointerIndex].id); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2474 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2475 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2476 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2477 | // Save changes unless the action was scroll in which case the temporary touch |
| 2478 | // state was only valid for this one action. |
| 2479 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2480 | if (tempTouchState.displayId >= 0) { |
| 2481 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2482 | } else { |
| 2483 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2484 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2485 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2486 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2487 | // Update hover state. |
| 2488 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2489 | } |
| 2490 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2491 | return touchedWindows; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2492 | } |
| 2493 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2494 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2495 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2496 | // have an explicit reason to support it. |
| 2497 | constexpr bool isStylus = false; |
| 2498 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2499 | const sp<WindowInfoHandle> dropWindow = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2500 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2501 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2502 | if (dropWindow) { |
| 2503 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2504 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2505 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2506 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2507 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2508 | } |
| 2509 | mDragState.reset(); |
| 2510 | } |
| 2511 | |
| 2512 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2513 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2514 | return; |
| 2515 | } |
| 2516 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2517 | if (!mDragState->isStartDrag) { |
| 2518 | mDragState->isStartDrag = true; |
| 2519 | mDragState->isStylusButtonDownAtStart = |
| 2520 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2521 | } |
| 2522 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2523 | // Find the pointer index by id. |
| 2524 | int32_t pointerIndex = 0; |
| 2525 | for (; static_cast<uint32_t>(pointerIndex) < entry.pointerCount; pointerIndex++) { |
| 2526 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2527 | if (pointerProperties.id == mDragState->pointerId) { |
| 2528 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2529 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2530 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2531 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2532 | if (uint32_t(pointerIndex) == entry.pointerCount) { |
| 2533 | 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] | 2534 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2535 | mDragState.reset(); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2536 | return; |
| 2537 | } |
| 2538 | |
| 2539 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2540 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2541 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2542 | |
| 2543 | switch (maskedAction) { |
| 2544 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2545 | // Handle the special case : stylus button no longer pressed. |
| 2546 | bool isStylusButtonDown = |
| 2547 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2548 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2549 | finishDragAndDrop(entry.displayId, x, y); |
| 2550 | return; |
| 2551 | } |
| 2552 | |
| 2553 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2554 | // until we have an explicit reason to support it. |
| 2555 | constexpr bool isStylus = false; |
| 2556 | |
| 2557 | const sp<WindowInfoHandle> hoverWindowHandle = |
| 2558 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
| 2559 | isStylus, false /*addOutsideTargets*/, |
| 2560 | true /*ignoreDragWindow*/); |
| 2561 | // enqueue drag exit if needed. |
| 2562 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2563 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2564 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2565 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, x, |
| 2566 | y); |
| 2567 | } |
| 2568 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2569 | } |
| 2570 | // enqueue drag location if needed. |
| 2571 | if (hoverWindowHandle != nullptr) { |
| 2572 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, x, y); |
| 2573 | } |
| 2574 | break; |
| 2575 | } |
| 2576 | |
| 2577 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 2578 | if (getMotionEventActionPointerIndex(entry.action) != pointerIndex) { |
| 2579 | break; |
| 2580 | } |
| 2581 | // The drag pointer is up. |
| 2582 | [[fallthrough]]; |
| 2583 | case AMOTION_EVENT_ACTION_UP: |
| 2584 | finishDragAndDrop(entry.displayId, x, y); |
| 2585 | break; |
| 2586 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2587 | ALOGD("Receiving cancel when drag and drop."); |
| 2588 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2589 | mDragState.reset(); |
| 2590 | break; |
| 2591 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2592 | } |
| 2593 | } |
| 2594 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2595 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2596 | int32_t targetFlags, BitSet32 pointerIds, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2597 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2598 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2599 | std::vector<InputTarget>::iterator it = |
| 2600 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2601 | [&windowHandle](const InputTarget& inputTarget) { |
| 2602 | return inputTarget.inputChannel->getConnectionToken() == |
| 2603 | windowHandle->getToken(); |
| 2604 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2605 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2606 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2607 | |
| 2608 | if (it == inputTargets.end()) { |
| 2609 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2610 | std::shared_ptr<InputChannel> inputChannel = |
| 2611 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2612 | if (inputChannel == nullptr) { |
| 2613 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2614 | return; |
| 2615 | } |
| 2616 | inputTarget.inputChannel = inputChannel; |
| 2617 | inputTarget.flags = targetFlags; |
| 2618 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2619 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2620 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2621 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2622 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2623 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2624 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2625 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2626 | inputTargets.push_back(inputTarget); |
| 2627 | it = inputTargets.end() - 1; |
| 2628 | } |
| 2629 | |
| 2630 | ALOG_ASSERT(it->flags == targetFlags); |
| 2631 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2632 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2633 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2634 | } |
| 2635 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2636 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2637 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2638 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2639 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2640 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2641 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2642 | InputTarget target; |
| 2643 | target.inputChannel = monitor.inputChannel; |
| 2644 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2645 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2646 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2647 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2648 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2649 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2650 | target.setDefaultPointerTransform(target.displayTransform); |
| 2651 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2652 | } |
| 2653 | } |
| 2654 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2655 | /** |
| 2656 | * Indicate whether one window handle should be considered as obscuring |
| 2657 | * another window handle. We only check a few preconditions. Actually |
| 2658 | * checking the bounds is left to the caller. |
| 2659 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2660 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2661 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2662 | // Compare by token so cloned layers aren't counted |
| 2663 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2664 | return false; |
| 2665 | } |
| 2666 | auto info = windowHandle->getInfo(); |
| 2667 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2668 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2669 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2670 | } else if (otherInfo->alpha == 0 && |
| 2671 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2672 | // Those act as if they were invisible, so we don't need to flag them. |
| 2673 | // We do want to potentially flag touchable windows even if they have 0 |
| 2674 | // opacity, since they can consume touches and alter the effects of the |
| 2675 | // user interaction (eg. apps that rely on |
| 2676 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2677 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2678 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2679 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2680 | // If ownerUid is the same we don't generate occlusion events as there |
| 2681 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2682 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2683 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2684 | return false; |
| 2685 | } else if (otherInfo->displayId != info->displayId) { |
| 2686 | return false; |
| 2687 | } |
| 2688 | return true; |
| 2689 | } |
| 2690 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2691 | /** |
| 2692 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2693 | * untrusted, one should check: |
| 2694 | * |
| 2695 | * 1. If result.hasBlockingOcclusion is true. |
| 2696 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2697 | * BLOCK_UNTRUSTED. |
| 2698 | * |
| 2699 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2700 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2701 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2702 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2703 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2704 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2705 | * |
| 2706 | * If neither of those is true, then it means the touch can be allowed. |
| 2707 | */ |
| 2708 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2709 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2710 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2711 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2712 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2713 | TouchOcclusionInfo info; |
| 2714 | info.hasBlockingOcclusion = false; |
| 2715 | info.obscuringOpacity = 0; |
| 2716 | info.obscuringUid = -1; |
| 2717 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2718 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2719 | if (windowHandle == otherHandle) { |
| 2720 | break; // All future windows are below us. Exit early. |
| 2721 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2722 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2723 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2724 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2725 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2726 | info.debugInfo.push_back( |
| 2727 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2728 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2729 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2730 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2731 | // window's touch occlusion mode |
| 2732 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2733 | info.hasBlockingOcclusion = true; |
| 2734 | info.obscuringUid = otherInfo->ownerUid; |
| 2735 | info.obscuringPackage = otherInfo->packageName; |
| 2736 | break; |
| 2737 | } |
| 2738 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2739 | uint32_t uid = otherInfo->ownerUid; |
| 2740 | float opacity = |
| 2741 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2742 | // Given windows A and B: |
| 2743 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2744 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2745 | opacityByUid[uid] = opacity; |
| 2746 | if (opacity > info.obscuringOpacity) { |
| 2747 | info.obscuringOpacity = opacity; |
| 2748 | info.obscuringUid = uid; |
| 2749 | info.obscuringPackage = otherInfo->packageName; |
| 2750 | } |
| 2751 | } |
| 2752 | } |
| 2753 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2754 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2755 | info.debugInfo.push_back( |
| 2756 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2757 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2758 | return info; |
| 2759 | } |
| 2760 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2761 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2762 | bool isTouchedWindow) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2763 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2764 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2765 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 2766 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2767 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 2768 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 2769 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 2770 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 2771 | info->name.c_str(), info->inputConfig.string().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2772 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2773 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2776 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2777 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2778 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2779 | occlusionInfo.obscuringUid); |
| 2780 | return false; |
| 2781 | } |
| 2782 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2783 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2784 | "%.2f, maximum allowed = %.2f)", |
| 2785 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2786 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2787 | return false; |
| 2788 | } |
| 2789 | return true; |
| 2790 | } |
| 2791 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2792 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2793 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2794 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2795 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2796 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2797 | if (windowHandle == otherHandle) { |
| 2798 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2799 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2800 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2801 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2802 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2803 | return true; |
| 2804 | } |
| 2805 | } |
| 2806 | return false; |
| 2807 | } |
| 2808 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2809 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2810 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2811 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2812 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2813 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2814 | if (windowHandle == otherHandle) { |
| 2815 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2816 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2817 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2818 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2819 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2820 | return true; |
| 2821 | } |
| 2822 | } |
| 2823 | return false; |
| 2824 | } |
| 2825 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2826 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2827 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2828 | if (applicationHandle != nullptr) { |
| 2829 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2830 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2831 | } else { |
| 2832 | return applicationHandle->getName(); |
| 2833 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2834 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2835 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2836 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2837 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2838 | } |
| 2839 | } |
| 2840 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2841 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2842 | if (!isUserActivityEvent(eventEntry)) { |
| 2843 | // 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] | 2844 | return; |
| 2845 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2846 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2847 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2848 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2849 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2850 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2851 | if (DEBUG_DISPATCH_CYCLE) { |
| 2852 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2853 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2854 | return; |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2859 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2860 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2861 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2862 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2863 | return; |
| 2864 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2865 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2866 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2867 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2868 | } |
| 2869 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2870 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2871 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2872 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2873 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2874 | return; |
| 2875 | } |
| 2876 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2877 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2878 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2879 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2880 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2881 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2882 | break; |
| 2883 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2884 | } |
| 2885 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2886 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2887 | REQUIRES(mLock) { |
| 2888 | scoped_unlock unlock(mLock); |
| 2889 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2890 | }; |
| 2891 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2895 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2896 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2897 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2898 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2899 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2900 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2901 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2902 | ATRACE_NAME(message.c_str()); |
| 2903 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2904 | if (DEBUG_DISPATCH_CYCLE) { |
| 2905 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2906 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2907 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2908 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2909 | inputTarget.getPointerInfoString().c_str()); |
| 2910 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2911 | |
| 2912 | // Skip this event if the connection status is not normal. |
| 2913 | // 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] | 2914 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2915 | if (DEBUG_DISPATCH_CYCLE) { |
| 2916 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2917 | connection->getInputChannelName().c_str(), |
| 2918 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2919 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2920 | return; |
| 2921 | } |
| 2922 | |
| 2923 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2924 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2925 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2926 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2927 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2928 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2929 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2930 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2931 | LOG_ALWAYS_FATAL_IF(!inputTarget.firstDownTimeInTarget.has_value(), |
| 2932 | "Splitting motion events requires a down time to be set for the " |
| 2933 | "target"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2934 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2935 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 2936 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2937 | if (!splitMotionEntry) { |
| 2938 | return; // split event was dropped |
| 2939 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2940 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2941 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2942 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2943 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2944 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2945 | if (DEBUG_FOCUS) { |
| 2946 | ALOGD("channel '%s' ~ Split motion event.", |
| 2947 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2948 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2949 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2950 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2951 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2952 | return; |
| 2953 | } |
| 2954 | } |
| 2955 | |
| 2956 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2957 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2958 | } |
| 2959 | |
| 2960 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2961 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2962 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2963 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2964 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2965 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2966 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2967 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2968 | ATRACE_NAME(message.c_str()); |
| 2969 | } |
| 2970 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 2971 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2972 | |
| 2973 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2974 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2975 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2976 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2977 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2978 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2979 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2980 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2981 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2982 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2983 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2984 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2985 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2986 | |
| 2987 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2988 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2989 | startDispatchCycleLocked(currentTime, connection); |
| 2990 | } |
| 2991 | } |
| 2992 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2993 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2994 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2995 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2996 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2997 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2998 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2999 | connection->getInputChannelName().c_str(), |
| 3000 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3001 | ATRACE_NAME(message.c_str()); |
| 3002 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3003 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3004 | if (!(inputTargetFlags & dispatchMode)) { |
| 3005 | return; |
| 3006 | } |
| 3007 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 3008 | |
| 3009 | // This is a new event. |
| 3010 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3011 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3012 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3013 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3014 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3015 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3016 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3017 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3018 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3019 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3020 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3021 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3022 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3023 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3024 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3025 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3026 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3027 | if (DEBUG_DISPATCH_CYCLE) { |
| 3028 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3029 | "event", |
| 3030 | connection->getInputChannelName().c_str()); |
| 3031 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3032 | return; // skip the inconsistent event |
| 3033 | } |
| 3034 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3035 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3036 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3037 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3038 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3039 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3040 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3041 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3042 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3043 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3044 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3045 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 3046 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 3047 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3048 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 3049 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3050 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 3051 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 3052 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 3053 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3054 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3055 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3056 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3057 | } |
| 3058 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3059 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3060 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3061 | if (DEBUG_DISPATCH_CYCLE) { |
| 3062 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3063 | "enter event", |
| 3064 | connection->getInputChannelName().c_str()); |
| 3065 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3066 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3067 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3068 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3069 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3070 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3071 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3072 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3073 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3074 | } |
| 3075 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3076 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3077 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3078 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3079 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3080 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3081 | if (DEBUG_DISPATCH_CYCLE) { |
| 3082 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3083 | "event", |
| 3084 | connection->getInputChannelName().c_str()); |
| 3085 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3086 | return; // skip the inconsistent event |
| 3087 | } |
| 3088 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3089 | dispatchEntry->resolvedEventId = |
| 3090 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3091 | ? mIdGenerator.nextId() |
| 3092 | : motionEntry.id; |
| 3093 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3094 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3095 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3096 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3097 | ATRACE_NAME(message.c_str()); |
| 3098 | } |
| 3099 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3100 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3101 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3102 | // Skip reporting pointer down outside focus to the policy. |
| 3103 | break; |
| 3104 | } |
| 3105 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3106 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3107 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3108 | |
| 3109 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3110 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3111 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3112 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3113 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3114 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3115 | break; |
| 3116 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3117 | case EventEntry::Type::SENSOR: { |
| 3118 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3119 | break; |
| 3120 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3121 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3122 | case EventEntry::Type::DEVICE_RESET: { |
| 3123 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3124 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3125 | break; |
| 3126 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | // Remember that we are waiting for this dispatch to complete. |
| 3130 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3131 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3135 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3136 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3137 | } |
| 3138 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3139 | /** |
| 3140 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3141 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3142 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3143 | * We will see both launcher and wallpaper in that list. |
| 3144 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3145 | * until the set of interacted connections changes. |
| 3146 | * |
| 3147 | * The following items are skipped, to reduce the logspam: |
| 3148 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3149 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3150 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3151 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3152 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3153 | */ |
| 3154 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3155 | const std::vector<InputTarget>& targets) { |
| 3156 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3157 | if (entry.type == EventEntry::Type::KEY) { |
| 3158 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3159 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3160 | return; |
| 3161 | } |
| 3162 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3163 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3164 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3165 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3166 | return; |
| 3167 | } |
| 3168 | } else { |
| 3169 | return; // Not a key or a motion |
| 3170 | } |
| 3171 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3172 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3173 | std::vector<sp<Connection>> newConnections; |
| 3174 | for (const InputTarget& target : targets) { |
| 3175 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3176 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3177 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3178 | } |
| 3179 | |
| 3180 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3181 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3182 | if (connection == nullptr) { |
| 3183 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3184 | } |
| 3185 | newConnectionTokens.insert(std::move(token)); |
| 3186 | newConnections.emplace_back(connection); |
| 3187 | } |
| 3188 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3189 | return; // no change |
| 3190 | } |
| 3191 | mInteractionConnectionTokens = newConnectionTokens; |
| 3192 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3193 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3194 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3195 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3196 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3197 | std::string message = "Interaction with: " + targetList; |
| 3198 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3199 | message += "<none>"; |
| 3200 | } |
| 3201 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3202 | } |
| 3203 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3204 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3205 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3206 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3207 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3208 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3209 | return; |
| 3210 | } |
| 3211 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3212 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3213 | if (focusedToken == token) { |
| 3214 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3215 | return; |
| 3216 | } |
| 3217 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3218 | auto command = [this, token]() REQUIRES(mLock) { |
| 3219 | scoped_unlock unlock(mLock); |
| 3220 | mPolicy->onPointerDownOutsideFocus(token); |
| 3221 | }; |
| 3222 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3223 | } |
| 3224 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3225 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3226 | DispatchEntry& dispatchEntry) const { |
| 3227 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3228 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3229 | |
| 3230 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 3231 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
| 3232 | |
| 3233 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3234 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
| 3235 | !(dispatchEntry.targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3236 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3237 | if (globalScaleFactor != 1.0f) { |
| 3238 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3239 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3240 | // Don't apply window scale here since we don't want scale to affect raw |
| 3241 | // coordinates. The scale will be sent back to the client and applied |
| 3242 | // later when requesting relative coordinates. |
| 3243 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3244 | 1 /* windowYScale */); |
| 3245 | } |
| 3246 | usingCoords = scaledCoords; |
| 3247 | } |
| 3248 | } else if (dispatchEntry.targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
| 3249 | // We don't want the dispatch target to know the coordinates |
| 3250 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3251 | scaledCoords[i].clear(); |
| 3252 | } |
| 3253 | usingCoords = scaledCoords; |
| 3254 | } |
| 3255 | |
| 3256 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3257 | |
| 3258 | // Publish the motion event. |
| 3259 | return connection.inputPublisher |
| 3260 | .publishMotionEvent(dispatchEntry.seq, dispatchEntry.resolvedEventId, |
| 3261 | motionEntry.deviceId, motionEntry.source, motionEntry.displayId, |
| 3262 | std::move(hmac), dispatchEntry.resolvedAction, |
| 3263 | motionEntry.actionButton, dispatchEntry.resolvedFlags, |
| 3264 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3265 | motionEntry.buttonState, motionEntry.classification, |
| 3266 | dispatchEntry.transform, motionEntry.xPrecision, |
| 3267 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 3268 | motionEntry.yCursorPosition, dispatchEntry.rawTransform, |
| 3269 | motionEntry.downTime, motionEntry.eventTime, |
| 3270 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 3271 | usingCoords); |
| 3272 | } |
| 3273 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3274 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3275 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3276 | if (ATRACE_ENABLED()) { |
| 3277 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3278 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3279 | ATRACE_NAME(message.c_str()); |
| 3280 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3281 | if (DEBUG_DISPATCH_CYCLE) { |
| 3282 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3283 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3284 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3285 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3286 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3287 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3288 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3289 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3290 | |
| 3291 | // Publish the event. |
| 3292 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3293 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3294 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3295 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3296 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3297 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3298 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3299 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3300 | status = connection->inputPublisher |
| 3301 | .publishKeyEvent(dispatchEntry->seq, |
| 3302 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3303 | keyEntry.source, keyEntry.displayId, |
| 3304 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3305 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3306 | keyEntry.scanCode, keyEntry.metaState, |
| 3307 | keyEntry.repeatCount, keyEntry.downTime, |
| 3308 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3309 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3310 | } |
| 3311 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3312 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3313 | status = publishMotionEvent(*connection, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3314 | break; |
| 3315 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3316 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3317 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3318 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3319 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3320 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3321 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3322 | break; |
| 3323 | } |
| 3324 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3325 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3326 | const TouchModeEntry& touchModeEntry = |
| 3327 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3328 | status = connection->inputPublisher |
| 3329 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3330 | touchModeEntry.inTouchMode); |
| 3331 | |
| 3332 | break; |
| 3333 | } |
| 3334 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3335 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3336 | const auto& captureEntry = |
| 3337 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3338 | status = connection->inputPublisher |
| 3339 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3340 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3341 | break; |
| 3342 | } |
| 3343 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3344 | case EventEntry::Type::DRAG: { |
| 3345 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3346 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3347 | dragEntry.id, dragEntry.x, |
| 3348 | dragEntry.y, |
| 3349 | dragEntry.isExiting); |
| 3350 | break; |
| 3351 | } |
| 3352 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3353 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3354 | case EventEntry::Type::DEVICE_RESET: |
| 3355 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3356 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3357 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3358 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3359 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | // Check the result. |
| 3363 | if (status) { |
| 3364 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3365 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3366 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3367 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3368 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3369 | "event to it, status=%s(%d)", |
| 3370 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3371 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3372 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3373 | } else { |
| 3374 | // Pipe is full and we are waiting for the app to finish process some events |
| 3375 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3376 | if (DEBUG_DISPATCH_CYCLE) { |
| 3377 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3378 | "waiting for the application to catch up", |
| 3379 | connection->getInputChannelName().c_str()); |
| 3380 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3381 | } |
| 3382 | } else { |
| 3383 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3384 | "status=%s(%d)", |
| 3385 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3386 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3387 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3388 | } |
| 3389 | return; |
| 3390 | } |
| 3391 | |
| 3392 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3393 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3394 | connection->outboundQueue.end(), |
| 3395 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3396 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3397 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3398 | if (connection->responsive) { |
| 3399 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3400 | connection->inputChannel->getConnectionToken()); |
| 3401 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3402 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3403 | } |
| 3404 | } |
| 3405 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3406 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3407 | size_t size; |
| 3408 | switch (event.type) { |
| 3409 | case VerifiedInputEvent::Type::KEY: { |
| 3410 | size = sizeof(VerifiedKeyEvent); |
| 3411 | break; |
| 3412 | } |
| 3413 | case VerifiedInputEvent::Type::MOTION: { |
| 3414 | size = sizeof(VerifiedMotionEvent); |
| 3415 | break; |
| 3416 | } |
| 3417 | } |
| 3418 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3419 | return mHmacKeyManager.sign(start, size); |
| 3420 | } |
| 3421 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3422 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3423 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3424 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3425 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3426 | // Only sign events up and down events as the purely move events |
| 3427 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3428 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3429 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3430 | |
| 3431 | VerifiedMotionEvent verifiedEvent = |
| 3432 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3433 | verifiedEvent.actionMasked = actionMasked; |
| 3434 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3435 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3439 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3440 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3441 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3442 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3443 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3444 | } |
| 3445 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3446 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3447 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3448 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3449 | if (DEBUG_DISPATCH_CYCLE) { |
| 3450 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3451 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3452 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3453 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3454 | if (connection->status == Connection::Status::BROKEN || |
| 3455 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3456 | return; |
| 3457 | } |
| 3458 | |
| 3459 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3460 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3461 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3462 | }; |
| 3463 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3467 | const sp<Connection>& connection, |
| 3468 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3469 | if (DEBUG_DISPATCH_CYCLE) { |
| 3470 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3471 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3472 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3473 | |
| 3474 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3475 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3476 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3477 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3478 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3479 | |
| 3480 | // The connection appears to be unrecoverably broken. |
| 3481 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3482 | if (connection->status == Connection::Status::NORMAL) { |
| 3483 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3484 | |
| 3485 | if (notify) { |
| 3486 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3487 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3488 | connection->getInputChannelName().c_str()); |
| 3489 | |
| 3490 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3491 | scoped_unlock unlock(mLock); |
| 3492 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3493 | }; |
| 3494 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3495 | } |
| 3496 | } |
| 3497 | } |
| 3498 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3499 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3500 | while (!queue.empty()) { |
| 3501 | DispatchEntry* dispatchEntry = queue.front(); |
| 3502 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3503 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3504 | } |
| 3505 | } |
| 3506 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3507 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3508 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3509 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3510 | } |
| 3511 | delete dispatchEntry; |
| 3512 | } |
| 3513 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3514 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3515 | std::scoped_lock _l(mLock); |
| 3516 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3517 | if (connection == nullptr) { |
| 3518 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3519 | connectionToken.get(), events); |
| 3520 | return 0; // remove the callback |
| 3521 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3522 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3523 | bool notify; |
| 3524 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3525 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3526 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3527 | "events=0x%x", |
| 3528 | connection->getInputChannelName().c_str(), events); |
| 3529 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3530 | } |
| 3531 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3532 | nsecs_t currentTime = now(); |
| 3533 | bool gotOne = false; |
| 3534 | status_t status = OK; |
| 3535 | for (;;) { |
| 3536 | Result<InputPublisher::ConsumerResponse> result = |
| 3537 | connection->inputPublisher.receiveConsumerResponse(); |
| 3538 | if (!result.ok()) { |
| 3539 | status = result.error().code(); |
| 3540 | break; |
| 3541 | } |
| 3542 | |
| 3543 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3544 | const InputPublisher::Finished& finish = |
| 3545 | std::get<InputPublisher::Finished>(*result); |
| 3546 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3547 | finish.consumeTime); |
| 3548 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3549 | if (shouldReportMetricsForConnection(*connection)) { |
| 3550 | const InputPublisher::Timeline& timeline = |
| 3551 | std::get<InputPublisher::Timeline>(*result); |
| 3552 | mLatencyTracker |
| 3553 | .trackGraphicsLatency(timeline.inputEventId, |
| 3554 | connection->inputChannel->getConnectionToken(), |
| 3555 | std::move(timeline.graphicsTimeline)); |
| 3556 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3557 | } |
| 3558 | gotOne = true; |
| 3559 | } |
| 3560 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3561 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3562 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3563 | return 1; |
| 3564 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3565 | } |
| 3566 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3567 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3568 | if (notify) { |
| 3569 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3570 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3571 | status); |
| 3572 | } |
| 3573 | } else { |
| 3574 | // Monitor channels are never explicitly unregistered. |
| 3575 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3576 | const bool stillHaveWindowHandle = |
| 3577 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3578 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3579 | if (notify) { |
| 3580 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3581 | connection->getInputChannelName().c_str(), events); |
| 3582 | } |
| 3583 | } |
| 3584 | |
| 3585 | // Remove the channel. |
| 3586 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3587 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3588 | } |
| 3589 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3590 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3591 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3592 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3593 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3594 | } |
| 3595 | } |
| 3596 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3597 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3598 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3599 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3600 | for (const Monitor& monitor : monitors) { |
| 3601 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3602 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3603 | } |
| 3604 | } |
| 3605 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3606 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3607 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3608 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3609 | if (connection == nullptr) { |
| 3610 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3611 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3612 | |
| 3613 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3617 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3618 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3619 | return; |
| 3620 | } |
| 3621 | |
| 3622 | nsecs_t currentTime = now(); |
| 3623 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3624 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3625 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3626 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3627 | if (cancelationEvents.empty()) { |
| 3628 | return; |
| 3629 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3630 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3631 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3632 | "with reality: %s, mode=%d.", |
| 3633 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3634 | options.mode); |
| 3635 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3636 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3637 | std::string reason = std::string("reason=").append(options.reason); |
| 3638 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3639 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3640 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3641 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3642 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3643 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3644 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3645 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3646 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3647 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3648 | } |
| 3649 | target.inputChannel = connection->inputChannel; |
| 3650 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3651 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3652 | const bool wasEmpty = connection->outboundQueue.empty(); |
| 3653 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3654 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3655 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3656 | switch (cancelationEventEntry->type) { |
| 3657 | case EventEntry::Type::KEY: { |
| 3658 | logOutboundKeyDetails("cancel - ", |
| 3659 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3660 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3661 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3662 | case EventEntry::Type::MOTION: { |
| 3663 | logOutboundMotionDetails("cancel - ", |
| 3664 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3665 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3666 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3667 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3668 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3669 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3670 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3671 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3672 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3673 | break; |
| 3674 | } |
| 3675 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3676 | case EventEntry::Type::DEVICE_RESET: |
| 3677 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3678 | 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] | 3679 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3680 | break; |
| 3681 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3682 | } |
| 3683 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3684 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3685 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3686 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3687 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3688 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3689 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3690 | startDispatchCycleLocked(currentTime, connection); |
| 3691 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3692 | } |
| 3693 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3694 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3695 | const nsecs_t downTime, const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3696 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3697 | return; |
| 3698 | } |
| 3699 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3700 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3701 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3702 | |
| 3703 | if (downEvents.empty()) { |
| 3704 | return; |
| 3705 | } |
| 3706 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3707 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3708 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3709 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3710 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3711 | |
| 3712 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3713 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3714 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3715 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3716 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3717 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3718 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3719 | } |
| 3720 | target.inputChannel = connection->inputChannel; |
| 3721 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3722 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3723 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3724 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3725 | switch (downEventEntry->type) { |
| 3726 | case EventEntry::Type::MOTION: { |
| 3727 | logOutboundMotionDetails("down - ", |
| 3728 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3729 | break; |
| 3730 | } |
| 3731 | |
| 3732 | case EventEntry::Type::KEY: |
| 3733 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3734 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3735 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3736 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3737 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3738 | case EventEntry::Type::SENSOR: |
| 3739 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3740 | 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] | 3741 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3742 | break; |
| 3743 | } |
| 3744 | } |
| 3745 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3746 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3747 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3748 | } |
| 3749 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3750 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3751 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3752 | startDispatchCycleLocked(downTime, connection); |
| 3753 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3754 | } |
| 3755 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3756 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3757 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds, nsecs_t splitDownTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3758 | ALOG_ASSERT(pointerIds.value != 0); |
| 3759 | |
| 3760 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3761 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3762 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3763 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3764 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3765 | uint32_t splitPointerCount = 0; |
| 3766 | |
| 3767 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3768 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3769 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3770 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3771 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3772 | if (pointerIds.hasBit(pointerId)) { |
| 3773 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3774 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3775 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3776 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3777 | splitPointerCount += 1; |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | if (splitPointerCount != pointerIds.count()) { |
| 3782 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3783 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3784 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3785 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3786 | // in this way. |
| 3787 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3788 | "we expected there to be %d pointers. This probably means we received " |
| 3789 | "a broken sequence of pointer ids from the input device.", |
| 3790 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3791 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3792 | } |
| 3793 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3794 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3795 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3796 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3797 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3798 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3799 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3800 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3801 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3802 | if (pointerIds.hasBit(pointerId)) { |
| 3803 | if (pointerIds.count() == 1) { |
| 3804 | // The first/last pointer went down/up. |
| 3805 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3806 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3807 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3808 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3809 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3810 | } else { |
| 3811 | // A secondary pointer went down/up. |
| 3812 | uint32_t splitPointerIndex = 0; |
| 3813 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3814 | splitPointerIndex += 1; |
| 3815 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3816 | action = maskedAction | |
| 3817 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3818 | } |
| 3819 | } else { |
| 3820 | // An unrelated pointer changed. |
| 3821 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3822 | } |
| 3823 | } |
| 3824 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3825 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 3826 | LOG_ALWAYS_FATAL_IF(splitDownTime != originalMotionEntry.eventTime, |
| 3827 | "Split motion event has mismatching downTime and eventTime for " |
| 3828 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64 "ms", |
| 3829 | originalMotionEntry.getDescription().c_str(), ns2ms(splitDownTime)); |
| 3830 | } |
| 3831 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3832 | int32_t newId = mIdGenerator.nextId(); |
| 3833 | if (ATRACE_ENABLED()) { |
| 3834 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3835 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3836 | originalMotionEntry.id, newId); |
| 3837 | ATRACE_NAME(message.c_str()); |
| 3838 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3839 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3840 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3841 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3842 | originalMotionEntry.displayId, |
| 3843 | originalMotionEntry.policyFlags, action, |
| 3844 | originalMotionEntry.actionButton, |
| 3845 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3846 | originalMotionEntry.buttonState, |
| 3847 | originalMotionEntry.classification, |
| 3848 | originalMotionEntry.edgeFlags, |
| 3849 | originalMotionEntry.xPrecision, |
| 3850 | originalMotionEntry.yPrecision, |
| 3851 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3852 | originalMotionEntry.yCursorPosition, splitDownTime, |
| 3853 | splitPointerCount, splitPointerProperties, |
| 3854 | splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3855 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3856 | if (originalMotionEntry.injectionState) { |
| 3857 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3858 | splitMotionEntry->injectionState->refCount += 1; |
| 3859 | } |
| 3860 | |
| 3861 | return splitMotionEntry; |
| 3862 | } |
| 3863 | |
| 3864 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3865 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3866 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3867 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3868 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3869 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3870 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3871 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3872 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3873 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3874 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3875 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3876 | } // release lock |
| 3877 | |
| 3878 | if (needWake) { |
| 3879 | mLooper->wake(); |
| 3880 | } |
| 3881 | } |
| 3882 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3883 | /** |
| 3884 | * If one of the meta shortcuts is detected, process them here: |
| 3885 | * Meta + Backspace -> generate BACK |
| 3886 | * Meta + Enter -> generate HOME |
| 3887 | * This will potentially overwrite keyCode and metaState. |
| 3888 | */ |
| 3889 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3890 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3891 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3892 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3893 | if (keyCode == AKEYCODE_DEL) { |
| 3894 | newKeyCode = AKEYCODE_BACK; |
| 3895 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3896 | newKeyCode = AKEYCODE_HOME; |
| 3897 | } |
| 3898 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3899 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3900 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3901 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3902 | keyCode = newKeyCode; |
| 3903 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3904 | } |
| 3905 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3906 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3907 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3908 | // 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] | 3909 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3910 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3911 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3912 | if (replacementIt != mReplacedKeys.end()) { |
| 3913 | keyCode = replacementIt->second; |
| 3914 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3915 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3916 | } |
| 3917 | } |
| 3918 | } |
| 3919 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3920 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3921 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3922 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3923 | "policyFlags=0x%x, action=0x%x, " |
| 3924 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3925 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3926 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3927 | args->downTime); |
| 3928 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3929 | if (!validateKeyEvent(args->action)) { |
| 3930 | return; |
| 3931 | } |
| 3932 | |
| 3933 | uint32_t policyFlags = args->policyFlags; |
| 3934 | int32_t flags = args->flags; |
| 3935 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3936 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3937 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3938 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3939 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3940 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3941 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3942 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3943 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3944 | metaState |= AMETA_FUNCTION_ON; |
| 3945 | } |
| 3946 | |
| 3947 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3948 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3949 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3950 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3951 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3952 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3953 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3954 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3955 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3956 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3957 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3958 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3959 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3960 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3961 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3962 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3963 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3964 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3965 | { // acquire lock |
| 3966 | mLock.lock(); |
| 3967 | |
| 3968 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3969 | mLock.unlock(); |
| 3970 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3971 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3972 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3973 | return; // event was consumed by the filter |
| 3974 | } |
| 3975 | |
| 3976 | mLock.lock(); |
| 3977 | } |
| 3978 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3979 | std::unique_ptr<KeyEntry> newEntry = |
| 3980 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3981 | args->displayId, policyFlags, args->action, flags, |
| 3982 | keyCode, args->scanCode, metaState, repeatCount, |
| 3983 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3984 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3985 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3986 | mLock.unlock(); |
| 3987 | } // release lock |
| 3988 | |
| 3989 | if (needWake) { |
| 3990 | mLooper->wake(); |
| 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3995 | return mInputFilterEnabled; |
| 3996 | } |
| 3997 | |
| 3998 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3999 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4000 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4001 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4002 | "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] | 4003 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4004 | "yCursorPosition=%f, downTime=%" PRId64, |
| 4005 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4006 | args->policyFlags, MotionEvent::actionToString(args->action).c_str(), |
| 4007 | args->actionButton, args->flags, args->metaState, args->buttonState, args->edgeFlags, |
| 4008 | args->xPrecision, args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 4009 | args->downTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4010 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 4011 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 4012 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 4013 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 4014 | "orientation=%f", |
| 4015 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 4016 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4017 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4018 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4019 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4020 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4021 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4022 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4023 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4024 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 4025 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4026 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4027 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 4028 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4029 | return; |
| 4030 | } |
| 4031 | |
| 4032 | uint32_t policyFlags = args->policyFlags; |
| 4033 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4034 | |
| 4035 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4036 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4037 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4038 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4039 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4040 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4041 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4042 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4043 | { // acquire lock |
| 4044 | mLock.lock(); |
| 4045 | |
| 4046 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4047 | ui::Transform displayTransform; |
| 4048 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4049 | displayTransform = it->second.transform; |
| 4050 | } |
| 4051 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4052 | mLock.unlock(); |
| 4053 | |
| 4054 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4055 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4056 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4057 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4058 | displayTransform, args->xPrecision, args->yPrecision, |
| 4059 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4060 | args->downTime, args->eventTime, args->pointerCount, |
| 4061 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4062 | |
| 4063 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4064 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4065 | return; // event was consumed by the filter |
| 4066 | } |
| 4067 | |
| 4068 | mLock.lock(); |
| 4069 | } |
| 4070 | |
| 4071 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4072 | std::unique_ptr<MotionEntry> newEntry = |
| 4073 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4074 | args->source, args->displayId, policyFlags, |
| 4075 | args->action, args->actionButton, args->flags, |
| 4076 | args->metaState, args->buttonState, |
| 4077 | args->classification, args->edgeFlags, |
| 4078 | args->xPrecision, args->yPrecision, |
| 4079 | args->xCursorPosition, args->yCursorPosition, |
| 4080 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4081 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4082 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4083 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4084 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4085 | !mInputFilterEnabled) { |
| 4086 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4087 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4088 | } |
| 4089 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4090 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4091 | mLock.unlock(); |
| 4092 | } // release lock |
| 4093 | |
| 4094 | if (needWake) { |
| 4095 | mLooper->wake(); |
| 4096 | } |
| 4097 | } |
| 4098 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4099 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4100 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4101 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4102 | " sensorType=%s", |
| 4103 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4104 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4105 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4106 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4107 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4108 | { // acquire lock |
| 4109 | mLock.lock(); |
| 4110 | |
| 4111 | // Just enqueue a new sensor event. |
| 4112 | std::unique_ptr<SensorEntry> newEntry = |
| 4113 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4114 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4115 | args->sensorType, args->accuracy, |
| 4116 | args->accuracyChanged, args->values); |
| 4117 | |
| 4118 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4119 | mLock.unlock(); |
| 4120 | } // release lock |
| 4121 | |
| 4122 | if (needWake) { |
| 4123 | mLooper->wake(); |
| 4124 | } |
| 4125 | } |
| 4126 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4127 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4128 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4129 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4130 | args->deviceId, args->isOn); |
| 4131 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4132 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4133 | } |
| 4134 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4135 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4136 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4140 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4141 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4142 | "switchMask=0x%08x", |
| 4143 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4144 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4145 | |
| 4146 | uint32_t policyFlags = args->policyFlags; |
| 4147 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4148 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4152 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4153 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4154 | args->deviceId); |
| 4155 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4156 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4157 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4158 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4159 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4160 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4161 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4162 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4163 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4164 | } // release lock |
| 4165 | |
| 4166 | if (needWake) { |
| 4167 | mLooper->wake(); |
| 4168 | } |
| 4169 | } |
| 4170 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4171 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4172 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4173 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4174 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4175 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4176 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4177 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4178 | { // acquire lock |
| 4179 | std::scoped_lock _l(mLock); |
| 4180 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4181 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4182 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4183 | } // release lock |
| 4184 | |
| 4185 | if (needWake) { |
| 4186 | mLooper->wake(); |
| 4187 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4188 | } |
| 4189 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4190 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
| 4191 | std::optional<int32_t> targetUid, |
| 4192 | InputEventInjectionSync syncMode, |
| 4193 | std::chrono::milliseconds timeout, |
| 4194 | uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4195 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4196 | ALOGD("injectInputEvent - eventType=%d, targetUid=%s, syncMode=%d, timeout=%lld, " |
| 4197 | "policyFlags=0x%08x", |
| 4198 | event->getType(), targetUid ? std::to_string(*targetUid).c_str() : "none", syncMode, |
| 4199 | timeout.count(), policyFlags); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4200 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4201 | 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] | 4202 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4203 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4204 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4205 | // 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] | 4206 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4207 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4208 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4209 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4210 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4211 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4212 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4213 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4216 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4217 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4218 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4219 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4220 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4221 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4222 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4223 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4224 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4225 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4226 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4227 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4228 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4229 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4230 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4231 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4232 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4233 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4234 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4235 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4236 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4237 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4238 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4239 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4240 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4241 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4242 | |
| 4243 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4244 | android::base::Timer t; |
| 4245 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4246 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4247 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4248 | std::to_string(t.duration().count()).c_str()); |
| 4249 | } |
| 4250 | } |
| 4251 | |
| 4252 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4253 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4254 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4255 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4256 | incomingKey.getDisplayId(), policyFlags, action, |
| 4257 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4258 | incomingKey.getRepeatCount(), |
| 4259 | incomingKey.getDownTime()); |
| 4260 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4261 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4262 | } |
| 4263 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4264 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4265 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4266 | const int32_t action = motionEvent.getAction(); |
| 4267 | const bool isPointerEvent = |
| 4268 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4269 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4270 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4271 | ? ADISPLAY_ID_DEFAULT |
| 4272 | : event->getDisplayId(); |
| 4273 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4274 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4275 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4276 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4277 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4278 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4282 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4283 | android::base::Timer t; |
| 4284 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4285 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4286 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4287 | std::to_string(t.duration().count()).c_str()); |
| 4288 | } |
| 4289 | } |
| 4290 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4291 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4292 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4293 | } |
| 4294 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4295 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4296 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4297 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4298 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4299 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4300 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4301 | displayId, policyFlags, action, actionButton, |
| 4302 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4303 | motionEvent.getButtonState(), |
| 4304 | motionEvent.getClassification(), |
| 4305 | motionEvent.getEdgeFlags(), |
| 4306 | motionEvent.getXPrecision(), |
| 4307 | motionEvent.getYPrecision(), |
| 4308 | motionEvent.getRawXCursorPosition(), |
| 4309 | motionEvent.getRawYCursorPosition(), |
| 4310 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4311 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4312 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4313 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4314 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4315 | sampleEventTimes += 1; |
| 4316 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4317 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4318 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4319 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4320 | displayId, policyFlags, action, actionButton, |
| 4321 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4322 | motionEvent.getButtonState(), |
| 4323 | motionEvent.getClassification(), |
| 4324 | motionEvent.getEdgeFlags(), |
| 4325 | motionEvent.getXPrecision(), |
| 4326 | motionEvent.getYPrecision(), |
| 4327 | motionEvent.getRawXCursorPosition(), |
| 4328 | motionEvent.getRawYCursorPosition(), |
| 4329 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4330 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4331 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4332 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4333 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4334 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4335 | } |
| 4336 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4337 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4338 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4339 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4340 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4341 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4342 | } |
| 4343 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4344 | InjectionState* injectionState = new InjectionState(targetUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4345 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4346 | injectionState->injectionIsAsync = true; |
| 4347 | } |
| 4348 | |
| 4349 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4350 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4351 | |
| 4352 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4353 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4354 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4355 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | mLock.unlock(); |
| 4359 | |
| 4360 | if (needWake) { |
| 4361 | mLooper->wake(); |
| 4362 | } |
| 4363 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4364 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4365 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4366 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4367 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4368 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4369 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4370 | } else { |
| 4371 | for (;;) { |
| 4372 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4373 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4374 | break; |
| 4375 | } |
| 4376 | |
| 4377 | nsecs_t remainingTimeout = endTime - now(); |
| 4378 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4379 | if (DEBUG_INJECTION) { |
| 4380 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4381 | "to become available."); |
| 4382 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4383 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4384 | break; |
| 4385 | } |
| 4386 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4387 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4388 | } |
| 4389 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4390 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4391 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4392 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4393 | if (DEBUG_INJECTION) { |
| 4394 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4395 | injectionState->pendingForegroundDispatches); |
| 4396 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4397 | nsecs_t remainingTimeout = endTime - now(); |
| 4398 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4399 | if (DEBUG_INJECTION) { |
| 4400 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4401 | "dispatches to finish."); |
| 4402 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4403 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4404 | break; |
| 4405 | } |
| 4406 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4407 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4408 | } |
| 4409 | } |
| 4410 | } |
| 4411 | |
| 4412 | injectionState->release(); |
| 4413 | } // release lock |
| 4414 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4415 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4416 | ALOGD("injectInputEvent - Finished with result %d.", injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4417 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4418 | |
| 4419 | return injectionResult; |
| 4420 | } |
| 4421 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4422 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4423 | std::array<uint8_t, 32> calculatedHmac; |
| 4424 | std::unique_ptr<VerifiedInputEvent> result; |
| 4425 | switch (event.getType()) { |
| 4426 | case AINPUT_EVENT_TYPE_KEY: { |
| 4427 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4428 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4429 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4430 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4431 | break; |
| 4432 | } |
| 4433 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4434 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4435 | VerifiedMotionEvent verifiedMotionEvent = |
| 4436 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4437 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4438 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4439 | break; |
| 4440 | } |
| 4441 | default: { |
| 4442 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4443 | return nullptr; |
| 4444 | } |
| 4445 | } |
| 4446 | if (calculatedHmac == INVALID_HMAC) { |
| 4447 | return nullptr; |
| 4448 | } |
| 4449 | if (calculatedHmac != event.getHmac()) { |
| 4450 | return nullptr; |
| 4451 | } |
| 4452 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4453 | } |
| 4454 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4455 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4456 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4457 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4458 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4459 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4460 | ALOGD("Setting input event injection result to %d.", injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4461 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4462 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4463 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4464 | // Log the outcome since the injector did not wait for the injection result. |
| 4465 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4466 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4467 | ALOGV("Asynchronous input event injection succeeded."); |
| 4468 | break; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4469 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4470 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4471 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4472 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4473 | ALOGW("Asynchronous input event injection failed."); |
| 4474 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4475 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4476 | ALOGW("Asynchronous input event injection timed out."); |
| 4477 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4478 | case InputEventInjectionResult::PENDING: |
| 4479 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4480 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4481 | } |
| 4482 | } |
| 4483 | |
| 4484 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4485 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4486 | } |
| 4487 | } |
| 4488 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4489 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4490 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4491 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4492 | // display space, so we need to transform the injected events accordingly. |
| 4493 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4494 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4495 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4496 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4497 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4498 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4499 | const vec2 cursor = |
| 4500 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4501 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4502 | entry.xCursorPosition = cursor.x; |
| 4503 | entry.yCursorPosition = cursor.y; |
| 4504 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4505 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4506 | entry.pointerCoords[i] = |
| 4507 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4508 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4509 | } |
| 4510 | } |
| 4511 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4512 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4513 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4514 | if (injectionState) { |
| 4515 | injectionState->pendingForegroundDispatches += 1; |
| 4516 | } |
| 4517 | } |
| 4518 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4519 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4520 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4521 | if (injectionState) { |
| 4522 | injectionState->pendingForegroundDispatches -= 1; |
| 4523 | |
| 4524 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4525 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4526 | } |
| 4527 | } |
| 4528 | } |
| 4529 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4530 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4531 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4532 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4533 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4534 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4535 | } |
| 4536 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4537 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4538 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4539 | if (windowHandleToken == nullptr) { |
| 4540 | return nullptr; |
| 4541 | } |
| 4542 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4543 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4544 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4545 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4546 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4547 | return windowHandle; |
| 4548 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4549 | } |
| 4550 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4551 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4552 | } |
| 4553 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4554 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4555 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4556 | if (windowHandleToken == nullptr) { |
| 4557 | return nullptr; |
| 4558 | } |
| 4559 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4560 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4561 | if (windowHandle->getToken() == windowHandleToken) { |
| 4562 | return windowHandle; |
| 4563 | } |
| 4564 | } |
| 4565 | return nullptr; |
| 4566 | } |
| 4567 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4568 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4569 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4570 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4571 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4572 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4573 | if (handle->getId() == windowHandle->getId() && |
| 4574 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4575 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4576 | ALOGE("Found window %s in display %" PRId32 |
| 4577 | ", but it should belong to display %" PRId32, |
| 4578 | windowHandle->getName().c_str(), it.first, |
| 4579 | windowHandle->getInfo()->displayId); |
| 4580 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4581 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4582 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4583 | } |
| 4584 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4585 | return nullptr; |
| 4586 | } |
| 4587 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4588 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4589 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4590 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4591 | } |
| 4592 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4593 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 4594 | const MotionEntry& motionEntry) const { |
| 4595 | const WindowInfo& info = *window->getInfo(); |
| 4596 | |
| 4597 | // Skip spy window targets that are not valid for targeted injection. |
| 4598 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4599 | return false; |
| 4600 | } |
| 4601 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4602 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 4603 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 4604 | return false; |
| 4605 | } |
| 4606 | |
| 4607 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 4608 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 4609 | window->getName().c_str()); |
| 4610 | return false; |
| 4611 | } |
| 4612 | |
| 4613 | sp<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4614 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4615 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 4616 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4617 | return false; |
| 4618 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4619 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4620 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4621 | 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] | 4622 | return false; |
| 4623 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4624 | |
| 4625 | // Drop events that can't be trusted due to occlusion |
| 4626 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 4627 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 4628 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 4629 | if (DEBUG_TOUCH_OCCLUSION) { |
| 4630 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 4631 | for (const auto& log : occlusionInfo.debugInfo) { |
| 4632 | ALOGD("%s", log.c_str()); |
| 4633 | } |
| 4634 | } |
| 4635 | ALOGW("Dropping untrusted touch event due to %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 4636 | occlusionInfo.obscuringUid); |
| 4637 | return false; |
| 4638 | } |
| 4639 | |
| 4640 | // Drop touch events if requested by input feature |
| 4641 | if (shouldDropInput(motionEntry, window)) { |
| 4642 | return false; |
| 4643 | } |
| 4644 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4645 | return true; |
| 4646 | } |
| 4647 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4648 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4649 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4650 | auto connectionIt = mConnectionsByToken.find(token); |
| 4651 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4652 | return nullptr; |
| 4653 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4654 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4655 | } |
| 4656 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4657 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4658 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4659 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4660 | // Remove all handles on a display if there are no windows left. |
| 4661 | mWindowHandlesByDisplay.erase(displayId); |
| 4662 | return; |
| 4663 | } |
| 4664 | |
| 4665 | // Since we compare the pointer of input window handles across window updates, we need |
| 4666 | // 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] | 4667 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4668 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4669 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4670 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4671 | } |
| 4672 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4673 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4674 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4675 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4676 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4677 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4678 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4679 | const bool canReceiveInput = |
| 4680 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4681 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4682 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4683 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4684 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4685 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4686 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4687 | } |
| 4688 | |
| 4689 | if (info->displayId != displayId) { |
| 4690 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4691 | handle->getName().c_str(), displayId, info->displayId); |
| 4692 | continue; |
| 4693 | } |
| 4694 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4695 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4696 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4697 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4698 | oldHandle->updateFrom(handle); |
| 4699 | newHandles.push_back(oldHandle); |
| 4700 | } else { |
| 4701 | newHandles.push_back(handle); |
| 4702 | } |
| 4703 | } |
| 4704 | |
| 4705 | // Insert or replace |
| 4706 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4707 | } |
| 4708 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4709 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4710 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4711 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4712 | { // acquire lock |
| 4713 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4714 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4715 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4716 | } |
| 4717 | } |
| 4718 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4719 | mLooper->wake(); |
| 4720 | } |
| 4721 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4722 | /** |
| 4723 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4724 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4725 | * If set an empty list, remove all handles from the specific display. |
| 4726 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4727 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4728 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4729 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4730 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4731 | if (DEBUG_FOCUS) { |
| 4732 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4733 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4734 | windowList += iwh->getName() + " "; |
| 4735 | } |
| 4736 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4737 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4738 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4739 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4740 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4741 | const WindowInfo& info = *window->getInfo(); |
| 4742 | |
| 4743 | // 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] | 4744 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4745 | if (noInputWindow && window->getToken() != nullptr) { |
| 4746 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4747 | window->getName().c_str()); |
| 4748 | window->releaseChannel(); |
| 4749 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4750 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4751 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4752 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 4753 | !info.inputConfig.test( |
| 4754 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4755 | "%s has feature SPY, but is not a trusted overlay.", |
| 4756 | window->getName().c_str()); |
| 4757 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4758 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4759 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 4760 | !info.inputConfig.test( |
| 4761 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4762 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4763 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4764 | } |
| 4765 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4766 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4767 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4768 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4769 | // Save the old windows' orientation by ID before it gets updated. |
| 4770 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4771 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4772 | oldWindowOrientations.emplace(handle->getId(), |
| 4773 | handle->getInfo()->transform.getOrientation()); |
| 4774 | } |
| 4775 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4776 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4777 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4778 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4779 | if (mLastHoverWindowHandle && |
| 4780 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4781 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4782 | mLastHoverWindowHandle = nullptr; |
| 4783 | } |
| 4784 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4785 | std::optional<FocusResolver::FocusChanges> changes = |
| 4786 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4787 | if (changes) { |
| 4788 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4789 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4790 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4791 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4792 | mTouchStatesByDisplay.find(displayId); |
| 4793 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4794 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4795 | for (size_t i = 0; i < state.windows.size();) { |
| 4796 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4797 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4798 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4799 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4800 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4801 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4802 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4803 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4804 | if (touchedInputChannel != nullptr) { |
| 4805 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4806 | "touched window was removed"); |
| 4807 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4808 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4809 | // well. |
| 4810 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4811 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 4812 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4813 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4814 | if (wallpaper != nullptr) { |
| 4815 | sp<Connection> wallpaperConnection = |
| 4816 | getConnectionLocked(wallpaper->getToken()); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4817 | if (wallpaperConnection != nullptr) { |
| 4818 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4819 | options); |
| 4820 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4821 | } |
| 4822 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4823 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4824 | state.windows.erase(state.windows.begin() + i); |
| 4825 | } else { |
| 4826 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4827 | } |
| 4828 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4829 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4830 | // 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] | 4831 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 4832 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4833 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4834 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 4835 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 4836 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4837 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4838 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4839 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4840 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4841 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4842 | // pointer events if necessary. |
| 4843 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4844 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4845 | if (newWindowHandle != nullptr && |
| 4846 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4847 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4848 | std::shared_ptr<InputChannel> inputChannel = |
| 4849 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4850 | if (inputChannel != nullptr) { |
| 4851 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4852 | "touched window's orientation changed"); |
| 4853 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4854 | } |
| 4855 | } |
| 4856 | } |
| 4857 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4858 | // Release information for windows that are no longer present. |
| 4859 | // This ensures that unused input channels are released promptly. |
| 4860 | // Otherwise, they might stick around until the window handle is destroyed |
| 4861 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4862 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4863 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4864 | if (DEBUG_FOCUS) { |
| 4865 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4866 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4867 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4868 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4869 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4870 | } |
| 4871 | |
| 4872 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4873 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4874 | if (DEBUG_FOCUS) { |
| 4875 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4876 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4877 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4878 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4879 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4880 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4881 | } // release lock |
| 4882 | |
| 4883 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4884 | mLooper->wake(); |
| 4885 | } |
| 4886 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4887 | void InputDispatcher::setFocusedApplicationLocked( |
| 4888 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4889 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4890 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4891 | |
| 4892 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4893 | return; // This application is already focused. No need to wake up or change anything. |
| 4894 | } |
| 4895 | |
| 4896 | // Set the new application handle. |
| 4897 | if (inputApplicationHandle != nullptr) { |
| 4898 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4899 | } else { |
| 4900 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4901 | } |
| 4902 | |
| 4903 | // No matter what the old focused application was, stop waiting on it because it is |
| 4904 | // no longer focused. |
| 4905 | resetNoFocusedWindowTimeoutLocked(); |
| 4906 | } |
| 4907 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4908 | /** |
| 4909 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4910 | * the display not specified. |
| 4911 | * |
| 4912 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4913 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4914 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4915 | * display. The display-specified events won't be affected. |
| 4916 | */ |
| 4917 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4918 | if (DEBUG_FOCUS) { |
| 4919 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4920 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4921 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4922 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4923 | |
| 4924 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4925 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4926 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4927 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4928 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4929 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4930 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4931 | CancelationOptions |
| 4932 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4933 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4934 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4935 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4936 | } |
| 4937 | } |
| 4938 | mFocusedDisplayId = displayId; |
| 4939 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4940 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4941 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4942 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4943 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4944 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4945 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4946 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4947 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4948 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4949 | } |
| 4950 | } |
| 4951 | } |
| 4952 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4953 | if (DEBUG_FOCUS) { |
| 4954 | logDispatchStateLocked(); |
| 4955 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4956 | } // release lock |
| 4957 | |
| 4958 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4959 | mLooper->wake(); |
| 4960 | } |
| 4961 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4962 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4963 | if (DEBUG_FOCUS) { |
| 4964 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4965 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4966 | |
| 4967 | bool changed; |
| 4968 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4969 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4970 | |
| 4971 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4972 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4973 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4974 | } |
| 4975 | |
| 4976 | if (mDispatchEnabled && !enabled) { |
| 4977 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4978 | } |
| 4979 | |
| 4980 | mDispatchEnabled = enabled; |
| 4981 | mDispatchFrozen = frozen; |
| 4982 | changed = true; |
| 4983 | } else { |
| 4984 | changed = false; |
| 4985 | } |
| 4986 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4987 | if (DEBUG_FOCUS) { |
| 4988 | logDispatchStateLocked(); |
| 4989 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4990 | } // release lock |
| 4991 | |
| 4992 | if (changed) { |
| 4993 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4994 | mLooper->wake(); |
| 4995 | } |
| 4996 | } |
| 4997 | |
| 4998 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4999 | if (DEBUG_FOCUS) { |
| 5000 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5001 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5002 | |
| 5003 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5004 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5005 | |
| 5006 | if (mInputFilterEnabled == enabled) { |
| 5007 | return; |
| 5008 | } |
| 5009 | |
| 5010 | mInputFilterEnabled = enabled; |
| 5011 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5012 | } // release lock |
| 5013 | |
| 5014 | // Wake up poll loop since there might be work to do to drop everything. |
| 5015 | mLooper->wake(); |
| 5016 | } |
| 5017 | |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 5018 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission, |
| 5019 | int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5020 | bool needWake = false; |
| 5021 | { |
| 5022 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5023 | ALOGD_IF(DEBUG_TOUCH_MODE, |
| 5024 | "Request to change touch mode to %s (calling pid=%d, uid=%d, " |
| 5025 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
| 5026 | toString(inTouchMode), pid, uid, toString(hasPermission), displayId, |
| 5027 | mTouchModePerDisplay.count(displayId) == 0 |
| 5028 | ? "not set" |
| 5029 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5030 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5031 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5032 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5033 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5034 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5035 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5036 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5037 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
| 5038 | ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " |
| 5039 | "window nor none of the previously interacted window", |
| 5040 | pid, uid); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5041 | return false; |
| 5042 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5043 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5044 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5045 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5046 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5047 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5048 | } // release lock |
| 5049 | |
| 5050 | if (needWake) { |
| 5051 | mLooper->wake(); |
| 5052 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5053 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5054 | } |
| 5055 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5056 | bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { |
| 5057 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5058 | if (focusedToken == nullptr) { |
| 5059 | return false; |
| 5060 | } |
| 5061 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5062 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5063 | } |
| 5064 | |
| 5065 | bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { |
| 5066 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5067 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5068 | const sp<WindowInfoHandle> windowHandle = |
| 5069 | getWindowHandleLocked(connectionToken); |
| 5070 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5071 | }) != mInteractionConnectionTokens.end(); |
| 5072 | } |
| 5073 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5074 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5075 | if (opacity < 0 || opacity > 1) { |
| 5076 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5077 | return; |
| 5078 | } |
| 5079 | |
| 5080 | std::scoped_lock lock(mLock); |
| 5081 | mMaximumObscuringOpacityForTouch = opacity; |
| 5082 | } |
| 5083 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5084 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 5085 | const sp<IBinder>& token) { |
| 5086 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5087 | for (TouchedWindow& w : state.windows) { |
| 5088 | if (w.windowHandle->getToken() == token) { |
| 5089 | return std::make_pair(&state, &w); |
| 5090 | } |
| 5091 | } |
| 5092 | } |
| 5093 | return std::make_pair(nullptr, nullptr); |
| 5094 | } |
| 5095 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5096 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5097 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5098 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5099 | if (DEBUG_FOCUS) { |
| 5100 | ALOGD("Trivial transfer to same window."); |
| 5101 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5102 | return true; |
| 5103 | } |
| 5104 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5105 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5106 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5107 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5108 | // Find the target touch state and touched window by fromToken. |
| 5109 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 5110 | if (state == nullptr || touchedWindow == nullptr) { |
| 5111 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5112 | return false; |
| 5113 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5114 | |
| 5115 | const int32_t displayId = state->displayId; |
| 5116 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5117 | if (toWindowHandle == nullptr) { |
| 5118 | ALOGW("Cannot transfer focus because to window not found."); |
| 5119 | return false; |
| 5120 | } |
| 5121 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5122 | if (DEBUG_FOCUS) { |
| 5123 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5124 | touchedWindow->windowHandle->getName().c_str(), |
| 5125 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5126 | } |
| 5127 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5128 | // Erase old window. |
| 5129 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 5130 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 5131 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5132 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5133 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5134 | nsecs_t downTimeInTarget = now(); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5135 | int32_t newTargetFlags = |
| 5136 | oldTargetFlags & (InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5137 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
| 5138 | newTargetFlags |= InputTarget::FLAG_FOREGROUND; |
| 5139 | } |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5140 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5141 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5142 | // Store the dragging window. |
| 5143 | if (isDragDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5144 | if (pointerIds.count() != 1) { |
| 5145 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5146 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5147 | return false; |
| 5148 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5149 | // Track the pointer id for drag window and generate the drag state. |
| 5150 | const int32_t id = pointerIds.firstMarkedBit(); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5151 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5152 | } |
| 5153 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5154 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5155 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5156 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5157 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5158 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5159 | CancelationOptions |
| 5160 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5161 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5162 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5163 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5164 | } |
| 5165 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5166 | if (DEBUG_FOCUS) { |
| 5167 | logDispatchStateLocked(); |
| 5168 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5169 | } // release lock |
| 5170 | |
| 5171 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5172 | mLooper->wake(); |
| 5173 | return true; |
| 5174 | } |
| 5175 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5176 | /** |
| 5177 | * Get the touched foreground window on the given display. |
| 5178 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5179 | * window is being touched. |
| 5180 | */ |
| 5181 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5182 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5183 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5184 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5185 | return nullptr; |
| 5186 | } |
| 5187 | |
| 5188 | const TouchState& state = stateIt->second; |
| 5189 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5190 | // If multiple foreground windows are touched, return nullptr |
| 5191 | for (const TouchedWindow& window : state.windows) { |
| 5192 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 5193 | if (touchedForegroundWindow != nullptr) { |
| 5194 | ALOGI("Two or more foreground windows: %s and %s", |
| 5195 | touchedForegroundWindow->getName().c_str(), |
| 5196 | window.windowHandle->getName().c_str()); |
| 5197 | return nullptr; |
| 5198 | } |
| 5199 | touchedForegroundWindow = window.windowHandle; |
| 5200 | } |
| 5201 | } |
| 5202 | return touchedForegroundWindow; |
| 5203 | } |
| 5204 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5205 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5206 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5207 | sp<IBinder> fromToken; |
| 5208 | { // acquire lock |
| 5209 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5210 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5211 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5212 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5213 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5214 | return false; |
| 5215 | } |
| 5216 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5217 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5218 | if (from == nullptr) { |
| 5219 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5220 | return false; |
| 5221 | } |
| 5222 | |
| 5223 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5224 | } // release lock |
| 5225 | |
| 5226 | return transferTouchFocus(fromToken, destChannelToken); |
| 5227 | } |
| 5228 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5229 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5230 | if (DEBUG_FOCUS) { |
| 5231 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5232 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5233 | |
| 5234 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5235 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5236 | |
| 5237 | resetKeyRepeatLocked(); |
| 5238 | releasePendingEventLocked(); |
| 5239 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5240 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5241 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5242 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5243 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5244 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5245 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5249 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5250 | dumpDispatchStateLocked(dump); |
| 5251 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5252 | std::istringstream stream(dump); |
| 5253 | std::string line; |
| 5254 | |
| 5255 | while (std::getline(stream, line, '\n')) { |
| 5256 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5257 | } |
| 5258 | } |
| 5259 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5260 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5261 | std::string dump; |
| 5262 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5263 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5264 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5265 | |
| 5266 | std::string windowName = "None"; |
| 5267 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5268 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5269 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5270 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5271 | : "token has capture without window"; |
| 5272 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5273 | 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] | 5274 | |
| 5275 | return dump; |
| 5276 | } |
| 5277 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5278 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5279 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5280 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5281 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5282 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5283 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5284 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5285 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5286 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5287 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5288 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5289 | const std::chrono::duration timeout = |
| 5290 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5291 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5292 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5293 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5294 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5295 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5296 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5297 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5298 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5299 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5300 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5301 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5302 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5303 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5304 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5305 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5306 | dump += StringPrintf(INDENT2 "%d: down=%s, split=%s, deviceId=%d, source=0x%08x\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5307 | state.displayId, toString(state.down), toString(state.split), |
| 5308 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5309 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5310 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5311 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5312 | const TouchedWindow& touchedWindow = state.windows[i]; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5313 | dump += StringPrintf(INDENT4 "%zu: name='%s', pointerIds=0x%0x, " |
| 5314 | "targetFlags=0x%x, firstDownTimeInTarget=%" PRId64 |
| 5315 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5316 | i, touchedWindow.windowHandle->getName().c_str(), |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5317 | touchedWindow.pointerIds.value, touchedWindow.targetFlags, |
| 5318 | ns2ms(touchedWindow.firstDownTimeInTarget.value_or(0))); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5319 | } |
| 5320 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5321 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5322 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5323 | } |
| 5324 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5325 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5326 | } |
| 5327 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5328 | if (mDragState) { |
| 5329 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5330 | mDragState->dump(dump, INDENT2); |
| 5331 | } |
| 5332 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5333 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5334 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5335 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5336 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5337 | const auto& displayInfo = it->second; |
| 5338 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5339 | displayInfo.logicalHeight); |
| 5340 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5341 | } else { |
| 5342 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5343 | } |
| 5344 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5345 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5346 | dump += INDENT2 "Windows:\n"; |
| 5347 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5348 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5349 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5350 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5351 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5352 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5353 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5354 | "applicationInfo.name=%s, " |
| 5355 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5356 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5357 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5358 | windowInfo->displayId, |
| 5359 | windowInfo->inputConfig.string().c_str(), |
| 5360 | windowInfo->alpha, windowInfo->frameLeft, |
| 5361 | windowInfo->frameTop, windowInfo->frameRight, |
| 5362 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5363 | windowInfo->applicationInfo.name.c_str(), |
| 5364 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5365 | dump += dumpRegion(windowInfo->touchableRegion); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5366 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5367 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5368 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5369 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5370 | millis(windowInfo->dispatchingTimeout), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5371 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5372 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5373 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5374 | } |
| 5375 | } else { |
| 5376 | dump += INDENT2 "Windows: <none>\n"; |
| 5377 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5378 | } |
| 5379 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5380 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5381 | } |
| 5382 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5383 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5384 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5385 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5386 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5387 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5388 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5389 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5390 | } |
| 5391 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5392 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5393 | |
| 5394 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5395 | if (!mRecentQueue.empty()) { |
| 5396 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5397 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5398 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5399 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5400 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5401 | } |
| 5402 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5403 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5404 | } |
| 5405 | |
| 5406 | // Dump event currently being dispatched. |
| 5407 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5408 | dump += INDENT "PendingEvent:\n"; |
| 5409 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5410 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5411 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5412 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5413 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5414 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5415 | } |
| 5416 | |
| 5417 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5418 | if (!mInboundQueue.empty()) { |
| 5419 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5420 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5421 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5422 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5423 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5424 | } |
| 5425 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5426 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5427 | } |
| 5428 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5429 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5430 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5431 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5432 | const KeyReplacement& replacement = pair.first; |
| 5433 | int32_t newKeyCode = pair.second; |
| 5434 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5435 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5436 | } |
| 5437 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5438 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5439 | } |
| 5440 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5441 | if (!mCommandQueue.empty()) { |
| 5442 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5443 | } else { |
| 5444 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5445 | } |
| 5446 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5447 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5448 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5449 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5450 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5451 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5452 | connection->inputChannel->getFd().get(), |
| 5453 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5454 | connection->getWindowName().c_str(), |
| 5455 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5456 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5457 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5458 | if (!connection->outboundQueue.empty()) { |
| 5459 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5460 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5461 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5462 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5463 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5464 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5465 | } |
| 5466 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5467 | if (!connection->waitQueue.empty()) { |
| 5468 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5469 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5470 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5471 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5472 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5473 | } |
| 5474 | } |
| 5475 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5476 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5477 | } |
| 5478 | |
| 5479 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5480 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5481 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5482 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5483 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5484 | } |
| 5485 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5486 | if (!mTouchModePerDisplay.empty()) { |
| 5487 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5488 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5489 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5490 | std::to_string(touchMode).c_str()); |
| 5491 | } |
| 5492 | } else { |
| 5493 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5494 | } |
| 5495 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5496 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5497 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5498 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5499 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5500 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5501 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5502 | } |
| 5503 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5504 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5505 | const size_t numMonitors = monitors.size(); |
| 5506 | for (size_t i = 0; i < numMonitors; i++) { |
| 5507 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5508 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5509 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5510 | dump += "\n"; |
| 5511 | } |
| 5512 | } |
| 5513 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5514 | class LooperEventCallback : public LooperCallback { |
| 5515 | public: |
| 5516 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5517 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5518 | |
| 5519 | private: |
| 5520 | std::function<int(int events)> mCallback; |
| 5521 | }; |
| 5522 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5523 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5524 | if (DEBUG_CHANNEL_CREATION) { |
| 5525 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5526 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5527 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5528 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5529 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5530 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5531 | |
| 5532 | if (result) { |
| 5533 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5534 | } |
| 5535 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5536 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5537 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5538 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5539 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5540 | sp<Connection> connection = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5541 | sp<Connection>::make(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
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 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5544 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5545 | } |
| 5546 | mConnectionsByToken.emplace(token, connection); |
| 5547 | |
| 5548 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5549 | this, std::placeholders::_1, token); |
| 5550 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5551 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5552 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5553 | } // release lock |
| 5554 | |
| 5555 | // Wake the looper because some connections have changed. |
| 5556 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5557 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5558 | } |
| 5559 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5560 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5561 | const std::string& name, |
| 5562 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5563 | std::shared_ptr<InputChannel> serverChannel; |
| 5564 | std::unique_ptr<InputChannel> clientChannel; |
| 5565 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5566 | if (result) { |
| 5567 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5568 | } |
| 5569 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5570 | { // acquire lock |
| 5571 | std::scoped_lock _l(mLock); |
| 5572 | |
| 5573 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5574 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5575 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5576 | } |
| 5577 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5578 | sp<Connection> connection = |
| 5579 | sp<Connection>::make(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5580 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5581 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5582 | |
| 5583 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5584 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5585 | } |
| 5586 | mConnectionsByToken.emplace(token, connection); |
| 5587 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5588 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5589 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5590 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5591 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5592 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5593 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5594 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5595 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5596 | // Wake the looper because some connections have changed. |
| 5597 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5598 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5599 | } |
| 5600 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5601 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5602 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5603 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5604 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5605 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5606 | if (status) { |
| 5607 | return status; |
| 5608 | } |
| 5609 | } // release lock |
| 5610 | |
| 5611 | // Wake the poll loop because removing the connection may have changed the current |
| 5612 | // synchronization state. |
| 5613 | mLooper->wake(); |
| 5614 | return OK; |
| 5615 | } |
| 5616 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5617 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5618 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5619 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5620 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5621 | // 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] | 5622 | return BAD_VALUE; |
| 5623 | } |
| 5624 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5625 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5626 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5627 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5628 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5629 | } |
| 5630 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5631 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5632 | |
| 5633 | nsecs_t currentTime = now(); |
| 5634 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5635 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5636 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5637 | return OK; |
| 5638 | } |
| 5639 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5640 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5641 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5642 | auto& [displayId, monitors] = *it; |
| 5643 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5644 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5645 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5646 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5647 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5648 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5649 | } else { |
| 5650 | ++it; |
| 5651 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5652 | } |
| 5653 | } |
| 5654 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5655 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5656 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5657 | return pilferPointersLocked(token); |
| 5658 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5659 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5660 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5661 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5662 | if (!requestingChannel) { |
| 5663 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5664 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5665 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5666 | |
| 5667 | auto [statePtr, windowPtr] = findTouchStateAndWindowLocked(token); |
| 5668 | if (statePtr == nullptr || windowPtr == nullptr || !statePtr->down) { |
| 5669 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5670 | " Ignoring."); |
| 5671 | return BAD_VALUE; |
| 5672 | } |
| 5673 | |
| 5674 | TouchState& state = *statePtr; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5675 | TouchedWindow& window = *windowPtr; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5676 | // Send cancel events to all the input channels we're stealing from. |
| 5677 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5678 | "input channel stole pointer stream"); |
| 5679 | options.deviceId = state.deviceId; |
| 5680 | options.displayId = state.displayId; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5681 | if (state.split) { |
| 5682 | // If split pointers then selectively cancel pointers otherwise cancel all pointers |
| 5683 | options.pointerIds = window.pointerIds; |
| 5684 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5685 | std::string canceledWindows; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5686 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5687 | const std::shared_ptr<InputChannel> channel = |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5688 | getInputChannelLocked(w.windowHandle->getToken()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5689 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5690 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5691 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5692 | canceledWindows += channel->getName(); |
| 5693 | } |
| 5694 | } |
| 5695 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5696 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5697 | canceledWindows.c_str()); |
| 5698 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5699 | // Prevent the gesture from being sent to any other windows. |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5700 | // This only blocks relevant pointers to be sent to other windows |
| 5701 | window.isPilferingPointers = true; |
| 5702 | |
| 5703 | if (state.split) { |
| 5704 | state.cancelPointersForWindowsExcept(window.pointerIds, token); |
| 5705 | } else { |
| 5706 | state.filterWindowsExcept(token); |
| 5707 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5708 | return OK; |
| 5709 | } |
| 5710 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5711 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5712 | { // acquire lock |
| 5713 | std::scoped_lock _l(mLock); |
| 5714 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5715 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5716 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5717 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5718 | : "token without window"); |
| 5719 | } |
| 5720 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5721 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5722 | if (focusedToken != windowToken) { |
| 5723 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5724 | enabled ? "enable" : "disable"); |
| 5725 | return; |
| 5726 | } |
| 5727 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5728 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5729 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5730 | "window has %s requested pointer capture.", |
| 5731 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5732 | return; |
| 5733 | } |
| 5734 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5735 | if (enabled) { |
| 5736 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5737 | mIneligibleDisplaysForPointerCapture.end(), |
| 5738 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5739 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5740 | return; |
| 5741 | } |
| 5742 | } |
| 5743 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5744 | setPointerCaptureLocked(enabled); |
| 5745 | } // release lock |
| 5746 | |
| 5747 | // Wake the thread to process command entries. |
| 5748 | mLooper->wake(); |
| 5749 | } |
| 5750 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5751 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5752 | { // acquire lock |
| 5753 | std::scoped_lock _l(mLock); |
| 5754 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5755 | if (!isEligible) { |
| 5756 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5757 | } |
| 5758 | } // release lock |
| 5759 | } |
| 5760 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5761 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5762 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5763 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5764 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5765 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5766 | } |
| 5767 | } |
| 5768 | } |
| 5769 | return std::nullopt; |
| 5770 | } |
| 5771 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5772 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5773 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5774 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5775 | } |
| 5776 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5777 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5778 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5779 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5780 | } |
| 5781 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5782 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5783 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5784 | } |
| 5785 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5786 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5787 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5788 | if (connection == nullptr) { |
| 5789 | return "<nullptr>"; |
| 5790 | } |
| 5791 | return connection->getInputChannelName(); |
| 5792 | } |
| 5793 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5794 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5795 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5796 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5797 | } |
| 5798 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5799 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5800 | const sp<Connection>& connection, uint32_t seq, |
| 5801 | bool handled, nsecs_t consumeTime) { |
| 5802 | // Handle post-event policy actions. |
| 5803 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5804 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5805 | return; |
| 5806 | } |
| 5807 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5808 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5809 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5810 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5811 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5812 | } |
| 5813 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5814 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5815 | connection->inputChannel->getConnectionToken(), |
| 5816 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5817 | } |
| 5818 | |
| 5819 | bool restartEvent; |
| 5820 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5821 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5822 | restartEvent = |
| 5823 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5824 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5825 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5826 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5827 | handled); |
| 5828 | } else { |
| 5829 | restartEvent = false; |
| 5830 | } |
| 5831 | |
| 5832 | // Dequeue the event and start the next cycle. |
| 5833 | // Because the lock might have been released, it is possible that the |
| 5834 | // contents of the wait queue to have been drained, so we need to double-check |
| 5835 | // a few things. |
| 5836 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5837 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5838 | dispatchEntry = *dispatchEntryIt; |
| 5839 | connection->waitQueue.erase(dispatchEntryIt); |
| 5840 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5841 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5842 | if (!connection->responsive) { |
| 5843 | connection->responsive = isConnectionResponsive(*connection); |
| 5844 | if (connection->responsive) { |
| 5845 | // The connection was unresponsive, and now it's responsive. |
| 5846 | processConnectionResponsiveLocked(*connection); |
| 5847 | } |
| 5848 | } |
| 5849 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5850 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5851 | connection->outboundQueue.push_front(dispatchEntry); |
| 5852 | traceOutboundQueueLength(*connection); |
| 5853 | } else { |
| 5854 | releaseDispatchEntry(dispatchEntry); |
| 5855 | } |
| 5856 | } |
| 5857 | |
| 5858 | // Start the next dispatch cycle for this connection. |
| 5859 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5860 | } |
| 5861 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5862 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5863 | const sp<IBinder>& newToken) { |
| 5864 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5865 | scoped_unlock unlock(mLock); |
| 5866 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5867 | }; |
| 5868 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5869 | } |
| 5870 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5871 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5872 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5873 | scoped_unlock unlock(mLock); |
| 5874 | mPolicy->notifyDropWindow(token, x, y); |
| 5875 | }; |
| 5876 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5877 | } |
| 5878 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5879 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5880 | if (connection == nullptr) { |
| 5881 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5882 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5883 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5884 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5885 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5886 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5887 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5888 | return; |
| 5889 | } |
| 5890 | /** |
| 5891 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5892 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5893 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5894 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5895 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5896 | * most useful. |
| 5897 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5898 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5899 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5900 | std::string reason = |
| 5901 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5902 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5903 | ns2ms(currentWait), |
| 5904 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5905 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5906 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5907 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5908 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5909 | |
| 5910 | // Stop waking up for events on this connection, it is already unresponsive |
| 5911 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5912 | } |
| 5913 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5914 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5915 | std::string reason = |
| 5916 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5917 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5918 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5919 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5920 | scoped_unlock unlock(mLock); |
| 5921 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5922 | }; |
| 5923 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5924 | } |
| 5925 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5926 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5927 | const std::string& reason) { |
| 5928 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5929 | updateLastAnrStateLocked(windowLabel, reason); |
| 5930 | } |
| 5931 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5932 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5933 | const std::string& reason) { |
| 5934 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5935 | updateLastAnrStateLocked(windowLabel, reason); |
| 5936 | } |
| 5937 | |
| 5938 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5939 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5940 | // 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] | 5941 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5942 | struct tm tm; |
| 5943 | localtime_r(&t, &tm); |
| 5944 | char timestr[64]; |
| 5945 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5946 | mLastAnrState.clear(); |
| 5947 | mLastAnrState += INDENT "ANR:\n"; |
| 5948 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5949 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5950 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5951 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5952 | } |
| 5953 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5954 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5955 | KeyEntry& entry) { |
| 5956 | const KeyEvent event = createKeyEvent(entry); |
| 5957 | nsecs_t delay = 0; |
| 5958 | { // release lock |
| 5959 | scoped_unlock unlock(mLock); |
| 5960 | android::base::Timer t; |
| 5961 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5962 | entry.policyFlags); |
| 5963 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5964 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5965 | std::to_string(t.duration().count()).c_str()); |
| 5966 | } |
| 5967 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5968 | |
| 5969 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5970 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5971 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5972 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5973 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5974 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5975 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5976 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5977 | } |
| 5978 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5979 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5980 | std::optional<int32_t> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5981 | std::string reason) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5982 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5983 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5984 | mPolicy->notifyWindowUnresponsive(token, pid, reason); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5985 | }; |
| 5986 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5987 | } |
| 5988 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5989 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
| 5990 | std::optional<int32_t> pid) { |
| 5991 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5992 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5993 | mPolicy->notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5994 | }; |
| 5995 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5996 | } |
| 5997 | |
| 5998 | /** |
| 5999 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6000 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6001 | * command entry to the command queue. |
| 6002 | */ |
| 6003 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6004 | std::string reason) { |
| 6005 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6006 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6007 | if (connection.monitor) { |
| 6008 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6009 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6010 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6011 | } else { |
| 6012 | // The connection is a window |
| 6013 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6014 | reason.c_str()); |
| 6015 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6016 | if (handle != nullptr) { |
| 6017 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6018 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6019 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6020 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6021 | } |
| 6022 | |
| 6023 | /** |
| 6024 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6025 | */ |
| 6026 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 6027 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6028 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6029 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6030 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6031 | } else { |
| 6032 | // The connection is a window |
| 6033 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6034 | if (handle != nullptr) { |
| 6035 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6036 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6037 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6038 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6039 | } |
| 6040 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6041 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6042 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6043 | KeyEntry& keyEntry, bool handled) { |
| 6044 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6045 | if (!handled) { |
| 6046 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6047 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6048 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6049 | return false; |
| 6050 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6051 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6052 | // Get the fallback key state. |
| 6053 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6054 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6055 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6056 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6057 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6058 | } |
| 6059 | |
| 6060 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6061 | // If the application handles the original key for which we previously |
| 6062 | // generated a fallback or if the window is not a foreground window, |
| 6063 | // then cancel the associated fallback key, if any. |
| 6064 | if (fallbackKeyCode != -1) { |
| 6065 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6066 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6067 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6068 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6069 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6070 | keyEntry.policyFlags); |
| 6071 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6072 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6073 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6074 | |
| 6075 | mLock.unlock(); |
| 6076 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6077 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6078 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6079 | |
| 6080 | mLock.lock(); |
| 6081 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6082 | // Cancel the fallback key. |
| 6083 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6084 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6085 | "application handled the original non-fallback key " |
| 6086 | "or is no longer a foreground target, " |
| 6087 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6088 | options.keyCode = fallbackKeyCode; |
| 6089 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6090 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6091 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6092 | } |
| 6093 | } else { |
| 6094 | // If the application did not handle a non-fallback key, first check |
| 6095 | // that we are in a good state to perform unhandled key event processing |
| 6096 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6097 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6098 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6099 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6100 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6101 | "since this is not an initial down. " |
| 6102 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6103 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6104 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6105 | return false; |
| 6106 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6107 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6108 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6109 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6110 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6111 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6112 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6113 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6114 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6115 | |
| 6116 | mLock.unlock(); |
| 6117 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6118 | bool fallback = |
| 6119 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6120 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6121 | |
| 6122 | mLock.lock(); |
| 6123 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6124 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6125 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6126 | return false; |
| 6127 | } |
| 6128 | |
| 6129 | // Latch the fallback keycode for this key on an initial down. |
| 6130 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6131 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6132 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6133 | fallbackKeyCode = event.getKeyCode(); |
| 6134 | } else { |
| 6135 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6136 | } |
| 6137 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6138 | } |
| 6139 | |
| 6140 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6141 | |
| 6142 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6143 | // We will continue to dispatch the key to the policy but we will no |
| 6144 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6145 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6146 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6147 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6148 | if (fallback) { |
| 6149 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6150 | "as a fallback for %d, but on the DOWN it had requested " |
| 6151 | "to send %d instead. Fallback canceled.", |
| 6152 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6153 | } else { |
| 6154 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6155 | "but on the DOWN it had requested to send %d. " |
| 6156 | "Fallback canceled.", |
| 6157 | originalKeyCode, fallbackKeyCode); |
| 6158 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6159 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6160 | |
| 6161 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6162 | "canceling fallback, policy no longer desires it"); |
| 6163 | options.keyCode = fallbackKeyCode; |
| 6164 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6165 | |
| 6166 | fallback = false; |
| 6167 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6168 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6169 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6170 | } |
| 6171 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6172 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6173 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6174 | { |
| 6175 | std::string msg; |
| 6176 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6177 | connection->inputState.getFallbackKeys(); |
| 6178 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6179 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6180 | } |
| 6181 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6182 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6183 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6184 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6185 | |
| 6186 | if (fallback) { |
| 6187 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6188 | keyEntry.eventTime = event.getEventTime(); |
| 6189 | keyEntry.deviceId = event.getDeviceId(); |
| 6190 | keyEntry.source = event.getSource(); |
| 6191 | keyEntry.displayId = event.getDisplayId(); |
| 6192 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6193 | keyEntry.keyCode = fallbackKeyCode; |
| 6194 | keyEntry.scanCode = event.getScanCode(); |
| 6195 | keyEntry.metaState = event.getMetaState(); |
| 6196 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6197 | keyEntry.downTime = event.getDownTime(); |
| 6198 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6199 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6200 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6201 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6202 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6203 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6204 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6205 | return true; // restart the event |
| 6206 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6207 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6208 | ALOGD("Unhandled key event: No fallback key."); |
| 6209 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6210 | |
| 6211 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6212 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6213 | } |
| 6214 | } |
| 6215 | return false; |
| 6216 | } |
| 6217 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6218 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6219 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6220 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6221 | return false; |
| 6222 | } |
| 6223 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6224 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6225 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6226 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6227 | } |
| 6228 | } |
| 6229 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6230 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6231 | if (ATRACE_ENABLED()) { |
| 6232 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6233 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6234 | ATRACE_INT(counterName, connection.outboundQueue.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::traceWaitQueueLength(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), "wq:%s", connection.getWindowName().c_str()); |
| 6242 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6243 | } |
| 6244 | } |
| 6245 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6246 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6247 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6248 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6249 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6250 | dumpDispatchStateLocked(dump); |
| 6251 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6252 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6253 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6254 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6255 | } |
| 6256 | } |
| 6257 | |
| 6258 | void InputDispatcher::monitor() { |
| 6259 | // 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] | 6260 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6261 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6262 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6263 | } |
| 6264 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6265 | /** |
| 6266 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6267 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6268 | * this method can be safely called from any thread, as long as you've ensured that |
| 6269 | * the work you are interested in completing has already been queued. |
| 6270 | */ |
| 6271 | bool InputDispatcher::waitForIdle() { |
| 6272 | /** |
| 6273 | * Timeout should represent the longest possible time that a device might spend processing |
| 6274 | * events and commands. |
| 6275 | */ |
| 6276 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6277 | std::unique_lock lock(mLock); |
| 6278 | mLooper->wake(); |
| 6279 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6280 | return result == std::cv_status::no_timeout; |
| 6281 | } |
| 6282 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6283 | /** |
| 6284 | * Sets focus to the window identified by the token. This must be called |
| 6285 | * after updating any input window handles. |
| 6286 | * |
| 6287 | * Params: |
| 6288 | * request.token - input channel token used to identify the window that should gain focus. |
| 6289 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6290 | * specified token does not match the currently focused window, this request will be dropped. |
| 6291 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6292 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6293 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6294 | * when requesting the focus change. This determines which request gets |
| 6295 | * precedence if there is a focus change request from another source such as pointer down. |
| 6296 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6297 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6298 | { // acquire lock |
| 6299 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6300 | std::optional<FocusResolver::FocusChanges> changes = |
| 6301 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6302 | if (changes) { |
| 6303 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6304 | } |
| 6305 | } // release lock |
| 6306 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6307 | mLooper->wake(); |
| 6308 | } |
| 6309 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6310 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6311 | if (changes.oldFocus) { |
| 6312 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6313 | if (focusedInputChannel) { |
| 6314 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6315 | "focus left window"); |
| 6316 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6317 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6318 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6319 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6320 | if (changes.newFocus) { |
| 6321 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6322 | } |
| 6323 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6324 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6325 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6326 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6327 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6328 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6329 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6330 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6331 | // front. |
| 6332 | disablePointerCaptureForcedLocked(); |
| 6333 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6334 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6335 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6336 | } |
| 6337 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6338 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6339 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6340 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6341 | return; |
| 6342 | } |
| 6343 | |
| 6344 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6345 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6346 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6347 | setPointerCaptureLocked(false); |
| 6348 | } |
| 6349 | |
| 6350 | if (!mWindowTokenWithPointerCapture) { |
| 6351 | // No need to send capture changes because no window has capture. |
| 6352 | return; |
| 6353 | } |
| 6354 | |
| 6355 | if (mPendingEvent != nullptr) { |
| 6356 | // Move the pending event to the front of the queue. This will give the chance |
| 6357 | // for the pending event to be dropped if it is a captured event. |
| 6358 | mInboundQueue.push_front(mPendingEvent); |
| 6359 | mPendingEvent = nullptr; |
| 6360 | } |
| 6361 | |
| 6362 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6363 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6364 | mInboundQueue.push_front(std::move(entry)); |
| 6365 | } |
| 6366 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6367 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6368 | mCurrentPointerCaptureRequest.enable = enable; |
| 6369 | mCurrentPointerCaptureRequest.seq++; |
| 6370 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6371 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6372 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6373 | }; |
| 6374 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6375 | } |
| 6376 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6377 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6378 | { // acquire lock |
| 6379 | std::scoped_lock _l(mLock); |
| 6380 | // Set an empty list to remove all handles from the specific display. |
| 6381 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6382 | setFocusedApplicationLocked(displayId, nullptr); |
| 6383 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6384 | // have been removed for the removed display. |
| 6385 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6386 | // Reset pointer capture eligibility, regardless of previous state. |
| 6387 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6388 | // Remove the associated touch mode state. |
| 6389 | mTouchModePerDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6390 | } // release lock |
| 6391 | |
| 6392 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6393 | mLooper->wake(); |
| 6394 | } |
| 6395 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6396 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6397 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6398 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6399 | // more convenient parsing. |
| 6400 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6401 | for (const auto& info : windowInfos) { |
| 6402 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6403 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6404 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6405 | |
| 6406 | { // acquire lock |
| 6407 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6408 | |
| 6409 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6410 | // no windows, we can tell that the windows were removed from the display. |
| 6411 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6412 | handlesPerDisplay[displayId]; |
| 6413 | } |
| 6414 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6415 | mDisplayInfos.clear(); |
| 6416 | for (const auto& displayInfo : displayInfos) { |
| 6417 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6418 | } |
| 6419 | |
| 6420 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6421 | setInputWindowsLocked(handles, displayId); |
| 6422 | } |
| 6423 | } |
| 6424 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6425 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6426 | } |
| 6427 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6428 | bool InputDispatcher::shouldDropInput( |
| 6429 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6430 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6431 | (windowHandle->getInfo()->inputConfig.test( |
| 6432 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6433 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6434 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6435 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6436 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6437 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6438 | windowHandle->getInfo()->displayId); |
| 6439 | return true; |
| 6440 | } |
| 6441 | return false; |
| 6442 | } |
| 6443 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6444 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6445 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6446 | const std::vector<DisplayInfo>& displayInfos) { |
| 6447 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6448 | } |
| 6449 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6450 | void InputDispatcher::cancelCurrentTouch() { |
| 6451 | { |
| 6452 | std::scoped_lock _l(mLock); |
| 6453 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
| 6454 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 6455 | "cancel current touch"); |
| 6456 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6457 | |
| 6458 | mTouchStatesByDisplay.clear(); |
| 6459 | mLastHoverWindowHandle.clear(); |
| 6460 | } |
| 6461 | // Wake up poll loop since there might be work to do. |
| 6462 | mLooper->wake(); |
| 6463 | } |
| 6464 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6465 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6466 | std::scoped_lock _l(mLock); |
| 6467 | mMonitorDispatchingTimeout = timeout; |
| 6468 | } |
| 6469 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6470 | } // namespace android::inputdispatcher |