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(), |
Siarhei Vishniakou | bd25272 | 2022-01-06 03:49:35 -0800 | [diff] [blame] | 556 | mLatencyTracker(&mLatencyAggregator) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 557 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 558 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 559 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 560 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 561 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 562 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 563 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 564 | |
| 565 | policy->getDispatcherConfiguration(&mConfig); |
| 566 | } |
| 567 | |
| 568 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 569 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 570 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 571 | resetKeyRepeatLocked(); |
| 572 | releasePendingEventLocked(); |
| 573 | drainInboundQueueLocked(); |
| 574 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 575 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 576 | while (!mConnectionsByToken.empty()) { |
| 577 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 578 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 579 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 583 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 584 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 585 | return ALREADY_EXISTS; |
| 586 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 587 | mThread = std::make_unique<InputThread>( |
| 588 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 589 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 593 | if (mThread && mThread->isCallingThread()) { |
| 594 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 595 | return INVALID_OPERATION; |
| 596 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 597 | mThread.reset(); |
| 598 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 601 | void InputDispatcher::dispatchOnce() { |
| 602 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 603 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 604 | std::scoped_lock _l(mLock); |
| 605 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 606 | |
| 607 | // Run a dispatch loop if there are no pending commands. |
| 608 | // The dispatch loop might enqueue commands to run afterwards. |
| 609 | if (!haveCommandsLocked()) { |
| 610 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 611 | } |
| 612 | |
| 613 | // Run all pending commands if there are any. |
| 614 | // 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] | 615 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 616 | nextWakeupTime = LONG_LONG_MIN; |
| 617 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 618 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 619 | // If we are still waiting for ack on some events, |
| 620 | // we might have to wake up earlier to check if an app is anr'ing. |
| 621 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 622 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 623 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 624 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 625 | // pending or queued events |
| 626 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 627 | mDispatcherEnteredIdle.notify_all(); |
| 628 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 629 | } // release lock |
| 630 | |
| 631 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 632 | nsecs_t currentTime = now(); |
| 633 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 634 | mLooper->pollOnce(timeoutMillis); |
| 635 | } |
| 636 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 637 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 638 | * Raise ANR if there is no focused window. |
| 639 | * Before the ANR is raised, do a final state check: |
| 640 | * 1. The currently focused application must be the same one we are waiting for. |
| 641 | * 2. Ensure we still don't have a focused window. |
| 642 | */ |
| 643 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 644 | // Check if the application that we are waiting for is still focused. |
| 645 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 646 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 647 | if (focusedApplication == nullptr || |
| 648 | focusedApplication->getApplicationToken() != |
| 649 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 650 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 651 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 652 | focusedApplication->getName().c_str()); |
| 653 | return; // The focused application has changed. |
| 654 | } |
| 655 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 656 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 657 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 658 | if (focusedWindowHandle != nullptr) { |
| 659 | return; // We now have a focused window. No need for ANR. |
| 660 | } |
| 661 | onAnrLocked(mAwaitedFocusedApplication); |
| 662 | } |
| 663 | |
| 664 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 665 | * Check if any of the connections' wait queues have events that are too old. |
| 666 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 667 | * Return the time at which we should wake up next. |
| 668 | */ |
| 669 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 670 | const nsecs_t currentTime = now(); |
| 671 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 672 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 673 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 674 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 675 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 676 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 677 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 678 | return LONG_LONG_MIN; |
| 679 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 680 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 681 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | // Check if any connection ANRs are due |
| 686 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 687 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 688 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 689 | } |
| 690 | |
| 691 | // If we reached here, we have an unresponsive connection. |
| 692 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 693 | if (connection == nullptr) { |
| 694 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 695 | return nextAnrCheck; |
| 696 | } |
| 697 | connection->responsive = false; |
| 698 | // Stop waking up for this unresponsive connection |
| 699 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 700 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 701 | return LONG_LONG_MIN; |
| 702 | } |
| 703 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 704 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
| 705 | const sp<Connection>& connection) { |
| 706 | if (connection->monitor) { |
| 707 | return mMonitorDispatchingTimeout; |
| 708 | } |
| 709 | const sp<WindowInfoHandle> window = |
| 710 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 711 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 712 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 713 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 714 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 717 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 718 | nsecs_t currentTime = now(); |
| 719 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 720 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 721 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 722 | // repeat if the device is just coming out of sleep. |
| 723 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 724 | resetKeyRepeatLocked(); |
| 725 | } |
| 726 | |
| 727 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 728 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 729 | if (DEBUG_FOCUS) { |
| 730 | ALOGD("Dispatch frozen. Waiting some more."); |
| 731 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 732 | return; |
| 733 | } |
| 734 | |
| 735 | // Optimize latency of app switches. |
| 736 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 737 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 738 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 739 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 740 | *nextWakeupTime = mAppSwitchDueTime; |
| 741 | } |
| 742 | |
| 743 | // Ready to start a new event. |
| 744 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 745 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 746 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 747 | if (isAppSwitchDue) { |
| 748 | // The inbound queue is empty so the app switch key we were waiting |
| 749 | // for will never arrive. Stop waiting for it. |
| 750 | resetPendingAppSwitchLocked(false); |
| 751 | isAppSwitchDue = false; |
| 752 | } |
| 753 | |
| 754 | // Synthesize a key repeat if appropriate. |
| 755 | if (mKeyRepeatState.lastKeyEntry) { |
| 756 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 757 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 758 | } else { |
| 759 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 760 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | // Nothing to do if there is no pending event. |
| 766 | if (!mPendingEvent) { |
| 767 | return; |
| 768 | } |
| 769 | } else { |
| 770 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 771 | mPendingEvent = mInboundQueue.front(); |
| 772 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 773 | traceInboundQueueLengthLocked(); |
| 774 | } |
| 775 | |
| 776 | // Poke user activity for this event. |
| 777 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 778 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 779 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | // Now we have an event to dispatch. |
| 783 | // 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] | 784 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 785 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 786 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 787 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 788 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 789 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 790 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 794 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 798 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 799 | const ConfigurationChangedEntry& typedEntry = |
| 800 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 801 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 802 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 803 | break; |
| 804 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 805 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 806 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 807 | const DeviceResetEntry& typedEntry = |
| 808 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 809 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 810 | dropReason = DropReason::NOT_DROPPED; // device resets 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 | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 814 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 815 | std::shared_ptr<FocusEntry> typedEntry = |
| 816 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 817 | dispatchFocusLocked(currentTime, typedEntry); |
| 818 | done = true; |
| 819 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 820 | break; |
| 821 | } |
| 822 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 823 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 824 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 825 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 826 | done = true; |
| 827 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 828 | break; |
| 829 | } |
| 830 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 831 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 832 | const auto typedEntry = |
| 833 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 834 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 835 | done = true; |
| 836 | break; |
| 837 | } |
| 838 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 839 | case EventEntry::Type::DRAG: { |
| 840 | std::shared_ptr<DragEntry> typedEntry = |
| 841 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 842 | dispatchDragLocked(currentTime, typedEntry); |
| 843 | done = true; |
| 844 | break; |
| 845 | } |
| 846 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 847 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 848 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 849 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 850 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 851 | resetPendingAppSwitchLocked(true); |
| 852 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 853 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 854 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 857 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 858 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 859 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 860 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 861 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 862 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 863 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 864 | break; |
| 865 | } |
| 866 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 867 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 868 | std::shared_ptr<MotionEntry> motionEntry = |
| 869 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 870 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 871 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 872 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 873 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 874 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 875 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 876 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 877 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 878 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 879 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 880 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 881 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 882 | |
| 883 | case EventEntry::Type::SENSOR: { |
| 884 | std::shared_ptr<SensorEntry> sensorEntry = |
| 885 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 886 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 887 | dropReason = DropReason::APP_SWITCH; |
| 888 | } |
| 889 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 890 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 891 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 892 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 893 | dropReason = DropReason::STALE; |
| 894 | } |
| 895 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 896 | done = true; |
| 897 | break; |
| 898 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 902 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 903 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 904 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 905 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 906 | |
| 907 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 908 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 912 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 913 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 914 | } |
| 915 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 916 | /** |
| 917 | * Return true if the events preceding this incoming motion event should be dropped |
| 918 | * Return false otherwise (the default behaviour) |
| 919 | */ |
| 920 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 921 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 922 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 923 | |
| 924 | // Optimize case where the current application is unresponsive and the user |
| 925 | // decides to touch a window in a different application. |
| 926 | // If the application takes too long to catch up then we drop all events preceding |
| 927 | // the touch into the other window. |
| 928 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 929 | int32_t displayId = motionEntry.displayId; |
| 930 | int32_t x = static_cast<int32_t>( |
| 931 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 932 | int32_t y = static_cast<int32_t>( |
| 933 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 934 | |
| 935 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 936 | sp<WindowInfoHandle> touchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 937 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 938 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 939 | touchedWindowHandle->getApplicationToken() != |
| 940 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 941 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 942 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 943 | "for %s", |
| 944 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 945 | return true; |
| 946 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 947 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 948 | // Alternatively, maybe there's a spy window that could handle this event. |
| 949 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 950 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 951 | for (const auto& windowHandle : touchedSpies) { |
| 952 | const sp<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 953 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 954 | // This spy window could take more input. Drop all events preceding this |
| 955 | // 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] | 956 | 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] | 957 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 958 | mAwaitedFocusedApplication->getName().c_str()); |
| 959 | return true; |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 965 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 966 | // events to be processed before dispatching the key event. This is because these motion events |
| 967 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 968 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 969 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 970 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 971 | "just send the pending key event to the focused window."); |
| 972 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 973 | } |
| 974 | return false; |
| 975 | } |
| 976 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 977 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 978 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 979 | mInboundQueue.push_back(std::move(newEntry)); |
| 980 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 981 | traceInboundQueueLengthLocked(); |
| 982 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 983 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 984 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 985 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 986 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 987 | // Optimize app switch latency. |
| 988 | // If the application takes too long to catch up then we drop all events preceding |
| 989 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 990 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 991 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 992 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 993 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 994 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 995 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 996 | if (DEBUG_APP_SWITCH) { |
| 997 | ALOGD("App switch is pending!"); |
| 998 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 999 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1000 | mAppSwitchSawKeyDown = false; |
| 1001 | needWake = true; |
| 1002 | } |
| 1003 | } |
| 1004 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1005 | |
| 1006 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1007 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1008 | // time for it may have been handled in the policy and could be dropped. |
| 1009 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1010 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1011 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1012 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1013 | pendingKey.interceptKeyResult == |
| 1014 | KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1015 | pendingKey.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1016 | pendingKey.interceptKeyWakeupTime = 0; |
| 1017 | needWake = true; |
| 1018 | } |
| 1019 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1020 | break; |
| 1021 | } |
| 1022 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1023 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1024 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1025 | "Unexpected untrusted event."); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1026 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1027 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1028 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1029 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1030 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1031 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1032 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1033 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1034 | break; |
| 1035 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1036 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1037 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1038 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1039 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1040 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1041 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1042 | // nothing to do |
| 1043 | break; |
| 1044 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | return needWake; |
| 1048 | } |
| 1049 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1050 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1051 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1052 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1053 | mRecentQueue.push_back(entry); |
| 1054 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1055 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1056 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1060 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1061 | int32_t y, TouchState* touchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1062 | bool isStylus, |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1063 | bool addOutsideTargets, |
| 1064 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1065 | if (addOutsideTargets && touchState == nullptr) { |
| 1066 | 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] | 1067 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1068 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1069 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1070 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1071 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1072 | continue; |
| 1073 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1074 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1075 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1076 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1077 | return windowHandle; |
| 1078 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1079 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1080 | if (addOutsideTargets && |
| 1081 | info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1082 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1083 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1084 | } |
| 1085 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1086 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1087 | } |
| 1088 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1089 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1090 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1091 | // Traverse windows from front to back and gather the touched spy windows. |
| 1092 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1093 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1094 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1095 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1096 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1097 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1098 | continue; |
| 1099 | } |
| 1100 | if (!info.isSpy()) { |
| 1101 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1102 | return spyWindows; |
| 1103 | } |
| 1104 | spyWindows.push_back(windowHandle); |
| 1105 | } |
| 1106 | return spyWindows; |
| 1107 | } |
| 1108 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1109 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1110 | const char* reason; |
| 1111 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1112 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1113 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1114 | ALOGD("Dropped event because policy consumed it."); |
| 1115 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1116 | reason = "inbound event was dropped because the policy consumed it"; |
| 1117 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1118 | case DropReason::DISABLED: |
| 1119 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1120 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1121 | } |
| 1122 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1123 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1124 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1125 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1126 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1127 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1128 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1129 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1130 | "has started interacting with a different application."); |
| 1131 | reason = "inbound event was dropped because the current application is not responding " |
| 1132 | "and the user has started interacting with a different application"; |
| 1133 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1134 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1135 | ALOGI("Dropped event because it is stale."); |
| 1136 | reason = "inbound event was dropped because it is stale"; |
| 1137 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1138 | case DropReason::NO_POINTER_CAPTURE: |
| 1139 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1140 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1141 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1142 | case DropReason::NOT_DROPPED: { |
| 1143 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1144 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1145 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1148 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1149 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1150 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1151 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1152 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1153 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1154 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1155 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1156 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1157 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1158 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1159 | } else { |
| 1160 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1161 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1162 | } |
| 1163 | break; |
| 1164 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1165 | case EventEntry::Type::SENSOR: { |
| 1166 | break; |
| 1167 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1168 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1169 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1170 | break; |
| 1171 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1172 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1173 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1174 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1175 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1176 | 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] | 1177 | break; |
| 1178 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1182 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1183 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1184 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1187 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1188 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1189 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1190 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1194 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1195 | } |
| 1196 | |
| 1197 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1198 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1199 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1200 | if (DEBUG_APP_SWITCH) { |
| 1201 | if (handled) { |
| 1202 | ALOGD("App switch has arrived."); |
| 1203 | } else { |
| 1204 | ALOGD("App switch was abandoned."); |
| 1205 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1206 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1207 | } |
| 1208 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1209 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1210 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1213 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1214 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1215 | return false; |
| 1216 | } |
| 1217 | |
| 1218 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1219 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1220 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1221 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1222 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1223 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1224 | return true; |
| 1225 | } |
| 1226 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1227 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1228 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1232 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1233 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1234 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1235 | releaseInboundEventLocked(entry); |
| 1236 | } |
| 1237 | traceInboundQueueLengthLocked(); |
| 1238 | } |
| 1239 | |
| 1240 | void InputDispatcher::releasePendingEventLocked() { |
| 1241 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1242 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1243 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1247 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1248 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1249 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1250 | if (DEBUG_DISPATCH_CYCLE) { |
| 1251 | ALOGD("Injected inbound event was dropped."); |
| 1252 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1253 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | } |
| 1255 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1256 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1257 | } |
| 1258 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1262 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1263 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1267 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1268 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1269 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1270 | uint32_t policyFlags = entry->policyFlags & |
| 1271 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1272 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1273 | std::shared_ptr<KeyEntry> newEntry = |
| 1274 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1275 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1276 | entry->flags, entry->keyCode, entry->scanCode, |
| 1277 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1278 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1279 | newEntry->syntheticRepeat = true; |
| 1280 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1281 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1282 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1285 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1286 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1287 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1288 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1289 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1290 | |
| 1291 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1292 | resetKeyRepeatLocked(); |
| 1293 | |
| 1294 | // 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] | 1295 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1296 | scoped_unlock unlock(mLock); |
| 1297 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1298 | }; |
| 1299 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1300 | return true; |
| 1301 | } |
| 1302 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1303 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1304 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1305 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1306 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1307 | entry.deviceId); |
| 1308 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1309 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1310 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1311 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1312 | resetKeyRepeatLocked(); |
| 1313 | } |
| 1314 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1315 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1316 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1317 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1318 | return true; |
| 1319 | } |
| 1320 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1321 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1322 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1323 | if (mPendingEvent != nullptr) { |
| 1324 | // Move the pending event to the front of the queue. This will give the chance |
| 1325 | // for the pending event to get dispatched to the newly focused window |
| 1326 | mInboundQueue.push_front(mPendingEvent); |
| 1327 | mPendingEvent = nullptr; |
| 1328 | } |
| 1329 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1330 | std::unique_ptr<FocusEntry> focusEntry = |
| 1331 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1332 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1333 | |
| 1334 | // This event should go to the front of the queue, but behind all other focus events |
| 1335 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1336 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1337 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1338 | [](const std::shared_ptr<EventEntry>& event) { |
| 1339 | return event->type == EventEntry::Type::FOCUS; |
| 1340 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1341 | |
| 1342 | // 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] | 1343 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1344 | } |
| 1345 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1346 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1347 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1348 | if (channel == nullptr) { |
| 1349 | return; // Window has gone away |
| 1350 | } |
| 1351 | InputTarget target; |
| 1352 | target.inputChannel = channel; |
| 1353 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1354 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1355 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1356 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1357 | std::string reason = std::string("reason=").append(entry->reason); |
| 1358 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1359 | dispatchEventLocked(currentTime, entry, {target}); |
| 1360 | } |
| 1361 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1362 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1363 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1364 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1365 | dropReason = DropReason::NOT_DROPPED; |
| 1366 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1367 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1368 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1369 | |
| 1370 | if (entry->pointerCaptureRequest.enable) { |
| 1371 | // Enable Pointer Capture. |
| 1372 | if (haveWindowWithPointerCapture && |
| 1373 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1374 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1375 | // app of the state change, so there is no need to notify the app. |
| 1376 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1377 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1378 | } |
| 1379 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1380 | // This can happen if a window requests capture and immediately releases capture. |
| 1381 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1382 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1383 | return; |
| 1384 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1385 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1386 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1387 | return; |
| 1388 | } |
| 1389 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1390 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1391 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1392 | mWindowTokenWithPointerCapture = token; |
| 1393 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1394 | // Disable Pointer Capture. |
| 1395 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1396 | // for two reasons: |
| 1397 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1398 | // to disable capture with the same sequence number: one generated by |
| 1399 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1400 | // Capture being disabled in InputReader. |
| 1401 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1402 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1403 | // in InputReader. |
| 1404 | if (!haveWindowWithPointerCapture) { |
| 1405 | // Pointer capture was already forcefully disabled because of focus change. |
| 1406 | dropReason = DropReason::NOT_DROPPED; |
| 1407 | return; |
| 1408 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1409 | token = mWindowTokenWithPointerCapture; |
| 1410 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1411 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1412 | setPointerCaptureLocked(false); |
| 1413 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | auto channel = getInputChannelLocked(token); |
| 1417 | if (channel == nullptr) { |
| 1418 | // Window has gone away, clean up Pointer Capture state. |
| 1419 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1420 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1421 | setPointerCaptureLocked(false); |
| 1422 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1423 | return; |
| 1424 | } |
| 1425 | InputTarget target; |
| 1426 | target.inputChannel = channel; |
| 1427 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1428 | entry->dispatchInProgress = true; |
| 1429 | dispatchEventLocked(currentTime, entry, {target}); |
| 1430 | |
| 1431 | dropReason = DropReason::NOT_DROPPED; |
| 1432 | } |
| 1433 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1434 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1435 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1436 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1437 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1438 | if (windowHandles.empty()) { |
| 1439 | return; |
| 1440 | } |
| 1441 | const std::vector<InputTarget> inputTargets = |
| 1442 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1443 | if (inputTargets.empty()) { |
| 1444 | return; |
| 1445 | } |
| 1446 | entry->dispatchInProgress = true; |
| 1447 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1448 | } |
| 1449 | |
| 1450 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1451 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1452 | std::vector<InputTarget> inputTargets; |
| 1453 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1454 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1455 | const sp<IBinder>& token = handle->getToken(); |
| 1456 | if (token == nullptr) { |
| 1457 | continue; |
| 1458 | } |
| 1459 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1460 | if (channel == nullptr) { |
| 1461 | continue; // Window has gone away |
| 1462 | } |
| 1463 | InputTarget target; |
| 1464 | target.inputChannel = channel; |
| 1465 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1466 | inputTargets.push_back(target); |
| 1467 | } |
| 1468 | return inputTargets; |
| 1469 | } |
| 1470 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1471 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1472 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1473 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1474 | if (!entry->dispatchInProgress) { |
| 1475 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1476 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1477 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1478 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1479 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1480 | // We have seen two identical key downs in a row which indicates that the device |
| 1481 | // driver is automatically generating key repeats itself. We take note of the |
| 1482 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1483 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1484 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1485 | // Make sure we don't get key down from a different device. If a different |
| 1486 | // device Id has same key pressed down, the new device Id will replace the |
| 1487 | // current one to hold the key repeat with repeat count reset. |
| 1488 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1489 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1490 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1491 | resetKeyRepeatLocked(); |
| 1492 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1493 | } else { |
| 1494 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1495 | resetKeyRepeatLocked(); |
| 1496 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1497 | } |
| 1498 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1499 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1500 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1501 | // 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] | 1502 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1503 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1504 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1505 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1506 | resetKeyRepeatLocked(); |
| 1507 | } |
| 1508 | |
| 1509 | if (entry->repeatCount == 1) { |
| 1510 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1511 | } else { |
| 1512 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1513 | } |
| 1514 | |
| 1515 | entry->dispatchInProgress = true; |
| 1516 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1517 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | // Handle case where the policy asked us to try again later last time. |
| 1521 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1522 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1523 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1524 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1525 | } |
| 1526 | return false; // wait until next wakeup |
| 1527 | } |
| 1528 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1529 | entry->interceptKeyWakeupTime = 0; |
| 1530 | } |
| 1531 | |
| 1532 | // Give the policy a chance to intercept the key. |
| 1533 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1534 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1535 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1536 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1537 | |
| 1538 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1539 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1540 | }; |
| 1541 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1542 | return false; // wait for the command to run |
| 1543 | } else { |
| 1544 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1545 | } |
| 1546 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1547 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1548 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1553 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1554 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1555 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1556 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1557 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1558 | return true; |
| 1559 | } |
| 1560 | |
| 1561 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1562 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1563 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1564 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1565 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1566 | return false; |
| 1567 | } |
| 1568 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1569 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1570 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1571 | return true; |
| 1572 | } |
| 1573 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1574 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1575 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1576 | |
| 1577 | // Dispatch the key. |
| 1578 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1579 | return true; |
| 1580 | } |
| 1581 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1582 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1583 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1584 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1585 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1586 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1587 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1588 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1589 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1590 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1593 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1594 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1595 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1596 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1597 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1598 | "source=0x%x, sensorType=%s", |
| 1599 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1600 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1601 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1602 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1603 | scoped_unlock unlock(mLock); |
| 1604 | |
| 1605 | if (entry->accuracyChanged) { |
| 1606 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1607 | } |
| 1608 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1609 | entry->hwTimestamp, entry->values); |
| 1610 | }; |
| 1611 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1615 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1616 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1617 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1618 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1619 | { // acquire lock |
| 1620 | std::scoped_lock _l(mLock); |
| 1621 | |
| 1622 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1623 | std::shared_ptr<EventEntry> entry = *it; |
| 1624 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1625 | it = mInboundQueue.erase(it); |
| 1626 | releaseInboundEventLocked(entry); |
| 1627 | } |
| 1628 | } |
| 1629 | } |
| 1630 | return true; |
| 1631 | } |
| 1632 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1633 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1634 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1635 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1636 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1637 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1638 | entry->dispatchInProgress = true; |
| 1639 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1640 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1644 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1645 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1646 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1647 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1648 | return true; |
| 1649 | } |
| 1650 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1651 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1652 | |
| 1653 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1654 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1655 | |
| 1656 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1657 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1658 | if (isPointerEvent) { |
| 1659 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1660 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1661 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1662 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1663 | } else { |
| 1664 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1665 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1666 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1667 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1668 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1669 | return false; |
| 1670 | } |
| 1671 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1672 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1673 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1674 | return true; |
| 1675 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1676 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1677 | CancelationOptions::Mode mode(isPointerEvent |
| 1678 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1679 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1680 | CancelationOptions options(mode, "input event injection failed"); |
| 1681 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1682 | return true; |
| 1683 | } |
| 1684 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1685 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1686 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1687 | |
| 1688 | // Dispatch the motion. |
| 1689 | if (conflictingPointerActions) { |
| 1690 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1691 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1692 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1693 | } |
| 1694 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1695 | return true; |
| 1696 | } |
| 1697 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1698 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1699 | bool isExiting, const int32_t rawX, |
| 1700 | const int32_t rawY) { |
| 1701 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1702 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1703 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 1704 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1705 | |
| 1706 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1707 | } |
| 1708 | |
| 1709 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1710 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1711 | if (channel == nullptr) { |
| 1712 | return; // Window has gone away |
| 1713 | } |
| 1714 | InputTarget target; |
| 1715 | target.inputChannel = channel; |
| 1716 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1717 | entry->dispatchInProgress = true; |
| 1718 | dispatchEventLocked(currentTime, entry, {target}); |
| 1719 | } |
| 1720 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1721 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1722 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1723 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1724 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1725 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1726 | "metaState=0x%x, buttonState=0x%x," |
| 1727 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1728 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1729 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1730 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1731 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1732 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1733 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1734 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1735 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1736 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1737 | "orientation=%f", |
| 1738 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1739 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1740 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1741 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1742 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1743 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1744 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1745 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1746 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1747 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1748 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1749 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1750 | } |
| 1751 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1752 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1753 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1754 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1755 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1756 | if (DEBUG_DISPATCH_CYCLE) { |
| 1757 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1758 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1759 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1760 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1761 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1762 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1763 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1764 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1765 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1766 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1767 | sp<Connection> connection = |
| 1768 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1769 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1770 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1771 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1772 | if (DEBUG_FOCUS) { |
| 1773 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1774 | "is no longer registered with the input dispatcher.", |
| 1775 | inputTarget.inputChannel->getName().c_str()); |
| 1776 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1777 | } |
| 1778 | } |
| 1779 | } |
| 1780 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1781 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1782 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1783 | // If the policy decides to close the app, we will get a channel removal event via |
| 1784 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1785 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1786 | // pile up. |
| 1787 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1788 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1789 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1790 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1791 | "application not responding"); |
| 1792 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1796 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1797 | if (DEBUG_FOCUS) { |
| 1798 | ALOGD("Resetting ANR timeouts."); |
| 1799 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1800 | |
| 1801 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1802 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1803 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1804 | } |
| 1805 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1806 | /** |
| 1807 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1808 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1809 | * Focused display is the display that the user most recently interacted with. |
| 1810 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1811 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1812 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1813 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1814 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1815 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1816 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1817 | break; |
| 1818 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1819 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1820 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1821 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1822 | break; |
| 1823 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1824 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1825 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1826 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1827 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1828 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1829 | case EventEntry::Type::SENSOR: |
| 1830 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1831 | 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] | 1832 | return ADISPLAY_ID_NONE; |
| 1833 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1834 | } |
| 1835 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1836 | } |
| 1837 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1838 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1839 | const char* focusedWindowName) { |
| 1840 | if (mAnrTracker.empty()) { |
| 1841 | // already processed all events that we waited for |
| 1842 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1843 | return false; |
| 1844 | } |
| 1845 | |
| 1846 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1847 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1848 | // 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] | 1849 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1850 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1851 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1852 | return true; |
| 1853 | } |
| 1854 | |
| 1855 | // We still have pending events, and already started the timer |
| 1856 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1857 | return true; // Still waiting |
| 1858 | } |
| 1859 | |
| 1860 | // Waited too long, and some connection still hasn't processed all motions |
| 1861 | // Just send the key to the focused window |
| 1862 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1863 | focusedWindowName); |
| 1864 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1865 | return false; |
| 1866 | } |
| 1867 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1868 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1869 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1870 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1871 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1872 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1873 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1874 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1875 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1876 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1877 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1878 | // If there is no currently focused window and no focused application |
| 1879 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1880 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1881 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1882 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1883 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1884 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1885 | } |
| 1886 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1887 | // Drop key events if requested by input feature |
| 1888 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1889 | return InputEventInjectionResult::FAILED; |
| 1890 | } |
| 1891 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1892 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1893 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1894 | // start interacting with another application via touch (app switch). This code can be removed |
| 1895 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1896 | // an app is expected to have a focused window. |
| 1897 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1898 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1899 | // 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] | 1900 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1901 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1902 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1903 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1904 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1905 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1906 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1907 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1908 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1909 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1910 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1911 | // Already raised ANR. Drop the event |
| 1912 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1913 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1914 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1915 | } else { |
| 1916 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1917 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | // we have a valid, non-null focused window |
| 1922 | resetNoFocusedWindowTimeoutLocked(); |
| 1923 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1924 | // Verify targeted injection. |
| 1925 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 1926 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
| 1927 | return InputEventInjectionResult::TARGET_MISMATCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1928 | } |
| 1929 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1930 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 1931 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1932 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1933 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | // If the event is a key event, then we must wait for all previous events to |
| 1937 | // complete before delivering it because previous events may have the |
| 1938 | // side-effect of transferring focus to a different window and we want to |
| 1939 | // ensure that the following keys are sent to the new window. |
| 1940 | // |
| 1941 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1942 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1943 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1944 | // often anticipate pending UI changes when typing on a keyboard. |
| 1945 | // To obtain this behavior, we must serialize key events with respect to all |
| 1946 | // prior input events. |
| 1947 | if (entry.type == EventEntry::Type::KEY) { |
| 1948 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1949 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1950 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1951 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1955 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1956 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1957 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1958 | |
| 1959 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1960 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1961 | } |
| 1962 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1963 | /** |
| 1964 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1965 | * that are currently unresponsive. |
| 1966 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1967 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1968 | const std::vector<Monitor>& monitors) const { |
| 1969 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1970 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1971 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 1972 | sp<Connection> connection = |
| 1973 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1974 | if (connection == nullptr) { |
| 1975 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1976 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1977 | return false; |
| 1978 | } |
| 1979 | if (!connection->responsive) { |
| 1980 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1981 | connection->inputChannel->getName().c_str()); |
| 1982 | return false; |
| 1983 | } |
| 1984 | return true; |
| 1985 | }); |
| 1986 | return responsiveMonitors; |
| 1987 | } |
| 1988 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1989 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1990 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1991 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1992 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1993 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1994 | // For security reasons, we defer updating the touch state until we are sure that |
| 1995 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1996 | const int32_t displayId = entry.displayId; |
| 1997 | const int32_t action = entry.action; |
| 1998 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1999 | |
| 2000 | // 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] | 2001 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2002 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 2003 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2004 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2005 | // Copy current touch state into tempTouchState. |
| 2006 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2007 | // 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] | 2008 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2009 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2010 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2011 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2012 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2013 | } |
| 2014 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2015 | bool isSplit = tempTouchState.split; |
| 2016 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 2017 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 2018 | tempTouchState.displayId != displayId); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2019 | |
| 2020 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2021 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2022 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2023 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2024 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2025 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2026 | bool wrongDevice = false; |
| 2027 | if (newGesture) { |
| 2028 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2029 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2030 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2031 | "in display %" PRId32, |
| 2032 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2033 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2034 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2035 | switchedDevice = false; |
| 2036 | wrongDevice = true; |
| 2037 | goto Failed; |
| 2038 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2039 | tempTouchState.reset(); |
| 2040 | tempTouchState.down = down; |
| 2041 | tempTouchState.deviceId = entry.deviceId; |
| 2042 | tempTouchState.source = entry.source; |
| 2043 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2044 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2045 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2046 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2047 | "in display %" PRId32, |
| 2048 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2049 | // TODO: test multiple simultaneous input streams. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2050 | injectionResult = InputEventInjectionResult::FAILED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2051 | switchedDevice = false; |
| 2052 | wrongDevice = true; |
| 2053 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2057 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2058 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2059 | int32_t x; |
| 2060 | int32_t y; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2061 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2062 | // Always dispatch mouse events to cursor position. |
| 2063 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2064 | x = int32_t(entry.xCursorPosition); |
| 2065 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2066 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2067 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2068 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2069 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2070 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2071 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2072 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2073 | isStylus, isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2074 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2075 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2076 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2077 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2078 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2079 | // 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] | 2080 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2083 | // Verify targeted injection. |
| 2084 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2085 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
| 2086 | injectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
| 2087 | newTouchedWindowHandle = nullptr; |
| 2088 | goto Failed; |
| 2089 | } |
| 2090 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2091 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2092 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2093 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2094 | // New window supports splitting, but we should never split mouse events. |
| 2095 | isSplit = !isFromMouse; |
| 2096 | } else if (isSplit) { |
| 2097 | // New window does not support splitting but we have already split events. |
| 2098 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2099 | newTouchedWindowHandle = nullptr; |
| 2100 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2101 | } else { |
| 2102 | // 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] | 2103 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2104 | // should never be split. |
| 2105 | tempTouchState.split = isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2106 | } |
| 2107 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2108 | // Update hover state. |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2109 | if (newTouchedWindowHandle != nullptr) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2110 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2111 | newHoverWindowHandle = nullptr; |
| 2112 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2113 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2114 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2115 | } |
| 2116 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2117 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2118 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2119 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2120 | // Process the foreground window first so that it is the first to receive the event. |
| 2121 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2122 | } |
| 2123 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2124 | if (newTouchedWindows.empty()) { |
| 2125 | ALOGI("Dropping event because there is no touchable window at (%d, %d) on display %d.", |
| 2126 | x, y, displayId); |
| 2127 | injectionResult = InputEventInjectionResult::FAILED; |
| 2128 | goto Failed; |
| 2129 | } |
| 2130 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2131 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
| 2132 | const WindowInfo& info = *windowHandle->getInfo(); |
| 2133 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2134 | // Skip spy window targets that are not valid for targeted injection. |
| 2135 | if (const auto err = verifyTargetedInjection(windowHandle, entry); err) { |
| 2136 | continue; |
| 2137 | } |
| 2138 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2139 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2140 | ALOGI("Not sending touch event to %s because it is paused", |
| 2141 | windowHandle->getName().c_str()); |
| 2142 | continue; |
| 2143 | } |
| 2144 | |
| 2145 | // Ensure the window has a connection and the connection is responsive |
| 2146 | const bool isResponsive = hasResponsiveConnectionLocked(*windowHandle); |
| 2147 | if (!isResponsive) { |
| 2148 | ALOGW("Not sending touch gesture to %s because it is not responsive", |
| 2149 | windowHandle->getName().c_str()); |
| 2150 | continue; |
| 2151 | } |
| 2152 | |
| 2153 | // Drop events that can't be trusted due to occlusion |
Hani Kazmi | 3ce9c3a | 2022-04-25 09:40:23 +0000 | [diff] [blame] | 2154 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(windowHandle, x, y); |
| 2155 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 2156 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2157 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2158 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2159 | ALOGD("%s", log.c_str()); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2160 | } |
| 2161 | } |
Hani Kazmi | 3ce9c3a | 2022-04-25 09:40:23 +0000 | [diff] [blame] | 2162 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2163 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2164 | continue; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | // Drop touch events if requested by input feature |
| 2168 | if (shouldDropInput(entry, windowHandle)) { |
| 2169 | continue; |
| 2170 | } |
| 2171 | |
| 2172 | // Set target flags. |
| 2173 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2174 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2175 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2176 | // 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] | 2177 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2178 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2179 | |
| 2180 | if (isSplit) { |
| 2181 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2182 | } |
| 2183 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
| 2184 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2185 | } else if (isWindowObscuredLocked(windowHandle)) { |
| 2186 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2187 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2188 | |
| 2189 | // Update the temporary touch state. |
| 2190 | BitSet32 pointerIds; |
| 2191 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2192 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2193 | pointerIds.markBit(pointerId); |
| 2194 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2195 | |
| 2196 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2197 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame^] | 2198 | |
| 2199 | // If any existing window is pilfering pointers from newly added window, remove it |
| 2200 | BitSet32 canceledPointers = BitSet32(0); |
| 2201 | for (const TouchedWindow& window : tempTouchState.windows) { |
| 2202 | if (window.isPilferingPointers) { |
| 2203 | canceledPointers |= window.pointerIds; |
| 2204 | } |
| 2205 | } |
| 2206 | tempTouchState.cancelPointersForNonPilferingWindows(canceledPointers); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2207 | } else { |
| 2208 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2209 | |
| 2210 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2211 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2212 | if (DEBUG_FOCUS) { |
| 2213 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2214 | "dropped the pointer down event in display %" PRId32, |
| 2215 | displayId); |
| 2216 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2217 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2218 | goto Failed; |
| 2219 | } |
| 2220 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2221 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2222 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2223 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2224 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2225 | tempTouchState.isSlippery()) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2226 | const int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2227 | 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] | 2228 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2229 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2230 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2231 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2232 | newTouchedWindowHandle = |
| 2233 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2234 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2235 | // Verify targeted injection. |
| 2236 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2237 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
| 2238 | injectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
| 2239 | newTouchedWindowHandle = nullptr; |
| 2240 | goto Failed; |
| 2241 | } |
| 2242 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2243 | // Drop touch events if requested by input feature |
| 2244 | if (newTouchedWindowHandle != nullptr && |
| 2245 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2246 | newTouchedWindowHandle = nullptr; |
| 2247 | } |
| 2248 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2249 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2250 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2251 | if (DEBUG_FOCUS) { |
| 2252 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2253 | oldTouchedWindowHandle->getName().c_str(), |
| 2254 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2255 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2256 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2257 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2258 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2259 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2260 | |
| 2261 | // Make a slippery entrance into the new window. |
| 2262 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2263 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2264 | } |
| 2265 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2266 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
| 2267 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
| 2268 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2269 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2270 | if (isSplit) { |
| 2271 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2272 | } |
| 2273 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2274 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2275 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2276 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | BitSet32 pointerIds; |
| 2280 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2281 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2282 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2283 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2284 | } |
| 2285 | } |
| 2286 | } |
| 2287 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2288 | // Update dispatching for hover enter and exit. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2289 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2290 | // Let the previous window know that the hover sequence is over, unless we already did |
| 2291 | // it when dispatching it as is to newTouchedWindowHandle. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2292 | if (mLastHoverWindowHandle != nullptr && |
| 2293 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2294 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2295 | if (DEBUG_HOVER) { |
| 2296 | ALOGD("Sending hover exit event to window %s.", |
| 2297 | mLastHoverWindowHandle->getName().c_str()); |
| 2298 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2299 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2300 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2301 | } |
| 2302 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2303 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2304 | // when dispatching it as is to newTouchedWindowHandle. |
| 2305 | if (newHoverWindowHandle != nullptr && |
| 2306 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2307 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2308 | if (DEBUG_HOVER) { |
| 2309 | ALOGD("Sending hover enter event to window %s.", |
| 2310 | newHoverWindowHandle->getName().c_str()); |
| 2311 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2312 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2313 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2314 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2315 | } |
| 2316 | } |
| 2317 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2318 | // Ensure that we have at least one foreground window or at least one window that cannot be a |
| 2319 | // foreground target. If we only have windows that are not receiving foreground touches (e.g. we |
| 2320 | // only have windows getting ACTION_OUTSIDE), then drop the event, because there is no window |
| 2321 | // that is actually receiving the entire gesture. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2322 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2323 | [](const TouchedWindow& touchedWindow) { |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2324 | return !canReceiveForegroundTouches( |
| 2325 | *touchedWindow.windowHandle->getInfo()) || |
| 2326 | (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2327 | })) { |
Siarhei Vishniakou | 1fb1891 | 2022-03-08 10:31:39 -0800 | [diff] [blame] | 2328 | ALOGI("Dropping event because there is no touched window on display %d to receive it: %s", |
| 2329 | displayId, entry.getDescription().c_str()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2330 | injectionResult = InputEventInjectionResult::FAILED; |
| 2331 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2332 | } |
| 2333 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2334 | // Ensure that all touched windows are valid for injection. |
| 2335 | if (entry.injectionState != nullptr) { |
| 2336 | std::string errs; |
| 2337 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
| 2338 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2339 | // Allow ACTION_OUTSIDE events generated by targeted injection to be |
| 2340 | // dispatched to any uid, since the coords will be zeroed out later. |
| 2341 | continue; |
| 2342 | } |
| 2343 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2344 | if (err) errs += "\n - " + *err; |
| 2345 | } |
| 2346 | if (!errs.empty()) { |
| 2347 | ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " |
| 2348 | "%d:%s", |
| 2349 | *entry.injectionState->targetUid, errs.c_str()); |
| 2350 | injectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2351 | goto Failed; |
| 2352 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2353 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2354 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2355 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2356 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2357 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2358 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2359 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2360 | if (foregroundWindowHandle) { |
| 2361 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2362 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2363 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2364 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2365 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2366 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2367 | InputTarget::FLAG_ZERO_COORDS, |
| 2368 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2369 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | } |
| 2374 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2375 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2376 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2377 | // of the touch gesture. |
| 2378 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2379 | // engine only supports touch events. We would need to add a mechanism similar |
| 2380 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2381 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2382 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2383 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2384 | if (foregroundWindowHandle && |
| 2385 | foregroundWindowHandle->getInfo()->inputConfig.test( |
| 2386 | WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2387 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2388 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2389 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2390 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2391 | if (info->displayId == displayId && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2392 | windowHandle->getInfo()->inputConfig.test( |
| 2393 | WindowInfo::InputConfig::IS_WALLPAPER)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2394 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2395 | .addOrUpdateWindow(windowHandle, |
| 2396 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2397 | InputTarget:: |
| 2398 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2399 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2400 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | } |
| 2404 | } |
| 2405 | |
| 2406 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2407 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2408 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2409 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2410 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2411 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | // Drop the outside or hover touch windows since we will not care about them |
| 2415 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2416 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2417 | |
| 2418 | Failed: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2419 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2420 | if (!wrongDevice) { |
| 2421 | if (switchedDevice) { |
| 2422 | if (DEBUG_FOCUS) { |
| 2423 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2424 | } |
| 2425 | *outConflictingPointerActions = true; |
| 2426 | } |
| 2427 | |
| 2428 | if (isHoverAction) { |
| 2429 | // Started hovering, therefore no longer down. |
| 2430 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2431 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2432 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2433 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2434 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2435 | *outConflictingPointerActions = true; |
| 2436 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2437 | tempTouchState.reset(); |
| 2438 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2439 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2440 | tempTouchState.deviceId = entry.deviceId; |
| 2441 | tempTouchState.source = entry.source; |
| 2442 | tempTouchState.displayId = displayId; |
| 2443 | } |
| 2444 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2445 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2446 | // All pointers up or canceled. |
| 2447 | tempTouchState.reset(); |
| 2448 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2449 | // First pointer went down. |
| 2450 | if (oldState && oldState->down) { |
| 2451 | if (DEBUG_FOCUS) { |
| 2452 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2453 | } |
| 2454 | *outConflictingPointerActions = true; |
| 2455 | } |
| 2456 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2457 | // One pointer went up. |
| 2458 | if (isSplit) { |
| 2459 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2460 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2461 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2462 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2463 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2464 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2465 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2466 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2467 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2468 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2469 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2470 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2471 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2472 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2473 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2474 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2475 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2476 | // Save changes unless the action was scroll in which case the temporary touch |
| 2477 | // state was only valid for this one action. |
| 2478 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2479 | if (tempTouchState.displayId >= 0) { |
| 2480 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2481 | } else { |
| 2482 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2483 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2484 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2485 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2486 | // Update hover state. |
| 2487 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2488 | } |
| 2489 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2490 | return injectionResult; |
| 2491 | } |
| 2492 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2493 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2494 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2495 | // have an explicit reason to support it. |
| 2496 | constexpr bool isStylus = false; |
| 2497 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2498 | const sp<WindowInfoHandle> dropWindow = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2499 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2500 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2501 | if (dropWindow) { |
| 2502 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2503 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2504 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2505 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2506 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2507 | } |
| 2508 | mDragState.reset(); |
| 2509 | } |
| 2510 | |
| 2511 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2512 | if (!mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2513 | return; |
| 2514 | } |
| 2515 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2516 | if (!mDragState->isStartDrag) { |
| 2517 | mDragState->isStartDrag = true; |
| 2518 | mDragState->isStylusButtonDownAtStart = |
| 2519 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2520 | } |
| 2521 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2522 | // Find the pointer index by id. |
| 2523 | int32_t pointerIndex = 0; |
| 2524 | for (; static_cast<uint32_t>(pointerIndex) < entry.pointerCount; pointerIndex++) { |
| 2525 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2526 | if (pointerProperties.id == mDragState->pointerId) { |
| 2527 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2528 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2529 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2530 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2531 | if (uint32_t(pointerIndex) == entry.pointerCount) { |
| 2532 | 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] | 2533 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2534 | mDragState.reset(); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2535 | return; |
| 2536 | } |
| 2537 | |
| 2538 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2539 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2540 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2541 | |
| 2542 | switch (maskedAction) { |
| 2543 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2544 | // Handle the special case : stylus button no longer pressed. |
| 2545 | bool isStylusButtonDown = |
| 2546 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2547 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2548 | finishDragAndDrop(entry.displayId, x, y); |
| 2549 | return; |
| 2550 | } |
| 2551 | |
| 2552 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2553 | // until we have an explicit reason to support it. |
| 2554 | constexpr bool isStylus = false; |
| 2555 | |
| 2556 | const sp<WindowInfoHandle> hoverWindowHandle = |
| 2557 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
| 2558 | isStylus, false /*addOutsideTargets*/, |
| 2559 | true /*ignoreDragWindow*/); |
| 2560 | // enqueue drag exit if needed. |
| 2561 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2562 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2563 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2564 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, x, |
| 2565 | y); |
| 2566 | } |
| 2567 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2568 | } |
| 2569 | // enqueue drag location if needed. |
| 2570 | if (hoverWindowHandle != nullptr) { |
| 2571 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, x, y); |
| 2572 | } |
| 2573 | break; |
| 2574 | } |
| 2575 | |
| 2576 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 2577 | if (getMotionEventActionPointerIndex(entry.action) != pointerIndex) { |
| 2578 | break; |
| 2579 | } |
| 2580 | // The drag pointer is up. |
| 2581 | [[fallthrough]]; |
| 2582 | case AMOTION_EVENT_ACTION_UP: |
| 2583 | finishDragAndDrop(entry.displayId, x, y); |
| 2584 | break; |
| 2585 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2586 | ALOGD("Receiving cancel when drag and drop."); |
| 2587 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2588 | mDragState.reset(); |
| 2589 | break; |
| 2590 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2591 | } |
| 2592 | } |
| 2593 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2594 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2595 | int32_t targetFlags, BitSet32 pointerIds, |
| 2596 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2597 | std::vector<InputTarget>::iterator it = |
| 2598 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2599 | [&windowHandle](const InputTarget& inputTarget) { |
| 2600 | return inputTarget.inputChannel->getConnectionToken() == |
| 2601 | windowHandle->getToken(); |
| 2602 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2603 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2604 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2605 | |
| 2606 | if (it == inputTargets.end()) { |
| 2607 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2608 | std::shared_ptr<InputChannel> inputChannel = |
| 2609 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2610 | if (inputChannel == nullptr) { |
| 2611 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2612 | return; |
| 2613 | } |
| 2614 | inputTarget.inputChannel = inputChannel; |
| 2615 | inputTarget.flags = targetFlags; |
| 2616 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2617 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2618 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2619 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2620 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2621 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2622 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2623 | inputTargets.push_back(inputTarget); |
| 2624 | it = inputTargets.end() - 1; |
| 2625 | } |
| 2626 | |
| 2627 | ALOG_ASSERT(it->flags == targetFlags); |
| 2628 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2629 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2630 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2631 | } |
| 2632 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2633 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2634 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2635 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2636 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2637 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2638 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2639 | InputTarget target; |
| 2640 | target.inputChannel = monitor.inputChannel; |
| 2641 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2642 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2643 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2644 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2645 | target.setDefaultPointerTransform(target.displayTransform); |
| 2646 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2647 | } |
| 2648 | } |
| 2649 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2650 | /** |
| 2651 | * Indicate whether one window handle should be considered as obscuring |
| 2652 | * another window handle. We only check a few preconditions. Actually |
| 2653 | * checking the bounds is left to the caller. |
| 2654 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2655 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2656 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2657 | // Compare by token so cloned layers aren't counted |
| 2658 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2659 | return false; |
| 2660 | } |
| 2661 | auto info = windowHandle->getInfo(); |
| 2662 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2663 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2664 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2665 | } else if (otherInfo->alpha == 0 && |
| 2666 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2667 | // Those act as if they were invisible, so we don't need to flag them. |
| 2668 | // We do want to potentially flag touchable windows even if they have 0 |
| 2669 | // opacity, since they can consume touches and alter the effects of the |
| 2670 | // user interaction (eg. apps that rely on |
| 2671 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2672 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2673 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2674 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2675 | // If ownerUid is the same we don't generate occlusion events as there |
| 2676 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2677 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2678 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2679 | return false; |
| 2680 | } else if (otherInfo->displayId != info->displayId) { |
| 2681 | return false; |
| 2682 | } |
| 2683 | return true; |
| 2684 | } |
| 2685 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2686 | /** |
| 2687 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2688 | * untrusted, one should check: |
| 2689 | * |
| 2690 | * 1. If result.hasBlockingOcclusion is true. |
| 2691 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2692 | * BLOCK_UNTRUSTED. |
| 2693 | * |
| 2694 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2695 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2696 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2697 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2698 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2699 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2700 | * |
| 2701 | * If neither of those is true, then it means the touch can be allowed. |
| 2702 | */ |
| 2703 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2704 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2705 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2706 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2707 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2708 | TouchOcclusionInfo info; |
| 2709 | info.hasBlockingOcclusion = false; |
| 2710 | info.obscuringOpacity = 0; |
| 2711 | info.obscuringUid = -1; |
| 2712 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2713 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2714 | if (windowHandle == otherHandle) { |
| 2715 | break; // All future windows are below us. Exit early. |
| 2716 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2717 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2718 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2719 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2720 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2721 | info.debugInfo.push_back( |
| 2722 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2723 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2724 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2725 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2726 | // window's touch occlusion mode |
| 2727 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2728 | info.hasBlockingOcclusion = true; |
| 2729 | info.obscuringUid = otherInfo->ownerUid; |
| 2730 | info.obscuringPackage = otherInfo->packageName; |
| 2731 | break; |
| 2732 | } |
| 2733 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2734 | uint32_t uid = otherInfo->ownerUid; |
| 2735 | float opacity = |
| 2736 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2737 | // Given windows A and B: |
| 2738 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2739 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2740 | opacityByUid[uid] = opacity; |
| 2741 | if (opacity > info.obscuringOpacity) { |
| 2742 | info.obscuringOpacity = opacity; |
| 2743 | info.obscuringUid = uid; |
| 2744 | info.obscuringPackage = otherInfo->packageName; |
| 2745 | } |
| 2746 | } |
| 2747 | } |
| 2748 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2749 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2750 | info.debugInfo.push_back( |
| 2751 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2752 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2753 | return info; |
| 2754 | } |
| 2755 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2756 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2757 | bool isTouchedWindow) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2758 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2759 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2760 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 2761 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2762 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 2763 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 2764 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 2765 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 2766 | info->name.c_str(), info->inputConfig.string().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2767 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2768 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2771 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2772 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2773 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2774 | occlusionInfo.obscuringUid); |
| 2775 | return false; |
| 2776 | } |
| 2777 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2778 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2779 | "%.2f, maximum allowed = %.2f)", |
| 2780 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2781 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2782 | return false; |
| 2783 | } |
| 2784 | return true; |
| 2785 | } |
| 2786 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2787 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2788 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2789 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2790 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2791 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2792 | if (windowHandle == otherHandle) { |
| 2793 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2794 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2795 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2796 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2797 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2798 | return true; |
| 2799 | } |
| 2800 | } |
| 2801 | return false; |
| 2802 | } |
| 2803 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2804 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2805 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2806 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2807 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2808 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2809 | if (windowHandle == otherHandle) { |
| 2810 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2811 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2812 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2813 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2814 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2815 | return true; |
| 2816 | } |
| 2817 | } |
| 2818 | return false; |
| 2819 | } |
| 2820 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2821 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2822 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2823 | if (applicationHandle != nullptr) { |
| 2824 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2825 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2826 | } else { |
| 2827 | return applicationHandle->getName(); |
| 2828 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2829 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2830 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2831 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2832 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2833 | } |
| 2834 | } |
| 2835 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2836 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2837 | if (!isUserActivityEvent(eventEntry)) { |
| 2838 | // 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] | 2839 | return; |
| 2840 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2841 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2842 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2843 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2844 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2845 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2846 | if (DEBUG_DISPATCH_CYCLE) { |
| 2847 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2848 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2849 | return; |
| 2850 | } |
| 2851 | } |
| 2852 | |
| 2853 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2854 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2855 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2856 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2857 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2858 | return; |
| 2859 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2860 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2861 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2862 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2863 | } |
| 2864 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2865 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2866 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2867 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2868 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2869 | return; |
| 2870 | } |
| 2871 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2872 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2873 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2874 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2875 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2876 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2877 | break; |
| 2878 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2879 | } |
| 2880 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2881 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2882 | REQUIRES(mLock) { |
| 2883 | scoped_unlock unlock(mLock); |
| 2884 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2885 | }; |
| 2886 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2890 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2891 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2892 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2893 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2894 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2895 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2896 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2897 | ATRACE_NAME(message.c_str()); |
| 2898 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2899 | if (DEBUG_DISPATCH_CYCLE) { |
| 2900 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2901 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2902 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2903 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2904 | inputTarget.getPointerInfoString().c_str()); |
| 2905 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2906 | |
| 2907 | // Skip this event if the connection status is not normal. |
| 2908 | // 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] | 2909 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2910 | if (DEBUG_DISPATCH_CYCLE) { |
| 2911 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2912 | connection->getInputChannelName().c_str(), |
| 2913 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2914 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2915 | return; |
| 2916 | } |
| 2917 | |
| 2918 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2919 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2920 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2921 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2922 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2923 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2924 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2925 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2926 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2927 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2928 | if (!splitMotionEntry) { |
| 2929 | return; // split event was dropped |
| 2930 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2931 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2932 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2933 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2934 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2935 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2936 | if (DEBUG_FOCUS) { |
| 2937 | ALOGD("channel '%s' ~ Split motion event.", |
| 2938 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2939 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2940 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2941 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2942 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2943 | return; |
| 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2948 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2949 | } |
| 2950 | |
| 2951 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2952 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2953 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2954 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2955 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2956 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2957 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2958 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2959 | ATRACE_NAME(message.c_str()); |
| 2960 | } |
| 2961 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2962 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2963 | |
| 2964 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2965 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2966 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2967 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2968 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2969 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2970 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2971 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2972 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2973 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2974 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2975 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2976 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2977 | |
| 2978 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2979 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2980 | startDispatchCycleLocked(currentTime, connection); |
| 2981 | } |
| 2982 | } |
| 2983 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2984 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2985 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2986 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2987 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2988 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2989 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2990 | connection->getInputChannelName().c_str(), |
| 2991 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2992 | ATRACE_NAME(message.c_str()); |
| 2993 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2994 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2995 | if (!(inputTargetFlags & dispatchMode)) { |
| 2996 | return; |
| 2997 | } |
| 2998 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2999 | |
| 3000 | // This is a new event. |
| 3001 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3002 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3003 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3004 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3005 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3006 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3007 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3008 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3009 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3010 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3011 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3012 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3013 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3014 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3015 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3016 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3017 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3018 | if (DEBUG_DISPATCH_CYCLE) { |
| 3019 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3020 | "event", |
| 3021 | connection->getInputChannelName().c_str()); |
| 3022 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3023 | return; // skip the inconsistent event |
| 3024 | } |
| 3025 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3026 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3027 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3028 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3029 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3030 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3031 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3032 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3033 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3034 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3035 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3036 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 3037 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 3038 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3039 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 3040 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3041 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 3042 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 3043 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 3044 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3045 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3046 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3047 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3048 | } |
| 3049 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3050 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3051 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3052 | if (DEBUG_DISPATCH_CYCLE) { |
| 3053 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3054 | "enter event", |
| 3055 | connection->getInputChannelName().c_str()); |
| 3056 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3057 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3058 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3059 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3060 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3061 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3062 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3063 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3064 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3065 | } |
| 3066 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3067 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3068 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3069 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3070 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3071 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3072 | if (DEBUG_DISPATCH_CYCLE) { |
| 3073 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3074 | "event", |
| 3075 | connection->getInputChannelName().c_str()); |
| 3076 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3077 | return; // skip the inconsistent event |
| 3078 | } |
| 3079 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3080 | dispatchEntry->resolvedEventId = |
| 3081 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3082 | ? mIdGenerator.nextId() |
| 3083 | : motionEntry.id; |
| 3084 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3085 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3086 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3087 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3088 | ATRACE_NAME(message.c_str()); |
| 3089 | } |
| 3090 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3091 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3092 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3093 | // Skip reporting pointer down outside focus to the policy. |
| 3094 | break; |
| 3095 | } |
| 3096 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3097 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3098 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3099 | |
| 3100 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3101 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3102 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3103 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3104 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3105 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3106 | break; |
| 3107 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3108 | case EventEntry::Type::SENSOR: { |
| 3109 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3110 | break; |
| 3111 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3112 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3113 | case EventEntry::Type::DEVICE_RESET: { |
| 3114 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3115 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3116 | break; |
| 3117 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | // Remember that we are waiting for this dispatch to complete. |
| 3121 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3122 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3126 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3127 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3128 | } |
| 3129 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3130 | /** |
| 3131 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3132 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3133 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3134 | * We will see both launcher and wallpaper in that list. |
| 3135 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3136 | * until the set of interacted connections changes. |
| 3137 | * |
| 3138 | * The following items are skipped, to reduce the logspam: |
| 3139 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3140 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3141 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3142 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3143 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3144 | */ |
| 3145 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3146 | const std::vector<InputTarget>& targets) { |
| 3147 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3148 | if (entry.type == EventEntry::Type::KEY) { |
| 3149 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3150 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3151 | return; |
| 3152 | } |
| 3153 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3154 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3155 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3156 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3157 | return; |
| 3158 | } |
| 3159 | } else { |
| 3160 | return; // Not a key or a motion |
| 3161 | } |
| 3162 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3163 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3164 | std::vector<sp<Connection>> newConnections; |
| 3165 | for (const InputTarget& target : targets) { |
| 3166 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3167 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3168 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3169 | } |
| 3170 | |
| 3171 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3172 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3173 | if (connection == nullptr) { |
| 3174 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3175 | } |
| 3176 | newConnectionTokens.insert(std::move(token)); |
| 3177 | newConnections.emplace_back(connection); |
| 3178 | } |
| 3179 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3180 | return; // no change |
| 3181 | } |
| 3182 | mInteractionConnectionTokens = newConnectionTokens; |
| 3183 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3184 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3185 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3186 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3187 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3188 | std::string message = "Interaction with: " + targetList; |
| 3189 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3190 | message += "<none>"; |
| 3191 | } |
| 3192 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3193 | } |
| 3194 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3195 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3196 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3197 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3198 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3199 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3200 | return; |
| 3201 | } |
| 3202 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3203 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3204 | if (focusedToken == token) { |
| 3205 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3206 | return; |
| 3207 | } |
| 3208 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3209 | auto command = [this, token]() REQUIRES(mLock) { |
| 3210 | scoped_unlock unlock(mLock); |
| 3211 | mPolicy->onPointerDownOutsideFocus(token); |
| 3212 | }; |
| 3213 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3214 | } |
| 3215 | |
| 3216 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3217 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3218 | if (ATRACE_ENABLED()) { |
| 3219 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3220 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3221 | ATRACE_NAME(message.c_str()); |
| 3222 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3223 | if (DEBUG_DISPATCH_CYCLE) { |
| 3224 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3225 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3226 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3227 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3228 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3229 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3230 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3231 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3232 | |
| 3233 | // Publish the event. |
| 3234 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3235 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3236 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3237 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3238 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3239 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3240 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3241 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3242 | status = connection->inputPublisher |
| 3243 | .publishKeyEvent(dispatchEntry->seq, |
| 3244 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3245 | keyEntry.source, keyEntry.displayId, |
| 3246 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3247 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3248 | keyEntry.scanCode, keyEntry.metaState, |
| 3249 | keyEntry.repeatCount, keyEntry.downTime, |
| 3250 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3251 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3252 | } |
| 3253 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3254 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3255 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3256 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3257 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3258 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3259 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3260 | // 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] | 3261 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3262 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3263 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3264 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3265 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3266 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3267 | // Don't apply window scale here since we don't want scale to affect raw |
| 3268 | // coordinates. The scale will be sent back to the client and applied |
| 3269 | // later when requesting relative coordinates. |
| 3270 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3271 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3272 | } |
| 3273 | usingCoords = scaledCoords; |
| 3274 | } |
| 3275 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3276 | // We don't want the dispatch target to know. |
| 3277 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3278 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3279 | scaledCoords[i].clear(); |
| 3280 | } |
| 3281 | usingCoords = scaledCoords; |
| 3282 | } |
| 3283 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3284 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3285 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3286 | |
| 3287 | // Publish the motion event. |
| 3288 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3289 | .publishMotionEvent(dispatchEntry->seq, |
| 3290 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3291 | motionEntry.deviceId, motionEntry.source, |
| 3292 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3293 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3294 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3295 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3296 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3297 | motionEntry.buttonState, |
| 3298 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3299 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3300 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3301 | motionEntry.xCursorPosition, |
| 3302 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3303 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3304 | motionEntry.downTime, motionEntry.eventTime, |
| 3305 | motionEntry.pointerCount, |
| 3306 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3307 | break; |
| 3308 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3309 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3310 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3311 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3312 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3313 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3314 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3315 | break; |
| 3316 | } |
| 3317 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3318 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3319 | const TouchModeEntry& touchModeEntry = |
| 3320 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3321 | status = connection->inputPublisher |
| 3322 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3323 | touchModeEntry.inTouchMode); |
| 3324 | |
| 3325 | break; |
| 3326 | } |
| 3327 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3328 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3329 | const auto& captureEntry = |
| 3330 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3331 | status = connection->inputPublisher |
| 3332 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3333 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3334 | break; |
| 3335 | } |
| 3336 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3337 | case EventEntry::Type::DRAG: { |
| 3338 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3339 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3340 | dragEntry.id, dragEntry.x, |
| 3341 | dragEntry.y, |
| 3342 | dragEntry.isExiting); |
| 3343 | break; |
| 3344 | } |
| 3345 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3346 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3347 | case EventEntry::Type::DEVICE_RESET: |
| 3348 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3349 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3350 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3351 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3352 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | // Check the result. |
| 3356 | if (status) { |
| 3357 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3358 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3359 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3360 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3361 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3362 | "event to it, status=%s(%d)", |
| 3363 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3364 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3365 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3366 | } else { |
| 3367 | // Pipe is full and we are waiting for the app to finish process some events |
| 3368 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3369 | if (DEBUG_DISPATCH_CYCLE) { |
| 3370 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3371 | "waiting for the application to catch up", |
| 3372 | connection->getInputChannelName().c_str()); |
| 3373 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3374 | } |
| 3375 | } else { |
| 3376 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3377 | "status=%s(%d)", |
| 3378 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3379 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3380 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3381 | } |
| 3382 | return; |
| 3383 | } |
| 3384 | |
| 3385 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3386 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3387 | connection->outboundQueue.end(), |
| 3388 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3389 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3390 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3391 | if (connection->responsive) { |
| 3392 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3393 | connection->inputChannel->getConnectionToken()); |
| 3394 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3395 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3396 | } |
| 3397 | } |
| 3398 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3399 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3400 | size_t size; |
| 3401 | switch (event.type) { |
| 3402 | case VerifiedInputEvent::Type::KEY: { |
| 3403 | size = sizeof(VerifiedKeyEvent); |
| 3404 | break; |
| 3405 | } |
| 3406 | case VerifiedInputEvent::Type::MOTION: { |
| 3407 | size = sizeof(VerifiedMotionEvent); |
| 3408 | break; |
| 3409 | } |
| 3410 | } |
| 3411 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3412 | return mHmacKeyManager.sign(start, size); |
| 3413 | } |
| 3414 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3415 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3416 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3417 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3418 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3419 | // Only sign events up and down events as the purely move events |
| 3420 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3421 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3422 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3423 | |
| 3424 | VerifiedMotionEvent verifiedEvent = |
| 3425 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3426 | verifiedEvent.actionMasked = actionMasked; |
| 3427 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3428 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3432 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3433 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3434 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3435 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3436 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3437 | } |
| 3438 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3439 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3440 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3441 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3442 | if (DEBUG_DISPATCH_CYCLE) { |
| 3443 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3444 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3445 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3446 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3447 | if (connection->status == Connection::Status::BROKEN || |
| 3448 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3449 | return; |
| 3450 | } |
| 3451 | |
| 3452 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3453 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3454 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3455 | }; |
| 3456 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3457 | } |
| 3458 | |
| 3459 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3460 | const sp<Connection>& connection, |
| 3461 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3462 | if (DEBUG_DISPATCH_CYCLE) { |
| 3463 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3464 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3465 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3466 | |
| 3467 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3468 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3469 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3470 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3471 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3472 | |
| 3473 | // The connection appears to be unrecoverably broken. |
| 3474 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3475 | if (connection->status == Connection::Status::NORMAL) { |
| 3476 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3477 | |
| 3478 | if (notify) { |
| 3479 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3480 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3481 | connection->getInputChannelName().c_str()); |
| 3482 | |
| 3483 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3484 | scoped_unlock unlock(mLock); |
| 3485 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3486 | }; |
| 3487 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3488 | } |
| 3489 | } |
| 3490 | } |
| 3491 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3492 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3493 | while (!queue.empty()) { |
| 3494 | DispatchEntry* dispatchEntry = queue.front(); |
| 3495 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3496 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3497 | } |
| 3498 | } |
| 3499 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3500 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3501 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3502 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3503 | } |
| 3504 | delete dispatchEntry; |
| 3505 | } |
| 3506 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3507 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3508 | std::scoped_lock _l(mLock); |
| 3509 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3510 | if (connection == nullptr) { |
| 3511 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3512 | connectionToken.get(), events); |
| 3513 | return 0; // remove the callback |
| 3514 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3515 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3516 | bool notify; |
| 3517 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3518 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3519 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3520 | "events=0x%x", |
| 3521 | connection->getInputChannelName().c_str(), events); |
| 3522 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3523 | } |
| 3524 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3525 | nsecs_t currentTime = now(); |
| 3526 | bool gotOne = false; |
| 3527 | status_t status = OK; |
| 3528 | for (;;) { |
| 3529 | Result<InputPublisher::ConsumerResponse> result = |
| 3530 | connection->inputPublisher.receiveConsumerResponse(); |
| 3531 | if (!result.ok()) { |
| 3532 | status = result.error().code(); |
| 3533 | break; |
| 3534 | } |
| 3535 | |
| 3536 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3537 | const InputPublisher::Finished& finish = |
| 3538 | std::get<InputPublisher::Finished>(*result); |
| 3539 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3540 | finish.consumeTime); |
| 3541 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3542 | if (shouldReportMetricsForConnection(*connection)) { |
| 3543 | const InputPublisher::Timeline& timeline = |
| 3544 | std::get<InputPublisher::Timeline>(*result); |
| 3545 | mLatencyTracker |
| 3546 | .trackGraphicsLatency(timeline.inputEventId, |
| 3547 | connection->inputChannel->getConnectionToken(), |
| 3548 | std::move(timeline.graphicsTimeline)); |
| 3549 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3550 | } |
| 3551 | gotOne = true; |
| 3552 | } |
| 3553 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3554 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3555 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3556 | return 1; |
| 3557 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3558 | } |
| 3559 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3560 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3561 | if (notify) { |
| 3562 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3563 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3564 | status); |
| 3565 | } |
| 3566 | } else { |
| 3567 | // Monitor channels are never explicitly unregistered. |
| 3568 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3569 | const bool stillHaveWindowHandle = |
| 3570 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3571 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3572 | if (notify) { |
| 3573 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3574 | connection->getInputChannelName().c_str(), events); |
| 3575 | } |
| 3576 | } |
| 3577 | |
| 3578 | // Remove the channel. |
| 3579 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3580 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3581 | } |
| 3582 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3583 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3584 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3585 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3586 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3587 | } |
| 3588 | } |
| 3589 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3590 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3591 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3592 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3593 | for (const Monitor& monitor : monitors) { |
| 3594 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3595 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3596 | } |
| 3597 | } |
| 3598 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3599 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3600 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3601 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3602 | if (connection == nullptr) { |
| 3603 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3604 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3605 | |
| 3606 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3610 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3611 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3612 | return; |
| 3613 | } |
| 3614 | |
| 3615 | nsecs_t currentTime = now(); |
| 3616 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3617 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3618 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3619 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3620 | if (cancelationEvents.empty()) { |
| 3621 | return; |
| 3622 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3623 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3624 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3625 | "with reality: %s, mode=%d.", |
| 3626 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3627 | options.mode); |
| 3628 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3629 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3630 | std::string reason = std::string("reason=").append(options.reason); |
| 3631 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3632 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3633 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3634 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3635 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3636 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3637 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3638 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3639 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3640 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3641 | } |
| 3642 | target.inputChannel = connection->inputChannel; |
| 3643 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3644 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3645 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3646 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3647 | switch (cancelationEventEntry->type) { |
| 3648 | case EventEntry::Type::KEY: { |
| 3649 | logOutboundKeyDetails("cancel - ", |
| 3650 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3651 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3652 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3653 | case EventEntry::Type::MOTION: { |
| 3654 | logOutboundMotionDetails("cancel - ", |
| 3655 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3656 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3657 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3658 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3659 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3660 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3661 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3662 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3663 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3664 | break; |
| 3665 | } |
| 3666 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3667 | case EventEntry::Type::DEVICE_RESET: |
| 3668 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3669 | 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] | 3670 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3671 | break; |
| 3672 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3673 | } |
| 3674 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3675 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3676 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3677 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3678 | |
| 3679 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3680 | } |
| 3681 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3682 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3683 | const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3684 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3685 | return; |
| 3686 | } |
| 3687 | |
| 3688 | nsecs_t currentTime = now(); |
| 3689 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3690 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3691 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3692 | |
| 3693 | if (downEvents.empty()) { |
| 3694 | return; |
| 3695 | } |
| 3696 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3697 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3698 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3699 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3700 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3701 | |
| 3702 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3703 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3704 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3705 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3706 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3707 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3708 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3709 | } |
| 3710 | target.inputChannel = connection->inputChannel; |
| 3711 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3712 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3713 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3714 | switch (downEventEntry->type) { |
| 3715 | case EventEntry::Type::MOTION: { |
| 3716 | logOutboundMotionDetails("down - ", |
| 3717 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3718 | break; |
| 3719 | } |
| 3720 | |
| 3721 | case EventEntry::Type::KEY: |
| 3722 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3723 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3724 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3725 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3726 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3727 | case EventEntry::Type::SENSOR: |
| 3728 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3729 | 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] | 3730 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3731 | break; |
| 3732 | } |
| 3733 | } |
| 3734 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3735 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3736 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | startDispatchCycleLocked(currentTime, connection); |
| 3740 | } |
| 3741 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3742 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3743 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3744 | ALOG_ASSERT(pointerIds.value != 0); |
| 3745 | |
| 3746 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3747 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3748 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3749 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3750 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3751 | uint32_t splitPointerCount = 0; |
| 3752 | |
| 3753 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3754 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3755 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3756 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3757 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3758 | if (pointerIds.hasBit(pointerId)) { |
| 3759 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3760 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3761 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3762 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3763 | splitPointerCount += 1; |
| 3764 | } |
| 3765 | } |
| 3766 | |
| 3767 | if (splitPointerCount != pointerIds.count()) { |
| 3768 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3769 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3770 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3771 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3772 | // in this way. |
| 3773 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3774 | "we expected there to be %d pointers. This probably means we received " |
| 3775 | "a broken sequence of pointer ids from the input device.", |
| 3776 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3777 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3778 | } |
| 3779 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3780 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3781 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3782 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3783 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3784 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3785 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3786 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3787 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3788 | if (pointerIds.hasBit(pointerId)) { |
| 3789 | if (pointerIds.count() == 1) { |
| 3790 | // The first/last pointer went down/up. |
| 3791 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3792 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3793 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3794 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3795 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3796 | } else { |
| 3797 | // A secondary pointer went down/up. |
| 3798 | uint32_t splitPointerIndex = 0; |
| 3799 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3800 | splitPointerIndex += 1; |
| 3801 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3802 | action = maskedAction | |
| 3803 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3804 | } |
| 3805 | } else { |
| 3806 | // An unrelated pointer changed. |
| 3807 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3808 | } |
| 3809 | } |
| 3810 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3811 | int32_t newId = mIdGenerator.nextId(); |
| 3812 | if (ATRACE_ENABLED()) { |
| 3813 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3814 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3815 | originalMotionEntry.id, newId); |
| 3816 | ATRACE_NAME(message.c_str()); |
| 3817 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3818 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3819 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3820 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3821 | originalMotionEntry.displayId, |
| 3822 | originalMotionEntry.policyFlags, action, |
| 3823 | originalMotionEntry.actionButton, |
| 3824 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3825 | originalMotionEntry.buttonState, |
| 3826 | originalMotionEntry.classification, |
| 3827 | originalMotionEntry.edgeFlags, |
| 3828 | originalMotionEntry.xPrecision, |
| 3829 | originalMotionEntry.yPrecision, |
| 3830 | originalMotionEntry.xCursorPosition, |
| 3831 | originalMotionEntry.yCursorPosition, |
| 3832 | originalMotionEntry.downTime, splitPointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3833 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3834 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3835 | if (originalMotionEntry.injectionState) { |
| 3836 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3837 | splitMotionEntry->injectionState->refCount += 1; |
| 3838 | } |
| 3839 | |
| 3840 | return splitMotionEntry; |
| 3841 | } |
| 3842 | |
| 3843 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3844 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3845 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3846 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3847 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3848 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3849 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3850 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3851 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3852 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3853 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3854 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3855 | } // release lock |
| 3856 | |
| 3857 | if (needWake) { |
| 3858 | mLooper->wake(); |
| 3859 | } |
| 3860 | } |
| 3861 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3862 | /** |
| 3863 | * If one of the meta shortcuts is detected, process them here: |
| 3864 | * Meta + Backspace -> generate BACK |
| 3865 | * Meta + Enter -> generate HOME |
| 3866 | * This will potentially overwrite keyCode and metaState. |
| 3867 | */ |
| 3868 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3869 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3870 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3871 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3872 | if (keyCode == AKEYCODE_DEL) { |
| 3873 | newKeyCode = AKEYCODE_BACK; |
| 3874 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3875 | newKeyCode = AKEYCODE_HOME; |
| 3876 | } |
| 3877 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3878 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3879 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3880 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3881 | keyCode = newKeyCode; |
| 3882 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3883 | } |
| 3884 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3885 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3886 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3887 | // 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] | 3888 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3889 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3890 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3891 | if (replacementIt != mReplacedKeys.end()) { |
| 3892 | keyCode = replacementIt->second; |
| 3893 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3894 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3895 | } |
| 3896 | } |
| 3897 | } |
| 3898 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3899 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3900 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3901 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3902 | "policyFlags=0x%x, action=0x%x, " |
| 3903 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3904 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3905 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3906 | args->downTime); |
| 3907 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3908 | if (!validateKeyEvent(args->action)) { |
| 3909 | return; |
| 3910 | } |
| 3911 | |
| 3912 | uint32_t policyFlags = args->policyFlags; |
| 3913 | int32_t flags = args->flags; |
| 3914 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3915 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3916 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3917 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3918 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3919 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3920 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3921 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3922 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3923 | metaState |= AMETA_FUNCTION_ON; |
| 3924 | } |
| 3925 | |
| 3926 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3927 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3928 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3929 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3930 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3931 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3932 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3933 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3934 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3935 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3936 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3937 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3938 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3939 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3940 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3941 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3942 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3943 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3944 | { // acquire lock |
| 3945 | mLock.lock(); |
| 3946 | |
| 3947 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3948 | mLock.unlock(); |
| 3949 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3950 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3951 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3952 | return; // event was consumed by the filter |
| 3953 | } |
| 3954 | |
| 3955 | mLock.lock(); |
| 3956 | } |
| 3957 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3958 | std::unique_ptr<KeyEntry> newEntry = |
| 3959 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3960 | args->displayId, policyFlags, args->action, flags, |
| 3961 | keyCode, args->scanCode, metaState, repeatCount, |
| 3962 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3963 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3964 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3965 | mLock.unlock(); |
| 3966 | } // release lock |
| 3967 | |
| 3968 | if (needWake) { |
| 3969 | mLooper->wake(); |
| 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3974 | return mInputFilterEnabled; |
| 3975 | } |
| 3976 | |
| 3977 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3978 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3979 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3980 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 3981 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3982 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 3983 | "yCursorPosition=%f, downTime=%" PRId64, |
| 3984 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3985 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3986 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3987 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 3988 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3989 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 3990 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3991 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3992 | "orientation=%f", |
| 3993 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3994 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3995 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3996 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3997 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3998 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3999 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4000 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4001 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4002 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 4003 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4004 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4005 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 4006 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4007 | return; |
| 4008 | } |
| 4009 | |
| 4010 | uint32_t policyFlags = args->policyFlags; |
| 4011 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4012 | |
| 4013 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4014 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4015 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4016 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4017 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4018 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4019 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4020 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4021 | { // acquire lock |
| 4022 | mLock.lock(); |
| 4023 | |
| 4024 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4025 | ui::Transform displayTransform; |
| 4026 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4027 | displayTransform = it->second.transform; |
| 4028 | } |
| 4029 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4030 | mLock.unlock(); |
| 4031 | |
| 4032 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4033 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4034 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4035 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4036 | displayTransform, args->xPrecision, args->yPrecision, |
| 4037 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4038 | args->downTime, args->eventTime, args->pointerCount, |
| 4039 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4040 | |
| 4041 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4042 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4043 | return; // event was consumed by the filter |
| 4044 | } |
| 4045 | |
| 4046 | mLock.lock(); |
| 4047 | } |
| 4048 | |
| 4049 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4050 | std::unique_ptr<MotionEntry> newEntry = |
| 4051 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4052 | args->source, args->displayId, policyFlags, |
| 4053 | args->action, args->actionButton, args->flags, |
| 4054 | args->metaState, args->buttonState, |
| 4055 | args->classification, args->edgeFlags, |
| 4056 | args->xPrecision, args->yPrecision, |
| 4057 | args->xCursorPosition, args->yCursorPosition, |
| 4058 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4059 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4060 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4061 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4062 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4063 | !mInputFilterEnabled) { |
| 4064 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4065 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4066 | } |
| 4067 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4068 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4069 | mLock.unlock(); |
| 4070 | } // release lock |
| 4071 | |
| 4072 | if (needWake) { |
| 4073 | mLooper->wake(); |
| 4074 | } |
| 4075 | } |
| 4076 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4077 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4078 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4079 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4080 | " sensorType=%s", |
| 4081 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4082 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4083 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4084 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4085 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4086 | { // acquire lock |
| 4087 | mLock.lock(); |
| 4088 | |
| 4089 | // Just enqueue a new sensor event. |
| 4090 | std::unique_ptr<SensorEntry> newEntry = |
| 4091 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4092 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4093 | args->sensorType, args->accuracy, |
| 4094 | args->accuracyChanged, args->values); |
| 4095 | |
| 4096 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4097 | mLock.unlock(); |
| 4098 | } // release lock |
| 4099 | |
| 4100 | if (needWake) { |
| 4101 | mLooper->wake(); |
| 4102 | } |
| 4103 | } |
| 4104 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4105 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4106 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4107 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4108 | args->deviceId, args->isOn); |
| 4109 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4110 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4111 | } |
| 4112 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4113 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4114 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4118 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4119 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4120 | "switchMask=0x%08x", |
| 4121 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4122 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4123 | |
| 4124 | uint32_t policyFlags = args->policyFlags; |
| 4125 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4126 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4130 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4131 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4132 | args->deviceId); |
| 4133 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4134 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4135 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4136 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4137 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4138 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4139 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4140 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4141 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4142 | } // release lock |
| 4143 | |
| 4144 | if (needWake) { |
| 4145 | mLooper->wake(); |
| 4146 | } |
| 4147 | } |
| 4148 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4149 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4150 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4151 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4152 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4153 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4154 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4155 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4156 | { // acquire lock |
| 4157 | std::scoped_lock _l(mLock); |
| 4158 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4159 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4160 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4161 | } // release lock |
| 4162 | |
| 4163 | if (needWake) { |
| 4164 | mLooper->wake(); |
| 4165 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4166 | } |
| 4167 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4168 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
| 4169 | std::optional<int32_t> targetUid, |
| 4170 | InputEventInjectionSync syncMode, |
| 4171 | std::chrono::milliseconds timeout, |
| 4172 | uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4173 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4174 | ALOGD("injectInputEvent - eventType=%d, targetUid=%s, syncMode=%d, timeout=%lld, " |
| 4175 | "policyFlags=0x%08x", |
| 4176 | event->getType(), targetUid ? std::to_string(*targetUid).c_str() : "none", syncMode, |
| 4177 | timeout.count(), policyFlags); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4178 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4179 | 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] | 4180 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4181 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4182 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4183 | // 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] | 4184 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4185 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4186 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4187 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4188 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4189 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4190 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4191 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4194 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4195 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4196 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4197 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4198 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4199 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4200 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4201 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4202 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4203 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4204 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4205 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4206 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4207 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4208 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4209 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4210 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4211 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4212 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4213 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4214 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4215 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4216 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4217 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4218 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4219 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4220 | |
| 4221 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4222 | android::base::Timer t; |
| 4223 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4224 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4225 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4226 | std::to_string(t.duration().count()).c_str()); |
| 4227 | } |
| 4228 | } |
| 4229 | |
| 4230 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4231 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4232 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4233 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4234 | incomingKey.getDisplayId(), policyFlags, action, |
| 4235 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4236 | incomingKey.getRepeatCount(), |
| 4237 | incomingKey.getDownTime()); |
| 4238 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4239 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4240 | } |
| 4241 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4242 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4243 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4244 | const int32_t action = motionEvent.getAction(); |
| 4245 | const bool isPointerEvent = |
| 4246 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4247 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4248 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4249 | ? ADISPLAY_ID_DEFAULT |
| 4250 | : event->getDisplayId(); |
| 4251 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4252 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4253 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4254 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4255 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4256 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4257 | } |
| 4258 | |
| 4259 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4260 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4261 | android::base::Timer t; |
| 4262 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4263 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4264 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4265 | std::to_string(t.duration().count()).c_str()); |
| 4266 | } |
| 4267 | } |
| 4268 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4269 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4270 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4271 | } |
| 4272 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4273 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4274 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4275 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4276 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4277 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4278 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4279 | displayId, policyFlags, action, actionButton, |
| 4280 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4281 | motionEvent.getButtonState(), |
| 4282 | motionEvent.getClassification(), |
| 4283 | motionEvent.getEdgeFlags(), |
| 4284 | motionEvent.getXPrecision(), |
| 4285 | motionEvent.getYPrecision(), |
| 4286 | motionEvent.getRawXCursorPosition(), |
| 4287 | motionEvent.getRawYCursorPosition(), |
| 4288 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4289 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4290 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4291 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4292 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4293 | sampleEventTimes += 1; |
| 4294 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4295 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4296 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4297 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4298 | displayId, policyFlags, action, actionButton, |
| 4299 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4300 | motionEvent.getButtonState(), |
| 4301 | motionEvent.getClassification(), |
| 4302 | motionEvent.getEdgeFlags(), |
| 4303 | motionEvent.getXPrecision(), |
| 4304 | motionEvent.getYPrecision(), |
| 4305 | motionEvent.getRawXCursorPosition(), |
| 4306 | motionEvent.getRawYCursorPosition(), |
| 4307 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4308 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4309 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4310 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4311 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4312 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4313 | } |
| 4314 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4315 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4316 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4317 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4318 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4319 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4320 | } |
| 4321 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4322 | InjectionState* injectionState = new InjectionState(targetUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4323 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4324 | injectionState->injectionIsAsync = true; |
| 4325 | } |
| 4326 | |
| 4327 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4328 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4329 | |
| 4330 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4331 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4332 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4333 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | mLock.unlock(); |
| 4337 | |
| 4338 | if (needWake) { |
| 4339 | mLooper->wake(); |
| 4340 | } |
| 4341 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4342 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4343 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4344 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4345 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4346 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4347 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4348 | } else { |
| 4349 | for (;;) { |
| 4350 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4351 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4352 | break; |
| 4353 | } |
| 4354 | |
| 4355 | nsecs_t remainingTimeout = endTime - now(); |
| 4356 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4357 | if (DEBUG_INJECTION) { |
| 4358 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4359 | "to become available."); |
| 4360 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4361 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4362 | break; |
| 4363 | } |
| 4364 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4365 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4366 | } |
| 4367 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4368 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4369 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4370 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4371 | if (DEBUG_INJECTION) { |
| 4372 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4373 | injectionState->pendingForegroundDispatches); |
| 4374 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4375 | nsecs_t remainingTimeout = endTime - now(); |
| 4376 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4377 | if (DEBUG_INJECTION) { |
| 4378 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4379 | "dispatches to finish."); |
| 4380 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4381 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4382 | break; |
| 4383 | } |
| 4384 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4385 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4386 | } |
| 4387 | } |
| 4388 | } |
| 4389 | |
| 4390 | injectionState->release(); |
| 4391 | } // release lock |
| 4392 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4393 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4394 | ALOGD("injectInputEvent - Finished with result %d.", injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4395 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4396 | |
| 4397 | return injectionResult; |
| 4398 | } |
| 4399 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4400 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4401 | std::array<uint8_t, 32> calculatedHmac; |
| 4402 | std::unique_ptr<VerifiedInputEvent> result; |
| 4403 | switch (event.getType()) { |
| 4404 | case AINPUT_EVENT_TYPE_KEY: { |
| 4405 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4406 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4407 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4408 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4409 | break; |
| 4410 | } |
| 4411 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4412 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4413 | VerifiedMotionEvent verifiedMotionEvent = |
| 4414 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4415 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4416 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4417 | break; |
| 4418 | } |
| 4419 | default: { |
| 4420 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4421 | return nullptr; |
| 4422 | } |
| 4423 | } |
| 4424 | if (calculatedHmac == INVALID_HMAC) { |
| 4425 | return nullptr; |
| 4426 | } |
| 4427 | if (calculatedHmac != event.getHmac()) { |
| 4428 | return nullptr; |
| 4429 | } |
| 4430 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4431 | } |
| 4432 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4433 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4434 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4435 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4436 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4437 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4438 | ALOGD("Setting input event injection result to %d.", injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4439 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4440 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4441 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4442 | // Log the outcome since the injector did not wait for the injection result. |
| 4443 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4444 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4445 | ALOGV("Asynchronous input event injection succeeded."); |
| 4446 | break; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4447 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4448 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4449 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4450 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4451 | ALOGW("Asynchronous input event injection failed."); |
| 4452 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4453 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4454 | ALOGW("Asynchronous input event injection timed out."); |
| 4455 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4456 | case InputEventInjectionResult::PENDING: |
| 4457 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4458 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4459 | } |
| 4460 | } |
| 4461 | |
| 4462 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4463 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4464 | } |
| 4465 | } |
| 4466 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4467 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4468 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4469 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4470 | // display space, so we need to transform the injected events accordingly. |
| 4471 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4472 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4473 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4474 | |
| 4475 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4476 | entry.pointerCoords[i] = |
| 4477 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4478 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4479 | } |
| 4480 | } |
| 4481 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4482 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4483 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4484 | if (injectionState) { |
| 4485 | injectionState->pendingForegroundDispatches += 1; |
| 4486 | } |
| 4487 | } |
| 4488 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4489 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4490 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4491 | if (injectionState) { |
| 4492 | injectionState->pendingForegroundDispatches -= 1; |
| 4493 | |
| 4494 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4495 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4496 | } |
| 4497 | } |
| 4498 | } |
| 4499 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4500 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4501 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4502 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4503 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4504 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4505 | } |
| 4506 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4507 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4508 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4509 | if (windowHandleToken == nullptr) { |
| 4510 | return nullptr; |
| 4511 | } |
| 4512 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4513 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4514 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4515 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4516 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4517 | return windowHandle; |
| 4518 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4519 | } |
| 4520 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4521 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4522 | } |
| 4523 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4524 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4525 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4526 | if (windowHandleToken == nullptr) { |
| 4527 | return nullptr; |
| 4528 | } |
| 4529 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4530 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4531 | if (windowHandle->getToken() == windowHandleToken) { |
| 4532 | return windowHandle; |
| 4533 | } |
| 4534 | } |
| 4535 | return nullptr; |
| 4536 | } |
| 4537 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4538 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4539 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4540 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4541 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4542 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4543 | if (handle->getId() == windowHandle->getId() && |
| 4544 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4545 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4546 | ALOGE("Found window %s in display %" PRId32 |
| 4547 | ", but it should belong to display %" PRId32, |
| 4548 | windowHandle->getName().c_str(), it.first, |
| 4549 | windowHandle->getInfo()->displayId); |
| 4550 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4551 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4552 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4553 | } |
| 4554 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4555 | return nullptr; |
| 4556 | } |
| 4557 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4558 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4559 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4560 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4561 | } |
| 4562 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4563 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4564 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4565 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4566 | windowHandle.getInfo()->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4567 | if (connection != nullptr && noInputChannel) { |
| 4568 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4569 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4570 | return false; |
| 4571 | } |
| 4572 | |
| 4573 | if (connection == nullptr) { |
| 4574 | if (!noInputChannel) { |
| 4575 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4576 | } |
| 4577 | return false; |
| 4578 | } |
| 4579 | if (!connection->responsive) { |
| 4580 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4581 | return false; |
| 4582 | } |
| 4583 | return true; |
| 4584 | } |
| 4585 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4586 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4587 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4588 | auto connectionIt = mConnectionsByToken.find(token); |
| 4589 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4590 | return nullptr; |
| 4591 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4592 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4593 | } |
| 4594 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4595 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4596 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4597 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4598 | // Remove all handles on a display if there are no windows left. |
| 4599 | mWindowHandlesByDisplay.erase(displayId); |
| 4600 | return; |
| 4601 | } |
| 4602 | |
| 4603 | // Since we compare the pointer of input window handles across window updates, we need |
| 4604 | // 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] | 4605 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4606 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4607 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4608 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4609 | } |
| 4610 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4611 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4612 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4613 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4614 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4615 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4616 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4617 | const bool canReceiveInput = |
| 4618 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4619 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4620 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4621 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4622 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4623 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4624 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4625 | } |
| 4626 | |
| 4627 | if (info->displayId != displayId) { |
| 4628 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4629 | handle->getName().c_str(), displayId, info->displayId); |
| 4630 | continue; |
| 4631 | } |
| 4632 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4633 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4634 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4635 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4636 | oldHandle->updateFrom(handle); |
| 4637 | newHandles.push_back(oldHandle); |
| 4638 | } else { |
| 4639 | newHandles.push_back(handle); |
| 4640 | } |
| 4641 | } |
| 4642 | |
| 4643 | // Insert or replace |
| 4644 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4645 | } |
| 4646 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4647 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4648 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4649 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4650 | { // acquire lock |
| 4651 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4652 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4653 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4654 | } |
| 4655 | } |
| 4656 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4657 | mLooper->wake(); |
| 4658 | } |
| 4659 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4660 | /** |
| 4661 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4662 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4663 | * If set an empty list, remove all handles from the specific display. |
| 4664 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4665 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4666 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4667 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4668 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4669 | if (DEBUG_FOCUS) { |
| 4670 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4671 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4672 | windowList += iwh->getName() + " "; |
| 4673 | } |
| 4674 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4675 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4676 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4677 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4678 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4679 | const WindowInfo& info = *window->getInfo(); |
| 4680 | |
| 4681 | // 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] | 4682 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4683 | if (noInputWindow && window->getToken() != nullptr) { |
| 4684 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4685 | window->getName().c_str()); |
| 4686 | window->releaseChannel(); |
| 4687 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4688 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4689 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4690 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 4691 | !info.inputConfig.test( |
| 4692 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4693 | "%s has feature SPY, but is not a trusted overlay.", |
| 4694 | window->getName().c_str()); |
| 4695 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4696 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4697 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 4698 | !info.inputConfig.test( |
| 4699 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4700 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4701 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4702 | } |
| 4703 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4704 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4705 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4706 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4707 | // Save the old windows' orientation by ID before it gets updated. |
| 4708 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4709 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4710 | oldWindowOrientations.emplace(handle->getId(), |
| 4711 | handle->getInfo()->transform.getOrientation()); |
| 4712 | } |
| 4713 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4714 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4715 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4716 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4717 | if (mLastHoverWindowHandle && |
| 4718 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4719 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4720 | mLastHoverWindowHandle = nullptr; |
| 4721 | } |
| 4722 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4723 | std::optional<FocusResolver::FocusChanges> changes = |
| 4724 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4725 | if (changes) { |
| 4726 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4727 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4728 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4729 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4730 | mTouchStatesByDisplay.find(displayId); |
| 4731 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4732 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4733 | for (size_t i = 0; i < state.windows.size();) { |
| 4734 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4735 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4736 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4737 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4738 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4739 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4740 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4741 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4742 | if (touchedInputChannel != nullptr) { |
| 4743 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4744 | "touched window was removed"); |
| 4745 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4746 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4747 | // well. |
| 4748 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4749 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 4750 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4751 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4752 | if (wallpaper != nullptr) { |
| 4753 | sp<Connection> wallpaperConnection = |
| 4754 | getConnectionLocked(wallpaper->getToken()); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4755 | if (wallpaperConnection != nullptr) { |
| 4756 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4757 | options); |
| 4758 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4759 | } |
| 4760 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4761 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4762 | state.windows.erase(state.windows.begin() + i); |
| 4763 | } else { |
| 4764 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4765 | } |
| 4766 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4767 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4768 | // 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] | 4769 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4770 | if (mDragState && |
| 4771 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4772 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4773 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4774 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4775 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4776 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4777 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4778 | // pointer events if necessary. |
| 4779 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4780 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4781 | if (newWindowHandle != nullptr && |
| 4782 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4783 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4784 | std::shared_ptr<InputChannel> inputChannel = |
| 4785 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4786 | if (inputChannel != nullptr) { |
| 4787 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4788 | "touched window's orientation changed"); |
| 4789 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4790 | } |
| 4791 | } |
| 4792 | } |
| 4793 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4794 | // Release information for windows that are no longer present. |
| 4795 | // This ensures that unused input channels are released promptly. |
| 4796 | // Otherwise, they might stick around until the window handle is destroyed |
| 4797 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4798 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4799 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4800 | if (DEBUG_FOCUS) { |
| 4801 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4802 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4803 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4804 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4805 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4809 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4810 | if (DEBUG_FOCUS) { |
| 4811 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4812 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4813 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4814 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4815 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4816 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4817 | } // release lock |
| 4818 | |
| 4819 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4820 | mLooper->wake(); |
| 4821 | } |
| 4822 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4823 | void InputDispatcher::setFocusedApplicationLocked( |
| 4824 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4825 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4826 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4827 | |
| 4828 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4829 | return; // This application is already focused. No need to wake up or change anything. |
| 4830 | } |
| 4831 | |
| 4832 | // Set the new application handle. |
| 4833 | if (inputApplicationHandle != nullptr) { |
| 4834 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4835 | } else { |
| 4836 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4837 | } |
| 4838 | |
| 4839 | // No matter what the old focused application was, stop waiting on it because it is |
| 4840 | // no longer focused. |
| 4841 | resetNoFocusedWindowTimeoutLocked(); |
| 4842 | } |
| 4843 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4844 | /** |
| 4845 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4846 | * the display not specified. |
| 4847 | * |
| 4848 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4849 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4850 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4851 | * display. The display-specified events won't be affected. |
| 4852 | */ |
| 4853 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4854 | if (DEBUG_FOCUS) { |
| 4855 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4856 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4857 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4858 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4859 | |
| 4860 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4861 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4862 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4863 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4864 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4865 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4866 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4867 | CancelationOptions |
| 4868 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4869 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4870 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4871 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4872 | } |
| 4873 | } |
| 4874 | mFocusedDisplayId = displayId; |
| 4875 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4876 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4877 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4878 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4879 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4880 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4881 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4882 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4883 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4884 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4885 | } |
| 4886 | } |
| 4887 | } |
| 4888 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4889 | if (DEBUG_FOCUS) { |
| 4890 | logDispatchStateLocked(); |
| 4891 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4892 | } // release lock |
| 4893 | |
| 4894 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4895 | mLooper->wake(); |
| 4896 | } |
| 4897 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4898 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4899 | if (DEBUG_FOCUS) { |
| 4900 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4901 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4902 | |
| 4903 | bool changed; |
| 4904 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4905 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4906 | |
| 4907 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4908 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4909 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | if (mDispatchEnabled && !enabled) { |
| 4913 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4914 | } |
| 4915 | |
| 4916 | mDispatchEnabled = enabled; |
| 4917 | mDispatchFrozen = frozen; |
| 4918 | changed = true; |
| 4919 | } else { |
| 4920 | changed = false; |
| 4921 | } |
| 4922 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4923 | if (DEBUG_FOCUS) { |
| 4924 | logDispatchStateLocked(); |
| 4925 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4926 | } // release lock |
| 4927 | |
| 4928 | if (changed) { |
| 4929 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4930 | mLooper->wake(); |
| 4931 | } |
| 4932 | } |
| 4933 | |
| 4934 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4935 | if (DEBUG_FOCUS) { |
| 4936 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4937 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4938 | |
| 4939 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4940 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4941 | |
| 4942 | if (mInputFilterEnabled == enabled) { |
| 4943 | return; |
| 4944 | } |
| 4945 | |
| 4946 | mInputFilterEnabled = enabled; |
| 4947 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4948 | } // release lock |
| 4949 | |
| 4950 | // Wake up poll loop since there might be work to do to drop everything. |
| 4951 | mLooper->wake(); |
| 4952 | } |
| 4953 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4954 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, |
| 4955 | bool hasPermission) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4956 | bool needWake = false; |
| 4957 | { |
| 4958 | std::scoped_lock lock(mLock); |
| 4959 | if (mInTouchMode == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4960 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4961 | } |
| 4962 | if (DEBUG_TOUCH_MODE) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4963 | ALOGD("Request to change touch mode from %s to %s (calling pid=%d, uid=%d, " |
| 4964 | "hasPermission=%s)", |
| 4965 | toString(mInTouchMode), toString(inTouchMode), pid, uid, toString(hasPermission)); |
| 4966 | } |
| 4967 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 4968 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 4969 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
| 4970 | ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " |
| 4971 | "window nor none of the previously interacted window", |
| 4972 | pid, uid); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4973 | return false; |
| 4974 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4975 | } |
| 4976 | |
| 4977 | // TODO(b/198499018): Store touch mode per display. |
| 4978 | mInTouchMode = inTouchMode; |
| 4979 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4980 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 4981 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4982 | } // release lock |
| 4983 | |
| 4984 | if (needWake) { |
| 4985 | mLooper->wake(); |
| 4986 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4987 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4988 | } |
| 4989 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 4990 | bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { |
| 4991 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 4992 | if (focusedToken == nullptr) { |
| 4993 | return false; |
| 4994 | } |
| 4995 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 4996 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 4997 | } |
| 4998 | |
| 4999 | bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { |
| 5000 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5001 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5002 | const sp<WindowInfoHandle> windowHandle = |
| 5003 | getWindowHandleLocked(connectionToken); |
| 5004 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5005 | }) != mInteractionConnectionTokens.end(); |
| 5006 | } |
| 5007 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5008 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5009 | if (opacity < 0 || opacity > 1) { |
| 5010 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5011 | return; |
| 5012 | } |
| 5013 | |
| 5014 | std::scoped_lock lock(mLock); |
| 5015 | mMaximumObscuringOpacityForTouch = opacity; |
| 5016 | } |
| 5017 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5018 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 5019 | const sp<IBinder>& token) { |
| 5020 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5021 | for (TouchedWindow& w : state.windows) { |
| 5022 | if (w.windowHandle->getToken() == token) { |
| 5023 | return std::make_pair(&state, &w); |
| 5024 | } |
| 5025 | } |
| 5026 | } |
| 5027 | return std::make_pair(nullptr, nullptr); |
| 5028 | } |
| 5029 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5030 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5031 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5032 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5033 | if (DEBUG_FOCUS) { |
| 5034 | ALOGD("Trivial transfer to same window."); |
| 5035 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5036 | return true; |
| 5037 | } |
| 5038 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5039 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5040 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5041 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5042 | // Find the target touch state and touched window by fromToken. |
| 5043 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 5044 | if (state == nullptr || touchedWindow == nullptr) { |
| 5045 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5046 | return false; |
| 5047 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5048 | |
| 5049 | const int32_t displayId = state->displayId; |
| 5050 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5051 | if (toWindowHandle == nullptr) { |
| 5052 | ALOGW("Cannot transfer focus because to window not found."); |
| 5053 | return false; |
| 5054 | } |
| 5055 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5056 | if (DEBUG_FOCUS) { |
| 5057 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5058 | touchedWindow->windowHandle->getName().c_str(), |
| 5059 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5060 | } |
| 5061 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5062 | // Erase old window. |
| 5063 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 5064 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 5065 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5066 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5067 | // Add new window. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5068 | int32_t newTargetFlags = |
| 5069 | oldTargetFlags & (InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5070 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
| 5071 | newTargetFlags |= InputTarget::FLAG_FOREGROUND; |
| 5072 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5073 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5074 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5075 | // Store the dragging window. |
| 5076 | if (isDragDrop) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5077 | if (pointerIds.count() > 1) { |
| 5078 | ALOGW("The drag and drop cannot be started when there is more than 1 pointer on the" |
| 5079 | " window."); |
| 5080 | return false; |
| 5081 | } |
| 5082 | // If the window didn't not support split or the source is mouse, the pointerIds count |
| 5083 | // would be 0, so we have to track the pointer 0. |
| 5084 | const int32_t id = pointerIds.count() == 0 ? 0 : pointerIds.firstMarkedBit(); |
| 5085 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5086 | } |
| 5087 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5088 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5089 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5090 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5091 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5092 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5093 | CancelationOptions |
| 5094 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5095 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5096 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5097 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5098 | } |
| 5099 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5100 | if (DEBUG_FOCUS) { |
| 5101 | logDispatchStateLocked(); |
| 5102 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5103 | } // release lock |
| 5104 | |
| 5105 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5106 | mLooper->wake(); |
| 5107 | return true; |
| 5108 | } |
| 5109 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5110 | /** |
| 5111 | * Get the touched foreground window on the given display. |
| 5112 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5113 | * window is being touched. |
| 5114 | */ |
| 5115 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5116 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5117 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5118 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5119 | return nullptr; |
| 5120 | } |
| 5121 | |
| 5122 | const TouchState& state = stateIt->second; |
| 5123 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5124 | // If multiple foreground windows are touched, return nullptr |
| 5125 | for (const TouchedWindow& window : state.windows) { |
| 5126 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 5127 | if (touchedForegroundWindow != nullptr) { |
| 5128 | ALOGI("Two or more foreground windows: %s and %s", |
| 5129 | touchedForegroundWindow->getName().c_str(), |
| 5130 | window.windowHandle->getName().c_str()); |
| 5131 | return nullptr; |
| 5132 | } |
| 5133 | touchedForegroundWindow = window.windowHandle; |
| 5134 | } |
| 5135 | } |
| 5136 | return touchedForegroundWindow; |
| 5137 | } |
| 5138 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5139 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5140 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5141 | sp<IBinder> fromToken; |
| 5142 | { // acquire lock |
| 5143 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5144 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5145 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5146 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5147 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5148 | return false; |
| 5149 | } |
| 5150 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5151 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5152 | if (from == nullptr) { |
| 5153 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5154 | return false; |
| 5155 | } |
| 5156 | |
| 5157 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5158 | } // release lock |
| 5159 | |
| 5160 | return transferTouchFocus(fromToken, destChannelToken); |
| 5161 | } |
| 5162 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5163 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5164 | if (DEBUG_FOCUS) { |
| 5165 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5166 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5167 | |
| 5168 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5169 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5170 | |
| 5171 | resetKeyRepeatLocked(); |
| 5172 | releasePendingEventLocked(); |
| 5173 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5174 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5175 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5176 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5177 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5178 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5179 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5183 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5184 | dumpDispatchStateLocked(dump); |
| 5185 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5186 | std::istringstream stream(dump); |
| 5187 | std::string line; |
| 5188 | |
| 5189 | while (std::getline(stream, line, '\n')) { |
| 5190 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5191 | } |
| 5192 | } |
| 5193 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5194 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5195 | std::string dump; |
| 5196 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5197 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5198 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5199 | |
| 5200 | std::string windowName = "None"; |
| 5201 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5202 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5203 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5204 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5205 | : "token has capture without window"; |
| 5206 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5207 | 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] | 5208 | |
| 5209 | return dump; |
| 5210 | } |
| 5211 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5212 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5213 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5214 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5215 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5216 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5217 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5218 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5219 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5220 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5221 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5222 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5223 | const std::chrono::duration timeout = |
| 5224 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5225 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5226 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5227 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5228 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5229 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5230 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5231 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5232 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5233 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5234 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5235 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5236 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5237 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5238 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5239 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5240 | 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] | 5241 | state.displayId, toString(state.down), toString(state.split), |
| 5242 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5243 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5244 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5245 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5246 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5247 | dump += StringPrintf(INDENT4 |
| 5248 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5249 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5250 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5251 | } |
| 5252 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5253 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5254 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5255 | } |
| 5256 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5257 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5258 | } |
| 5259 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5260 | if (mDragState) { |
| 5261 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5262 | mDragState->dump(dump, INDENT2); |
| 5263 | } |
| 5264 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5265 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5266 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5267 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5268 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5269 | const auto& displayInfo = it->second; |
| 5270 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5271 | displayInfo.logicalHeight); |
| 5272 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5273 | } else { |
| 5274 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5275 | } |
| 5276 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5277 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5278 | dump += INDENT2 "Windows:\n"; |
| 5279 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5280 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5281 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5282 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5283 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5284 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5285 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5286 | "applicationInfo.name=%s, " |
| 5287 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5288 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5289 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5290 | windowInfo->displayId, |
| 5291 | windowInfo->inputConfig.string().c_str(), |
| 5292 | windowInfo->alpha, windowInfo->frameLeft, |
| 5293 | windowInfo->frameTop, windowInfo->frameRight, |
| 5294 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5295 | windowInfo->applicationInfo.name.c_str(), |
| 5296 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5297 | dump += dumpRegion(windowInfo->touchableRegion); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5298 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5299 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5300 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5301 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5302 | millis(windowInfo->dispatchingTimeout), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5303 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5304 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5305 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5306 | } |
| 5307 | } else { |
| 5308 | dump += INDENT2 "Windows: <none>\n"; |
| 5309 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5310 | } |
| 5311 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5312 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5313 | } |
| 5314 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5315 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5316 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5317 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5318 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5319 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5320 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5321 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5322 | } |
| 5323 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5324 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5325 | |
| 5326 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5327 | if (!mRecentQueue.empty()) { |
| 5328 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5329 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5330 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5331 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5332 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5333 | } |
| 5334 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5335 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | // Dump event currently being dispatched. |
| 5339 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5340 | dump += INDENT "PendingEvent:\n"; |
| 5341 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5342 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5343 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5344 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5345 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5346 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5350 | if (!mInboundQueue.empty()) { |
| 5351 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5352 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5353 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5354 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5355 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5356 | } |
| 5357 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5358 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5359 | } |
| 5360 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5361 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5362 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5363 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5364 | const KeyReplacement& replacement = pair.first; |
| 5365 | int32_t newKeyCode = pair.second; |
| 5366 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5367 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5368 | } |
| 5369 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5370 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5371 | } |
| 5372 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5373 | if (!mCommandQueue.empty()) { |
| 5374 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5375 | } else { |
| 5376 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5377 | } |
| 5378 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5379 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5380 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5381 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5382 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5383 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5384 | connection->inputChannel->getFd().get(), |
| 5385 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5386 | connection->getWindowName().c_str(), |
| 5387 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5388 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5389 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5390 | if (!connection->outboundQueue.empty()) { |
| 5391 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5392 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5393 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5394 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5395 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5396 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5397 | } |
| 5398 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5399 | if (!connection->waitQueue.empty()) { |
| 5400 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5401 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5402 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5403 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5404 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5405 | } |
| 5406 | } |
| 5407 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5408 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5409 | } |
| 5410 | |
| 5411 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5412 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5413 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5414 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5415 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5416 | } |
| 5417 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5418 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5419 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5420 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5421 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5422 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5423 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5424 | } |
| 5425 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5426 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5427 | const size_t numMonitors = monitors.size(); |
| 5428 | for (size_t i = 0; i < numMonitors; i++) { |
| 5429 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5430 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5431 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5432 | dump += "\n"; |
| 5433 | } |
| 5434 | } |
| 5435 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5436 | class LooperEventCallback : public LooperCallback { |
| 5437 | public: |
| 5438 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5439 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5440 | |
| 5441 | private: |
| 5442 | std::function<int(int events)> mCallback; |
| 5443 | }; |
| 5444 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5445 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5446 | if (DEBUG_CHANNEL_CREATION) { |
| 5447 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5448 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5449 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5450 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5451 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5452 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5453 | |
| 5454 | if (result) { |
| 5455 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5456 | } |
| 5457 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5458 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5459 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5460 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5461 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5462 | sp<Connection> connection = |
| 5463 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5464 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5465 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5466 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5467 | } |
| 5468 | mConnectionsByToken.emplace(token, connection); |
| 5469 | |
| 5470 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5471 | this, std::placeholders::_1, token); |
| 5472 | |
| 5473 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5474 | } // release lock |
| 5475 | |
| 5476 | // Wake the looper because some connections have changed. |
| 5477 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5478 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5479 | } |
| 5480 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5481 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5482 | const std::string& name, |
| 5483 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5484 | std::shared_ptr<InputChannel> serverChannel; |
| 5485 | std::unique_ptr<InputChannel> clientChannel; |
| 5486 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5487 | if (result) { |
| 5488 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5489 | } |
| 5490 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5491 | { // acquire lock |
| 5492 | std::scoped_lock _l(mLock); |
| 5493 | |
| 5494 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5495 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5496 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5499 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5500 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5501 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5502 | |
| 5503 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5504 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5505 | } |
| 5506 | mConnectionsByToken.emplace(token, connection); |
| 5507 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5508 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5509 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5510 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5511 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5512 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5513 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5514 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5515 | // Wake the looper because some connections have changed. |
| 5516 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5517 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5518 | } |
| 5519 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5520 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5521 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5522 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5523 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5524 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5525 | if (status) { |
| 5526 | return status; |
| 5527 | } |
| 5528 | } // release lock |
| 5529 | |
| 5530 | // Wake the poll loop because removing the connection may have changed the current |
| 5531 | // synchronization state. |
| 5532 | mLooper->wake(); |
| 5533 | return OK; |
| 5534 | } |
| 5535 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5536 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5537 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5538 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5539 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5540 | // 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] | 5541 | return BAD_VALUE; |
| 5542 | } |
| 5543 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5544 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5545 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5546 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5547 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5548 | } |
| 5549 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5550 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5551 | |
| 5552 | nsecs_t currentTime = now(); |
| 5553 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5554 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5555 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5556 | return OK; |
| 5557 | } |
| 5558 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5559 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5560 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5561 | auto& [displayId, monitors] = *it; |
| 5562 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5563 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5564 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5565 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5566 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5567 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5568 | } else { |
| 5569 | ++it; |
| 5570 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5571 | } |
| 5572 | } |
| 5573 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5574 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5575 | std::scoped_lock _l(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5576 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5577 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5578 | if (!requestingChannel) { |
| 5579 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5580 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5581 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5582 | |
| 5583 | auto [statePtr, windowPtr] = findTouchStateAndWindowLocked(token); |
| 5584 | if (statePtr == nullptr || windowPtr == nullptr || !statePtr->down) { |
| 5585 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5586 | " Ignoring."); |
| 5587 | return BAD_VALUE; |
| 5588 | } |
| 5589 | |
| 5590 | TouchState& state = *statePtr; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame^] | 5591 | TouchedWindow& window = *windowPtr; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5592 | // Send cancel events to all the input channels we're stealing from. |
| 5593 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5594 | "input channel stole pointer stream"); |
| 5595 | options.deviceId = state.deviceId; |
| 5596 | options.displayId = state.displayId; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame^] | 5597 | if (state.split) { |
| 5598 | // If split pointers then selectively cancel pointers otherwise cancel all pointers |
| 5599 | options.pointerIds = window.pointerIds; |
| 5600 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5601 | std::string canceledWindows; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame^] | 5602 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5603 | const std::shared_ptr<InputChannel> channel = |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame^] | 5604 | getInputChannelLocked(w.windowHandle->getToken()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5605 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5606 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5607 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5608 | canceledWindows += channel->getName(); |
| 5609 | } |
| 5610 | } |
| 5611 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5612 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5613 | canceledWindows.c_str()); |
| 5614 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5615 | // Prevent the gesture from being sent to any other windows. |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame^] | 5616 | // This only blocks relevant pointers to be sent to other windows |
| 5617 | window.isPilferingPointers = true; |
| 5618 | |
| 5619 | if (state.split) { |
| 5620 | state.cancelPointersForWindowsExcept(window.pointerIds, token); |
| 5621 | } else { |
| 5622 | state.filterWindowsExcept(token); |
| 5623 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5624 | return OK; |
| 5625 | } |
| 5626 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5627 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5628 | { // acquire lock |
| 5629 | std::scoped_lock _l(mLock); |
| 5630 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5631 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5632 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5633 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5634 | : "token without window"); |
| 5635 | } |
| 5636 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5637 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5638 | if (focusedToken != windowToken) { |
| 5639 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5640 | enabled ? "enable" : "disable"); |
| 5641 | return; |
| 5642 | } |
| 5643 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5644 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5645 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5646 | "window has %s requested pointer capture.", |
| 5647 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5648 | return; |
| 5649 | } |
| 5650 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5651 | if (enabled) { |
| 5652 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5653 | mIneligibleDisplaysForPointerCapture.end(), |
| 5654 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5655 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5656 | return; |
| 5657 | } |
| 5658 | } |
| 5659 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5660 | setPointerCaptureLocked(enabled); |
| 5661 | } // release lock |
| 5662 | |
| 5663 | // Wake the thread to process command entries. |
| 5664 | mLooper->wake(); |
| 5665 | } |
| 5666 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5667 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5668 | { // acquire lock |
| 5669 | std::scoped_lock _l(mLock); |
| 5670 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5671 | if (!isEligible) { |
| 5672 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5673 | } |
| 5674 | } // release lock |
| 5675 | } |
| 5676 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5677 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5678 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5679 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5680 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5681 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5682 | } |
| 5683 | } |
| 5684 | } |
| 5685 | return std::nullopt; |
| 5686 | } |
| 5687 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5688 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5689 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5690 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5691 | } |
| 5692 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5693 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5694 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5695 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5696 | } |
| 5697 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5698 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5699 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5700 | } |
| 5701 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5702 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5703 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5704 | if (connection == nullptr) { |
| 5705 | return "<nullptr>"; |
| 5706 | } |
| 5707 | return connection->getInputChannelName(); |
| 5708 | } |
| 5709 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5710 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5711 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5712 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5713 | } |
| 5714 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5715 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5716 | const sp<Connection>& connection, uint32_t seq, |
| 5717 | bool handled, nsecs_t consumeTime) { |
| 5718 | // Handle post-event policy actions. |
| 5719 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5720 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5721 | return; |
| 5722 | } |
| 5723 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5724 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5725 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5726 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5727 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5728 | } |
| 5729 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5730 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5731 | connection->inputChannel->getConnectionToken(), |
| 5732 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5733 | } |
| 5734 | |
| 5735 | bool restartEvent; |
| 5736 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5737 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5738 | restartEvent = |
| 5739 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5740 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5741 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5742 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5743 | handled); |
| 5744 | } else { |
| 5745 | restartEvent = false; |
| 5746 | } |
| 5747 | |
| 5748 | // Dequeue the event and start the next cycle. |
| 5749 | // Because the lock might have been released, it is possible that the |
| 5750 | // contents of the wait queue to have been drained, so we need to double-check |
| 5751 | // a few things. |
| 5752 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5753 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5754 | dispatchEntry = *dispatchEntryIt; |
| 5755 | connection->waitQueue.erase(dispatchEntryIt); |
| 5756 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5757 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5758 | if (!connection->responsive) { |
| 5759 | connection->responsive = isConnectionResponsive(*connection); |
| 5760 | if (connection->responsive) { |
| 5761 | // The connection was unresponsive, and now it's responsive. |
| 5762 | processConnectionResponsiveLocked(*connection); |
| 5763 | } |
| 5764 | } |
| 5765 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5766 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5767 | connection->outboundQueue.push_front(dispatchEntry); |
| 5768 | traceOutboundQueueLength(*connection); |
| 5769 | } else { |
| 5770 | releaseDispatchEntry(dispatchEntry); |
| 5771 | } |
| 5772 | } |
| 5773 | |
| 5774 | // Start the next dispatch cycle for this connection. |
| 5775 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5776 | } |
| 5777 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5778 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5779 | const sp<IBinder>& newToken) { |
| 5780 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5781 | scoped_unlock unlock(mLock); |
| 5782 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5783 | }; |
| 5784 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5785 | } |
| 5786 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5787 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5788 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5789 | scoped_unlock unlock(mLock); |
| 5790 | mPolicy->notifyDropWindow(token, x, y); |
| 5791 | }; |
| 5792 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5793 | } |
| 5794 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5795 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5796 | if (connection == nullptr) { |
| 5797 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5798 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5799 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5800 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5801 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5802 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5803 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5804 | return; |
| 5805 | } |
| 5806 | /** |
| 5807 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5808 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5809 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5810 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5811 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5812 | * most useful. |
| 5813 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5814 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5815 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5816 | std::string reason = |
| 5817 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5818 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5819 | ns2ms(currentWait), |
| 5820 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5821 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5822 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5823 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5824 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5825 | |
| 5826 | // Stop waking up for events on this connection, it is already unresponsive |
| 5827 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5828 | } |
| 5829 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5830 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5831 | std::string reason = |
| 5832 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5833 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5834 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5835 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5836 | scoped_unlock unlock(mLock); |
| 5837 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5838 | }; |
| 5839 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5840 | } |
| 5841 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5842 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5843 | const std::string& reason) { |
| 5844 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5845 | updateLastAnrStateLocked(windowLabel, reason); |
| 5846 | } |
| 5847 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5848 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5849 | const std::string& reason) { |
| 5850 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5851 | updateLastAnrStateLocked(windowLabel, reason); |
| 5852 | } |
| 5853 | |
| 5854 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5855 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5856 | // 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] | 5857 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5858 | struct tm tm; |
| 5859 | localtime_r(&t, &tm); |
| 5860 | char timestr[64]; |
| 5861 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5862 | mLastAnrState.clear(); |
| 5863 | mLastAnrState += INDENT "ANR:\n"; |
| 5864 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5865 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5866 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5867 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5868 | } |
| 5869 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5870 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5871 | KeyEntry& entry) { |
| 5872 | const KeyEvent event = createKeyEvent(entry); |
| 5873 | nsecs_t delay = 0; |
| 5874 | { // release lock |
| 5875 | scoped_unlock unlock(mLock); |
| 5876 | android::base::Timer t; |
| 5877 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5878 | entry.policyFlags); |
| 5879 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5880 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5881 | std::to_string(t.duration().count()).c_str()); |
| 5882 | } |
| 5883 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5884 | |
| 5885 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5886 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5887 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5888 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5889 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5890 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5891 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5892 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5893 | } |
| 5894 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5895 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5896 | std::optional<int32_t> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5897 | std::string reason) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5898 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5899 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5900 | mPolicy->notifyWindowUnresponsive(token, pid, reason); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5901 | }; |
| 5902 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5903 | } |
| 5904 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5905 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
| 5906 | std::optional<int32_t> pid) { |
| 5907 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5908 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5909 | mPolicy->notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5910 | }; |
| 5911 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5912 | } |
| 5913 | |
| 5914 | /** |
| 5915 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5916 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5917 | * command entry to the command queue. |
| 5918 | */ |
| 5919 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5920 | std::string reason) { |
| 5921 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5922 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5923 | if (connection.monitor) { |
| 5924 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5925 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5926 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 5927 | } else { |
| 5928 | // The connection is a window |
| 5929 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5930 | reason.c_str()); |
| 5931 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 5932 | if (handle != nullptr) { |
| 5933 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5934 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5935 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5936 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5937 | } |
| 5938 | |
| 5939 | /** |
| 5940 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5941 | */ |
| 5942 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5943 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5944 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5945 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5946 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 5947 | } else { |
| 5948 | // The connection is a window |
| 5949 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 5950 | if (handle != nullptr) { |
| 5951 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5952 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5953 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5954 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5955 | } |
| 5956 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5957 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5958 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5959 | KeyEntry& keyEntry, bool handled) { |
| 5960 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5961 | if (!handled) { |
| 5962 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5963 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5964 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5965 | return false; |
| 5966 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5967 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5968 | // Get the fallback key state. |
| 5969 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5970 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5971 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5972 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5973 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5974 | } |
| 5975 | |
| 5976 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5977 | // If the application handles the original key for which we previously |
| 5978 | // generated a fallback or if the window is not a foreground window, |
| 5979 | // then cancel the associated fallback key, if any. |
| 5980 | if (fallbackKeyCode != -1) { |
| 5981 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5982 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5983 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 5984 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5985 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 5986 | keyEntry.policyFlags); |
| 5987 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5988 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5989 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5990 | |
| 5991 | mLock.unlock(); |
| 5992 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5993 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5994 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5995 | |
| 5996 | mLock.lock(); |
| 5997 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5998 | // Cancel the fallback key. |
| 5999 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6000 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6001 | "application handled the original non-fallback key " |
| 6002 | "or is no longer a foreground target, " |
| 6003 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6004 | options.keyCode = fallbackKeyCode; |
| 6005 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6006 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6007 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6008 | } |
| 6009 | } else { |
| 6010 | // If the application did not handle a non-fallback key, first check |
| 6011 | // that we are in a good state to perform unhandled key event processing |
| 6012 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6013 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6014 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6015 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6016 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6017 | "since this is not an initial down. " |
| 6018 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6019 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6020 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6021 | return false; |
| 6022 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6023 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6024 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6025 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6026 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6027 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6028 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6029 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6030 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6031 | |
| 6032 | mLock.unlock(); |
| 6033 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6034 | bool fallback = |
| 6035 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6036 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6037 | |
| 6038 | mLock.lock(); |
| 6039 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6040 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6041 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6042 | return false; |
| 6043 | } |
| 6044 | |
| 6045 | // Latch the fallback keycode for this key on an initial down. |
| 6046 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6047 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6048 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6049 | fallbackKeyCode = event.getKeyCode(); |
| 6050 | } else { |
| 6051 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6052 | } |
| 6053 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6054 | } |
| 6055 | |
| 6056 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6057 | |
| 6058 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6059 | // We will continue to dispatch the key to the policy but we will no |
| 6060 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6061 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6062 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6063 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6064 | if (fallback) { |
| 6065 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6066 | "as a fallback for %d, but on the DOWN it had requested " |
| 6067 | "to send %d instead. Fallback canceled.", |
| 6068 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6069 | } else { |
| 6070 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6071 | "but on the DOWN it had requested to send %d. " |
| 6072 | "Fallback canceled.", |
| 6073 | originalKeyCode, fallbackKeyCode); |
| 6074 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6075 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6076 | |
| 6077 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6078 | "canceling fallback, policy no longer desires it"); |
| 6079 | options.keyCode = fallbackKeyCode; |
| 6080 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6081 | |
| 6082 | fallback = false; |
| 6083 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6084 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6085 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6086 | } |
| 6087 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6088 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6089 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6090 | { |
| 6091 | std::string msg; |
| 6092 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6093 | connection->inputState.getFallbackKeys(); |
| 6094 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6095 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6096 | } |
| 6097 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6098 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6099 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6100 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6101 | |
| 6102 | if (fallback) { |
| 6103 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6104 | keyEntry.eventTime = event.getEventTime(); |
| 6105 | keyEntry.deviceId = event.getDeviceId(); |
| 6106 | keyEntry.source = event.getSource(); |
| 6107 | keyEntry.displayId = event.getDisplayId(); |
| 6108 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6109 | keyEntry.keyCode = fallbackKeyCode; |
| 6110 | keyEntry.scanCode = event.getScanCode(); |
| 6111 | keyEntry.metaState = event.getMetaState(); |
| 6112 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6113 | keyEntry.downTime = event.getDownTime(); |
| 6114 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6115 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6116 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6117 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6118 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6119 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6120 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6121 | return true; // restart the event |
| 6122 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6123 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6124 | ALOGD("Unhandled key event: No fallback key."); |
| 6125 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6126 | |
| 6127 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6128 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6129 | } |
| 6130 | } |
| 6131 | return false; |
| 6132 | } |
| 6133 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6134 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6135 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6136 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6137 | return false; |
| 6138 | } |
| 6139 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6140 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6141 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6142 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6143 | } |
| 6144 | } |
| 6145 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6146 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6147 | if (ATRACE_ENABLED()) { |
| 6148 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6149 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6150 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6151 | } |
| 6152 | } |
| 6153 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6154 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6155 | if (ATRACE_ENABLED()) { |
| 6156 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6157 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6158 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6159 | } |
| 6160 | } |
| 6161 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6162 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6163 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6164 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6165 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6166 | dumpDispatchStateLocked(dump); |
| 6167 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6168 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6169 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6170 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6171 | } |
| 6172 | } |
| 6173 | |
| 6174 | void InputDispatcher::monitor() { |
| 6175 | // 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] | 6176 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6177 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6178 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6179 | } |
| 6180 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6181 | /** |
| 6182 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6183 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6184 | * this method can be safely called from any thread, as long as you've ensured that |
| 6185 | * the work you are interested in completing has already been queued. |
| 6186 | */ |
| 6187 | bool InputDispatcher::waitForIdle() { |
| 6188 | /** |
| 6189 | * Timeout should represent the longest possible time that a device might spend processing |
| 6190 | * events and commands. |
| 6191 | */ |
| 6192 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6193 | std::unique_lock lock(mLock); |
| 6194 | mLooper->wake(); |
| 6195 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6196 | return result == std::cv_status::no_timeout; |
| 6197 | } |
| 6198 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6199 | /** |
| 6200 | * Sets focus to the window identified by the token. This must be called |
| 6201 | * after updating any input window handles. |
| 6202 | * |
| 6203 | * Params: |
| 6204 | * request.token - input channel token used to identify the window that should gain focus. |
| 6205 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6206 | * specified token does not match the currently focused window, this request will be dropped. |
| 6207 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6208 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6209 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6210 | * when requesting the focus change. This determines which request gets |
| 6211 | * precedence if there is a focus change request from another source such as pointer down. |
| 6212 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6213 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6214 | { // acquire lock |
| 6215 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6216 | std::optional<FocusResolver::FocusChanges> changes = |
| 6217 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6218 | if (changes) { |
| 6219 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6220 | } |
| 6221 | } // release lock |
| 6222 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6223 | mLooper->wake(); |
| 6224 | } |
| 6225 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6226 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6227 | if (changes.oldFocus) { |
| 6228 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6229 | if (focusedInputChannel) { |
| 6230 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6231 | "focus left window"); |
| 6232 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6233 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6234 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6235 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6236 | if (changes.newFocus) { |
| 6237 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6238 | } |
| 6239 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6240 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6241 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6242 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6243 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6244 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6245 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6246 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6247 | // front. |
| 6248 | disablePointerCaptureForcedLocked(); |
| 6249 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6250 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6251 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6252 | } |
| 6253 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6254 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6255 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6256 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6257 | return; |
| 6258 | } |
| 6259 | |
| 6260 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6261 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6262 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6263 | setPointerCaptureLocked(false); |
| 6264 | } |
| 6265 | |
| 6266 | if (!mWindowTokenWithPointerCapture) { |
| 6267 | // No need to send capture changes because no window has capture. |
| 6268 | return; |
| 6269 | } |
| 6270 | |
| 6271 | if (mPendingEvent != nullptr) { |
| 6272 | // Move the pending event to the front of the queue. This will give the chance |
| 6273 | // for the pending event to be dropped if it is a captured event. |
| 6274 | mInboundQueue.push_front(mPendingEvent); |
| 6275 | mPendingEvent = nullptr; |
| 6276 | } |
| 6277 | |
| 6278 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6279 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6280 | mInboundQueue.push_front(std::move(entry)); |
| 6281 | } |
| 6282 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6283 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6284 | mCurrentPointerCaptureRequest.enable = enable; |
| 6285 | mCurrentPointerCaptureRequest.seq++; |
| 6286 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6287 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6288 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6289 | }; |
| 6290 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6291 | } |
| 6292 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6293 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6294 | { // acquire lock |
| 6295 | std::scoped_lock _l(mLock); |
| 6296 | // Set an empty list to remove all handles from the specific display. |
| 6297 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6298 | setFocusedApplicationLocked(displayId, nullptr); |
| 6299 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6300 | // have been removed for the removed display. |
| 6301 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6302 | // Reset pointer capture eligibility, regardless of previous state. |
| 6303 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6304 | } // release lock |
| 6305 | |
| 6306 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6307 | mLooper->wake(); |
| 6308 | } |
| 6309 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6310 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6311 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6312 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6313 | // more convenient parsing. |
| 6314 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6315 | for (const auto& info : windowInfos) { |
| 6316 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6317 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6318 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6319 | |
| 6320 | { // acquire lock |
| 6321 | std::scoped_lock _l(mLock); |
| 6322 | mDisplayInfos.clear(); |
| 6323 | for (const auto& displayInfo : displayInfos) { |
| 6324 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6325 | } |
| 6326 | |
| 6327 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6328 | setInputWindowsLocked(handles, displayId); |
| 6329 | } |
| 6330 | } |
| 6331 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6332 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6333 | } |
| 6334 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6335 | bool InputDispatcher::shouldDropInput( |
| 6336 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6337 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6338 | (windowHandle->getInfo()->inputConfig.test( |
| 6339 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6340 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6341 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6342 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6343 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6344 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6345 | windowHandle->getInfo()->displayId); |
| 6346 | return true; |
| 6347 | } |
| 6348 | return false; |
| 6349 | } |
| 6350 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6351 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6352 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6353 | const std::vector<DisplayInfo>& displayInfos) { |
| 6354 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6355 | } |
| 6356 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6357 | void InputDispatcher::cancelCurrentTouch() { |
| 6358 | { |
| 6359 | std::scoped_lock _l(mLock); |
| 6360 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
| 6361 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 6362 | "cancel current touch"); |
| 6363 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6364 | |
| 6365 | mTouchStatesByDisplay.clear(); |
| 6366 | mLastHoverWindowHandle.clear(); |
| 6367 | } |
| 6368 | // Wake up poll loop since there might be work to do. |
| 6369 | mLooper->wake(); |
| 6370 | } |
| 6371 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6372 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6373 | std::scoped_lock _l(mLock); |
| 6374 | mMonitorDispatchingTimeout = timeout; |
| 6375 | } |
| 6376 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6377 | } // namespace android::inputdispatcher |