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> |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 28 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 29 | #include <input/InputDevice.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 30 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 31 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 32 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 33 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 34 | #include <cerrno> |
| 35 | #include <cinttypes> |
| 36 | #include <climits> |
| 37 | #include <cstddef> |
| 38 | #include <ctime> |
| 39 | #include <queue> |
| 40 | #include <sstream> |
| 41 | |
| 42 | #include "Connection.h" |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 43 | #include "DebugConfig.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 44 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 45 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 46 | #define INDENT " " |
| 47 | #define INDENT2 " " |
| 48 | #define INDENT3 " " |
| 49 | #define INDENT4 " " |
| 50 | |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 51 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 52 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 53 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 54 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 55 | using android::gui::FocusRequest; |
| 56 | using android::gui::TouchOcclusionMode; |
| 57 | using android::gui::WindowInfo; |
| 58 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 59 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 60 | using android::os::InputEventInjectionResult; |
| 61 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 62 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 63 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 65 | namespace { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 66 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 67 | // Named to match std::scoped_lock |
| 68 | class scoped_unlock { |
| 69 | public: |
| 70 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 71 | ~scoped_unlock() { mMutex.lock(); } |
| 72 | |
| 73 | private: |
| 74 | std::mutex& mMutex; |
| 75 | }; |
| 76 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 77 | // Default input dispatching timeout if there is no focused application or paused window |
| 78 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 79 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 80 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 81 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 82 | |
| 83 | // Amount of time to allow for all pending events to be processed when an app switch |
| 84 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 85 | // 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] | 86 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 87 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 88 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 89 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 90 | // 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] | 91 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 92 | |
| 93 | // Log a warning when an interception call takes longer than this to process. |
| 94 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 95 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 96 | // Additional key latency in case a connection is still processing some motion events. |
| 97 | // This will help with the case when a user touched a button that opens a new window, |
| 98 | // and gives us the chance to dispatch the key to this new window. |
| 99 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 100 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 101 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 102 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 103 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 104 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 105 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 106 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 107 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 108 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 109 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 110 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 111 | } |
| 112 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 113 | inline const char* toString(bool value) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 114 | return value ? "true" : "false"; |
| 115 | } |
| 116 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 117 | inline const std::string toString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 118 | if (binder == nullptr) { |
| 119 | return "<null>"; |
| 120 | } |
| 121 | return StringPrintf("%p", binder.get()); |
| 122 | } |
| 123 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 124 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 125 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 126 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 129 | bool isValidKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 130 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 131 | case AKEY_EVENT_ACTION_DOWN: |
| 132 | case AKEY_EVENT_ACTION_UP: |
| 133 | return true; |
| 134 | default: |
| 135 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 139 | bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 140 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 141 | ALOGE("Key event has invalid action code 0x%x", action); |
| 142 | return false; |
| 143 | } |
| 144 | return true; |
| 145 | } |
| 146 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 147 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 148 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 149 | case AMOTION_EVENT_ACTION_DOWN: |
| 150 | case AMOTION_EVENT_ACTION_UP: |
| 151 | case AMOTION_EVENT_ACTION_CANCEL: |
| 152 | case AMOTION_EVENT_ACTION_MOVE: |
| 153 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 154 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 155 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 156 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 157 | case AMOTION_EVENT_ACTION_SCROLL: |
| 158 | return true; |
| 159 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 160 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 161 | int32_t index = getMotionEventActionPointerIndex(action); |
| 162 | return index >= 0 && index < pointerCount; |
| 163 | } |
| 164 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 165 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 166 | return actionButton != 0; |
| 167 | default: |
| 168 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 172 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 173 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 174 | } |
| 175 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 176 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 177 | const PointerProperties* pointerProperties) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 178 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 179 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 180 | return false; |
| 181 | } |
| 182 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 183 | 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] | 184 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 185 | return false; |
| 186 | } |
| 187 | BitSet32 pointerIdBits; |
| 188 | for (size_t i = 0; i < pointerCount; i++) { |
| 189 | int32_t id = pointerProperties[i].id; |
| 190 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 191 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 192 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | if (pointerIdBits.hasBit(id)) { |
| 196 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 197 | return false; |
| 198 | } |
| 199 | pointerIdBits.markBit(id); |
| 200 | } |
| 201 | return true; |
| 202 | } |
| 203 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 204 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 205 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 206 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 209 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 210 | bool first = true; |
| 211 | Region::const_iterator cur = region.begin(); |
| 212 | Region::const_iterator const tail = region.end(); |
| 213 | while (cur != tail) { |
| 214 | if (first) { |
| 215 | first = false; |
| 216 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 217 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 218 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 219 | 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] | 220 | cur++; |
| 221 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 222 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 225 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 226 | constexpr size_t maxEntries = 50; // max events to print |
| 227 | constexpr size_t skipBegin = maxEntries / 2; |
| 228 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 229 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 230 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 231 | |
| 232 | std::string dump; |
| 233 | for (size_t i = 0; i < queue.size(); i++) { |
| 234 | const DispatchEntry& entry = *queue[i]; |
| 235 | if (i >= skipBegin && i < skipEnd) { |
| 236 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 237 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 238 | continue; |
| 239 | } |
| 240 | dump.append(INDENT4); |
| 241 | dump += entry.eventEntry->getDescription(); |
| 242 | dump += StringPrintf(", seq=%" PRIu32 |
| 243 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 244 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 245 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 246 | if (entry.deliveryTime != 0) { |
| 247 | // This entry was delivered, so add information on how long we've been waiting |
| 248 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 249 | } |
| 250 | dump.append("\n"); |
| 251 | } |
| 252 | return dump; |
| 253 | } |
| 254 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Find the entry in std::unordered_map by key, and return it. |
| 257 | * If the entry is not found, return a default constructed entry. |
| 258 | * |
| 259 | * Useful when the entries are vectors, since an empty vector will be returned |
| 260 | * if the entry is not found. |
| 261 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 262 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 263 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 264 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 265 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 266 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 267 | } |
| 268 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 269 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 270 | if (first == second) { |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | if (first == nullptr || second == nullptr) { |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | return first->getToken() == second->getToken(); |
| 279 | } |
| 280 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 281 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 282 | if (first == nullptr || second == nullptr) { |
| 283 | return false; |
| 284 | } |
| 285 | return first->applicationInfo.token != nullptr && |
| 286 | first->applicationInfo.token == second->applicationInfo.token; |
| 287 | } |
| 288 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 289 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 290 | std::shared_ptr<EventEntry> eventEntry, |
| 291 | int32_t inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 292 | if (inputTarget.useDefaultPointerTransform()) { |
| 293 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 294 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 295 | inputTarget.displayTransform, |
| 296 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 300 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 301 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 302 | std::vector<PointerCoords> pointerCoords; |
| 303 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 304 | |
| 305 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 306 | // 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] | 307 | // uses the transform for the normalized pointer. |
| 308 | const ui::Transform& firstPointerTransform = |
| 309 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 310 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 311 | |
| 312 | // Iterate through all pointers in the event to normalize against the first. |
| 313 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 314 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 315 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 316 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 317 | |
| 318 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 319 | // First, apply the current pointer's transform to update the coordinates into |
| 320 | // window space. |
| 321 | pointerCoords[pointerIndex].transform(currTransform); |
| 322 | // Next, apply the inverse transform of the normalized coordinates so the |
| 323 | // current coordinates are transformed into the normalized coordinate space. |
| 324 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 327 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 328 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 329 | motionEntry.deviceId, motionEntry.source, |
| 330 | motionEntry.displayId, motionEntry.policyFlags, |
| 331 | motionEntry.action, motionEntry.actionButton, |
| 332 | motionEntry.flags, motionEntry.metaState, |
| 333 | motionEntry.buttonState, motionEntry.classification, |
| 334 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 335 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 336 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 337 | motionEntry.pointerCount, motionEntry.pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 338 | pointerCoords.data()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 339 | |
| 340 | if (motionEntry.injectionState) { |
| 341 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 342 | combinedMotionEntry->injectionState->refCount += 1; |
| 343 | } |
| 344 | |
| 345 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 346 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 347 | firstPointerTransform, inputTarget.displayTransform, |
| 348 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 349 | return dispatchEntry; |
| 350 | } |
| 351 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 352 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 353 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 354 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 355 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 356 | |
| 357 | serverChannel = std::move(uniqueServerChannel); |
| 358 | return result; |
| 359 | } |
| 360 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 361 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 362 | 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] | 363 | if (lhs == nullptr && rhs == nullptr) { |
| 364 | return true; |
| 365 | } |
| 366 | if (lhs == nullptr || rhs == nullptr) { |
| 367 | return false; |
| 368 | } |
| 369 | return *lhs == *rhs; |
| 370 | } |
| 371 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 372 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 373 | KeyEvent event; |
| 374 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 375 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 376 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 377 | return event; |
| 378 | } |
| 379 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 380 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 381 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 382 | // affect the statistics. |
| 383 | if (connection.monitor) { |
| 384 | return false; |
| 385 | } |
| 386 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 387 | if (!connection.responsive) { |
| 388 | return false; |
| 389 | } |
| 390 | return true; |
| 391 | } |
| 392 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 393 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 394 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 395 | const int32_t& inputEventId = eventEntry.id; |
| 396 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 397 | // Event was transmuted |
| 398 | return false; |
| 399 | } |
| 400 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 401 | return false; |
| 402 | } |
| 403 | // Only track latency for events that originated from hardware |
| 404 | if (eventEntry.isSynthesized()) { |
| 405 | return false; |
| 406 | } |
| 407 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 408 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 409 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 410 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 411 | return false; |
| 412 | } |
| 413 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 414 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 415 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 416 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 417 | return false; |
| 418 | } |
| 419 | } else { |
| 420 | // Not a key or a motion |
| 421 | return false; |
| 422 | } |
| 423 | if (!shouldReportMetricsForConnection(connection)) { |
| 424 | return false; |
| 425 | } |
| 426 | return true; |
| 427 | } |
| 428 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 429 | /** |
| 430 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 431 | * current time. |
| 432 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 433 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 434 | const nsecs_t currentTime = now(); |
| 435 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 436 | if (entry->timeoutTime < currentTime) { |
| 437 | return false; |
| 438 | } |
| 439 | } |
| 440 | return true; |
| 441 | } |
| 442 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 443 | // Returns true if the event type passed as argument represents a user activity. |
| 444 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 445 | switch (eventEntry.type) { |
| 446 | case EventEntry::Type::FOCUS: |
| 447 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 448 | case EventEntry::Type::DRAG: |
| 449 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 450 | case EventEntry::Type::SENSOR: |
| 451 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 452 | return false; |
| 453 | case EventEntry::Type::DEVICE_RESET: |
| 454 | case EventEntry::Type::KEY: |
| 455 | case EventEntry::Type::MOTION: |
| 456 | return true; |
| 457 | } |
| 458 | } |
| 459 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 460 | // 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] | 461 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, int32_t x, int32_t y, |
| 462 | bool isStylus) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 463 | const auto inputConfig = windowInfo.inputConfig; |
| 464 | if (windowInfo.displayId != displayId || |
| 465 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 466 | return false; |
| 467 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 468 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 469 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 470 | return false; |
| 471 | } |
Prabir Pradhan | 0634904 | 2022-02-04 09:19:17 -0800 | [diff] [blame] | 472 | if (!windowInfo.touchableRegionContainsPoint(x, y)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | return true; |
| 476 | } |
| 477 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 478 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 479 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
| 480 | (entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || |
| 481 | entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_ERASER); |
| 482 | } |
| 483 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 484 | // Determines if the given window can be targeted as InputTarget::FLAG_FOREGROUND. |
| 485 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 486 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 487 | // be sent to such a window, but it is not a foreground event and doesn't use |
| 488 | // InputTarget::FLAG_FOREGROUND. |
| 489 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 490 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 491 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 492 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 493 | } |
| 494 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 495 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, int32_t pid, int32_t uid) { |
| 496 | if (windowHandle == nullptr) { |
| 497 | return false; |
| 498 | } |
| 499 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 500 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 501 | return true; |
| 502 | } |
| 503 | return false; |
| 504 | } |
| 505 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 506 | // Checks targeted injection using the window's owner's uid. |
| 507 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 508 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 509 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 510 | const EventEntry& entry) { |
| 511 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 512 | // The event was not injected, or the injected event does not target a window. |
| 513 | return {}; |
| 514 | } |
| 515 | const int32_t uid = *entry.injectionState->targetUid; |
| 516 | if (window == nullptr) { |
| 517 | return StringPrintf("No valid window target for injection into uid %d.", uid); |
| 518 | } |
| 519 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
| 520 | return StringPrintf("Injected event targeted at uid %d would be dispatched to window '%s' " |
| 521 | "owned by uid %d.", |
| 522 | uid, window->getName().c_str(), window->getInfo()->ownerUid); |
| 523 | } |
| 524 | return {}; |
| 525 | } |
| 526 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 527 | } // namespace |
| 528 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 529 | // --- InputDispatcher --- |
| 530 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 531 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 532 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} |
| 533 | |
| 534 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy, |
| 535 | std::chrono::nanoseconds staleEventTimeout) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 536 | : mPolicy(policy), |
| 537 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 538 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 539 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 540 | mAppSwitchSawKeyDown(false), |
| 541 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 542 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 543 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 544 | mDispatchEnabled(false), |
| 545 | mDispatchFrozen(false), |
| 546 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 547 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 548 | // To avoid leaking stack in case that call never comes, and for tests, |
| 549 | // initialize it here anyways. |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 550 | mInTouchMode(kDefaultInTouchMode), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 551 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 552 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 553 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 554 | mStaleEventTimeout(staleEventTimeout), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 555 | mLatencyAggregator(), |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 556 | mLatencyTracker(&mLatencyAggregator), |
| 557 | kPerDisplayTouchModeEnabled(mPolicy->isPerDisplayTouchModeEnabled()) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 558 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 559 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 560 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 561 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 562 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 563 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 564 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 565 | |
| 566 | policy->getDispatcherConfiguration(&mConfig); |
| 567 | } |
| 568 | |
| 569 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 570 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 571 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 572 | resetKeyRepeatLocked(); |
| 573 | releasePendingEventLocked(); |
| 574 | drainInboundQueueLocked(); |
| 575 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 576 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 577 | while (!mConnectionsByToken.empty()) { |
| 578 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 579 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 580 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 584 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 585 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 586 | return ALREADY_EXISTS; |
| 587 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 588 | mThread = std::make_unique<InputThread>( |
| 589 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 590 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 594 | if (mThread && mThread->isCallingThread()) { |
| 595 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 596 | return INVALID_OPERATION; |
| 597 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 598 | mThread.reset(); |
| 599 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | void InputDispatcher::dispatchOnce() { |
| 603 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 604 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 605 | std::scoped_lock _l(mLock); |
| 606 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 607 | |
| 608 | // Run a dispatch loop if there are no pending commands. |
| 609 | // The dispatch loop might enqueue commands to run afterwards. |
| 610 | if (!haveCommandsLocked()) { |
| 611 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 612 | } |
| 613 | |
| 614 | // Run all pending commands if there are any. |
| 615 | // 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] | 616 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 617 | nextWakeupTime = LONG_LONG_MIN; |
| 618 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 619 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 620 | // If we are still waiting for ack on some events, |
| 621 | // we might have to wake up earlier to check if an app is anr'ing. |
| 622 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 623 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 624 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 625 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 626 | // pending or queued events |
| 627 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 628 | mDispatcherEnteredIdle.notify_all(); |
| 629 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 630 | } // release lock |
| 631 | |
| 632 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 633 | nsecs_t currentTime = now(); |
| 634 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 635 | mLooper->pollOnce(timeoutMillis); |
| 636 | } |
| 637 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 638 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 639 | * Raise ANR if there is no focused window. |
| 640 | * Before the ANR is raised, do a final state check: |
| 641 | * 1. The currently focused application must be the same one we are waiting for. |
| 642 | * 2. Ensure we still don't have a focused window. |
| 643 | */ |
| 644 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 645 | // Check if the application that we are waiting for is still focused. |
| 646 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 647 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 648 | if (focusedApplication == nullptr || |
| 649 | focusedApplication->getApplicationToken() != |
| 650 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 651 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 652 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 653 | focusedApplication->getName().c_str()); |
| 654 | return; // The focused application has changed. |
| 655 | } |
| 656 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 657 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 658 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 659 | if (focusedWindowHandle != nullptr) { |
| 660 | return; // We now have a focused window. No need for ANR. |
| 661 | } |
Vishnu Nair | 2f5bc8b | 2022-08-09 00:03:11 +0000 | [diff] [blame] | 662 | std::optional<FocusRequest> pendingRequest = |
| 663 | mFocusResolver.getFocusRequest(mAwaitedApplicationDisplayId); |
| 664 | if (pendingRequest.has_value() && onAnrLocked(*pendingRequest)) { |
| 665 | // We don't have a focusable window but we know which window should have |
| 666 | // been focused. Blame that process in case it doesn't belong to the focused app. |
| 667 | return; |
| 668 | } |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 669 | onAnrLocked(mAwaitedFocusedApplication); |
| 670 | } |
| 671 | |
| 672 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 673 | * Check if any of the connections' wait queues have events that are too old. |
| 674 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 675 | * Return the time at which we should wake up next. |
| 676 | */ |
| 677 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 678 | const nsecs_t currentTime = now(); |
| 679 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 680 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 681 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 682 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 683 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 684 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 685 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 686 | return LONG_LONG_MIN; |
| 687 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 688 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 689 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | // Check if any connection ANRs are due |
| 694 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 695 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 696 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 697 | } |
| 698 | |
| 699 | // If we reached here, we have an unresponsive connection. |
| 700 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 701 | if (connection == nullptr) { |
| 702 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 703 | return nextAnrCheck; |
| 704 | } |
| 705 | connection->responsive = false; |
| 706 | // Stop waking up for this unresponsive connection |
| 707 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 708 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 709 | return LONG_LONG_MIN; |
| 710 | } |
| 711 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 712 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
| 713 | const sp<Connection>& connection) { |
| 714 | if (connection->monitor) { |
| 715 | return mMonitorDispatchingTimeout; |
| 716 | } |
| 717 | const sp<WindowInfoHandle> window = |
| 718 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 719 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 720 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 721 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 722 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 725 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 726 | nsecs_t currentTime = now(); |
| 727 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 728 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 729 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 730 | // repeat if the device is just coming out of sleep. |
| 731 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 732 | resetKeyRepeatLocked(); |
| 733 | } |
| 734 | |
| 735 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 736 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 737 | if (DEBUG_FOCUS) { |
| 738 | ALOGD("Dispatch frozen. Waiting some more."); |
| 739 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 740 | return; |
| 741 | } |
| 742 | |
| 743 | // Optimize latency of app switches. |
| 744 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 745 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 746 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 747 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 748 | *nextWakeupTime = mAppSwitchDueTime; |
| 749 | } |
| 750 | |
| 751 | // Ready to start a new event. |
| 752 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 753 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 754 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 755 | if (isAppSwitchDue) { |
| 756 | // The inbound queue is empty so the app switch key we were waiting |
| 757 | // for will never arrive. Stop waiting for it. |
| 758 | resetPendingAppSwitchLocked(false); |
| 759 | isAppSwitchDue = false; |
| 760 | } |
| 761 | |
| 762 | // Synthesize a key repeat if appropriate. |
| 763 | if (mKeyRepeatState.lastKeyEntry) { |
| 764 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 765 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 766 | } else { |
| 767 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 768 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | // Nothing to do if there is no pending event. |
| 774 | if (!mPendingEvent) { |
| 775 | return; |
| 776 | } |
| 777 | } else { |
| 778 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 779 | mPendingEvent = mInboundQueue.front(); |
| 780 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 781 | traceInboundQueueLengthLocked(); |
| 782 | } |
| 783 | |
| 784 | // Poke user activity for this event. |
| 785 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 786 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 787 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | // Now we have an event to dispatch. |
| 791 | // 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] | 792 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 793 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 794 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 795 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 796 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 797 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 798 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 802 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 806 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 807 | const ConfigurationChangedEntry& typedEntry = |
| 808 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 809 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 810 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 811 | break; |
| 812 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 813 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 814 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 815 | const DeviceResetEntry& typedEntry = |
| 816 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 817 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 818 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 819 | break; |
| 820 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 821 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 822 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 823 | std::shared_ptr<FocusEntry> typedEntry = |
| 824 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 825 | dispatchFocusLocked(currentTime, typedEntry); |
| 826 | done = true; |
| 827 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 828 | break; |
| 829 | } |
| 830 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 831 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 832 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 833 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 834 | done = true; |
| 835 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 836 | break; |
| 837 | } |
| 838 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 839 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 840 | const auto typedEntry = |
| 841 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 842 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 843 | done = true; |
| 844 | break; |
| 845 | } |
| 846 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 847 | case EventEntry::Type::DRAG: { |
| 848 | std::shared_ptr<DragEntry> typedEntry = |
| 849 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 850 | dispatchDragLocked(currentTime, typedEntry); |
| 851 | done = true; |
| 852 | break; |
| 853 | } |
| 854 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 855 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 856 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 857 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 858 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 859 | resetPendingAppSwitchLocked(true); |
| 860 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 861 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 862 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 863 | } |
| 864 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 865 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 866 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 867 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 868 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 869 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 870 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 871 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 872 | break; |
| 873 | } |
| 874 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 875 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 876 | std::shared_ptr<MotionEntry> motionEntry = |
| 877 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 878 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 879 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 880 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 881 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
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 = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 888 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 889 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 890 | |
| 891 | case EventEntry::Type::SENSOR: { |
| 892 | std::shared_ptr<SensorEntry> sensorEntry = |
| 893 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 894 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 895 | dropReason = DropReason::APP_SWITCH; |
| 896 | } |
| 897 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 898 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 899 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 900 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 901 | dropReason = DropReason::STALE; |
| 902 | } |
| 903 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 904 | done = true; |
| 905 | break; |
| 906 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 910 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 911 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 912 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 913 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 914 | |
| 915 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 916 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 920 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 921 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 922 | } |
| 923 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 924 | /** |
| 925 | * Return true if the events preceding this incoming motion event should be dropped |
| 926 | * Return false otherwise (the default behaviour) |
| 927 | */ |
| 928 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 929 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 930 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 931 | |
| 932 | // Optimize case where the current application is unresponsive and the user |
| 933 | // decides to touch a window in a different application. |
| 934 | // If the application takes too long to catch up then we drop all events preceding |
| 935 | // the touch into the other window. |
| 936 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 937 | int32_t displayId = motionEntry.displayId; |
| 938 | int32_t x = static_cast<int32_t>( |
| 939 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 940 | int32_t y = static_cast<int32_t>( |
| 941 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 942 | |
| 943 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 944 | sp<WindowInfoHandle> touchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 945 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 946 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 947 | touchedWindowHandle->getApplicationToken() != |
| 948 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 949 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 950 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 951 | "for %s", |
| 952 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 953 | return true; |
| 954 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 955 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 956 | // Alternatively, maybe there's a spy window that could handle this event. |
| 957 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 958 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 959 | for (const auto& windowHandle : touchedSpies) { |
| 960 | const sp<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 961 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 962 | // This spy window could take more input. Drop all events preceding this |
| 963 | // 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] | 964 | 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] | 965 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 966 | mAwaitedFocusedApplication->getName().c_str()); |
| 967 | return true; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 973 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 974 | // events to be processed before dispatching the key event. This is because these motion events |
| 975 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 976 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 977 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 978 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 979 | "just send the pending key event to the focused window."); |
| 980 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 981 | } |
| 982 | return false; |
| 983 | } |
| 984 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 985 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 986 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 987 | mInboundQueue.push_back(std::move(newEntry)); |
| 988 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 989 | traceInboundQueueLengthLocked(); |
| 990 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 991 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 992 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 993 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 994 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 995 | // Optimize app switch latency. |
| 996 | // If the application takes too long to catch up then we drop all events preceding |
| 997 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 998 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 999 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1000 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1001 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1002 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1003 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1004 | if (DEBUG_APP_SWITCH) { |
| 1005 | ALOGD("App switch is pending!"); |
| 1006 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1007 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1008 | mAppSwitchSawKeyDown = false; |
| 1009 | needWake = true; |
| 1010 | } |
| 1011 | } |
| 1012 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1013 | |
| 1014 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1015 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1016 | // time for it may have been handled in the policy and could be dropped. |
| 1017 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1018 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1019 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1020 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1021 | pendingKey.interceptKeyResult == |
| 1022 | KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1023 | pendingKey.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1024 | pendingKey.interceptKeyWakeupTime = 0; |
| 1025 | needWake = true; |
| 1026 | } |
| 1027 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1028 | break; |
| 1029 | } |
| 1030 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1031 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1032 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1033 | "Unexpected untrusted event."); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1034 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1035 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1036 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1037 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1038 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1039 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1040 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1041 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1042 | break; |
| 1043 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1044 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1045 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1046 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1047 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1048 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1049 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1050 | // nothing to do |
| 1051 | break; |
| 1052 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | return needWake; |
| 1056 | } |
| 1057 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1058 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1059 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1060 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1061 | mRecentQueue.push_back(entry); |
| 1062 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1063 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1064 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1068 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1069 | int32_t y, TouchState* touchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1070 | bool isStylus, |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1071 | bool addOutsideTargets, |
| 1072 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1073 | if (addOutsideTargets && touchState == nullptr) { |
| 1074 | 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] | 1075 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1076 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1077 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1078 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1079 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1080 | continue; |
| 1081 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1082 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1083 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1084 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1085 | return windowHandle; |
| 1086 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1087 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1088 | if (addOutsideTargets && |
| 1089 | info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1090 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1091 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1092 | } |
| 1093 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1094 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1097 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1098 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1099 | // Traverse windows from front to back and gather the touched spy windows. |
| 1100 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1101 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1102 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1103 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1104 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1105 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1106 | continue; |
| 1107 | } |
| 1108 | if (!info.isSpy()) { |
| 1109 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1110 | return spyWindows; |
| 1111 | } |
| 1112 | spyWindows.push_back(windowHandle); |
| 1113 | } |
| 1114 | return spyWindows; |
| 1115 | } |
| 1116 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1117 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1118 | const char* reason; |
| 1119 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1120 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1121 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1122 | ALOGD("Dropped event because policy consumed it."); |
| 1123 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1124 | reason = "inbound event was dropped because the policy consumed it"; |
| 1125 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1126 | case DropReason::DISABLED: |
| 1127 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1128 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1129 | } |
| 1130 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1131 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1132 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1133 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1134 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1135 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1136 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1137 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1138 | "has started interacting with a different application."); |
| 1139 | reason = "inbound event was dropped because the current application is not responding " |
| 1140 | "and the user has started interacting with a different application"; |
| 1141 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1142 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1143 | ALOGI("Dropped event because it is stale."); |
| 1144 | reason = "inbound event was dropped because it is stale"; |
| 1145 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1146 | case DropReason::NO_POINTER_CAPTURE: |
| 1147 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1148 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1149 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1150 | case DropReason::NOT_DROPPED: { |
| 1151 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1152 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1153 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1156 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1157 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1158 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1159 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1160 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1161 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1162 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1163 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1164 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1165 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1166 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1167 | } else { |
| 1168 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1169 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1170 | } |
| 1171 | break; |
| 1172 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1173 | case EventEntry::Type::SENSOR: { |
| 1174 | break; |
| 1175 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1176 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1177 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1178 | break; |
| 1179 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1180 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1181 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1182 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1183 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1184 | 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] | 1185 | break; |
| 1186 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1190 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1191 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1192 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1195 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1196 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1197 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1198 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1202 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1203 | } |
| 1204 | |
| 1205 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1206 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1207 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1208 | if (DEBUG_APP_SWITCH) { |
| 1209 | if (handled) { |
| 1210 | ALOGD("App switch has arrived."); |
| 1211 | } else { |
| 1212 | ALOGD("App switch was abandoned."); |
| 1213 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1214 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1217 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1218 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1219 | } |
| 1220 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1221 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1222 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1223 | return false; |
| 1224 | } |
| 1225 | |
| 1226 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1227 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1228 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1229 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1230 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1231 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1232 | return true; |
| 1233 | } |
| 1234 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1235 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1236 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1240 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1241 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1242 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1243 | releaseInboundEventLocked(entry); |
| 1244 | } |
| 1245 | traceInboundQueueLengthLocked(); |
| 1246 | } |
| 1247 | |
| 1248 | void InputDispatcher::releasePendingEventLocked() { |
| 1249 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1250 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1251 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1252 | } |
| 1253 | } |
| 1254 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1255 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1256 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1257 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1258 | if (DEBUG_DISPATCH_CYCLE) { |
| 1259 | ALOGD("Injected inbound event was dropped."); |
| 1260 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1261 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1262 | } |
| 1263 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1264 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1265 | } |
| 1266 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1270 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1271 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1272 | } |
| 1273 | } |
| 1274 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1275 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1276 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1277 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1278 | uint32_t policyFlags = entry->policyFlags & |
| 1279 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1280 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1281 | std::shared_ptr<KeyEntry> newEntry = |
| 1282 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1283 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1284 | entry->flags, entry->keyCode, entry->scanCode, |
| 1285 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1286 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1287 | newEntry->syntheticRepeat = true; |
| 1288 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1289 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1290 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1293 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1294 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1295 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1296 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1297 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1298 | |
| 1299 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1300 | resetKeyRepeatLocked(); |
| 1301 | |
| 1302 | // 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] | 1303 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1304 | scoped_unlock unlock(mLock); |
| 1305 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1306 | }; |
| 1307 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1308 | return true; |
| 1309 | } |
| 1310 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1311 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1312 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1313 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1314 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1315 | entry.deviceId); |
| 1316 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1317 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1318 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1319 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1320 | resetKeyRepeatLocked(); |
| 1321 | } |
| 1322 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1323 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1324 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1325 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1326 | return true; |
| 1327 | } |
| 1328 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1329 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1330 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1331 | if (mPendingEvent != nullptr) { |
| 1332 | // Move the pending event to the front of the queue. This will give the chance |
| 1333 | // for the pending event to get dispatched to the newly focused window |
| 1334 | mInboundQueue.push_front(mPendingEvent); |
| 1335 | mPendingEvent = nullptr; |
| 1336 | } |
| 1337 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1338 | std::unique_ptr<FocusEntry> focusEntry = |
| 1339 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1340 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1341 | |
| 1342 | // This event should go to the front of the queue, but behind all other focus events |
| 1343 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1344 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1345 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1346 | [](const std::shared_ptr<EventEntry>& event) { |
| 1347 | return event->type == EventEntry::Type::FOCUS; |
| 1348 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1349 | |
| 1350 | // 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] | 1351 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1352 | } |
| 1353 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1354 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1355 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1356 | if (channel == nullptr) { |
| 1357 | return; // Window has gone away |
| 1358 | } |
| 1359 | InputTarget target; |
| 1360 | target.inputChannel = channel; |
| 1361 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1362 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1363 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1364 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1365 | std::string reason = std::string("reason=").append(entry->reason); |
| 1366 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1367 | dispatchEventLocked(currentTime, entry, {target}); |
| 1368 | } |
| 1369 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1370 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1371 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1372 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1373 | dropReason = DropReason::NOT_DROPPED; |
| 1374 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1375 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1376 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1377 | |
| 1378 | if (entry->pointerCaptureRequest.enable) { |
| 1379 | // Enable Pointer Capture. |
| 1380 | if (haveWindowWithPointerCapture && |
| 1381 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1382 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1383 | // app of the state change, so there is no need to notify the app. |
| 1384 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1385 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1386 | } |
| 1387 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1388 | // This can happen if a window requests capture and immediately releases capture. |
| 1389 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1390 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1391 | return; |
| 1392 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1393 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1394 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1395 | return; |
| 1396 | } |
| 1397 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1398 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1399 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1400 | mWindowTokenWithPointerCapture = token; |
| 1401 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1402 | // Disable Pointer Capture. |
| 1403 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1404 | // for two reasons: |
| 1405 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1406 | // to disable capture with the same sequence number: one generated by |
| 1407 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1408 | // Capture being disabled in InputReader. |
| 1409 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1410 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1411 | // in InputReader. |
| 1412 | if (!haveWindowWithPointerCapture) { |
| 1413 | // Pointer capture was already forcefully disabled because of focus change. |
| 1414 | dropReason = DropReason::NOT_DROPPED; |
| 1415 | return; |
| 1416 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1417 | token = mWindowTokenWithPointerCapture; |
| 1418 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1419 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1420 | setPointerCaptureLocked(false); |
| 1421 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | auto channel = getInputChannelLocked(token); |
| 1425 | if (channel == nullptr) { |
| 1426 | // Window has gone away, clean up Pointer Capture state. |
| 1427 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1428 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1429 | setPointerCaptureLocked(false); |
| 1430 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1431 | return; |
| 1432 | } |
| 1433 | InputTarget target; |
| 1434 | target.inputChannel = channel; |
| 1435 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1436 | entry->dispatchInProgress = true; |
| 1437 | dispatchEventLocked(currentTime, entry, {target}); |
| 1438 | |
| 1439 | dropReason = DropReason::NOT_DROPPED; |
| 1440 | } |
| 1441 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1442 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1443 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1444 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1445 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1446 | if (windowHandles.empty()) { |
| 1447 | return; |
| 1448 | } |
| 1449 | const std::vector<InputTarget> inputTargets = |
| 1450 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1451 | if (inputTargets.empty()) { |
| 1452 | return; |
| 1453 | } |
| 1454 | entry->dispatchInProgress = true; |
| 1455 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1456 | } |
| 1457 | |
| 1458 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1459 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1460 | std::vector<InputTarget> inputTargets; |
| 1461 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1462 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1463 | const sp<IBinder>& token = handle->getToken(); |
| 1464 | if (token == nullptr) { |
| 1465 | continue; |
| 1466 | } |
| 1467 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1468 | if (channel == nullptr) { |
| 1469 | continue; // Window has gone away |
| 1470 | } |
| 1471 | InputTarget target; |
| 1472 | target.inputChannel = channel; |
| 1473 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1474 | inputTargets.push_back(target); |
| 1475 | } |
| 1476 | return inputTargets; |
| 1477 | } |
| 1478 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1479 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1480 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1481 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1482 | if (!entry->dispatchInProgress) { |
| 1483 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1484 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1485 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1486 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1487 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1488 | // We have seen two identical key downs in a row which indicates that the device |
| 1489 | // driver is automatically generating key repeats itself. We take note of the |
| 1490 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1491 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1492 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1493 | // Make sure we don't get key down from a different device. If a different |
| 1494 | // device Id has same key pressed down, the new device Id will replace the |
| 1495 | // current one to hold the key repeat with repeat count reset. |
| 1496 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1497 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1498 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1499 | resetKeyRepeatLocked(); |
| 1500 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1501 | } else { |
| 1502 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1503 | resetKeyRepeatLocked(); |
| 1504 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1505 | } |
| 1506 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1507 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1508 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1509 | // 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] | 1510 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1511 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1512 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1513 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1514 | resetKeyRepeatLocked(); |
| 1515 | } |
| 1516 | |
| 1517 | if (entry->repeatCount == 1) { |
| 1518 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1519 | } else { |
| 1520 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1521 | } |
| 1522 | |
| 1523 | entry->dispatchInProgress = true; |
| 1524 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1525 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | // Handle case where the policy asked us to try again later last time. |
| 1529 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1530 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1531 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1532 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1533 | } |
| 1534 | return false; // wait until next wakeup |
| 1535 | } |
| 1536 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1537 | entry->interceptKeyWakeupTime = 0; |
| 1538 | } |
| 1539 | |
| 1540 | // Give the policy a chance to intercept the key. |
| 1541 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1542 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1543 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1544 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1545 | |
| 1546 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1547 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1548 | }; |
| 1549 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1550 | return false; // wait for the command to run |
| 1551 | } else { |
| 1552 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1553 | } |
| 1554 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1555 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1556 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1561 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1562 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1563 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1564 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1565 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1566 | return true; |
| 1567 | } |
| 1568 | |
| 1569 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1570 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1571 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1572 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1573 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1574 | return false; |
| 1575 | } |
| 1576 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1577 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1578 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1579 | return true; |
| 1580 | } |
| 1581 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1582 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1583 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1584 | |
| 1585 | // Dispatch the key. |
| 1586 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1587 | return true; |
| 1588 | } |
| 1589 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1590 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1591 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1592 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1593 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1594 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1595 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1596 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1597 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1598 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1599 | } |
| 1600 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1601 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1602 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1603 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1604 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1605 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1606 | "source=0x%x, sensorType=%s", |
| 1607 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1608 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1609 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1610 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1611 | scoped_unlock unlock(mLock); |
| 1612 | |
| 1613 | if (entry->accuracyChanged) { |
| 1614 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1615 | } |
| 1616 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1617 | entry->hwTimestamp, entry->values); |
| 1618 | }; |
| 1619 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1623 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1624 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1625 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1626 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1627 | { // acquire lock |
| 1628 | std::scoped_lock _l(mLock); |
| 1629 | |
| 1630 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1631 | std::shared_ptr<EventEntry> entry = *it; |
| 1632 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1633 | it = mInboundQueue.erase(it); |
| 1634 | releaseInboundEventLocked(entry); |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | return true; |
| 1639 | } |
| 1640 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1641 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1642 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1643 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1644 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1645 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1646 | entry->dispatchInProgress = true; |
| 1647 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1648 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1652 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1653 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1654 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1655 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1656 | return true; |
| 1657 | } |
| 1658 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1659 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1660 | |
| 1661 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1662 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1663 | |
| 1664 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1665 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1666 | if (isPointerEvent) { |
| 1667 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1668 | |
| 1669 | if (mDragState && |
| 1670 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1671 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1672 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1673 | } |
| 1674 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1675 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1676 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1677 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1678 | } else { |
| 1679 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1680 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1681 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1682 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1683 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1684 | return false; |
| 1685 | } |
| 1686 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1687 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1688 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1689 | return true; |
| 1690 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1691 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1692 | CancelationOptions::Mode mode(isPointerEvent |
| 1693 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1694 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1695 | CancelationOptions options(mode, "input event injection failed"); |
| 1696 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1697 | return true; |
| 1698 | } |
| 1699 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1700 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1701 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1702 | |
| 1703 | // Dispatch the motion. |
| 1704 | if (conflictingPointerActions) { |
| 1705 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1706 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1707 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1708 | } |
| 1709 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1710 | return true; |
| 1711 | } |
| 1712 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1713 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1714 | bool isExiting, const int32_t rawX, |
| 1715 | const int32_t rawY) { |
| 1716 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1717 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1718 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 1719 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1720 | |
| 1721 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1722 | } |
| 1723 | |
| 1724 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1725 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1726 | if (channel == nullptr) { |
| 1727 | return; // Window has gone away |
| 1728 | } |
| 1729 | InputTarget target; |
| 1730 | target.inputChannel = channel; |
| 1731 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1732 | entry->dispatchInProgress = true; |
| 1733 | dispatchEventLocked(currentTime, entry, {target}); |
| 1734 | } |
| 1735 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1736 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1737 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1738 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1739 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1740 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1741 | "metaState=0x%x, buttonState=0x%x," |
| 1742 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1743 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1744 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1745 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1746 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1747 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1748 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1749 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1750 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1751 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1752 | "orientation=%f", |
| 1753 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1754 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1755 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1756 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1757 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1758 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1759 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1760 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1761 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1762 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1763 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1764 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1765 | } |
| 1766 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1767 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1768 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1769 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1770 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1771 | if (DEBUG_DISPATCH_CYCLE) { |
| 1772 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1773 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1774 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1775 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1776 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1777 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1778 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1779 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1780 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1781 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1782 | sp<Connection> connection = |
| 1783 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1784 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1785 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1786 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1787 | if (DEBUG_FOCUS) { |
| 1788 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1789 | "is no longer registered with the input dispatcher.", |
| 1790 | inputTarget.inputChannel->getName().c_str()); |
| 1791 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1792 | } |
| 1793 | } |
| 1794 | } |
| 1795 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1796 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1797 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1798 | // If the policy decides to close the app, we will get a channel removal event via |
| 1799 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1800 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1801 | // pile up. |
| 1802 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1803 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1804 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1805 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1806 | "application not responding"); |
| 1807 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1808 | } |
| 1809 | } |
| 1810 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1811 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1812 | if (DEBUG_FOCUS) { |
| 1813 | ALOGD("Resetting ANR timeouts."); |
| 1814 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1815 | |
| 1816 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1817 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1818 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1819 | } |
| 1820 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1821 | /** |
| 1822 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1823 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1824 | * Focused display is the display that the user most recently interacted with. |
| 1825 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1826 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1827 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1828 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1829 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1830 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1831 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1832 | break; |
| 1833 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1834 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1835 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1836 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1837 | break; |
| 1838 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1839 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1840 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1841 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1842 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1843 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1844 | case EventEntry::Type::SENSOR: |
| 1845 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1846 | 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] | 1847 | return ADISPLAY_ID_NONE; |
| 1848 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1849 | } |
| 1850 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1851 | } |
| 1852 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1853 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1854 | const char* focusedWindowName) { |
| 1855 | if (mAnrTracker.empty()) { |
| 1856 | // already processed all events that we waited for |
| 1857 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1858 | return false; |
| 1859 | } |
| 1860 | |
| 1861 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1862 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1863 | // 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] | 1864 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1865 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1866 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1867 | return true; |
| 1868 | } |
| 1869 | |
| 1870 | // We still have pending events, and already started the timer |
| 1871 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1872 | return true; // Still waiting |
| 1873 | } |
| 1874 | |
| 1875 | // Waited too long, and some connection still hasn't processed all motions |
| 1876 | // Just send the key to the focused window |
| 1877 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1878 | focusedWindowName); |
| 1879 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1880 | return false; |
| 1881 | } |
| 1882 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 1883 | static std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 1884 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 1885 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 1886 | return keyEntry.downTime; |
| 1887 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 1888 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 1889 | return motionEntry.downTime; |
| 1890 | } |
| 1891 | return std::nullopt; |
| 1892 | } |
| 1893 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1894 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1895 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1896 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1897 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1898 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1899 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1900 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1901 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1902 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1903 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1904 | // If there is no currently focused window and no focused application |
| 1905 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1906 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1907 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1908 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1909 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1910 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1911 | } |
| 1912 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1913 | // Drop key events if requested by input feature |
| 1914 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1915 | return InputEventInjectionResult::FAILED; |
| 1916 | } |
| 1917 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1918 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1919 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1920 | // start interacting with another application via touch (app switch). This code can be removed |
| 1921 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1922 | // an app is expected to have a focused window. |
| 1923 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1924 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1925 | // 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] | 1926 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1927 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1928 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1929 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1930 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1931 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1932 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1933 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1934 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1935 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1936 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1937 | // Already raised ANR. Drop the event |
| 1938 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1939 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1940 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1941 | } else { |
| 1942 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1943 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | // we have a valid, non-null focused window |
| 1948 | resetNoFocusedWindowTimeoutLocked(); |
| 1949 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1950 | // Verify targeted injection. |
| 1951 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 1952 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
| 1953 | return InputEventInjectionResult::TARGET_MISMATCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1954 | } |
| 1955 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1956 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 1957 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1958 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1959 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | // If the event is a key event, then we must wait for all previous events to |
| 1963 | // complete before delivering it because previous events may have the |
| 1964 | // side-effect of transferring focus to a different window and we want to |
| 1965 | // ensure that the following keys are sent to the new window. |
| 1966 | // |
| 1967 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1968 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1969 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1970 | // often anticipate pending UI changes when typing on a keyboard. |
| 1971 | // To obtain this behavior, we must serialize key events with respect to all |
| 1972 | // prior input events. |
| 1973 | if (entry.type == EventEntry::Type::KEY) { |
| 1974 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1975 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1976 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1977 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1981 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1982 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 1983 | BitSet32(0), getDownTime(entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1984 | |
| 1985 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1986 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1987 | } |
| 1988 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1989 | /** |
| 1990 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1991 | * that are currently unresponsive. |
| 1992 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1993 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1994 | const std::vector<Monitor>& monitors) const { |
| 1995 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1996 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1997 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 1998 | sp<Connection> connection = |
| 1999 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2000 | if (connection == nullptr) { |
| 2001 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2002 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2003 | return false; |
| 2004 | } |
| 2005 | if (!connection->responsive) { |
| 2006 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2007 | connection->inputChannel->getName().c_str()); |
| 2008 | return false; |
| 2009 | } |
| 2010 | return true; |
| 2011 | }); |
| 2012 | return responsiveMonitors; |
| 2013 | } |
| 2014 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2015 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 2016 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 2017 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2018 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2019 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2020 | // For security reasons, we defer updating the touch state until we are sure that |
| 2021 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2022 | const int32_t displayId = entry.displayId; |
| 2023 | const int32_t action = entry.action; |
| 2024 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2025 | |
| 2026 | // Update the touch state as needed based on the properties of the touch event. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2027 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2028 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 2029 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2030 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2031 | // Copy current touch state into tempTouchState. |
| 2032 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2033 | // 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] | 2034 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2035 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2036 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2037 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2038 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2039 | } |
| 2040 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2041 | bool isSplit = tempTouchState.split; |
| 2042 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 2043 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 2044 | tempTouchState.displayId != displayId); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2045 | |
| 2046 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2047 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2048 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2049 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2050 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2051 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2052 | bool wrongDevice = false; |
| 2053 | if (newGesture) { |
| 2054 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2055 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2056 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2057 | "in display %" PRId32, |
| 2058 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2059 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2060 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2061 | switchedDevice = false; |
| 2062 | wrongDevice = true; |
| 2063 | goto Failed; |
| 2064 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2065 | tempTouchState.reset(); |
| 2066 | tempTouchState.down = down; |
| 2067 | tempTouchState.deviceId = entry.deviceId; |
| 2068 | tempTouchState.source = entry.source; |
| 2069 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2070 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2071 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2072 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2073 | "in display %" PRId32, |
| 2074 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2075 | // TODO: test multiple simultaneous input streams. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2076 | injectionResult = InputEventInjectionResult::FAILED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2077 | switchedDevice = false; |
| 2078 | wrongDevice = true; |
| 2079 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2083 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2084 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2085 | int32_t x; |
| 2086 | int32_t y; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2087 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2088 | // Always dispatch mouse events to cursor position. |
| 2089 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2090 | x = int32_t(entry.xCursorPosition); |
| 2091 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2092 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2093 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2094 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2095 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2096 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2097 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2098 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2099 | isStylus, isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2100 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2101 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2102 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2103 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2104 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2105 | // 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] | 2106 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2109 | // Verify targeted injection. |
| 2110 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2111 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
| 2112 | injectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
| 2113 | newTouchedWindowHandle = nullptr; |
| 2114 | goto Failed; |
| 2115 | } |
| 2116 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2117 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2118 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2119 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2120 | // New window supports splitting, but we should never split mouse events. |
| 2121 | isSplit = !isFromMouse; |
| 2122 | } else if (isSplit) { |
| 2123 | // New window does not support splitting but we have already split events. |
| 2124 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2125 | newTouchedWindowHandle = nullptr; |
| 2126 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2127 | } else { |
| 2128 | // 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] | 2129 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2130 | // should never be split. |
| 2131 | tempTouchState.split = isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2132 | } |
| 2133 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2134 | // Update hover state. |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2135 | if (newTouchedWindowHandle != nullptr) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2136 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2137 | newHoverWindowHandle = nullptr; |
| 2138 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2139 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2140 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2141 | } |
| 2142 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2143 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2144 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2145 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2146 | // Process the foreground window first so that it is the first to receive the event. |
| 2147 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2148 | } |
| 2149 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2150 | if (newTouchedWindows.empty()) { |
| 2151 | ALOGI("Dropping event because there is no touchable window at (%d, %d) on display %d.", |
| 2152 | x, y, displayId); |
| 2153 | injectionResult = InputEventInjectionResult::FAILED; |
| 2154 | goto Failed; |
| 2155 | } |
| 2156 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2157 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
| 2158 | const WindowInfo& info = *windowHandle->getInfo(); |
| 2159 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2160 | // Skip spy window targets that are not valid for targeted injection. |
| 2161 | if (const auto err = verifyTargetedInjection(windowHandle, entry); err) { |
| 2162 | continue; |
| 2163 | } |
| 2164 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2165 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2166 | ALOGI("Not sending touch event to %s because it is paused", |
| 2167 | windowHandle->getName().c_str()); |
| 2168 | continue; |
| 2169 | } |
| 2170 | |
| 2171 | // Ensure the window has a connection and the connection is responsive |
| 2172 | const bool isResponsive = hasResponsiveConnectionLocked(*windowHandle); |
| 2173 | if (!isResponsive) { |
| 2174 | ALOGW("Not sending touch gesture to %s because it is not responsive", |
| 2175 | windowHandle->getName().c_str()); |
| 2176 | continue; |
| 2177 | } |
| 2178 | |
| 2179 | // Drop events that can't be trusted due to occlusion |
Hani Kazmi | 3ce9c3a | 2022-04-25 09:40:23 +0000 | [diff] [blame] | 2180 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(windowHandle, x, y); |
| 2181 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 2182 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2183 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2184 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2185 | ALOGD("%s", log.c_str()); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2186 | } |
| 2187 | } |
Hani Kazmi | 3ce9c3a | 2022-04-25 09:40:23 +0000 | [diff] [blame] | 2188 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2189 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2190 | continue; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | // Drop touch events if requested by input feature |
| 2194 | if (shouldDropInput(entry, windowHandle)) { |
| 2195 | continue; |
| 2196 | } |
| 2197 | |
| 2198 | // Set target flags. |
| 2199 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2200 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2201 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2202 | // 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] | 2203 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2204 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2205 | |
| 2206 | if (isSplit) { |
| 2207 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2208 | } |
| 2209 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
| 2210 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2211 | } else if (isWindowObscuredLocked(windowHandle)) { |
| 2212 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2213 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2214 | |
| 2215 | // Update the temporary touch state. |
| 2216 | BitSet32 pointerIds; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2217 | pointerIds.markBit(entry.pointerProperties[pointerIndex].id); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2218 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2219 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds, |
| 2220 | entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2221 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2222 | |
| 2223 | // If any existing window is pilfering pointers from newly added window, remove it |
| 2224 | BitSet32 canceledPointers = BitSet32(0); |
| 2225 | for (const TouchedWindow& window : tempTouchState.windows) { |
| 2226 | if (window.isPilferingPointers) { |
| 2227 | canceledPointers |= window.pointerIds; |
| 2228 | } |
| 2229 | } |
| 2230 | tempTouchState.cancelPointersForNonPilferingWindows(canceledPointers); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2231 | } else { |
| 2232 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2233 | |
| 2234 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2235 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2236 | if (DEBUG_FOCUS) { |
| 2237 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2238 | "dropped the pointer down event in display %" PRId32, |
| 2239 | displayId); |
| 2240 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2241 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2242 | goto Failed; |
| 2243 | } |
| 2244 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2245 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2246 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2247 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2248 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2249 | tempTouchState.isSlippery()) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2250 | const int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2251 | const int32_t y = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2252 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2253 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2254 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2255 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2256 | newTouchedWindowHandle = |
| 2257 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2258 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2259 | // Verify targeted injection. |
| 2260 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2261 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
| 2262 | injectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
| 2263 | newTouchedWindowHandle = nullptr; |
| 2264 | goto Failed; |
| 2265 | } |
| 2266 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2267 | // Drop touch events if requested by input feature |
| 2268 | if (newTouchedWindowHandle != nullptr && |
| 2269 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2270 | newTouchedWindowHandle = nullptr; |
| 2271 | } |
| 2272 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2273 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2274 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2275 | if (DEBUG_FOCUS) { |
| 2276 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2277 | oldTouchedWindowHandle->getName().c_str(), |
| 2278 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2279 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2280 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2281 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2282 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2283 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2284 | |
| 2285 | // Make a slippery entrance into the new window. |
| 2286 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2287 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2288 | } |
| 2289 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2290 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
| 2291 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
| 2292 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2293 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2294 | if (isSplit) { |
| 2295 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2296 | } |
| 2297 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2298 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2299 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2300 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | BitSet32 pointerIds; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2304 | pointerIds.markBit(entry.pointerProperties[0].id); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2305 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds, |
| 2306 | entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2307 | } |
| 2308 | } |
| 2309 | } |
| 2310 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2311 | // Update dispatching for hover enter and exit. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2312 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2313 | // Let the previous window know that the hover sequence is over, unless we already did |
| 2314 | // it when dispatching it as is to newTouchedWindowHandle. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2315 | if (mLastHoverWindowHandle != nullptr && |
| 2316 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2317 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2318 | if (DEBUG_HOVER) { |
| 2319 | ALOGD("Sending hover exit event to window %s.", |
| 2320 | mLastHoverWindowHandle->getName().c_str()); |
| 2321 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2322 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2323 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2324 | } |
| 2325 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2326 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2327 | // when dispatching it as is to newTouchedWindowHandle. |
| 2328 | if (newHoverWindowHandle != nullptr && |
| 2329 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2330 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2331 | if (DEBUG_HOVER) { |
| 2332 | ALOGD("Sending hover enter event to window %s.", |
| 2333 | newHoverWindowHandle->getName().c_str()); |
| 2334 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2335 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2336 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2337 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2338 | } |
| 2339 | } |
| 2340 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2341 | // Ensure that we have at least one foreground window or at least one window that cannot be a |
| 2342 | // foreground target. If we only have windows that are not receiving foreground touches (e.g. we |
| 2343 | // only have windows getting ACTION_OUTSIDE), then drop the event, because there is no window |
| 2344 | // that is actually receiving the entire gesture. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2345 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2346 | [](const TouchedWindow& touchedWindow) { |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2347 | return !canReceiveForegroundTouches( |
| 2348 | *touchedWindow.windowHandle->getInfo()) || |
| 2349 | (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2350 | })) { |
Siarhei Vishniakou | 1fb1891 | 2022-03-08 10:31:39 -0800 | [diff] [blame] | 2351 | ALOGI("Dropping event because there is no touched window on display %d to receive it: %s", |
| 2352 | displayId, entry.getDescription().c_str()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2353 | injectionResult = InputEventInjectionResult::FAILED; |
| 2354 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2355 | } |
| 2356 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2357 | // Ensure that all touched windows are valid for injection. |
| 2358 | if (entry.injectionState != nullptr) { |
| 2359 | std::string errs; |
| 2360 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
| 2361 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2362 | // Allow ACTION_OUTSIDE events generated by targeted injection to be |
| 2363 | // dispatched to any uid, since the coords will be zeroed out later. |
| 2364 | continue; |
| 2365 | } |
| 2366 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2367 | if (err) errs += "\n - " + *err; |
| 2368 | } |
| 2369 | if (!errs.empty()) { |
| 2370 | ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " |
| 2371 | "%d:%s", |
| 2372 | *entry.injectionState->targetUid, errs.c_str()); |
| 2373 | injectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2374 | goto Failed; |
| 2375 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2376 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2377 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2378 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2379 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2380 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2381 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2382 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2383 | if (foregroundWindowHandle) { |
| 2384 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2385 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2386 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2387 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2388 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2389 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2390 | InputTarget::FLAG_ZERO_COORDS, |
| 2391 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2392 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2393 | } |
| 2394 | } |
| 2395 | } |
| 2396 | } |
| 2397 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2398 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2399 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2400 | // of the touch gesture. |
| 2401 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2402 | // engine only supports touch events. We would need to add a mechanism similar |
| 2403 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2404 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2405 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2406 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2407 | if (foregroundWindowHandle && |
| 2408 | foregroundWindowHandle->getInfo()->inputConfig.test( |
| 2409 | WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2410 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2411 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2412 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2413 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2414 | if (info->displayId == displayId && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2415 | windowHandle->getInfo()->inputConfig.test( |
| 2416 | WindowInfo::InputConfig::IS_WALLPAPER)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2417 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2418 | .addOrUpdateWindow(windowHandle, |
| 2419 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2420 | InputTarget:: |
| 2421 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2422 | InputTarget::FLAG_DISPATCH_AS_IS, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2423 | BitSet32(0), entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2424 | } |
| 2425 | } |
| 2426 | } |
| 2427 | } |
| 2428 | |
| 2429 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2430 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2431 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2432 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2433 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2434 | touchedWindow.pointerIds, touchedWindow.firstDownTimeInTarget, |
| 2435 | inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | // Drop the outside or hover touch windows since we will not care about them |
| 2439 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2440 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2441 | |
| 2442 | Failed: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2443 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2444 | if (!wrongDevice) { |
| 2445 | if (switchedDevice) { |
| 2446 | if (DEBUG_FOCUS) { |
| 2447 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2448 | } |
| 2449 | *outConflictingPointerActions = true; |
| 2450 | } |
| 2451 | |
| 2452 | if (isHoverAction) { |
| 2453 | // Started hovering, therefore no longer down. |
| 2454 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2455 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2456 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2457 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2458 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2459 | *outConflictingPointerActions = true; |
| 2460 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2461 | tempTouchState.reset(); |
| 2462 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2463 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2464 | tempTouchState.deviceId = entry.deviceId; |
| 2465 | tempTouchState.source = entry.source; |
| 2466 | tempTouchState.displayId = displayId; |
| 2467 | } |
| 2468 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2469 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2470 | // All pointers up or canceled. |
| 2471 | tempTouchState.reset(); |
| 2472 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2473 | // First pointer went down. |
| 2474 | if (oldState && oldState->down) { |
| 2475 | if (DEBUG_FOCUS) { |
| 2476 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2477 | } |
| 2478 | *outConflictingPointerActions = true; |
| 2479 | } |
| 2480 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2481 | // One pointer went up. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2482 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2483 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2484 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2485 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2486 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2487 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2488 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2489 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2490 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2491 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2492 | i += 1; |
| 2493 | } |
| 2494 | } else if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2495 | // If no split, we suppose all touched windows should receive pointer down. |
| 2496 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2497 | for (size_t i = 0; i < tempTouchState.windows.size(); i++) { |
| 2498 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2499 | // Ignore drag window for it should just track one pointer. |
| 2500 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2501 | continue; |
| 2502 | } |
| 2503 | touchedWindow.pointerIds.markBit(entry.pointerProperties[pointerIndex].id); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2504 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2505 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2506 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2507 | // Save changes unless the action was scroll in which case the temporary touch |
| 2508 | // state was only valid for this one action. |
| 2509 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2510 | if (tempTouchState.displayId >= 0) { |
| 2511 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2512 | } else { |
| 2513 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2514 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2515 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2516 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2517 | // Update hover state. |
| 2518 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2519 | } |
| 2520 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2521 | return injectionResult; |
| 2522 | } |
| 2523 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2524 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2525 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2526 | // have an explicit reason to support it. |
| 2527 | constexpr bool isStylus = false; |
| 2528 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2529 | const sp<WindowInfoHandle> dropWindow = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2530 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2531 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2532 | if (dropWindow) { |
| 2533 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2534 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2535 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2536 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2537 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2538 | } |
| 2539 | mDragState.reset(); |
| 2540 | } |
| 2541 | |
| 2542 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2543 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2544 | return; |
| 2545 | } |
| 2546 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2547 | if (!mDragState->isStartDrag) { |
| 2548 | mDragState->isStartDrag = true; |
| 2549 | mDragState->isStylusButtonDownAtStart = |
| 2550 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2551 | } |
| 2552 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2553 | // Find the pointer index by id. |
| 2554 | int32_t pointerIndex = 0; |
| 2555 | for (; static_cast<uint32_t>(pointerIndex) < entry.pointerCount; pointerIndex++) { |
| 2556 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2557 | if (pointerProperties.id == mDragState->pointerId) { |
| 2558 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2559 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2560 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2561 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2562 | if (uint32_t(pointerIndex) == entry.pointerCount) { |
| 2563 | 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] | 2564 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2565 | mDragState.reset(); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2566 | return; |
| 2567 | } |
| 2568 | |
| 2569 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2570 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2571 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2572 | |
| 2573 | switch (maskedAction) { |
| 2574 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2575 | // Handle the special case : stylus button no longer pressed. |
| 2576 | bool isStylusButtonDown = |
| 2577 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2578 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2579 | finishDragAndDrop(entry.displayId, x, y); |
| 2580 | return; |
| 2581 | } |
| 2582 | |
| 2583 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2584 | // until we have an explicit reason to support it. |
| 2585 | constexpr bool isStylus = false; |
| 2586 | |
| 2587 | const sp<WindowInfoHandle> hoverWindowHandle = |
| 2588 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
| 2589 | isStylus, false /*addOutsideTargets*/, |
| 2590 | true /*ignoreDragWindow*/); |
| 2591 | // enqueue drag exit if needed. |
| 2592 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2593 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2594 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2595 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, x, |
| 2596 | y); |
| 2597 | } |
| 2598 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2599 | } |
| 2600 | // enqueue drag location if needed. |
| 2601 | if (hoverWindowHandle != nullptr) { |
| 2602 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, x, y); |
| 2603 | } |
| 2604 | break; |
| 2605 | } |
| 2606 | |
| 2607 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 2608 | if (getMotionEventActionPointerIndex(entry.action) != pointerIndex) { |
| 2609 | break; |
| 2610 | } |
| 2611 | // The drag pointer is up. |
| 2612 | [[fallthrough]]; |
| 2613 | case AMOTION_EVENT_ACTION_UP: |
| 2614 | finishDragAndDrop(entry.displayId, x, y); |
| 2615 | break; |
| 2616 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2617 | ALOGD("Receiving cancel when drag and drop."); |
| 2618 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2619 | mDragState.reset(); |
| 2620 | break; |
| 2621 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2622 | } |
| 2623 | } |
| 2624 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2625 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2626 | int32_t targetFlags, BitSet32 pointerIds, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2627 | std::optional<nsecs_t> firstDownTimeInTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2628 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2629 | std::vector<InputTarget>::iterator it = |
| 2630 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2631 | [&windowHandle](const InputTarget& inputTarget) { |
| 2632 | return inputTarget.inputChannel->getConnectionToken() == |
| 2633 | windowHandle->getToken(); |
| 2634 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2635 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2636 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2637 | |
| 2638 | if (it == inputTargets.end()) { |
| 2639 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2640 | std::shared_ptr<InputChannel> inputChannel = |
| 2641 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2642 | if (inputChannel == nullptr) { |
| 2643 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2644 | return; |
| 2645 | } |
| 2646 | inputTarget.inputChannel = inputChannel; |
| 2647 | inputTarget.flags = targetFlags; |
| 2648 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2649 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2650 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2651 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2652 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2653 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2654 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2655 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2656 | inputTargets.push_back(inputTarget); |
| 2657 | it = inputTargets.end() - 1; |
| 2658 | } |
| 2659 | |
| 2660 | ALOG_ASSERT(it->flags == targetFlags); |
| 2661 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2662 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2663 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2664 | } |
| 2665 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2666 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2667 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2668 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2669 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2670 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2671 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2672 | InputTarget target; |
| 2673 | target.inputChannel = monitor.inputChannel; |
| 2674 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2675 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2676 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2677 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2678 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2679 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2680 | target.setDefaultPointerTransform(target.displayTransform); |
| 2681 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2682 | } |
| 2683 | } |
| 2684 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2685 | /** |
| 2686 | * Indicate whether one window handle should be considered as obscuring |
| 2687 | * another window handle. We only check a few preconditions. Actually |
| 2688 | * checking the bounds is left to the caller. |
| 2689 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2690 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2691 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2692 | // Compare by token so cloned layers aren't counted |
| 2693 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2694 | return false; |
| 2695 | } |
| 2696 | auto info = windowHandle->getInfo(); |
| 2697 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2698 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2699 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2700 | } else if (otherInfo->alpha == 0 && |
| 2701 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2702 | // Those act as if they were invisible, so we don't need to flag them. |
| 2703 | // We do want to potentially flag touchable windows even if they have 0 |
| 2704 | // opacity, since they can consume touches and alter the effects of the |
| 2705 | // user interaction (eg. apps that rely on |
| 2706 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2707 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2708 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2709 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2710 | // If ownerUid is the same we don't generate occlusion events as there |
| 2711 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2712 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2713 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2714 | return false; |
| 2715 | } else if (otherInfo->displayId != info->displayId) { |
| 2716 | return false; |
| 2717 | } |
| 2718 | return true; |
| 2719 | } |
| 2720 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2721 | /** |
| 2722 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2723 | * untrusted, one should check: |
| 2724 | * |
| 2725 | * 1. If result.hasBlockingOcclusion is true. |
| 2726 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2727 | * BLOCK_UNTRUSTED. |
| 2728 | * |
| 2729 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2730 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2731 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2732 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2733 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2734 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2735 | * |
| 2736 | * If neither of those is true, then it means the touch can be allowed. |
| 2737 | */ |
| 2738 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2739 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2740 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2741 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2742 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2743 | TouchOcclusionInfo info; |
| 2744 | info.hasBlockingOcclusion = false; |
| 2745 | info.obscuringOpacity = 0; |
| 2746 | info.obscuringUid = -1; |
| 2747 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2748 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2749 | if (windowHandle == otherHandle) { |
| 2750 | break; // All future windows are below us. Exit early. |
| 2751 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2752 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2753 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2754 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2755 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2756 | info.debugInfo.push_back( |
| 2757 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2758 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2759 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2760 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2761 | // window's touch occlusion mode |
| 2762 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2763 | info.hasBlockingOcclusion = true; |
| 2764 | info.obscuringUid = otherInfo->ownerUid; |
| 2765 | info.obscuringPackage = otherInfo->packageName; |
| 2766 | break; |
| 2767 | } |
| 2768 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2769 | uint32_t uid = otherInfo->ownerUid; |
| 2770 | float opacity = |
| 2771 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2772 | // Given windows A and B: |
| 2773 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2774 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2775 | opacityByUid[uid] = opacity; |
| 2776 | if (opacity > info.obscuringOpacity) { |
| 2777 | info.obscuringOpacity = opacity; |
| 2778 | info.obscuringUid = uid; |
| 2779 | info.obscuringPackage = otherInfo->packageName; |
| 2780 | } |
| 2781 | } |
| 2782 | } |
| 2783 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2784 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2785 | info.debugInfo.push_back( |
| 2786 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2787 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2788 | return info; |
| 2789 | } |
| 2790 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2791 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2792 | bool isTouchedWindow) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2793 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2794 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2795 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 2796 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2797 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 2798 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 2799 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 2800 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 2801 | info->name.c_str(), info->inputConfig.string().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2802 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2803 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2806 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2807 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2808 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2809 | occlusionInfo.obscuringUid); |
| 2810 | return false; |
| 2811 | } |
| 2812 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2813 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2814 | "%.2f, maximum allowed = %.2f)", |
| 2815 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2816 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2817 | return false; |
| 2818 | } |
| 2819 | return true; |
| 2820 | } |
| 2821 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2822 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2823 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2824 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2825 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2826 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2827 | if (windowHandle == otherHandle) { |
| 2828 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2829 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2830 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2831 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2832 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2833 | return true; |
| 2834 | } |
| 2835 | } |
| 2836 | return false; |
| 2837 | } |
| 2838 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2839 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2840 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2841 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2842 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2843 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2844 | if (windowHandle == otherHandle) { |
| 2845 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2846 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2847 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2848 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2849 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2850 | return true; |
| 2851 | } |
| 2852 | } |
| 2853 | return false; |
| 2854 | } |
| 2855 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2856 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2857 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2858 | if (applicationHandle != nullptr) { |
| 2859 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2860 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2861 | } else { |
| 2862 | return applicationHandle->getName(); |
| 2863 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2864 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2865 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2866 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2867 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2868 | } |
| 2869 | } |
| 2870 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2871 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2872 | if (!isUserActivityEvent(eventEntry)) { |
| 2873 | // 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] | 2874 | return; |
| 2875 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2876 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2877 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2878 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2879 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2880 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2881 | if (DEBUG_DISPATCH_CYCLE) { |
| 2882 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2883 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2884 | return; |
| 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2889 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2890 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2891 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2892 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2893 | return; |
| 2894 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2895 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2896 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2897 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2898 | } |
| 2899 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2900 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2901 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2902 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2903 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2904 | return; |
| 2905 | } |
| 2906 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2907 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2908 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2909 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2910 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2911 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2912 | break; |
| 2913 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2914 | } |
| 2915 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2916 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2917 | REQUIRES(mLock) { |
| 2918 | scoped_unlock unlock(mLock); |
| 2919 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2920 | }; |
| 2921 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2925 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2926 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2927 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2928 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2929 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2930 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2931 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2932 | ATRACE_NAME(message.c_str()); |
| 2933 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2934 | if (DEBUG_DISPATCH_CYCLE) { |
| 2935 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2936 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2937 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2938 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2939 | inputTarget.getPointerInfoString().c_str()); |
| 2940 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2941 | |
| 2942 | // Skip this event if the connection status is not normal. |
| 2943 | // 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] | 2944 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2945 | if (DEBUG_DISPATCH_CYCLE) { |
| 2946 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2947 | connection->getInputChannelName().c_str(), |
| 2948 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2949 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2950 | return; |
| 2951 | } |
| 2952 | |
| 2953 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2954 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2955 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2956 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2957 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2958 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2959 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2960 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2961 | LOG_ALWAYS_FATAL_IF(!inputTarget.firstDownTimeInTarget.has_value(), |
| 2962 | "Splitting motion events requires a down time to be set for the " |
| 2963 | "target"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2964 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2965 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 2966 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2967 | if (!splitMotionEntry) { |
| 2968 | return; // split event was dropped |
| 2969 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2970 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2971 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2972 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2973 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2974 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2975 | if (DEBUG_FOCUS) { |
| 2976 | ALOGD("channel '%s' ~ Split motion event.", |
| 2977 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2978 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2979 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2980 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2981 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2982 | return; |
| 2983 | } |
| 2984 | } |
| 2985 | |
| 2986 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2987 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2988 | } |
| 2989 | |
| 2990 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2991 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2992 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2993 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2994 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2995 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2996 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2997 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2998 | ATRACE_NAME(message.c_str()); |
| 2999 | } |
| 3000 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame^] | 3001 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3002 | |
| 3003 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3004 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3005 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3006 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3007 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3008 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3009 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3010 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3011 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3012 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3013 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3014 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3015 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3016 | |
| 3017 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3018 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3019 | startDispatchCycleLocked(currentTime, connection); |
| 3020 | } |
| 3021 | } |
| 3022 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3023 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3024 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3025 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3026 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3027 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3028 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 3029 | connection->getInputChannelName().c_str(), |
| 3030 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3031 | ATRACE_NAME(message.c_str()); |
| 3032 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3033 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3034 | if (!(inputTargetFlags & dispatchMode)) { |
| 3035 | return; |
| 3036 | } |
| 3037 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 3038 | |
| 3039 | // This is a new event. |
| 3040 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3041 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3042 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3043 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3044 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3045 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3046 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3047 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3048 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3049 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3050 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3051 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3052 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3053 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3054 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3055 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3056 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3057 | if (DEBUG_DISPATCH_CYCLE) { |
| 3058 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3059 | "event", |
| 3060 | connection->getInputChannelName().c_str()); |
| 3061 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3062 | return; // skip the inconsistent event |
| 3063 | } |
| 3064 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3065 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3066 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3067 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3068 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3069 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3070 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3071 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3072 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3073 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3074 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3075 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 3076 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 3077 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3078 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 3079 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3080 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 3081 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 3082 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 3083 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3084 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3085 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3086 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3087 | } |
| 3088 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3089 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3090 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3091 | if (DEBUG_DISPATCH_CYCLE) { |
| 3092 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3093 | "enter event", |
| 3094 | connection->getInputChannelName().c_str()); |
| 3095 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3096 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3097 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3098 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3099 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3100 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3101 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3102 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3103 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3104 | } |
| 3105 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3106 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3107 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3108 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3109 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3110 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3111 | if (DEBUG_DISPATCH_CYCLE) { |
| 3112 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3113 | "event", |
| 3114 | connection->getInputChannelName().c_str()); |
| 3115 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3116 | return; // skip the inconsistent event |
| 3117 | } |
| 3118 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3119 | dispatchEntry->resolvedEventId = |
| 3120 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3121 | ? mIdGenerator.nextId() |
| 3122 | : motionEntry.id; |
| 3123 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3124 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3125 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3126 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3127 | ATRACE_NAME(message.c_str()); |
| 3128 | } |
| 3129 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3130 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3131 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3132 | // Skip reporting pointer down outside focus to the policy. |
| 3133 | break; |
| 3134 | } |
| 3135 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3136 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3137 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3138 | |
| 3139 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3140 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3141 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3142 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3143 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3144 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3145 | break; |
| 3146 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3147 | case EventEntry::Type::SENSOR: { |
| 3148 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3149 | break; |
| 3150 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3151 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3152 | case EventEntry::Type::DEVICE_RESET: { |
| 3153 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3154 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3155 | break; |
| 3156 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | // Remember that we are waiting for this dispatch to complete. |
| 3160 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3161 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3165 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3166 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3169 | /** |
| 3170 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3171 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3172 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3173 | * We will see both launcher and wallpaper in that list. |
| 3174 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3175 | * until the set of interacted connections changes. |
| 3176 | * |
| 3177 | * The following items are skipped, to reduce the logspam: |
| 3178 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3179 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3180 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3181 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3182 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3183 | */ |
| 3184 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3185 | const std::vector<InputTarget>& targets) { |
| 3186 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3187 | if (entry.type == EventEntry::Type::KEY) { |
| 3188 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3189 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3190 | return; |
| 3191 | } |
| 3192 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3193 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3194 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3195 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3196 | return; |
| 3197 | } |
| 3198 | } else { |
| 3199 | return; // Not a key or a motion |
| 3200 | } |
| 3201 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3202 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3203 | std::vector<sp<Connection>> newConnections; |
| 3204 | for (const InputTarget& target : targets) { |
| 3205 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3206 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3207 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3208 | } |
| 3209 | |
| 3210 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3211 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3212 | if (connection == nullptr) { |
| 3213 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3214 | } |
| 3215 | newConnectionTokens.insert(std::move(token)); |
| 3216 | newConnections.emplace_back(connection); |
| 3217 | } |
| 3218 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3219 | return; // no change |
| 3220 | } |
| 3221 | mInteractionConnectionTokens = newConnectionTokens; |
| 3222 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3223 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3224 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3225 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3226 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3227 | std::string message = "Interaction with: " + targetList; |
| 3228 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3229 | message += "<none>"; |
| 3230 | } |
| 3231 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3232 | } |
| 3233 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3234 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3235 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3236 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3237 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3238 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3239 | return; |
| 3240 | } |
| 3241 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3242 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3243 | if (focusedToken == token) { |
| 3244 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3245 | return; |
| 3246 | } |
| 3247 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3248 | auto command = [this, token]() REQUIRES(mLock) { |
| 3249 | scoped_unlock unlock(mLock); |
| 3250 | mPolicy->onPointerDownOutsideFocus(token); |
| 3251 | }; |
| 3252 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3256 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3257 | if (ATRACE_ENABLED()) { |
| 3258 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3259 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3260 | ATRACE_NAME(message.c_str()); |
| 3261 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3262 | if (DEBUG_DISPATCH_CYCLE) { |
| 3263 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3264 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3265 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3266 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3267 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3268 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3269 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3270 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3271 | |
| 3272 | // Publish the event. |
| 3273 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3274 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3275 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3276 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3277 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3278 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3279 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3280 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3281 | status = connection->inputPublisher |
| 3282 | .publishKeyEvent(dispatchEntry->seq, |
| 3283 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3284 | keyEntry.source, keyEntry.displayId, |
| 3285 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3286 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3287 | keyEntry.scanCode, keyEntry.metaState, |
| 3288 | keyEntry.repeatCount, keyEntry.downTime, |
| 3289 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3290 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3291 | } |
| 3292 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3293 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3294 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3295 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3296 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3297 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3298 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3299 | // Set the X and Y offset and X and Y scale depending on the input source. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3300 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3301 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3302 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3303 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3304 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3305 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3306 | // Don't apply window scale here since we don't want scale to affect raw |
| 3307 | // coordinates. The scale will be sent back to the client and applied |
| 3308 | // later when requesting relative coordinates. |
| 3309 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3310 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3311 | } |
| 3312 | usingCoords = scaledCoords; |
| 3313 | } |
| 3314 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3315 | // We don't want the dispatch target to know. |
| 3316 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3317 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3318 | scaledCoords[i].clear(); |
| 3319 | } |
| 3320 | usingCoords = scaledCoords; |
| 3321 | } |
| 3322 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3323 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3324 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3325 | |
| 3326 | // Publish the motion event. |
| 3327 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3328 | .publishMotionEvent(dispatchEntry->seq, |
| 3329 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3330 | motionEntry.deviceId, motionEntry.source, |
| 3331 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3332 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3333 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3334 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3335 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3336 | motionEntry.buttonState, |
| 3337 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3338 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3339 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3340 | motionEntry.xCursorPosition, |
| 3341 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3342 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3343 | motionEntry.downTime, motionEntry.eventTime, |
| 3344 | motionEntry.pointerCount, |
| 3345 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3346 | break; |
| 3347 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3348 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3349 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3350 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3351 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3352 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3353 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3354 | break; |
| 3355 | } |
| 3356 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3357 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3358 | const TouchModeEntry& touchModeEntry = |
| 3359 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3360 | status = connection->inputPublisher |
| 3361 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3362 | touchModeEntry.inTouchMode); |
| 3363 | |
| 3364 | break; |
| 3365 | } |
| 3366 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3367 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3368 | const auto& captureEntry = |
| 3369 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3370 | status = connection->inputPublisher |
| 3371 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3372 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3373 | break; |
| 3374 | } |
| 3375 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3376 | case EventEntry::Type::DRAG: { |
| 3377 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3378 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3379 | dragEntry.id, dragEntry.x, |
| 3380 | dragEntry.y, |
| 3381 | dragEntry.isExiting); |
| 3382 | break; |
| 3383 | } |
| 3384 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3385 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3386 | case EventEntry::Type::DEVICE_RESET: |
| 3387 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3388 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3389 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3390 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3391 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | // Check the result. |
| 3395 | if (status) { |
| 3396 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3397 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3398 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3399 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3400 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3401 | "event to it, status=%s(%d)", |
| 3402 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3403 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3404 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3405 | } else { |
| 3406 | // Pipe is full and we are waiting for the app to finish process some events |
| 3407 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3408 | if (DEBUG_DISPATCH_CYCLE) { |
| 3409 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3410 | "waiting for the application to catch up", |
| 3411 | connection->getInputChannelName().c_str()); |
| 3412 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3413 | } |
| 3414 | } else { |
| 3415 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3416 | "status=%s(%d)", |
| 3417 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3418 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3419 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3420 | } |
| 3421 | return; |
| 3422 | } |
| 3423 | |
| 3424 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3425 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3426 | connection->outboundQueue.end(), |
| 3427 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3428 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3429 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3430 | if (connection->responsive) { |
| 3431 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3432 | connection->inputChannel->getConnectionToken()); |
| 3433 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3434 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3435 | } |
| 3436 | } |
| 3437 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3438 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3439 | size_t size; |
| 3440 | switch (event.type) { |
| 3441 | case VerifiedInputEvent::Type::KEY: { |
| 3442 | size = sizeof(VerifiedKeyEvent); |
| 3443 | break; |
| 3444 | } |
| 3445 | case VerifiedInputEvent::Type::MOTION: { |
| 3446 | size = sizeof(VerifiedMotionEvent); |
| 3447 | break; |
| 3448 | } |
| 3449 | } |
| 3450 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3451 | return mHmacKeyManager.sign(start, size); |
| 3452 | } |
| 3453 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3454 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3455 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3456 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3457 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3458 | // Only sign events up and down events as the purely move events |
| 3459 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3460 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3461 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3462 | |
| 3463 | VerifiedMotionEvent verifiedEvent = |
| 3464 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3465 | verifiedEvent.actionMasked = actionMasked; |
| 3466 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3467 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3471 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3472 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3473 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3474 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3475 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3476 | } |
| 3477 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3478 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3479 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3480 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3481 | if (DEBUG_DISPATCH_CYCLE) { |
| 3482 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3483 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3484 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3485 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3486 | if (connection->status == Connection::Status::BROKEN || |
| 3487 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3488 | return; |
| 3489 | } |
| 3490 | |
| 3491 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3492 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3493 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3494 | }; |
| 3495 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3499 | const sp<Connection>& connection, |
| 3500 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3501 | if (DEBUG_DISPATCH_CYCLE) { |
| 3502 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3503 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3504 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3505 | |
| 3506 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3507 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3508 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3509 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3510 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3511 | |
| 3512 | // The connection appears to be unrecoverably broken. |
| 3513 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3514 | if (connection->status == Connection::Status::NORMAL) { |
| 3515 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3516 | |
| 3517 | if (notify) { |
| 3518 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3519 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3520 | connection->getInputChannelName().c_str()); |
| 3521 | |
| 3522 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3523 | scoped_unlock unlock(mLock); |
| 3524 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3525 | }; |
| 3526 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3527 | } |
| 3528 | } |
| 3529 | } |
| 3530 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3531 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3532 | while (!queue.empty()) { |
| 3533 | DispatchEntry* dispatchEntry = queue.front(); |
| 3534 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3535 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3536 | } |
| 3537 | } |
| 3538 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3539 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3540 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3541 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3542 | } |
| 3543 | delete dispatchEntry; |
| 3544 | } |
| 3545 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3546 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3547 | std::scoped_lock _l(mLock); |
| 3548 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3549 | if (connection == nullptr) { |
| 3550 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3551 | connectionToken.get(), events); |
| 3552 | return 0; // remove the callback |
| 3553 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3554 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3555 | bool notify; |
| 3556 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3557 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3558 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3559 | "events=0x%x", |
| 3560 | connection->getInputChannelName().c_str(), events); |
| 3561 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3562 | } |
| 3563 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3564 | nsecs_t currentTime = now(); |
| 3565 | bool gotOne = false; |
| 3566 | status_t status = OK; |
| 3567 | for (;;) { |
| 3568 | Result<InputPublisher::ConsumerResponse> result = |
| 3569 | connection->inputPublisher.receiveConsumerResponse(); |
| 3570 | if (!result.ok()) { |
| 3571 | status = result.error().code(); |
| 3572 | break; |
| 3573 | } |
| 3574 | |
| 3575 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3576 | const InputPublisher::Finished& finish = |
| 3577 | std::get<InputPublisher::Finished>(*result); |
| 3578 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3579 | finish.consumeTime); |
| 3580 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3581 | if (shouldReportMetricsForConnection(*connection)) { |
| 3582 | const InputPublisher::Timeline& timeline = |
| 3583 | std::get<InputPublisher::Timeline>(*result); |
| 3584 | mLatencyTracker |
| 3585 | .trackGraphicsLatency(timeline.inputEventId, |
| 3586 | connection->inputChannel->getConnectionToken(), |
| 3587 | std::move(timeline.graphicsTimeline)); |
| 3588 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3589 | } |
| 3590 | gotOne = true; |
| 3591 | } |
| 3592 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3593 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3594 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3595 | return 1; |
| 3596 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3597 | } |
| 3598 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3599 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3600 | if (notify) { |
| 3601 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3602 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3603 | status); |
| 3604 | } |
| 3605 | } else { |
| 3606 | // Monitor channels are never explicitly unregistered. |
| 3607 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3608 | const bool stillHaveWindowHandle = |
| 3609 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3610 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3611 | if (notify) { |
| 3612 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3613 | connection->getInputChannelName().c_str(), events); |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | // Remove the channel. |
| 3618 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3619 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3620 | } |
| 3621 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3622 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3623 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3624 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3625 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3626 | } |
| 3627 | } |
| 3628 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3629 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3630 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3631 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3632 | for (const Monitor& monitor : monitors) { |
| 3633 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3634 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3635 | } |
| 3636 | } |
| 3637 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3638 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3639 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3640 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3641 | if (connection == nullptr) { |
| 3642 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3643 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3644 | |
| 3645 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3646 | } |
| 3647 | |
| 3648 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3649 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3650 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3651 | return; |
| 3652 | } |
| 3653 | |
| 3654 | nsecs_t currentTime = now(); |
| 3655 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3656 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3657 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3658 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3659 | if (cancelationEvents.empty()) { |
| 3660 | return; |
| 3661 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3662 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3663 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3664 | "with reality: %s, mode=%d.", |
| 3665 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3666 | options.mode); |
| 3667 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3668 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3669 | std::string reason = std::string("reason=").append(options.reason); |
| 3670 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3671 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3672 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3673 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3674 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3675 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3676 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3677 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3678 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3679 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3680 | } |
| 3681 | target.inputChannel = connection->inputChannel; |
| 3682 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3683 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame^] | 3684 | const bool wasEmpty = connection->outboundQueue.empty(); |
| 3685 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3686 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3687 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3688 | switch (cancelationEventEntry->type) { |
| 3689 | case EventEntry::Type::KEY: { |
| 3690 | logOutboundKeyDetails("cancel - ", |
| 3691 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3692 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3693 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3694 | case EventEntry::Type::MOTION: { |
| 3695 | logOutboundMotionDetails("cancel - ", |
| 3696 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3697 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3698 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3699 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3700 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3701 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3702 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3703 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3704 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3705 | break; |
| 3706 | } |
| 3707 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3708 | case EventEntry::Type::DEVICE_RESET: |
| 3709 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3710 | 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] | 3711 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3712 | break; |
| 3713 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3714 | } |
| 3715 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3716 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3717 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3718 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3719 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame^] | 3720 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3721 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3722 | startDispatchCycleLocked(currentTime, connection); |
| 3723 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3724 | } |
| 3725 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3726 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3727 | const nsecs_t downTime, const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3728 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3729 | return; |
| 3730 | } |
| 3731 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3732 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3733 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3734 | |
| 3735 | if (downEvents.empty()) { |
| 3736 | return; |
| 3737 | } |
| 3738 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3739 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3740 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3741 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3742 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3743 | |
| 3744 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3745 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3746 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3747 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3748 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3749 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3750 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3751 | } |
| 3752 | target.inputChannel = connection->inputChannel; |
| 3753 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3754 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame^] | 3755 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3756 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3757 | switch (downEventEntry->type) { |
| 3758 | case EventEntry::Type::MOTION: { |
| 3759 | logOutboundMotionDetails("down - ", |
| 3760 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3761 | break; |
| 3762 | } |
| 3763 | |
| 3764 | case EventEntry::Type::KEY: |
| 3765 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3766 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3767 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3768 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3769 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3770 | case EventEntry::Type::SENSOR: |
| 3771 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3772 | 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] | 3773 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3774 | break; |
| 3775 | } |
| 3776 | } |
| 3777 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3778 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3779 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3780 | } |
| 3781 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame^] | 3782 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3783 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3784 | startDispatchCycleLocked(downTime, connection); |
| 3785 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3786 | } |
| 3787 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3788 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3789 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds, nsecs_t splitDownTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3790 | ALOG_ASSERT(pointerIds.value != 0); |
| 3791 | |
| 3792 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3793 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3794 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3795 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3796 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3797 | uint32_t splitPointerCount = 0; |
| 3798 | |
| 3799 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3800 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3801 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3802 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3803 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3804 | if (pointerIds.hasBit(pointerId)) { |
| 3805 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3806 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3807 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3808 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3809 | splitPointerCount += 1; |
| 3810 | } |
| 3811 | } |
| 3812 | |
| 3813 | if (splitPointerCount != pointerIds.count()) { |
| 3814 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3815 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3816 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3817 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3818 | // in this way. |
| 3819 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3820 | "we expected there to be %d pointers. This probably means we received " |
| 3821 | "a broken sequence of pointer ids from the input device.", |
| 3822 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3823 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3824 | } |
| 3825 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3826 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3827 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3828 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3829 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3830 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3831 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3832 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3833 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3834 | if (pointerIds.hasBit(pointerId)) { |
| 3835 | if (pointerIds.count() == 1) { |
| 3836 | // The first/last pointer went down/up. |
| 3837 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3838 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3839 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3840 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3841 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3842 | } else { |
| 3843 | // A secondary pointer went down/up. |
| 3844 | uint32_t splitPointerIndex = 0; |
| 3845 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3846 | splitPointerIndex += 1; |
| 3847 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3848 | action = maskedAction | |
| 3849 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3850 | } |
| 3851 | } else { |
| 3852 | // An unrelated pointer changed. |
| 3853 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3854 | } |
| 3855 | } |
| 3856 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3857 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 3858 | LOG_ALWAYS_FATAL_IF(splitDownTime != originalMotionEntry.eventTime, |
| 3859 | "Split motion event has mismatching downTime and eventTime for " |
| 3860 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64 "ms", |
| 3861 | originalMotionEntry.getDescription().c_str(), ns2ms(splitDownTime)); |
| 3862 | } |
| 3863 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3864 | int32_t newId = mIdGenerator.nextId(); |
| 3865 | if (ATRACE_ENABLED()) { |
| 3866 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3867 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3868 | originalMotionEntry.id, newId); |
| 3869 | ATRACE_NAME(message.c_str()); |
| 3870 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3871 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3872 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3873 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3874 | originalMotionEntry.displayId, |
| 3875 | originalMotionEntry.policyFlags, action, |
| 3876 | originalMotionEntry.actionButton, |
| 3877 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3878 | originalMotionEntry.buttonState, |
| 3879 | originalMotionEntry.classification, |
| 3880 | originalMotionEntry.edgeFlags, |
| 3881 | originalMotionEntry.xPrecision, |
| 3882 | originalMotionEntry.yPrecision, |
| 3883 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3884 | originalMotionEntry.yCursorPosition, splitDownTime, |
| 3885 | splitPointerCount, splitPointerProperties, |
| 3886 | splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3887 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3888 | if (originalMotionEntry.injectionState) { |
| 3889 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3890 | splitMotionEntry->injectionState->refCount += 1; |
| 3891 | } |
| 3892 | |
| 3893 | return splitMotionEntry; |
| 3894 | } |
| 3895 | |
| 3896 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3897 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3898 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3899 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3900 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3901 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3902 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3903 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3904 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3905 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3906 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3907 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3908 | } // release lock |
| 3909 | |
| 3910 | if (needWake) { |
| 3911 | mLooper->wake(); |
| 3912 | } |
| 3913 | } |
| 3914 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3915 | /** |
| 3916 | * If one of the meta shortcuts is detected, process them here: |
| 3917 | * Meta + Backspace -> generate BACK |
| 3918 | * Meta + Enter -> generate HOME |
| 3919 | * This will potentially overwrite keyCode and metaState. |
| 3920 | */ |
| 3921 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3922 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3923 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3924 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3925 | if (keyCode == AKEYCODE_DEL) { |
| 3926 | newKeyCode = AKEYCODE_BACK; |
| 3927 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3928 | newKeyCode = AKEYCODE_HOME; |
| 3929 | } |
| 3930 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3931 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3932 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3933 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3934 | keyCode = newKeyCode; |
| 3935 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3936 | } |
| 3937 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3938 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3939 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3940 | // 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] | 3941 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3942 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3943 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3944 | if (replacementIt != mReplacedKeys.end()) { |
| 3945 | keyCode = replacementIt->second; |
| 3946 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3947 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3948 | } |
| 3949 | } |
| 3950 | } |
| 3951 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3952 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3953 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3954 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3955 | "policyFlags=0x%x, action=0x%x, " |
| 3956 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3957 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3958 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3959 | args->downTime); |
| 3960 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3961 | if (!validateKeyEvent(args->action)) { |
| 3962 | return; |
| 3963 | } |
| 3964 | |
| 3965 | uint32_t policyFlags = args->policyFlags; |
| 3966 | int32_t flags = args->flags; |
| 3967 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3968 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3969 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3970 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3971 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3972 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3973 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3974 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3975 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3976 | metaState |= AMETA_FUNCTION_ON; |
| 3977 | } |
| 3978 | |
| 3979 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3980 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3981 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3982 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3983 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3984 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3985 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3986 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3987 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3988 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3989 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3990 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3991 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3992 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3993 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3994 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3995 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3996 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3997 | { // acquire lock |
| 3998 | mLock.lock(); |
| 3999 | |
| 4000 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4001 | mLock.unlock(); |
| 4002 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4003 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4004 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4005 | return; // event was consumed by the filter |
| 4006 | } |
| 4007 | |
| 4008 | mLock.lock(); |
| 4009 | } |
| 4010 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4011 | std::unique_ptr<KeyEntry> newEntry = |
| 4012 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 4013 | args->displayId, policyFlags, args->action, flags, |
| 4014 | keyCode, args->scanCode, metaState, repeatCount, |
| 4015 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4016 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4017 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4018 | mLock.unlock(); |
| 4019 | } // release lock |
| 4020 | |
| 4021 | if (needWake) { |
| 4022 | mLooper->wake(); |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 4027 | return mInputFilterEnabled; |
| 4028 | } |
| 4029 | |
| 4030 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4031 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4032 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4033 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 4034 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 4035 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4036 | "yCursorPosition=%f, downTime=%" PRId64, |
| 4037 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 4038 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 4039 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 4040 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 4041 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 4042 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 4043 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 4044 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 4045 | "orientation=%f", |
| 4046 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 4047 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4048 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4049 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4050 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4051 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4052 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4053 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4054 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4055 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 4056 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4057 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4058 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 4059 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4060 | return; |
| 4061 | } |
| 4062 | |
| 4063 | uint32_t policyFlags = args->policyFlags; |
| 4064 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4065 | |
| 4066 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4067 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4068 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4069 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4070 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4071 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4072 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4073 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4074 | { // acquire lock |
| 4075 | mLock.lock(); |
| 4076 | |
| 4077 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4078 | ui::Transform displayTransform; |
| 4079 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4080 | displayTransform = it->second.transform; |
| 4081 | } |
| 4082 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4083 | mLock.unlock(); |
| 4084 | |
| 4085 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4086 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4087 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4088 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4089 | displayTransform, args->xPrecision, args->yPrecision, |
| 4090 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4091 | args->downTime, args->eventTime, args->pointerCount, |
| 4092 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4093 | |
| 4094 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4095 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4096 | return; // event was consumed by the filter |
| 4097 | } |
| 4098 | |
| 4099 | mLock.lock(); |
| 4100 | } |
| 4101 | |
| 4102 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4103 | std::unique_ptr<MotionEntry> newEntry = |
| 4104 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4105 | args->source, args->displayId, policyFlags, |
| 4106 | args->action, args->actionButton, args->flags, |
| 4107 | args->metaState, args->buttonState, |
| 4108 | args->classification, args->edgeFlags, |
| 4109 | args->xPrecision, args->yPrecision, |
| 4110 | args->xCursorPosition, args->yCursorPosition, |
| 4111 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4112 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4113 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4114 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4115 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4116 | !mInputFilterEnabled) { |
| 4117 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4118 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4119 | } |
| 4120 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4121 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4122 | mLock.unlock(); |
| 4123 | } // release lock |
| 4124 | |
| 4125 | if (needWake) { |
| 4126 | mLooper->wake(); |
| 4127 | } |
| 4128 | } |
| 4129 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4130 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4131 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4132 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4133 | " sensorType=%s", |
| 4134 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4135 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4136 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4137 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4138 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4139 | { // acquire lock |
| 4140 | mLock.lock(); |
| 4141 | |
| 4142 | // Just enqueue a new sensor event. |
| 4143 | std::unique_ptr<SensorEntry> newEntry = |
| 4144 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4145 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4146 | args->sensorType, args->accuracy, |
| 4147 | args->accuracyChanged, args->values); |
| 4148 | |
| 4149 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4150 | mLock.unlock(); |
| 4151 | } // release lock |
| 4152 | |
| 4153 | if (needWake) { |
| 4154 | mLooper->wake(); |
| 4155 | } |
| 4156 | } |
| 4157 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4158 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4159 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4160 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4161 | args->deviceId, args->isOn); |
| 4162 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4163 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4164 | } |
| 4165 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4166 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4167 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4171 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4172 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4173 | "switchMask=0x%08x", |
| 4174 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4175 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4176 | |
| 4177 | uint32_t policyFlags = args->policyFlags; |
| 4178 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4179 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4183 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4184 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4185 | args->deviceId); |
| 4186 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4187 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4188 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4189 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4190 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4191 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4192 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4193 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4194 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4195 | } // release lock |
| 4196 | |
| 4197 | if (needWake) { |
| 4198 | mLooper->wake(); |
| 4199 | } |
| 4200 | } |
| 4201 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4202 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4203 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4204 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4205 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4206 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4207 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4208 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4209 | { // acquire lock |
| 4210 | std::scoped_lock _l(mLock); |
| 4211 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4212 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4213 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4214 | } // release lock |
| 4215 | |
| 4216 | if (needWake) { |
| 4217 | mLooper->wake(); |
| 4218 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4219 | } |
| 4220 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4221 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
| 4222 | std::optional<int32_t> targetUid, |
| 4223 | InputEventInjectionSync syncMode, |
| 4224 | std::chrono::milliseconds timeout, |
| 4225 | uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4226 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4227 | ALOGD("injectInputEvent - eventType=%d, targetUid=%s, syncMode=%d, timeout=%lld, " |
| 4228 | "policyFlags=0x%08x", |
| 4229 | event->getType(), targetUid ? std::to_string(*targetUid).c_str() : "none", syncMode, |
| 4230 | timeout.count(), policyFlags); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4231 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4232 | 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] | 4233 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4234 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4235 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4236 | // 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] | 4237 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4238 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4239 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4240 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4241 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4242 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4243 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4244 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4247 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4248 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4249 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4250 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4251 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4252 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4253 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4254 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4255 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4256 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4257 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4258 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4259 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4260 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4261 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4262 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4263 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4264 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4265 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4266 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4267 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4268 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4269 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4270 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4271 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4272 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4273 | |
| 4274 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4275 | android::base::Timer t; |
| 4276 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4277 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4278 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4279 | std::to_string(t.duration().count()).c_str()); |
| 4280 | } |
| 4281 | } |
| 4282 | |
| 4283 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4284 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4285 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4286 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4287 | incomingKey.getDisplayId(), policyFlags, action, |
| 4288 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4289 | incomingKey.getRepeatCount(), |
| 4290 | incomingKey.getDownTime()); |
| 4291 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4292 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4293 | } |
| 4294 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4295 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4296 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4297 | const int32_t action = motionEvent.getAction(); |
| 4298 | const bool isPointerEvent = |
| 4299 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4300 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4301 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4302 | ? ADISPLAY_ID_DEFAULT |
| 4303 | : event->getDisplayId(); |
| 4304 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4305 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4306 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4307 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4308 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4309 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4310 | } |
| 4311 | |
| 4312 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4313 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4314 | android::base::Timer t; |
| 4315 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4316 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4317 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4318 | std::to_string(t.duration().count()).c_str()); |
| 4319 | } |
| 4320 | } |
| 4321 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4322 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4323 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4324 | } |
| 4325 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4326 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4327 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4328 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4329 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4330 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4331 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4332 | displayId, policyFlags, action, actionButton, |
| 4333 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4334 | motionEvent.getButtonState(), |
| 4335 | motionEvent.getClassification(), |
| 4336 | motionEvent.getEdgeFlags(), |
| 4337 | motionEvent.getXPrecision(), |
| 4338 | motionEvent.getYPrecision(), |
| 4339 | motionEvent.getRawXCursorPosition(), |
| 4340 | motionEvent.getRawYCursorPosition(), |
| 4341 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4342 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4343 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4344 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4345 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4346 | sampleEventTimes += 1; |
| 4347 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4348 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4349 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4350 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4351 | displayId, policyFlags, action, actionButton, |
| 4352 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4353 | motionEvent.getButtonState(), |
| 4354 | motionEvent.getClassification(), |
| 4355 | motionEvent.getEdgeFlags(), |
| 4356 | motionEvent.getXPrecision(), |
| 4357 | motionEvent.getYPrecision(), |
| 4358 | motionEvent.getRawXCursorPosition(), |
| 4359 | motionEvent.getRawYCursorPosition(), |
| 4360 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4361 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4362 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4363 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4364 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4365 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4366 | } |
| 4367 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4368 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4369 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4370 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4371 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4372 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4373 | } |
| 4374 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4375 | InjectionState* injectionState = new InjectionState(targetUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4376 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4377 | injectionState->injectionIsAsync = true; |
| 4378 | } |
| 4379 | |
| 4380 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4381 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4382 | |
| 4383 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4384 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4385 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4386 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4387 | } |
| 4388 | |
| 4389 | mLock.unlock(); |
| 4390 | |
| 4391 | if (needWake) { |
| 4392 | mLooper->wake(); |
| 4393 | } |
| 4394 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4395 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4396 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4397 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4398 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4399 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4400 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4401 | } else { |
| 4402 | for (;;) { |
| 4403 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4404 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4405 | break; |
| 4406 | } |
| 4407 | |
| 4408 | nsecs_t remainingTimeout = endTime - now(); |
| 4409 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4410 | if (DEBUG_INJECTION) { |
| 4411 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4412 | "to become available."); |
| 4413 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4414 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4415 | break; |
| 4416 | } |
| 4417 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4418 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4419 | } |
| 4420 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4421 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4422 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4423 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4424 | if (DEBUG_INJECTION) { |
| 4425 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4426 | injectionState->pendingForegroundDispatches); |
| 4427 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4428 | nsecs_t remainingTimeout = endTime - now(); |
| 4429 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4430 | if (DEBUG_INJECTION) { |
| 4431 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4432 | "dispatches to finish."); |
| 4433 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4434 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4435 | break; |
| 4436 | } |
| 4437 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4438 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4439 | } |
| 4440 | } |
| 4441 | } |
| 4442 | |
| 4443 | injectionState->release(); |
| 4444 | } // release lock |
| 4445 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4446 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4447 | ALOGD("injectInputEvent - Finished with result %d.", injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4448 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4449 | |
| 4450 | return injectionResult; |
| 4451 | } |
| 4452 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4453 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4454 | std::array<uint8_t, 32> calculatedHmac; |
| 4455 | std::unique_ptr<VerifiedInputEvent> result; |
| 4456 | switch (event.getType()) { |
| 4457 | case AINPUT_EVENT_TYPE_KEY: { |
| 4458 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4459 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4460 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4461 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4462 | break; |
| 4463 | } |
| 4464 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4465 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4466 | VerifiedMotionEvent verifiedMotionEvent = |
| 4467 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4468 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4469 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4470 | break; |
| 4471 | } |
| 4472 | default: { |
| 4473 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4474 | return nullptr; |
| 4475 | } |
| 4476 | } |
| 4477 | if (calculatedHmac == INVALID_HMAC) { |
| 4478 | return nullptr; |
| 4479 | } |
| 4480 | if (calculatedHmac != event.getHmac()) { |
| 4481 | return nullptr; |
| 4482 | } |
| 4483 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4484 | } |
| 4485 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4486 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4487 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4488 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4489 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4490 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4491 | ALOGD("Setting input event injection result to %d.", injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4492 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4493 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4494 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4495 | // Log the outcome since the injector did not wait for the injection result. |
| 4496 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4497 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4498 | ALOGV("Asynchronous input event injection succeeded."); |
| 4499 | break; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4500 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4501 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4502 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4503 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4504 | ALOGW("Asynchronous input event injection failed."); |
| 4505 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4506 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4507 | ALOGW("Asynchronous input event injection timed out."); |
| 4508 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4509 | case InputEventInjectionResult::PENDING: |
| 4510 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4511 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4512 | } |
| 4513 | } |
| 4514 | |
| 4515 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4516 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4517 | } |
| 4518 | } |
| 4519 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4520 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4521 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4522 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4523 | // display space, so we need to transform the injected events accordingly. |
| 4524 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4525 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4526 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4527 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4528 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4529 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4530 | const vec2 cursor = |
| 4531 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4532 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4533 | entry.xCursorPosition = cursor.x; |
| 4534 | entry.yCursorPosition = cursor.y; |
| 4535 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4536 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4537 | entry.pointerCoords[i] = |
| 4538 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4539 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4540 | } |
| 4541 | } |
| 4542 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4543 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4544 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4545 | if (injectionState) { |
| 4546 | injectionState->pendingForegroundDispatches += 1; |
| 4547 | } |
| 4548 | } |
| 4549 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4550 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4551 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4552 | if (injectionState) { |
| 4553 | injectionState->pendingForegroundDispatches -= 1; |
| 4554 | |
| 4555 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4556 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4557 | } |
| 4558 | } |
| 4559 | } |
| 4560 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4561 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4562 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4563 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4564 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4565 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4566 | } |
| 4567 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4568 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4569 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4570 | if (windowHandleToken == nullptr) { |
| 4571 | return nullptr; |
| 4572 | } |
| 4573 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4574 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4575 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4576 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4577 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4578 | return windowHandle; |
| 4579 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4580 | } |
| 4581 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4582 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4583 | } |
| 4584 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4585 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4586 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4587 | if (windowHandleToken == nullptr) { |
| 4588 | return nullptr; |
| 4589 | } |
| 4590 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4591 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4592 | if (windowHandle->getToken() == windowHandleToken) { |
| 4593 | return windowHandle; |
| 4594 | } |
| 4595 | } |
| 4596 | return nullptr; |
| 4597 | } |
| 4598 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4599 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4600 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4601 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4602 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4603 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4604 | if (handle->getId() == windowHandle->getId() && |
| 4605 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4606 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4607 | ALOGE("Found window %s in display %" PRId32 |
| 4608 | ", but it should belong to display %" PRId32, |
| 4609 | windowHandle->getName().c_str(), it.first, |
| 4610 | windowHandle->getInfo()->displayId); |
| 4611 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4612 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4613 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4614 | } |
| 4615 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4616 | return nullptr; |
| 4617 | } |
| 4618 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4619 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4620 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4621 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4622 | } |
| 4623 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4624 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4625 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4626 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4627 | windowHandle.getInfo()->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4628 | if (connection != nullptr && noInputChannel) { |
| 4629 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4630 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4631 | return false; |
| 4632 | } |
| 4633 | |
| 4634 | if (connection == nullptr) { |
| 4635 | if (!noInputChannel) { |
| 4636 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4637 | } |
| 4638 | return false; |
| 4639 | } |
| 4640 | if (!connection->responsive) { |
| 4641 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4642 | return false; |
| 4643 | } |
| 4644 | return true; |
| 4645 | } |
| 4646 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4647 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4648 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4649 | auto connectionIt = mConnectionsByToken.find(token); |
| 4650 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4651 | return nullptr; |
| 4652 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4653 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4654 | } |
| 4655 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4656 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4657 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4658 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4659 | // Remove all handles on a display if there are no windows left. |
| 4660 | mWindowHandlesByDisplay.erase(displayId); |
| 4661 | return; |
| 4662 | } |
| 4663 | |
| 4664 | // Since we compare the pointer of input window handles across window updates, we need |
| 4665 | // 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] | 4666 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4667 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4668 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4669 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4670 | } |
| 4671 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4672 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4673 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4674 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4675 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4676 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4677 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4678 | const bool canReceiveInput = |
| 4679 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4680 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4681 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4682 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4683 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4684 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4685 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4686 | } |
| 4687 | |
| 4688 | if (info->displayId != displayId) { |
| 4689 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4690 | handle->getName().c_str(), displayId, info->displayId); |
| 4691 | continue; |
| 4692 | } |
| 4693 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4694 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4695 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4696 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4697 | oldHandle->updateFrom(handle); |
| 4698 | newHandles.push_back(oldHandle); |
| 4699 | } else { |
| 4700 | newHandles.push_back(handle); |
| 4701 | } |
| 4702 | } |
| 4703 | |
| 4704 | // Insert or replace |
| 4705 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4706 | } |
| 4707 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4708 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4709 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4710 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4711 | { // acquire lock |
| 4712 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4713 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4714 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4715 | } |
| 4716 | } |
| 4717 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4718 | mLooper->wake(); |
| 4719 | } |
| 4720 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4721 | /** |
| 4722 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4723 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4724 | * If set an empty list, remove all handles from the specific display. |
| 4725 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4726 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4727 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4728 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4729 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4730 | if (DEBUG_FOCUS) { |
| 4731 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4732 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4733 | windowList += iwh->getName() + " "; |
| 4734 | } |
| 4735 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4736 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4737 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4738 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4739 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4740 | const WindowInfo& info = *window->getInfo(); |
| 4741 | |
| 4742 | // 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] | 4743 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4744 | if (noInputWindow && window->getToken() != nullptr) { |
| 4745 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4746 | window->getName().c_str()); |
| 4747 | window->releaseChannel(); |
| 4748 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4749 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4750 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4751 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 4752 | !info.inputConfig.test( |
| 4753 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4754 | "%s has feature SPY, but is not a trusted overlay.", |
| 4755 | window->getName().c_str()); |
| 4756 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4757 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4758 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 4759 | !info.inputConfig.test( |
| 4760 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4761 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4762 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4763 | } |
| 4764 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4765 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4766 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4767 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4768 | // Save the old windows' orientation by ID before it gets updated. |
| 4769 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4770 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4771 | oldWindowOrientations.emplace(handle->getId(), |
| 4772 | handle->getInfo()->transform.getOrientation()); |
| 4773 | } |
| 4774 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4775 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4776 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4777 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4778 | if (mLastHoverWindowHandle && |
| 4779 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4780 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4781 | mLastHoverWindowHandle = nullptr; |
| 4782 | } |
| 4783 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4784 | std::optional<FocusResolver::FocusChanges> changes = |
| 4785 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4786 | if (changes) { |
| 4787 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4788 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4789 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4790 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4791 | mTouchStatesByDisplay.find(displayId); |
| 4792 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4793 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4794 | for (size_t i = 0; i < state.windows.size();) { |
| 4795 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4796 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4797 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4798 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4799 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4800 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4801 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4802 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4803 | if (touchedInputChannel != nullptr) { |
| 4804 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4805 | "touched window was removed"); |
| 4806 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4807 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4808 | // well. |
| 4809 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4810 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 4811 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4812 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4813 | if (wallpaper != nullptr) { |
| 4814 | sp<Connection> wallpaperConnection = |
| 4815 | getConnectionLocked(wallpaper->getToken()); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4816 | if (wallpaperConnection != nullptr) { |
| 4817 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4818 | options); |
| 4819 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4820 | } |
| 4821 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4822 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4823 | state.windows.erase(state.windows.begin() + i); |
| 4824 | } else { |
| 4825 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4826 | } |
| 4827 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4828 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4829 | // 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] | 4830 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 4831 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4832 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4833 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 4834 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 4835 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4836 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4837 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4838 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4839 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4840 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4841 | // pointer events if necessary. |
| 4842 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4843 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4844 | if (newWindowHandle != nullptr && |
| 4845 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4846 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4847 | std::shared_ptr<InputChannel> inputChannel = |
| 4848 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4849 | if (inputChannel != nullptr) { |
| 4850 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4851 | "touched window's orientation changed"); |
| 4852 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4853 | } |
| 4854 | } |
| 4855 | } |
| 4856 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4857 | // Release information for windows that are no longer present. |
| 4858 | // This ensures that unused input channels are released promptly. |
| 4859 | // Otherwise, they might stick around until the window handle is destroyed |
| 4860 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4861 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4862 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4863 | if (DEBUG_FOCUS) { |
| 4864 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4865 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4866 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4867 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4868 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4872 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4873 | if (DEBUG_FOCUS) { |
| 4874 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4875 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4876 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4877 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4878 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4879 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4880 | } // release lock |
| 4881 | |
| 4882 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4883 | mLooper->wake(); |
| 4884 | } |
| 4885 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4886 | void InputDispatcher::setFocusedApplicationLocked( |
| 4887 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4888 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4889 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4890 | |
| 4891 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4892 | return; // This application is already focused. No need to wake up or change anything. |
| 4893 | } |
| 4894 | |
| 4895 | // Set the new application handle. |
| 4896 | if (inputApplicationHandle != nullptr) { |
| 4897 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4898 | } else { |
| 4899 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4900 | } |
| 4901 | |
| 4902 | // No matter what the old focused application was, stop waiting on it because it is |
| 4903 | // no longer focused. |
| 4904 | resetNoFocusedWindowTimeoutLocked(); |
| 4905 | } |
| 4906 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4907 | /** |
| 4908 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4909 | * the display not specified. |
| 4910 | * |
| 4911 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4912 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4913 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4914 | * display. The display-specified events won't be affected. |
| 4915 | */ |
| 4916 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4917 | if (DEBUG_FOCUS) { |
| 4918 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4919 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4920 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4921 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4922 | |
| 4923 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4924 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4925 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4926 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4927 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4928 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4929 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4930 | CancelationOptions |
| 4931 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4932 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4933 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4934 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4935 | } |
| 4936 | } |
| 4937 | mFocusedDisplayId = displayId; |
| 4938 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4939 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4940 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4941 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4942 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4943 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4944 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4945 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4946 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4947 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4948 | } |
| 4949 | } |
| 4950 | } |
| 4951 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4952 | if (DEBUG_FOCUS) { |
| 4953 | logDispatchStateLocked(); |
| 4954 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4955 | } // release lock |
| 4956 | |
| 4957 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4958 | mLooper->wake(); |
| 4959 | } |
| 4960 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4961 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4962 | if (DEBUG_FOCUS) { |
| 4963 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4964 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4965 | |
| 4966 | bool changed; |
| 4967 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4968 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4969 | |
| 4970 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4971 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4972 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | if (mDispatchEnabled && !enabled) { |
| 4976 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4977 | } |
| 4978 | |
| 4979 | mDispatchEnabled = enabled; |
| 4980 | mDispatchFrozen = frozen; |
| 4981 | changed = true; |
| 4982 | } else { |
| 4983 | changed = false; |
| 4984 | } |
| 4985 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4986 | if (DEBUG_FOCUS) { |
| 4987 | logDispatchStateLocked(); |
| 4988 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4989 | } // release lock |
| 4990 | |
| 4991 | if (changed) { |
| 4992 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4993 | mLooper->wake(); |
| 4994 | } |
| 4995 | } |
| 4996 | |
| 4997 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4998 | if (DEBUG_FOCUS) { |
| 4999 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5000 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5001 | |
| 5002 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5003 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5004 | |
| 5005 | if (mInputFilterEnabled == enabled) { |
| 5006 | return; |
| 5007 | } |
| 5008 | |
| 5009 | mInputFilterEnabled = enabled; |
| 5010 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5011 | } // release lock |
| 5012 | |
| 5013 | // Wake up poll loop since there might be work to do to drop everything. |
| 5014 | mLooper->wake(); |
| 5015 | } |
| 5016 | |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 5017 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission, |
| 5018 | int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5019 | bool needWake = false; |
| 5020 | { |
| 5021 | std::scoped_lock lock(mLock); |
| 5022 | if (mInTouchMode == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5023 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5024 | } |
| 5025 | if (DEBUG_TOUCH_MODE) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5026 | ALOGD("Request to change touch mode from %s to %s (calling pid=%d, uid=%d, " |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 5027 | "hasPermission=%s, target displayId=%d, perDisplayTouchModeEnabled=%s)", |
| 5028 | toString(mInTouchMode), toString(inTouchMode), pid, uid, toString(hasPermission), |
| 5029 | displayId, toString(kPerDisplayTouchModeEnabled)); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5030 | } |
| 5031 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5032 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5033 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
| 5034 | ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " |
| 5035 | "window nor none of the previously interacted window", |
| 5036 | pid, uid); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5037 | return false; |
| 5038 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5039 | } |
| 5040 | |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 5041 | // TODO(b/198499018): Store touch mode per display (kPerDisplayTouchModeEnabled) |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5042 | mInTouchMode = inTouchMode; |
| 5043 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5044 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 5045 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5046 | } // release lock |
| 5047 | |
| 5048 | if (needWake) { |
| 5049 | mLooper->wake(); |
| 5050 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5051 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5052 | } |
| 5053 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5054 | bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { |
| 5055 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5056 | if (focusedToken == nullptr) { |
| 5057 | return false; |
| 5058 | } |
| 5059 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5060 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5061 | } |
| 5062 | |
| 5063 | bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { |
| 5064 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5065 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5066 | const sp<WindowInfoHandle> windowHandle = |
| 5067 | getWindowHandleLocked(connectionToken); |
| 5068 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5069 | }) != mInteractionConnectionTokens.end(); |
| 5070 | } |
| 5071 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5072 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5073 | if (opacity < 0 || opacity > 1) { |
| 5074 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5075 | return; |
| 5076 | } |
| 5077 | |
| 5078 | std::scoped_lock lock(mLock); |
| 5079 | mMaximumObscuringOpacityForTouch = opacity; |
| 5080 | } |
| 5081 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5082 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 5083 | const sp<IBinder>& token) { |
| 5084 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5085 | for (TouchedWindow& w : state.windows) { |
| 5086 | if (w.windowHandle->getToken() == token) { |
| 5087 | return std::make_pair(&state, &w); |
| 5088 | } |
| 5089 | } |
| 5090 | } |
| 5091 | return std::make_pair(nullptr, nullptr); |
| 5092 | } |
| 5093 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5094 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5095 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5096 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5097 | if (DEBUG_FOCUS) { |
| 5098 | ALOGD("Trivial transfer to same window."); |
| 5099 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5100 | return true; |
| 5101 | } |
| 5102 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5103 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5104 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5105 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5106 | // Find the target touch state and touched window by fromToken. |
| 5107 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 5108 | if (state == nullptr || touchedWindow == nullptr) { |
| 5109 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5110 | return false; |
| 5111 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5112 | |
| 5113 | const int32_t displayId = state->displayId; |
| 5114 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5115 | if (toWindowHandle == nullptr) { |
| 5116 | ALOGW("Cannot transfer focus because to window not found."); |
| 5117 | return false; |
| 5118 | } |
| 5119 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5120 | if (DEBUG_FOCUS) { |
| 5121 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5122 | touchedWindow->windowHandle->getName().c_str(), |
| 5123 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5124 | } |
| 5125 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5126 | // Erase old window. |
| 5127 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 5128 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 5129 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5130 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5131 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5132 | nsecs_t downTimeInTarget = now(); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5133 | int32_t newTargetFlags = |
| 5134 | oldTargetFlags & (InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5135 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
| 5136 | newTargetFlags |= InputTarget::FLAG_FOREGROUND; |
| 5137 | } |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5138 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5139 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5140 | // Store the dragging window. |
| 5141 | if (isDragDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5142 | if (pointerIds.count() != 1) { |
| 5143 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5144 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5145 | return false; |
| 5146 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5147 | // Track the pointer id for drag window and generate the drag state. |
| 5148 | const int32_t id = pointerIds.firstMarkedBit(); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5149 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5150 | } |
| 5151 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5152 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5153 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5154 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5155 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5156 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5157 | CancelationOptions |
| 5158 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5159 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5160 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5161 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5162 | } |
| 5163 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5164 | if (DEBUG_FOCUS) { |
| 5165 | logDispatchStateLocked(); |
| 5166 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5167 | } // release lock |
| 5168 | |
| 5169 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5170 | mLooper->wake(); |
| 5171 | return true; |
| 5172 | } |
| 5173 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5174 | /** |
| 5175 | * Get the touched foreground window on the given display. |
| 5176 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5177 | * window is being touched. |
| 5178 | */ |
| 5179 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5180 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5181 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5182 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5183 | return nullptr; |
| 5184 | } |
| 5185 | |
| 5186 | const TouchState& state = stateIt->second; |
| 5187 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5188 | // If multiple foreground windows are touched, return nullptr |
| 5189 | for (const TouchedWindow& window : state.windows) { |
| 5190 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 5191 | if (touchedForegroundWindow != nullptr) { |
| 5192 | ALOGI("Two or more foreground windows: %s and %s", |
| 5193 | touchedForegroundWindow->getName().c_str(), |
| 5194 | window.windowHandle->getName().c_str()); |
| 5195 | return nullptr; |
| 5196 | } |
| 5197 | touchedForegroundWindow = window.windowHandle; |
| 5198 | } |
| 5199 | } |
| 5200 | return touchedForegroundWindow; |
| 5201 | } |
| 5202 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5203 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5204 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5205 | sp<IBinder> fromToken; |
| 5206 | { // acquire lock |
| 5207 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5208 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5209 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5210 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5211 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5212 | return false; |
| 5213 | } |
| 5214 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5215 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5216 | if (from == nullptr) { |
| 5217 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5218 | return false; |
| 5219 | } |
| 5220 | |
| 5221 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5222 | } // release lock |
| 5223 | |
| 5224 | return transferTouchFocus(fromToken, destChannelToken); |
| 5225 | } |
| 5226 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5227 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5228 | if (DEBUG_FOCUS) { |
| 5229 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5230 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5231 | |
| 5232 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5233 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5234 | |
| 5235 | resetKeyRepeatLocked(); |
| 5236 | releasePendingEventLocked(); |
| 5237 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5238 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5239 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5240 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5241 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5242 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5243 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5244 | } |
| 5245 | |
| 5246 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5247 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5248 | dumpDispatchStateLocked(dump); |
| 5249 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5250 | std::istringstream stream(dump); |
| 5251 | std::string line; |
| 5252 | |
| 5253 | while (std::getline(stream, line, '\n')) { |
| 5254 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5255 | } |
| 5256 | } |
| 5257 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5258 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5259 | std::string dump; |
| 5260 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5261 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5262 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5263 | |
| 5264 | std::string windowName = "None"; |
| 5265 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5266 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5267 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5268 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5269 | : "token has capture without window"; |
| 5270 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5271 | 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] | 5272 | |
| 5273 | return dump; |
| 5274 | } |
| 5275 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5276 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5277 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5278 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5279 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5280 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5281 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5282 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5283 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5284 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5285 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5286 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5287 | const std::chrono::duration timeout = |
| 5288 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5289 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5290 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5291 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5292 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5293 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5294 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5295 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5296 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5297 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5298 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5299 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5300 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5301 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5302 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5303 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5304 | 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] | 5305 | state.displayId, toString(state.down), toString(state.split), |
| 5306 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5307 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5308 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5309 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5310 | const TouchedWindow& touchedWindow = state.windows[i]; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5311 | dump += StringPrintf(INDENT4 "%zu: name='%s', pointerIds=0x%0x, " |
| 5312 | "targetFlags=0x%x, firstDownTimeInTarget=%" PRId64 |
| 5313 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5314 | i, touchedWindow.windowHandle->getName().c_str(), |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5315 | touchedWindow.pointerIds.value, touchedWindow.targetFlags, |
| 5316 | ns2ms(touchedWindow.firstDownTimeInTarget.value_or(0))); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5317 | } |
| 5318 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5319 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5320 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5321 | } |
| 5322 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5323 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5324 | } |
| 5325 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5326 | if (mDragState) { |
| 5327 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5328 | mDragState->dump(dump, INDENT2); |
| 5329 | } |
| 5330 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5331 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5332 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5333 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5334 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5335 | const auto& displayInfo = it->second; |
| 5336 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5337 | displayInfo.logicalHeight); |
| 5338 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5339 | } else { |
| 5340 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5341 | } |
| 5342 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5343 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5344 | dump += INDENT2 "Windows:\n"; |
| 5345 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5346 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5347 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5348 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5349 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5350 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5351 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5352 | "applicationInfo.name=%s, " |
| 5353 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5354 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5355 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5356 | windowInfo->displayId, |
| 5357 | windowInfo->inputConfig.string().c_str(), |
| 5358 | windowInfo->alpha, windowInfo->frameLeft, |
| 5359 | windowInfo->frameTop, windowInfo->frameRight, |
| 5360 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5361 | windowInfo->applicationInfo.name.c_str(), |
| 5362 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5363 | dump += dumpRegion(windowInfo->touchableRegion); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5364 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5365 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5366 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5367 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5368 | millis(windowInfo->dispatchingTimeout), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5369 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5370 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5371 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5372 | } |
| 5373 | } else { |
| 5374 | dump += INDENT2 "Windows: <none>\n"; |
| 5375 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5376 | } |
| 5377 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5378 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5379 | } |
| 5380 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5381 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5382 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5383 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5384 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5385 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5386 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5387 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5388 | } |
| 5389 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5390 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5391 | |
| 5392 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5393 | if (!mRecentQueue.empty()) { |
| 5394 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5395 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5396 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5397 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5398 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5399 | } |
| 5400 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5401 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5402 | } |
| 5403 | |
| 5404 | // Dump event currently being dispatched. |
| 5405 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5406 | dump += INDENT "PendingEvent:\n"; |
| 5407 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5408 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5409 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5410 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5411 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5412 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5413 | } |
| 5414 | |
| 5415 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5416 | if (!mInboundQueue.empty()) { |
| 5417 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5418 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5419 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5420 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5421 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5422 | } |
| 5423 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5424 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5425 | } |
| 5426 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5427 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5428 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5429 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5430 | const KeyReplacement& replacement = pair.first; |
| 5431 | int32_t newKeyCode = pair.second; |
| 5432 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5433 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5434 | } |
| 5435 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5436 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5437 | } |
| 5438 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5439 | if (!mCommandQueue.empty()) { |
| 5440 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5441 | } else { |
| 5442 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5443 | } |
| 5444 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5445 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5446 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5447 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5448 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5449 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5450 | connection->inputChannel->getFd().get(), |
| 5451 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5452 | connection->getWindowName().c_str(), |
| 5453 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5454 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5455 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5456 | if (!connection->outboundQueue.empty()) { |
| 5457 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5458 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5459 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5460 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5461 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5462 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5463 | } |
| 5464 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5465 | if (!connection->waitQueue.empty()) { |
| 5466 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5467 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5468 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5469 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5470 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5471 | } |
| 5472 | } |
| 5473 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5474 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5478 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5479 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5480 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5481 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5482 | } |
| 5483 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5484 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5485 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5486 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5487 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5488 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5489 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5490 | } |
| 5491 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5492 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5493 | const size_t numMonitors = monitors.size(); |
| 5494 | for (size_t i = 0; i < numMonitors; i++) { |
| 5495 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5496 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5497 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5498 | dump += "\n"; |
| 5499 | } |
| 5500 | } |
| 5501 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5502 | class LooperEventCallback : public LooperCallback { |
| 5503 | public: |
| 5504 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5505 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5506 | |
| 5507 | private: |
| 5508 | std::function<int(int events)> mCallback; |
| 5509 | }; |
| 5510 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5511 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5512 | if (DEBUG_CHANNEL_CREATION) { |
| 5513 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5514 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5515 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5516 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5517 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5518 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5519 | |
| 5520 | if (result) { |
| 5521 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5522 | } |
| 5523 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5524 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5525 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5526 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5527 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5528 | sp<Connection> connection = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5529 | sp<Connection>::make(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5530 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5531 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5532 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5533 | } |
| 5534 | mConnectionsByToken.emplace(token, connection); |
| 5535 | |
| 5536 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5537 | this, std::placeholders::_1, token); |
| 5538 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5539 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5540 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5541 | } // release lock |
| 5542 | |
| 5543 | // Wake the looper because some connections have changed. |
| 5544 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5545 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5546 | } |
| 5547 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5548 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5549 | const std::string& name, |
| 5550 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5551 | std::shared_ptr<InputChannel> serverChannel; |
| 5552 | std::unique_ptr<InputChannel> clientChannel; |
| 5553 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5554 | if (result) { |
| 5555 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5556 | } |
| 5557 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5558 | { // acquire lock |
| 5559 | std::scoped_lock _l(mLock); |
| 5560 | |
| 5561 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5562 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5563 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5564 | } |
| 5565 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5566 | sp<Connection> connection = |
| 5567 | sp<Connection>::make(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5568 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5569 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5570 | |
| 5571 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5572 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5573 | } |
| 5574 | mConnectionsByToken.emplace(token, connection); |
| 5575 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5576 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5577 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5578 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5579 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5580 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5581 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5582 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5583 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5584 | // Wake the looper because some connections have changed. |
| 5585 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5586 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5587 | } |
| 5588 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5589 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5590 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5591 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5592 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5593 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5594 | if (status) { |
| 5595 | return status; |
| 5596 | } |
| 5597 | } // release lock |
| 5598 | |
| 5599 | // Wake the poll loop because removing the connection may have changed the current |
| 5600 | // synchronization state. |
| 5601 | mLooper->wake(); |
| 5602 | return OK; |
| 5603 | } |
| 5604 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5605 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5606 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5607 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5608 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5609 | // 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] | 5610 | return BAD_VALUE; |
| 5611 | } |
| 5612 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5613 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5614 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5615 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5616 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5617 | } |
| 5618 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5619 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5620 | |
| 5621 | nsecs_t currentTime = now(); |
| 5622 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5623 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5624 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5625 | return OK; |
| 5626 | } |
| 5627 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5628 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5629 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5630 | auto& [displayId, monitors] = *it; |
| 5631 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5632 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5633 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5634 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5635 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5636 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5637 | } else { |
| 5638 | ++it; |
| 5639 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5640 | } |
| 5641 | } |
| 5642 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5643 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5644 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5645 | return pilferPointersLocked(token); |
| 5646 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5647 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5648 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5649 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5650 | if (!requestingChannel) { |
| 5651 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5652 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5653 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5654 | |
| 5655 | auto [statePtr, windowPtr] = findTouchStateAndWindowLocked(token); |
| 5656 | if (statePtr == nullptr || windowPtr == nullptr || !statePtr->down) { |
| 5657 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5658 | " Ignoring."); |
| 5659 | return BAD_VALUE; |
| 5660 | } |
| 5661 | |
| 5662 | TouchState& state = *statePtr; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5663 | TouchedWindow& window = *windowPtr; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5664 | // Send cancel events to all the input channels we're stealing from. |
| 5665 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5666 | "input channel stole pointer stream"); |
| 5667 | options.deviceId = state.deviceId; |
| 5668 | options.displayId = state.displayId; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5669 | if (state.split) { |
| 5670 | // If split pointers then selectively cancel pointers otherwise cancel all pointers |
| 5671 | options.pointerIds = window.pointerIds; |
| 5672 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5673 | std::string canceledWindows; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5674 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5675 | const std::shared_ptr<InputChannel> channel = |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5676 | getInputChannelLocked(w.windowHandle->getToken()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5677 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5678 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5679 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5680 | canceledWindows += channel->getName(); |
| 5681 | } |
| 5682 | } |
| 5683 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5684 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5685 | canceledWindows.c_str()); |
| 5686 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5687 | // Prevent the gesture from being sent to any other windows. |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5688 | // This only blocks relevant pointers to be sent to other windows |
| 5689 | window.isPilferingPointers = true; |
| 5690 | |
| 5691 | if (state.split) { |
| 5692 | state.cancelPointersForWindowsExcept(window.pointerIds, token); |
| 5693 | } else { |
| 5694 | state.filterWindowsExcept(token); |
| 5695 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5696 | return OK; |
| 5697 | } |
| 5698 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5699 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5700 | { // acquire lock |
| 5701 | std::scoped_lock _l(mLock); |
| 5702 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5703 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5704 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5705 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5706 | : "token without window"); |
| 5707 | } |
| 5708 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5709 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5710 | if (focusedToken != windowToken) { |
| 5711 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5712 | enabled ? "enable" : "disable"); |
| 5713 | return; |
| 5714 | } |
| 5715 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5716 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5717 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5718 | "window has %s requested pointer capture.", |
| 5719 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5720 | return; |
| 5721 | } |
| 5722 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5723 | if (enabled) { |
| 5724 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5725 | mIneligibleDisplaysForPointerCapture.end(), |
| 5726 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5727 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5728 | return; |
| 5729 | } |
| 5730 | } |
| 5731 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5732 | setPointerCaptureLocked(enabled); |
| 5733 | } // release lock |
| 5734 | |
| 5735 | // Wake the thread to process command entries. |
| 5736 | mLooper->wake(); |
| 5737 | } |
| 5738 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5739 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5740 | { // acquire lock |
| 5741 | std::scoped_lock _l(mLock); |
| 5742 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5743 | if (!isEligible) { |
| 5744 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5745 | } |
| 5746 | } // release lock |
| 5747 | } |
| 5748 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5749 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5750 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5751 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5752 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5753 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5754 | } |
| 5755 | } |
| 5756 | } |
| 5757 | return std::nullopt; |
| 5758 | } |
| 5759 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5760 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5761 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5762 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5763 | } |
| 5764 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5765 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5766 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5767 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5768 | } |
| 5769 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5770 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5771 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5772 | } |
| 5773 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5774 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5775 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5776 | if (connection == nullptr) { |
| 5777 | return "<nullptr>"; |
| 5778 | } |
| 5779 | return connection->getInputChannelName(); |
| 5780 | } |
| 5781 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5782 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5783 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5784 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5785 | } |
| 5786 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5787 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5788 | const sp<Connection>& connection, uint32_t seq, |
| 5789 | bool handled, nsecs_t consumeTime) { |
| 5790 | // Handle post-event policy actions. |
| 5791 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5792 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5793 | return; |
| 5794 | } |
| 5795 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5796 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5797 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5798 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5799 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5800 | } |
| 5801 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5802 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5803 | connection->inputChannel->getConnectionToken(), |
| 5804 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5805 | } |
| 5806 | |
| 5807 | bool restartEvent; |
| 5808 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5809 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5810 | restartEvent = |
| 5811 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5812 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5813 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5814 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5815 | handled); |
| 5816 | } else { |
| 5817 | restartEvent = false; |
| 5818 | } |
| 5819 | |
| 5820 | // Dequeue the event and start the next cycle. |
| 5821 | // Because the lock might have been released, it is possible that the |
| 5822 | // contents of the wait queue to have been drained, so we need to double-check |
| 5823 | // a few things. |
| 5824 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5825 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5826 | dispatchEntry = *dispatchEntryIt; |
| 5827 | connection->waitQueue.erase(dispatchEntryIt); |
| 5828 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5829 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5830 | if (!connection->responsive) { |
| 5831 | connection->responsive = isConnectionResponsive(*connection); |
| 5832 | if (connection->responsive) { |
| 5833 | // The connection was unresponsive, and now it's responsive. |
| 5834 | processConnectionResponsiveLocked(*connection); |
| 5835 | } |
| 5836 | } |
| 5837 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5838 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5839 | connection->outboundQueue.push_front(dispatchEntry); |
| 5840 | traceOutboundQueueLength(*connection); |
| 5841 | } else { |
| 5842 | releaseDispatchEntry(dispatchEntry); |
| 5843 | } |
| 5844 | } |
| 5845 | |
| 5846 | // Start the next dispatch cycle for this connection. |
| 5847 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5848 | } |
| 5849 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5850 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5851 | const sp<IBinder>& newToken) { |
| 5852 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5853 | scoped_unlock unlock(mLock); |
| 5854 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5855 | }; |
| 5856 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5857 | } |
| 5858 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5859 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5860 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5861 | scoped_unlock unlock(mLock); |
| 5862 | mPolicy->notifyDropWindow(token, x, y); |
| 5863 | }; |
| 5864 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5865 | } |
| 5866 | |
Vishnu Nair | 2f5bc8b | 2022-08-09 00:03:11 +0000 | [diff] [blame] | 5867 | bool InputDispatcher::onAnrLocked(const android::gui::FocusRequest& pendingFocusRequest) { |
| 5868 | if (pendingFocusRequest.token == nullptr) { |
| 5869 | return false; |
| 5870 | } |
| 5871 | |
| 5872 | const std::string reason = android::base::StringPrintf("%s is not focusable.", |
| 5873 | pendingFocusRequest.windowName.c_str()); |
| 5874 | updateLastAnrStateLocked(pendingFocusRequest.windowName, reason); |
| 5875 | sp<Connection> connection = getConnectionLocked(pendingFocusRequest.token); |
| 5876 | if (connection != nullptr) { |
| 5877 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5878 | // Stop waking up for events on this connection, it is already unresponsive |
| 5879 | cancelEventsForAnrLocked(connection); |
| 5880 | } else { |
| 5881 | sendWindowUnresponsiveCommandLocked(pendingFocusRequest.token, std::nullopt, reason); |
| 5882 | } |
| 5883 | return true; |
| 5884 | } |
| 5885 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5886 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5887 | if (connection == nullptr) { |
| 5888 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5889 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5890 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5891 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5892 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5893 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5894 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5895 | return; |
| 5896 | } |
| 5897 | /** |
| 5898 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5899 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5900 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5901 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5902 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5903 | * most useful. |
| 5904 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5905 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5906 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5907 | std::string reason = |
| 5908 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5909 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5910 | ns2ms(currentWait), |
| 5911 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5912 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5913 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5914 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5915 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5916 | |
| 5917 | // Stop waking up for events on this connection, it is already unresponsive |
| 5918 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5919 | } |
| 5920 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5921 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5922 | std::string reason = |
| 5923 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5924 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5925 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5926 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5927 | scoped_unlock unlock(mLock); |
| 5928 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5929 | }; |
| 5930 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5931 | } |
| 5932 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5933 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5934 | const std::string& reason) { |
| 5935 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5936 | updateLastAnrStateLocked(windowLabel, reason); |
| 5937 | } |
| 5938 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5939 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5940 | const std::string& reason) { |
| 5941 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5942 | updateLastAnrStateLocked(windowLabel, reason); |
| 5943 | } |
| 5944 | |
| 5945 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5946 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5947 | // 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] | 5948 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5949 | struct tm tm; |
| 5950 | localtime_r(&t, &tm); |
| 5951 | char timestr[64]; |
| 5952 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5953 | mLastAnrState.clear(); |
| 5954 | mLastAnrState += INDENT "ANR:\n"; |
| 5955 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5956 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5957 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5958 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5959 | } |
| 5960 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5961 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5962 | KeyEntry& entry) { |
| 5963 | const KeyEvent event = createKeyEvent(entry); |
| 5964 | nsecs_t delay = 0; |
| 5965 | { // release lock |
| 5966 | scoped_unlock unlock(mLock); |
| 5967 | android::base::Timer t; |
| 5968 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5969 | entry.policyFlags); |
| 5970 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5971 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5972 | std::to_string(t.duration().count()).c_str()); |
| 5973 | } |
| 5974 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5975 | |
| 5976 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5977 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5978 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5979 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5980 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5981 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5982 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5983 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5984 | } |
| 5985 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5986 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5987 | std::optional<int32_t> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5988 | std::string reason) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5989 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5990 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5991 | mPolicy->notifyWindowUnresponsive(token, pid, reason); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5992 | }; |
| 5993 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5994 | } |
| 5995 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5996 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
| 5997 | std::optional<int32_t> pid) { |
| 5998 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5999 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6000 | mPolicy->notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6001 | }; |
| 6002 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6003 | } |
| 6004 | |
| 6005 | /** |
| 6006 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6007 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6008 | * command entry to the command queue. |
| 6009 | */ |
| 6010 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6011 | std::string reason) { |
| 6012 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6013 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6014 | if (connection.monitor) { |
| 6015 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6016 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6017 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6018 | } else { |
| 6019 | // The connection is a window |
| 6020 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6021 | reason.c_str()); |
| 6022 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6023 | if (handle != nullptr) { |
| 6024 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6025 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6026 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6027 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6028 | } |
| 6029 | |
| 6030 | /** |
| 6031 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6032 | */ |
| 6033 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 6034 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6035 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6036 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6037 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6038 | } else { |
| 6039 | // The connection is a window |
| 6040 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6041 | if (handle != nullptr) { |
| 6042 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6043 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6044 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6045 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6046 | } |
| 6047 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6048 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6049 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6050 | KeyEntry& keyEntry, bool handled) { |
| 6051 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6052 | if (!handled) { |
| 6053 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6054 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6055 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6056 | return false; |
| 6057 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6058 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6059 | // Get the fallback key state. |
| 6060 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6061 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6062 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6063 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6064 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6065 | } |
| 6066 | |
| 6067 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6068 | // If the application handles the original key for which we previously |
| 6069 | // generated a fallback or if the window is not a foreground window, |
| 6070 | // then cancel the associated fallback key, if any. |
| 6071 | if (fallbackKeyCode != -1) { |
| 6072 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6073 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6074 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6075 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6076 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6077 | keyEntry.policyFlags); |
| 6078 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6079 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6080 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6081 | |
| 6082 | mLock.unlock(); |
| 6083 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6084 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6085 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6086 | |
| 6087 | mLock.lock(); |
| 6088 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6089 | // Cancel the fallback key. |
| 6090 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6091 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6092 | "application handled the original non-fallback key " |
| 6093 | "or is no longer a foreground target, " |
| 6094 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6095 | options.keyCode = fallbackKeyCode; |
| 6096 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6097 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6098 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6099 | } |
| 6100 | } else { |
| 6101 | // If the application did not handle a non-fallback key, first check |
| 6102 | // that we are in a good state to perform unhandled key event processing |
| 6103 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6104 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6105 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6106 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6107 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6108 | "since this is not an initial down. " |
| 6109 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6110 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6111 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6112 | return false; |
| 6113 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6114 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6115 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6116 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6117 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6118 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6119 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6120 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6121 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6122 | |
| 6123 | mLock.unlock(); |
| 6124 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6125 | bool fallback = |
| 6126 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6127 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6128 | |
| 6129 | mLock.lock(); |
| 6130 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6131 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6132 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6133 | return false; |
| 6134 | } |
| 6135 | |
| 6136 | // Latch the fallback keycode for this key on an initial down. |
| 6137 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6138 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6139 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6140 | fallbackKeyCode = event.getKeyCode(); |
| 6141 | } else { |
| 6142 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6143 | } |
| 6144 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6145 | } |
| 6146 | |
| 6147 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6148 | |
| 6149 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6150 | // We will continue to dispatch the key to the policy but we will no |
| 6151 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6152 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6153 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6154 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6155 | if (fallback) { |
| 6156 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6157 | "as a fallback for %d, but on the DOWN it had requested " |
| 6158 | "to send %d instead. Fallback canceled.", |
| 6159 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6160 | } else { |
| 6161 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6162 | "but on the DOWN it had requested to send %d. " |
| 6163 | "Fallback canceled.", |
| 6164 | originalKeyCode, fallbackKeyCode); |
| 6165 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6166 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6167 | |
| 6168 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6169 | "canceling fallback, policy no longer desires it"); |
| 6170 | options.keyCode = fallbackKeyCode; |
| 6171 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6172 | |
| 6173 | fallback = false; |
| 6174 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6175 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6176 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6177 | } |
| 6178 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6179 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6180 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6181 | { |
| 6182 | std::string msg; |
| 6183 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6184 | connection->inputState.getFallbackKeys(); |
| 6185 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6186 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6187 | } |
| 6188 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6189 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6190 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6191 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6192 | |
| 6193 | if (fallback) { |
| 6194 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6195 | keyEntry.eventTime = event.getEventTime(); |
| 6196 | keyEntry.deviceId = event.getDeviceId(); |
| 6197 | keyEntry.source = event.getSource(); |
| 6198 | keyEntry.displayId = event.getDisplayId(); |
| 6199 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6200 | keyEntry.keyCode = fallbackKeyCode; |
| 6201 | keyEntry.scanCode = event.getScanCode(); |
| 6202 | keyEntry.metaState = event.getMetaState(); |
| 6203 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6204 | keyEntry.downTime = event.getDownTime(); |
| 6205 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6206 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6207 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6208 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6209 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6210 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6211 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6212 | return true; // restart the event |
| 6213 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6214 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6215 | ALOGD("Unhandled key event: No fallback key."); |
| 6216 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6217 | |
| 6218 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6219 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6220 | } |
| 6221 | } |
| 6222 | return false; |
| 6223 | } |
| 6224 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6225 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6226 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6227 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6228 | return false; |
| 6229 | } |
| 6230 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6231 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6232 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6233 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6234 | } |
| 6235 | } |
| 6236 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6237 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6238 | if (ATRACE_ENABLED()) { |
| 6239 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6240 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6241 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6242 | } |
| 6243 | } |
| 6244 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6245 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6246 | if (ATRACE_ENABLED()) { |
| 6247 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6248 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6249 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6250 | } |
| 6251 | } |
| 6252 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6253 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6254 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6255 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6256 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6257 | dumpDispatchStateLocked(dump); |
| 6258 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6259 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6260 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6261 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6262 | } |
| 6263 | } |
| 6264 | |
| 6265 | void InputDispatcher::monitor() { |
| 6266 | // 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] | 6267 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6268 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6269 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6270 | } |
| 6271 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6272 | /** |
| 6273 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6274 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6275 | * this method can be safely called from any thread, as long as you've ensured that |
| 6276 | * the work you are interested in completing has already been queued. |
| 6277 | */ |
| 6278 | bool InputDispatcher::waitForIdle() { |
| 6279 | /** |
| 6280 | * Timeout should represent the longest possible time that a device might spend processing |
| 6281 | * events and commands. |
| 6282 | */ |
| 6283 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6284 | std::unique_lock lock(mLock); |
| 6285 | mLooper->wake(); |
| 6286 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6287 | return result == std::cv_status::no_timeout; |
| 6288 | } |
| 6289 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6290 | /** |
| 6291 | * Sets focus to the window identified by the token. This must be called |
| 6292 | * after updating any input window handles. |
| 6293 | * |
| 6294 | * Params: |
| 6295 | * request.token - input channel token used to identify the window that should gain focus. |
| 6296 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6297 | * specified token does not match the currently focused window, this request will be dropped. |
| 6298 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6299 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6300 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6301 | * when requesting the focus change. This determines which request gets |
| 6302 | * precedence if there is a focus change request from another source such as pointer down. |
| 6303 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6304 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6305 | { // acquire lock |
| 6306 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6307 | std::optional<FocusResolver::FocusChanges> changes = |
| 6308 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6309 | if (changes) { |
| 6310 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6311 | } |
| 6312 | } // release lock |
| 6313 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6314 | mLooper->wake(); |
| 6315 | } |
| 6316 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6317 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6318 | if (changes.oldFocus) { |
| 6319 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6320 | if (focusedInputChannel) { |
| 6321 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6322 | "focus left window"); |
| 6323 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6324 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6325 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6326 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6327 | if (changes.newFocus) { |
| 6328 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6329 | } |
| 6330 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6331 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6332 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6333 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6334 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6335 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6336 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6337 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6338 | // front. |
| 6339 | disablePointerCaptureForcedLocked(); |
| 6340 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6341 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6342 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6343 | } |
| 6344 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6345 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6346 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6347 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6348 | return; |
| 6349 | } |
| 6350 | |
| 6351 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6352 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6353 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6354 | setPointerCaptureLocked(false); |
| 6355 | } |
| 6356 | |
| 6357 | if (!mWindowTokenWithPointerCapture) { |
| 6358 | // No need to send capture changes because no window has capture. |
| 6359 | return; |
| 6360 | } |
| 6361 | |
| 6362 | if (mPendingEvent != nullptr) { |
| 6363 | // Move the pending event to the front of the queue. This will give the chance |
| 6364 | // for the pending event to be dropped if it is a captured event. |
| 6365 | mInboundQueue.push_front(mPendingEvent); |
| 6366 | mPendingEvent = nullptr; |
| 6367 | } |
| 6368 | |
| 6369 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6370 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6371 | mInboundQueue.push_front(std::move(entry)); |
| 6372 | } |
| 6373 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6374 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6375 | mCurrentPointerCaptureRequest.enable = enable; |
| 6376 | mCurrentPointerCaptureRequest.seq++; |
| 6377 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6378 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6379 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6380 | }; |
| 6381 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6382 | } |
| 6383 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6384 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6385 | { // acquire lock |
| 6386 | std::scoped_lock _l(mLock); |
| 6387 | // Set an empty list to remove all handles from the specific display. |
| 6388 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6389 | setFocusedApplicationLocked(displayId, nullptr); |
| 6390 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6391 | // have been removed for the removed display. |
| 6392 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6393 | // Reset pointer capture eligibility, regardless of previous state. |
| 6394 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6395 | } // release lock |
| 6396 | |
| 6397 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6398 | mLooper->wake(); |
| 6399 | } |
| 6400 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6401 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6402 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6403 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6404 | // more convenient parsing. |
| 6405 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6406 | for (const auto& info : windowInfos) { |
| 6407 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6408 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6409 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6410 | |
| 6411 | { // acquire lock |
| 6412 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6413 | |
| 6414 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6415 | // no windows, we can tell that the windows were removed from the display. |
| 6416 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6417 | handlesPerDisplay[displayId]; |
| 6418 | } |
| 6419 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6420 | mDisplayInfos.clear(); |
| 6421 | for (const auto& displayInfo : displayInfos) { |
| 6422 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6423 | } |
| 6424 | |
| 6425 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6426 | setInputWindowsLocked(handles, displayId); |
| 6427 | } |
| 6428 | } |
| 6429 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6430 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6431 | } |
| 6432 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6433 | bool InputDispatcher::shouldDropInput( |
| 6434 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6435 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6436 | (windowHandle->getInfo()->inputConfig.test( |
| 6437 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6438 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6439 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6440 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6441 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6442 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6443 | windowHandle->getInfo()->displayId); |
| 6444 | return true; |
| 6445 | } |
| 6446 | return false; |
| 6447 | } |
| 6448 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6449 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6450 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6451 | const std::vector<DisplayInfo>& displayInfos) { |
| 6452 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6453 | } |
| 6454 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6455 | void InputDispatcher::cancelCurrentTouch() { |
| 6456 | { |
| 6457 | std::scoped_lock _l(mLock); |
| 6458 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
| 6459 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 6460 | "cancel current touch"); |
| 6461 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6462 | |
| 6463 | mTouchStatesByDisplay.clear(); |
| 6464 | mLastHoverWindowHandle.clear(); |
| 6465 | } |
| 6466 | // Wake up poll loop since there might be work to do. |
| 6467 | mLooper->wake(); |
| 6468 | } |
| 6469 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6470 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6471 | std::scoped_lock _l(mLock); |
| 6472 | mMonitorDispatchingTimeout = timeout; |
| 6473 | } |
| 6474 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6475 | } // namespace android::inputdispatcher |