Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "InputDispatcher" |
| 18 | #define ATRACE_TAG ATRACE_TAG_INPUT |
| 19 | |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 20 | #define LOG_NDEBUG 1 |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 22 | #include <android-base/chrono_utils.h> |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 23 | #include <android-base/properties.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 25 | #include <android/os/IInputConstants.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 26 | #include <binder/Binder.h> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 27 | #include <ftl/enum.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 28 | #if defined(__ANDROID__) |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 29 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 30 | #endif |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 31 | #include <input/InputDevice.h> |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 32 | #include <input/PrintTools.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 33 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 34 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 35 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 36 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 37 | #include <cerrno> |
| 38 | #include <cinttypes> |
| 39 | #include <climits> |
| 40 | #include <cstddef> |
| 41 | #include <ctime> |
| 42 | #include <queue> |
| 43 | #include <sstream> |
| 44 | |
| 45 | #include "Connection.h" |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 46 | #include "DebugConfig.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 47 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 48 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 49 | #define INDENT " " |
| 50 | #define INDENT2 " " |
| 51 | #define INDENT3 " " |
| 52 | #define INDENT4 " " |
| 53 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 54 | using namespace android::ftl::flag_operators; |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 55 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 56 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 57 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 58 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 59 | using android::gui::FocusRequest; |
| 60 | using android::gui::TouchOcclusionMode; |
| 61 | using android::gui::WindowInfo; |
| 62 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 63 | using android::os::InputEventInjectionResult; |
| 64 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 65 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 66 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 67 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 68 | namespace { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 69 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 70 | // Named to match std::scoped_lock |
| 71 | class scoped_unlock { |
| 72 | public: |
| 73 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 74 | ~scoped_unlock() { mMutex.lock(); } |
| 75 | |
| 76 | private: |
| 77 | std::mutex& mMutex; |
| 78 | }; |
| 79 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 80 | // Default input dispatching timeout if there is no focused application or paused window |
| 81 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 82 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 83 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 84 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 85 | |
| 86 | // Amount of time to allow for all pending events to be processed when an app switch |
| 87 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 88 | // 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] | 89 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 90 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 91 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 93 | // 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] | 94 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 95 | |
| 96 | // Log a warning when an interception call takes longer than this to process. |
| 97 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 98 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 99 | // Additional key latency in case a connection is still processing some motion events. |
| 100 | // This will help with the case when a user touched a button that opens a new window, |
| 101 | // and gives us the chance to dispatch the key to this new window. |
| 102 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 103 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 104 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 105 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 106 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 107 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 108 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 109 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 110 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 111 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 112 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 113 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 114 | } |
| 115 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 116 | inline const char* toString(bool value) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 117 | return value ? "true" : "false"; |
| 118 | } |
| 119 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 120 | inline const std::string toString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 121 | if (binder == nullptr) { |
| 122 | return "<null>"; |
| 123 | } |
| 124 | return StringPrintf("%p", binder.get()); |
| 125 | } |
| 126 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 127 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 128 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 129 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 132 | bool isValidKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 133 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 134 | case AKEY_EVENT_ACTION_DOWN: |
| 135 | case AKEY_EVENT_ACTION_UP: |
| 136 | return true; |
| 137 | default: |
| 138 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 142 | bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 143 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 144 | ALOGE("Key event has invalid action code 0x%x", action); |
| 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 150 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 151 | switch (MotionEvent::getActionMasked(action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 152 | case AMOTION_EVENT_ACTION_DOWN: |
| 153 | case AMOTION_EVENT_ACTION_UP: |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 154 | return pointerCount == 1; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 155 | case AMOTION_EVENT_ACTION_MOVE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 156 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 157 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 158 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 159 | return pointerCount >= 1; |
| 160 | case AMOTION_EVENT_ACTION_CANCEL: |
| 161 | case AMOTION_EVENT_ACTION_OUTSIDE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 162 | case AMOTION_EVENT_ACTION_SCROLL: |
| 163 | return true; |
| 164 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 165 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 166 | const int32_t index = MotionEvent::getActionIndex(action); |
| 167 | return index >= 0 && index < pointerCount && pointerCount > 1; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 168 | } |
| 169 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 170 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 171 | return actionButton != 0; |
| 172 | default: |
| 173 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 177 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 178 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 179 | } |
| 180 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 181 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 182 | const PointerProperties* pointerProperties) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 183 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 184 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 185 | return false; |
| 186 | } |
| 187 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 188 | 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] | 189 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 190 | return false; |
| 191 | } |
| 192 | BitSet32 pointerIdBits; |
| 193 | for (size_t i = 0; i < pointerCount; i++) { |
| 194 | int32_t id = pointerProperties[i].id; |
| 195 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 196 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 197 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 198 | return false; |
| 199 | } |
| 200 | if (pointerIdBits.hasBit(id)) { |
| 201 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 202 | return false; |
| 203 | } |
| 204 | pointerIdBits.markBit(id); |
| 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 209 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 210 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 211 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 214 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 215 | bool first = true; |
| 216 | Region::const_iterator cur = region.begin(); |
| 217 | Region::const_iterator const tail = region.end(); |
| 218 | while (cur != tail) { |
| 219 | if (first) { |
| 220 | first = false; |
| 221 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 222 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 223 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 224 | 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] | 225 | cur++; |
| 226 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 227 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 230 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 231 | constexpr size_t maxEntries = 50; // max events to print |
| 232 | constexpr size_t skipBegin = maxEntries / 2; |
| 233 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 234 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 235 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 236 | |
| 237 | std::string dump; |
| 238 | for (size_t i = 0; i < queue.size(); i++) { |
| 239 | const DispatchEntry& entry = *queue[i]; |
| 240 | if (i >= skipBegin && i < skipEnd) { |
| 241 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 242 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 243 | continue; |
| 244 | } |
| 245 | dump.append(INDENT4); |
| 246 | dump += entry.eventEntry->getDescription(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 247 | dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, resolvedAction=%d, age=%" PRId64 |
| 248 | "ms", |
| 249 | entry.seq, entry.targetFlags.string().c_str(), entry.resolvedAction, |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 250 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 251 | if (entry.deliveryTime != 0) { |
| 252 | // This entry was delivered, so add information on how long we've been waiting |
| 253 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 254 | } |
| 255 | dump.append("\n"); |
| 256 | } |
| 257 | return dump; |
| 258 | } |
| 259 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 260 | /** |
| 261 | * Find the entry in std::unordered_map by key, and return it. |
| 262 | * If the entry is not found, return a default constructed entry. |
| 263 | * |
| 264 | * Useful when the entries are vectors, since an empty vector will be returned |
| 265 | * if the entry is not found. |
| 266 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 267 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 268 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 269 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 270 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 271 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 272 | } |
| 273 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 274 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 275 | if (first == second) { |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | if (first == nullptr || second == nullptr) { |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | return first->getToken() == second->getToken(); |
| 284 | } |
| 285 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 286 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 287 | if (first == nullptr || second == nullptr) { |
| 288 | return false; |
| 289 | } |
| 290 | return first->applicationInfo.token != nullptr && |
| 291 | first->applicationInfo.token == second->applicationInfo.token; |
| 292 | } |
| 293 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 294 | std::unique_ptr<DispatchEntry> createDispatchEntry( |
| 295 | const InputTarget& inputTarget, std::shared_ptr<EventEntry> eventEntry, |
| 296 | ftl::Flags<InputTarget::Flags> inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 297 | if (inputTarget.useDefaultPointerTransform()) { |
| 298 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 299 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 300 | inputTarget.displayTransform, |
| 301 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 305 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 306 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 307 | std::vector<PointerCoords> pointerCoords; |
| 308 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 309 | |
| 310 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 311 | // 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] | 312 | // uses the transform for the normalized pointer. |
| 313 | const ui::Transform& firstPointerTransform = |
| 314 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 315 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 316 | |
| 317 | // Iterate through all pointers in the event to normalize against the first. |
| 318 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 319 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 320 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 321 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 322 | |
| 323 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 324 | // First, apply the current pointer's transform to update the coordinates into |
| 325 | // window space. |
| 326 | pointerCoords[pointerIndex].transform(currTransform); |
| 327 | // Next, apply the inverse transform of the normalized coordinates so the |
| 328 | // current coordinates are transformed into the normalized coordinate space. |
| 329 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 332 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 333 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 334 | motionEntry.deviceId, motionEntry.source, |
| 335 | motionEntry.displayId, motionEntry.policyFlags, |
| 336 | motionEntry.action, motionEntry.actionButton, |
| 337 | motionEntry.flags, motionEntry.metaState, |
| 338 | motionEntry.buttonState, motionEntry.classification, |
| 339 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 340 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 341 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 342 | motionEntry.pointerCount, motionEntry.pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 343 | pointerCoords.data()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 344 | |
| 345 | if (motionEntry.injectionState) { |
| 346 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 347 | combinedMotionEntry->injectionState->refCount += 1; |
| 348 | } |
| 349 | |
| 350 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 351 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 352 | firstPointerTransform, inputTarget.displayTransform, |
| 353 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 354 | return dispatchEntry; |
| 355 | } |
| 356 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 357 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 358 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 359 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 360 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 361 | |
| 362 | serverChannel = std::move(uniqueServerChannel); |
| 363 | return result; |
| 364 | } |
| 365 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 366 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 367 | 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] | 368 | if (lhs == nullptr && rhs == nullptr) { |
| 369 | return true; |
| 370 | } |
| 371 | if (lhs == nullptr || rhs == nullptr) { |
| 372 | return false; |
| 373 | } |
| 374 | return *lhs == *rhs; |
| 375 | } |
| 376 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 377 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 378 | KeyEvent event; |
| 379 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 380 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 381 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 382 | return event; |
| 383 | } |
| 384 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 385 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 386 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 387 | // affect the statistics. |
| 388 | if (connection.monitor) { |
| 389 | return false; |
| 390 | } |
| 391 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 392 | if (!connection.responsive) { |
| 393 | return false; |
| 394 | } |
| 395 | return true; |
| 396 | } |
| 397 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 398 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 399 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 400 | const int32_t& inputEventId = eventEntry.id; |
| 401 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 402 | // Event was transmuted |
| 403 | return false; |
| 404 | } |
| 405 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 406 | return false; |
| 407 | } |
| 408 | // Only track latency for events that originated from hardware |
| 409 | if (eventEntry.isSynthesized()) { |
| 410 | return false; |
| 411 | } |
| 412 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 413 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 414 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 415 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 416 | return false; |
| 417 | } |
| 418 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 419 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 420 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 421 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 422 | return false; |
| 423 | } |
| 424 | } else { |
| 425 | // Not a key or a motion |
| 426 | return false; |
| 427 | } |
| 428 | if (!shouldReportMetricsForConnection(connection)) { |
| 429 | return false; |
| 430 | } |
| 431 | return true; |
| 432 | } |
| 433 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 434 | /** |
| 435 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 436 | * current time. |
| 437 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 438 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 439 | const nsecs_t currentTime = now(); |
| 440 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 441 | if (entry->timeoutTime < currentTime) { |
| 442 | return false; |
| 443 | } |
| 444 | } |
| 445 | return true; |
| 446 | } |
| 447 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 448 | // Returns true if the event type passed as argument represents a user activity. |
| 449 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 450 | switch (eventEntry.type) { |
| 451 | case EventEntry::Type::FOCUS: |
| 452 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 453 | case EventEntry::Type::DRAG: |
| 454 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 455 | case EventEntry::Type::SENSOR: |
| 456 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 457 | return false; |
| 458 | case EventEntry::Type::DEVICE_RESET: |
| 459 | case EventEntry::Type::KEY: |
| 460 | case EventEntry::Type::MOTION: |
| 461 | return true; |
| 462 | } |
| 463 | } |
| 464 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 465 | // 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] | 466 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, int32_t x, int32_t y, |
| 467 | bool isStylus) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 468 | const auto inputConfig = windowInfo.inputConfig; |
| 469 | if (windowInfo.displayId != displayId || |
| 470 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 471 | return false; |
| 472 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 473 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 474 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 475 | return false; |
| 476 | } |
Prabir Pradhan | 0634904 | 2022-02-04 09:19:17 -0800 | [diff] [blame] | 477 | if (!windowInfo.touchableRegionContainsPoint(x, y)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 478 | return false; |
| 479 | } |
| 480 | return true; |
| 481 | } |
| 482 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 483 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 484 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 485 | isStylusToolType(entry.pointerProperties[pointerIndex].toolType); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 488 | // Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 489 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 490 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 491 | // be sent to such a window, but it is not a foreground event and doesn't use |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 492 | // InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 493 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 494 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 495 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 496 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 497 | } |
| 498 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 499 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, int32_t pid, int32_t uid) { |
| 500 | if (windowHandle == nullptr) { |
| 501 | return false; |
| 502 | } |
| 503 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 504 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 505 | return true; |
| 506 | } |
| 507 | return false; |
| 508 | } |
| 509 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 510 | // Checks targeted injection using the window's owner's uid. |
| 511 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 512 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 513 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 514 | const EventEntry& entry) { |
| 515 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 516 | // The event was not injected, or the injected event does not target a window. |
| 517 | return {}; |
| 518 | } |
| 519 | const int32_t uid = *entry.injectionState->targetUid; |
| 520 | if (window == nullptr) { |
| 521 | return StringPrintf("No valid window target for injection into uid %d.", uid); |
| 522 | } |
| 523 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
| 524 | return StringPrintf("Injected event targeted at uid %d would be dispatched to window '%s' " |
| 525 | "owned by uid %d.", |
| 526 | uid, window->getName().c_str(), window->getInfo()->ownerUid); |
| 527 | } |
| 528 | return {}; |
| 529 | } |
| 530 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 531 | Point resolveTouchedPosition(const MotionEntry& entry) { |
| 532 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 533 | // Always dispatch mouse events to cursor position. |
| 534 | if (isFromMouse) { |
| 535 | return Point(static_cast<int32_t>(entry.xCursorPosition), |
| 536 | static_cast<int32_t>(entry.yCursorPosition)); |
| 537 | } |
| 538 | |
| 539 | const int32_t pointerIndex = getMotionEventActionPointerIndex(entry.action); |
| 540 | return Point(static_cast<int32_t>( |
| 541 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)), |
| 542 | static_cast<int32_t>( |
| 543 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y))); |
| 544 | } |
| 545 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 546 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 547 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 548 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 549 | return keyEntry.downTime; |
| 550 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 551 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 552 | return motionEntry.downTime; |
| 553 | } |
| 554 | return std::nullopt; |
| 555 | } |
| 556 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 557 | } // namespace |
| 558 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 559 | // --- InputDispatcher --- |
| 560 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 561 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 562 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} |
| 563 | |
| 564 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy, |
| 565 | std::chrono::nanoseconds staleEventTimeout) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 566 | : mPolicy(policy), |
| 567 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 568 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 569 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 570 | mAppSwitchSawKeyDown(false), |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 571 | mAppSwitchDueTime(LLONG_MAX), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 572 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 573 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 574 | mDispatchEnabled(false), |
| 575 | mDispatchFrozen(false), |
| 576 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 577 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 578 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 579 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 580 | mStaleEventTimeout(staleEventTimeout), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 581 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 582 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 583 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 584 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 585 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 586 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 587 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 588 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 589 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 590 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 591 | policy->getDispatcherConfiguration(&mConfig); |
| 592 | } |
| 593 | |
| 594 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 595 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 596 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 597 | resetKeyRepeatLocked(); |
| 598 | releasePendingEventLocked(); |
| 599 | drainInboundQueueLocked(); |
| 600 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 601 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 602 | while (!mConnectionsByToken.empty()) { |
| 603 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 604 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 605 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 609 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 610 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 611 | return ALREADY_EXISTS; |
| 612 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 613 | mThread = std::make_unique<InputThread>( |
| 614 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 615 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 619 | if (mThread && mThread->isCallingThread()) { |
| 620 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 621 | return INVALID_OPERATION; |
| 622 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 623 | mThread.reset(); |
| 624 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 627 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 628 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 629 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 630 | std::scoped_lock _l(mLock); |
| 631 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 632 | |
| 633 | // Run a dispatch loop if there are no pending commands. |
| 634 | // The dispatch loop might enqueue commands to run afterwards. |
| 635 | if (!haveCommandsLocked()) { |
| 636 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 637 | } |
| 638 | |
| 639 | // Run all pending commands if there are any. |
| 640 | // 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] | 641 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 642 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 643 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 644 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 645 | // If we are still waiting for ack on some events, |
| 646 | // we might have to wake up earlier to check if an app is anr'ing. |
| 647 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 648 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 649 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 650 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 651 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 652 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 653 | mDispatcherEnteredIdle.notify_all(); |
| 654 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 655 | } // release lock |
| 656 | |
| 657 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 658 | nsecs_t currentTime = now(); |
| 659 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 660 | mLooper->pollOnce(timeoutMillis); |
| 661 | } |
| 662 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 663 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 664 | * Raise ANR if there is no focused window. |
| 665 | * Before the ANR is raised, do a final state check: |
| 666 | * 1. The currently focused application must be the same one we are waiting for. |
| 667 | * 2. Ensure we still don't have a focused window. |
| 668 | */ |
| 669 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 670 | // Check if the application that we are waiting for is still focused. |
| 671 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 672 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 673 | if (focusedApplication == nullptr || |
| 674 | focusedApplication->getApplicationToken() != |
| 675 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 676 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 677 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 678 | focusedApplication->getName().c_str()); |
| 679 | return; // The focused application has changed. |
| 680 | } |
| 681 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 682 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 683 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 684 | if (focusedWindowHandle != nullptr) { |
| 685 | return; // We now have a focused window. No need for ANR. |
| 686 | } |
| 687 | onAnrLocked(mAwaitedFocusedApplication); |
| 688 | } |
| 689 | |
| 690 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 691 | * Check if any of the connections' wait queues have events that are too old. |
| 692 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 693 | * Return the time at which we should wake up next. |
| 694 | */ |
| 695 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 696 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 697 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 698 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 699 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 700 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 701 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 702 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 703 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 704 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 705 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 706 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 707 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | // Check if any connection ANRs are due |
| 712 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 713 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 714 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 715 | } |
| 716 | |
| 717 | // If we reached here, we have an unresponsive connection. |
| 718 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 719 | if (connection == nullptr) { |
| 720 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 721 | return nextAnrCheck; |
| 722 | } |
| 723 | connection->responsive = false; |
| 724 | // Stop waking up for this unresponsive connection |
| 725 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 726 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 727 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 730 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
| 731 | const sp<Connection>& connection) { |
| 732 | if (connection->monitor) { |
| 733 | return mMonitorDispatchingTimeout; |
| 734 | } |
| 735 | const sp<WindowInfoHandle> window = |
| 736 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 737 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 738 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 739 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 740 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 743 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 744 | nsecs_t currentTime = now(); |
| 745 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 746 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 747 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 748 | // repeat if the device is just coming out of sleep. |
| 749 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 750 | resetKeyRepeatLocked(); |
| 751 | } |
| 752 | |
| 753 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 754 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 755 | if (DEBUG_FOCUS) { |
| 756 | ALOGD("Dispatch frozen. Waiting some more."); |
| 757 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 758 | return; |
| 759 | } |
| 760 | |
| 761 | // Optimize latency of app switches. |
| 762 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 763 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 764 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 765 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 766 | *nextWakeupTime = mAppSwitchDueTime; |
| 767 | } |
| 768 | |
| 769 | // Ready to start a new event. |
| 770 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 771 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 772 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 773 | if (isAppSwitchDue) { |
| 774 | // The inbound queue is empty so the app switch key we were waiting |
| 775 | // for will never arrive. Stop waiting for it. |
| 776 | resetPendingAppSwitchLocked(false); |
| 777 | isAppSwitchDue = false; |
| 778 | } |
| 779 | |
| 780 | // Synthesize a key repeat if appropriate. |
| 781 | if (mKeyRepeatState.lastKeyEntry) { |
| 782 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 783 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 784 | } else { |
| 785 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 786 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | // Nothing to do if there is no pending event. |
| 792 | if (!mPendingEvent) { |
| 793 | return; |
| 794 | } |
| 795 | } else { |
| 796 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 797 | mPendingEvent = mInboundQueue.front(); |
| 798 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 799 | traceInboundQueueLengthLocked(); |
| 800 | } |
| 801 | |
| 802 | // Poke user activity for this event. |
| 803 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 804 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 805 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | // Now we have an event to dispatch. |
| 809 | // 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] | 810 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 811 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 812 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 813 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 814 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 815 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 816 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 820 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 824 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 825 | const ConfigurationChangedEntry& typedEntry = |
| 826 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 827 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 828 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 829 | break; |
| 830 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 831 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 832 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 833 | const DeviceResetEntry& typedEntry = |
| 834 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 835 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 836 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 837 | break; |
| 838 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 839 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 840 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 841 | std::shared_ptr<FocusEntry> typedEntry = |
| 842 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 843 | dispatchFocusLocked(currentTime, typedEntry); |
| 844 | done = true; |
| 845 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 846 | break; |
| 847 | } |
| 848 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 849 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 850 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 851 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 852 | done = true; |
| 853 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 854 | break; |
| 855 | } |
| 856 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 857 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 858 | const auto typedEntry = |
| 859 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 860 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 861 | done = true; |
| 862 | break; |
| 863 | } |
| 864 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 865 | case EventEntry::Type::DRAG: { |
| 866 | std::shared_ptr<DragEntry> typedEntry = |
| 867 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 868 | dispatchDragLocked(currentTime, typedEntry); |
| 869 | done = true; |
| 870 | break; |
| 871 | } |
| 872 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 873 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 874 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 875 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 876 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 877 | resetPendingAppSwitchLocked(true); |
| 878 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 879 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 880 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 881 | } |
| 882 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 883 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 884 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 885 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 886 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 887 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 888 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 889 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 890 | break; |
| 891 | } |
| 892 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 893 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 894 | std::shared_ptr<MotionEntry> motionEntry = |
| 895 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 896 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 897 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 898 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 899 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 900 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 901 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 902 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 903 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 904 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 905 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 906 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 907 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 908 | |
| 909 | case EventEntry::Type::SENSOR: { |
| 910 | std::shared_ptr<SensorEntry> sensorEntry = |
| 911 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 912 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 913 | dropReason = DropReason::APP_SWITCH; |
| 914 | } |
| 915 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 916 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 917 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 918 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 919 | dropReason = DropReason::STALE; |
| 920 | } |
| 921 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 922 | done = true; |
| 923 | break; |
| 924 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 928 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 929 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 930 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 931 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 932 | |
| 933 | releasePendingEventLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 934 | *nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 938 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 939 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 940 | } |
| 941 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 942 | /** |
| 943 | * Return true if the events preceding this incoming motion event should be dropped |
| 944 | * Return false otherwise (the default behaviour) |
| 945 | */ |
| 946 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 947 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 948 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 949 | |
| 950 | // Optimize case where the current application is unresponsive and the user |
| 951 | // decides to touch a window in a different application. |
| 952 | // If the application takes too long to catch up then we drop all events preceding |
| 953 | // the touch into the other window. |
| 954 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 955 | const int32_t displayId = motionEntry.displayId; |
| 956 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 957 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 958 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 959 | sp<WindowInfoHandle> touchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 960 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 961 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 962 | touchedWindowHandle->getApplicationToken() != |
| 963 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 964 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 965 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 966 | "for %s", |
| 967 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 968 | return true; |
| 969 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 970 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 971 | // Alternatively, maybe there's a spy window that could handle this event. |
| 972 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 973 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 974 | for (const auto& windowHandle : touchedSpies) { |
| 975 | const sp<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 976 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 977 | // This spy window could take more input. Drop all events preceding this |
| 978 | // 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] | 979 | 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] | 980 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 981 | mAwaitedFocusedApplication->getName().c_str()); |
| 982 | return true; |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 988 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 989 | // events to be processed before dispatching the key event. This is because these motion events |
| 990 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 991 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 992 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 993 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 994 | "just send the pending key event to the focused window."); |
| 995 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 996 | } |
| 997 | return false; |
| 998 | } |
| 999 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1000 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1001 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1002 | mInboundQueue.push_back(std::move(newEntry)); |
| 1003 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1004 | traceInboundQueueLengthLocked(); |
| 1005 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1006 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1007 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1008 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1009 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1010 | // Optimize app switch latency. |
| 1011 | // If the application takes too long to catch up then we drop all events preceding |
| 1012 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1013 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1014 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1015 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1016 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1017 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1018 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1019 | if (DEBUG_APP_SWITCH) { |
| 1020 | ALOGD("App switch is pending!"); |
| 1021 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1022 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1023 | mAppSwitchSawKeyDown = false; |
| 1024 | needWake = true; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1028 | |
| 1029 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1030 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1031 | // time for it may have been handled in the policy and could be dropped. |
| 1032 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1033 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1034 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1035 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1036 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1037 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1038 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1039 | pendingKey.interceptKeyWakeupTime = 0; |
| 1040 | needWake = true; |
| 1041 | } |
| 1042 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1043 | break; |
| 1044 | } |
| 1045 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1046 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1047 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1048 | "Unexpected untrusted event."); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1049 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1050 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1051 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1052 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1053 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1054 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1055 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1056 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1057 | break; |
| 1058 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1059 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1060 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1061 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1062 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1063 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1064 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1065 | // nothing to do |
| 1066 | break; |
| 1067 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | return needWake; |
| 1071 | } |
| 1072 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1073 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1074 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1075 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1076 | mRecentQueue.push_back(entry); |
| 1077 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1078 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1079 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1083 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1084 | int32_t y, TouchState* touchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1085 | bool isStylus, |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1086 | bool addOutsideTargets, |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 1087 | bool ignoreDragWindow) const { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1088 | if (addOutsideTargets && touchState == nullptr) { |
| 1089 | 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] | 1090 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1091 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1092 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1093 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1094 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1095 | continue; |
| 1096 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1097 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1098 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1099 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1100 | return windowHandle; |
| 1101 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1102 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1103 | if (addOutsideTargets && |
| 1104 | info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1105 | touchState->addOrUpdateWindow(windowHandle, InputTarget::Flags::DISPATCH_AS_OUTSIDE, |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1106 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1107 | } |
| 1108 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1109 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1112 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1113 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1114 | // Traverse windows from front to back and gather the touched spy windows. |
| 1115 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1116 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1117 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1118 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1119 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1120 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1121 | continue; |
| 1122 | } |
| 1123 | if (!info.isSpy()) { |
| 1124 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1125 | return spyWindows; |
| 1126 | } |
| 1127 | spyWindows.push_back(windowHandle); |
| 1128 | } |
| 1129 | return spyWindows; |
| 1130 | } |
| 1131 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1132 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1133 | const char* reason; |
| 1134 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1135 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1136 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1137 | ALOGD("Dropped event because policy consumed it."); |
| 1138 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1139 | reason = "inbound event was dropped because the policy consumed it"; |
| 1140 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1141 | case DropReason::DISABLED: |
| 1142 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1143 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1144 | } |
| 1145 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1146 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1147 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1148 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1149 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1150 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1151 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1152 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1153 | "has started interacting with a different application."); |
| 1154 | reason = "inbound event was dropped because the current application is not responding " |
| 1155 | "and the user has started interacting with a different application"; |
| 1156 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1157 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1158 | ALOGI("Dropped event because it is stale."); |
| 1159 | reason = "inbound event was dropped because it is stale"; |
| 1160 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1161 | case DropReason::NO_POINTER_CAPTURE: |
| 1162 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1163 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1164 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1165 | case DropReason::NOT_DROPPED: { |
| 1166 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1167 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1168 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1171 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1172 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1173 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1174 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1175 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1176 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1177 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1178 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1179 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1180 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1181 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1182 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1183 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1184 | reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1185 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1186 | } |
| 1187 | break; |
| 1188 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1189 | case EventEntry::Type::SENSOR: { |
| 1190 | break; |
| 1191 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1192 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1193 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1194 | break; |
| 1195 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1196 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1197 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1198 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1199 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1200 | 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] | 1201 | break; |
| 1202 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1206 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1207 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1208 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1211 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1212 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1213 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1214 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | bool InputDispatcher::isAppSwitchPendingLocked() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1218 | return mAppSwitchDueTime != LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1222 | mAppSwitchDueTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1223 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1224 | if (DEBUG_APP_SWITCH) { |
| 1225 | if (handled) { |
| 1226 | ALOGD("App switch has arrived."); |
| 1227 | } else { |
| 1228 | ALOGD("App switch was abandoned."); |
| 1229 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1231 | } |
| 1232 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1233 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1234 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1237 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1238 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1239 | return false; |
| 1240 | } |
| 1241 | |
| 1242 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1243 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1244 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1245 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1246 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1247 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1248 | return true; |
| 1249 | } |
| 1250 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1251 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1252 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1256 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1257 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1258 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1259 | releaseInboundEventLocked(entry); |
| 1260 | } |
| 1261 | traceInboundQueueLengthLocked(); |
| 1262 | } |
| 1263 | |
| 1264 | void InputDispatcher::releasePendingEventLocked() { |
| 1265 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1266 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1267 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1271 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1272 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1273 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1274 | if (DEBUG_DISPATCH_CYCLE) { |
| 1275 | ALOGD("Injected inbound event was dropped."); |
| 1276 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1277 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1278 | } |
| 1279 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1280 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1281 | } |
| 1282 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1286 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1287 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1291 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1292 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1293 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1294 | uint32_t policyFlags = entry->policyFlags & |
| 1295 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1296 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1297 | std::shared_ptr<KeyEntry> newEntry = |
| 1298 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1299 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1300 | entry->flags, entry->keyCode, entry->scanCode, |
| 1301 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1302 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1303 | newEntry->syntheticRepeat = true; |
| 1304 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1305 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1306 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1307 | } |
| 1308 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1309 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1310 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1311 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1312 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1313 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1314 | |
| 1315 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1316 | resetKeyRepeatLocked(); |
| 1317 | |
| 1318 | // 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] | 1319 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1320 | scoped_unlock unlock(mLock); |
| 1321 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1322 | }; |
| 1323 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1324 | return true; |
| 1325 | } |
| 1326 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1327 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1328 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1329 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1330 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1331 | entry.deviceId); |
| 1332 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1333 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1334 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1335 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1336 | resetKeyRepeatLocked(); |
| 1337 | } |
| 1338 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1339 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1340 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1341 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1342 | return true; |
| 1343 | } |
| 1344 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1345 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1346 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1347 | if (mPendingEvent != nullptr) { |
| 1348 | // Move the pending event to the front of the queue. This will give the chance |
| 1349 | // for the pending event to get dispatched to the newly focused window |
| 1350 | mInboundQueue.push_front(mPendingEvent); |
| 1351 | mPendingEvent = nullptr; |
| 1352 | } |
| 1353 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1354 | std::unique_ptr<FocusEntry> focusEntry = |
| 1355 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1356 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1357 | |
| 1358 | // This event should go to the front of the queue, but behind all other focus events |
| 1359 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1360 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1361 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1362 | [](const std::shared_ptr<EventEntry>& event) { |
| 1363 | return event->type == EventEntry::Type::FOCUS; |
| 1364 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1365 | |
| 1366 | // 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] | 1367 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1368 | } |
| 1369 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1370 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1371 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1372 | if (channel == nullptr) { |
| 1373 | return; // Window has gone away |
| 1374 | } |
| 1375 | InputTarget target; |
| 1376 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1377 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1378 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1379 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1380 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1381 | std::string reason = std::string("reason=").append(entry->reason); |
| 1382 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1383 | dispatchEventLocked(currentTime, entry, {target}); |
| 1384 | } |
| 1385 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1386 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1387 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1388 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1389 | dropReason = DropReason::NOT_DROPPED; |
| 1390 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1391 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1392 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1393 | |
| 1394 | if (entry->pointerCaptureRequest.enable) { |
| 1395 | // Enable Pointer Capture. |
| 1396 | if (haveWindowWithPointerCapture && |
| 1397 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1398 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1399 | // app of the state change, so there is no need to notify the app. |
| 1400 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1401 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1402 | } |
| 1403 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1404 | // This can happen if a window requests capture and immediately releases capture. |
| 1405 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1406 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1407 | return; |
| 1408 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1409 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1410 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1411 | return; |
| 1412 | } |
| 1413 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1414 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1415 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1416 | mWindowTokenWithPointerCapture = token; |
| 1417 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1418 | // Disable Pointer Capture. |
| 1419 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1420 | // for two reasons: |
| 1421 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1422 | // to disable capture with the same sequence number: one generated by |
| 1423 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1424 | // Capture being disabled in InputReader. |
| 1425 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1426 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1427 | // in InputReader. |
| 1428 | if (!haveWindowWithPointerCapture) { |
| 1429 | // Pointer capture was already forcefully disabled because of focus change. |
| 1430 | dropReason = DropReason::NOT_DROPPED; |
| 1431 | return; |
| 1432 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1433 | token = mWindowTokenWithPointerCapture; |
| 1434 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1435 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1436 | setPointerCaptureLocked(false); |
| 1437 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | auto channel = getInputChannelLocked(token); |
| 1441 | if (channel == nullptr) { |
| 1442 | // Window has gone away, clean up Pointer Capture state. |
| 1443 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1444 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1445 | setPointerCaptureLocked(false); |
| 1446 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1447 | return; |
| 1448 | } |
| 1449 | InputTarget target; |
| 1450 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1451 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1452 | entry->dispatchInProgress = true; |
| 1453 | dispatchEventLocked(currentTime, entry, {target}); |
| 1454 | |
| 1455 | dropReason = DropReason::NOT_DROPPED; |
| 1456 | } |
| 1457 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1458 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1459 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1460 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1461 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1462 | if (windowHandles.empty()) { |
| 1463 | return; |
| 1464 | } |
| 1465 | const std::vector<InputTarget> inputTargets = |
| 1466 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1467 | if (inputTargets.empty()) { |
| 1468 | return; |
| 1469 | } |
| 1470 | entry->dispatchInProgress = true; |
| 1471 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1472 | } |
| 1473 | |
| 1474 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1475 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1476 | std::vector<InputTarget> inputTargets; |
| 1477 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1478 | const sp<IBinder>& token = handle->getToken(); |
| 1479 | if (token == nullptr) { |
| 1480 | continue; |
| 1481 | } |
| 1482 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1483 | if (channel == nullptr) { |
| 1484 | continue; // Window has gone away |
| 1485 | } |
| 1486 | InputTarget target; |
| 1487 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1488 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1489 | inputTargets.push_back(target); |
| 1490 | } |
| 1491 | return inputTargets; |
| 1492 | } |
| 1493 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1494 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1495 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1496 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1497 | if (!entry->dispatchInProgress) { |
| 1498 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1499 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1500 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1501 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1502 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1503 | // We have seen two identical key downs in a row which indicates that the device |
| 1504 | // driver is automatically generating key repeats itself. We take note of the |
| 1505 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1506 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1507 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1508 | // Make sure we don't get key down from a different device. If a different |
| 1509 | // device Id has same key pressed down, the new device Id will replace the |
| 1510 | // current one to hold the key repeat with repeat count reset. |
| 1511 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1512 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1513 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1514 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1515 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1516 | } else { |
| 1517 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1518 | resetKeyRepeatLocked(); |
| 1519 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1520 | } |
| 1521 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1522 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1523 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1524 | // 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] | 1525 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1526 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1527 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1528 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1529 | resetKeyRepeatLocked(); |
| 1530 | } |
| 1531 | |
| 1532 | if (entry->repeatCount == 1) { |
| 1533 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1534 | } else { |
| 1535 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1536 | } |
| 1537 | |
| 1538 | entry->dispatchInProgress = true; |
| 1539 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1540 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | // Handle case where the policy asked us to try again later last time. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1544 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1545 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1546 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1547 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1548 | } |
| 1549 | return false; // wait until next wakeup |
| 1550 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1551 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1552 | entry->interceptKeyWakeupTime = 0; |
| 1553 | } |
| 1554 | |
| 1555 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1556 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1557 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1558 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1559 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1560 | |
| 1561 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1562 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1563 | }; |
| 1564 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1565 | return false; // wait for the command to run |
| 1566 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1567 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1568 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1569 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1570 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1571 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1576 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1577 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1578 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1579 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1580 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1581 | return true; |
| 1582 | } |
| 1583 | |
| 1584 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1585 | InputEventInjectionResult injectionResult; |
| 1586 | sp<WindowInfoHandle> focusedWindow = |
| 1587 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1588 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1589 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1590 | return false; |
| 1591 | } |
| 1592 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1593 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1594 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1595 | return true; |
| 1596 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1597 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1598 | |
| 1599 | std::vector<InputTarget> inputTargets; |
| 1600 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1601 | InputTarget::Flags::FOREGROUND | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1602 | BitSet32(0), getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1603 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1604 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1605 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1606 | |
| 1607 | // Dispatch the key. |
| 1608 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1609 | return true; |
| 1610 | } |
| 1611 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1612 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1613 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1614 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1615 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1616 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1617 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1618 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1619 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1620 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1621 | } |
| 1622 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1623 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1624 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1625 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1626 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1627 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1628 | "source=0x%x, sensorType=%s", |
| 1629 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1630 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1631 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1632 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1633 | scoped_unlock unlock(mLock); |
| 1634 | |
| 1635 | if (entry->accuracyChanged) { |
| 1636 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1637 | } |
| 1638 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1639 | entry->hwTimestamp, entry->values); |
| 1640 | }; |
| 1641 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1645 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1646 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1647 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1648 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1649 | { // acquire lock |
| 1650 | std::scoped_lock _l(mLock); |
| 1651 | |
| 1652 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1653 | std::shared_ptr<EventEntry> entry = *it; |
| 1654 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1655 | it = mInboundQueue.erase(it); |
| 1656 | releaseInboundEventLocked(entry); |
| 1657 | } |
| 1658 | } |
| 1659 | } |
| 1660 | return true; |
| 1661 | } |
| 1662 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1663 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1664 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1665 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1666 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1667 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1668 | entry->dispatchInProgress = true; |
| 1669 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1670 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1674 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1675 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1676 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1677 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1678 | return true; |
| 1679 | } |
| 1680 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1681 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1682 | |
| 1683 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1684 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1685 | |
| 1686 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1687 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1688 | if (isPointerEvent) { |
| 1689 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1690 | |
| 1691 | if (mDragState && |
| 1692 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1693 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1694 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1695 | } |
| 1696 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1697 | std::vector<TouchedWindow> touchedWindows = |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 1698 | findTouchedWindowTargetsLocked(currentTime, *entry, &conflictingPointerActions, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1699 | /*byref*/ injectionResult); |
| 1700 | for (const TouchedWindow& touchedWindow : touchedWindows) { |
| 1701 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED, |
| 1702 | "Shouldn't be adding window if the injection didn't succeed."); |
| 1703 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 1704 | touchedWindow.pointerIds, touchedWindow.firstDownTimeInTarget, |
| 1705 | inputTargets); |
| 1706 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1707 | } else { |
| 1708 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1709 | sp<WindowInfoHandle> focusedWindow = |
| 1710 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1711 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1712 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1713 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1714 | InputTarget::Flags::FOREGROUND | |
| 1715 | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1716 | BitSet32(0), getDownTime(*entry), inputTargets); |
| 1717 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1718 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1719 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1720 | return false; |
| 1721 | } |
| 1722 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1723 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1724 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1725 | return true; |
| 1726 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1727 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1728 | CancelationOptions::Mode mode( |
| 1729 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 1730 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1731 | CancelationOptions options(mode, "input event injection failed"); |
| 1732 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1733 | return true; |
| 1734 | } |
| 1735 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1736 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1737 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1738 | |
| 1739 | // Dispatch the motion. |
| 1740 | if (conflictingPointerActions) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1741 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1742 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1743 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1744 | } |
| 1745 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1746 | return true; |
| 1747 | } |
| 1748 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1749 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1750 | bool isExiting, const int32_t rawX, |
| 1751 | const int32_t rawY) { |
| 1752 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1753 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1754 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 1755 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1756 | |
| 1757 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1758 | } |
| 1759 | |
| 1760 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1761 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1762 | if (channel == nullptr) { |
| 1763 | return; // Window has gone away |
| 1764 | } |
| 1765 | InputTarget target; |
| 1766 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1767 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1768 | entry->dispatchInProgress = true; |
| 1769 | dispatchEventLocked(currentTime, entry, {target}); |
| 1770 | } |
| 1771 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1772 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1773 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1774 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32 |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1775 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1776 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1777 | "metaState=0x%x, buttonState=0x%x," |
| 1778 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1779 | prefix, entry.eventTime, entry.deviceId, |
| 1780 | inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags, |
| 1781 | MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags, |
| 1782 | entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision, |
| 1783 | entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1784 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1785 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1786 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1787 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1788 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1789 | "orientation=%f", |
| 1790 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1791 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1792 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1793 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1794 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1795 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1796 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1797 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1798 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1799 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1800 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1801 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1802 | } |
| 1803 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1804 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1805 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1806 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1807 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1808 | if (DEBUG_DISPATCH_CYCLE) { |
| 1809 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1810 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1811 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1812 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1813 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1814 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1815 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1816 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1817 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1818 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1819 | sp<Connection> connection = |
| 1820 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1821 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1822 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1823 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1824 | if (DEBUG_FOCUS) { |
| 1825 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1826 | "is no longer registered with the input dispatcher.", |
| 1827 | inputTarget.inputChannel->getName().c_str()); |
| 1828 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | } |
| 1832 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1833 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1834 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1835 | // If the policy decides to close the app, we will get a channel removal event via |
| 1836 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1837 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1838 | // pile up. |
| 1839 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1840 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1841 | if (connection->status == Connection::Status::NORMAL) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1842 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1843 | "application not responding"); |
| 1844 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1845 | } |
| 1846 | } |
| 1847 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1848 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1849 | if (DEBUG_FOCUS) { |
| 1850 | ALOGD("Resetting ANR timeouts."); |
| 1851 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1852 | |
| 1853 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1854 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1855 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1856 | } |
| 1857 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1858 | /** |
| 1859 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1860 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1861 | * Focused display is the display that the user most recently interacted with. |
| 1862 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1863 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1864 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1865 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1866 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1867 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1868 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1869 | break; |
| 1870 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1871 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1872 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1873 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1874 | break; |
| 1875 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1876 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1877 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1878 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1879 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1880 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1881 | case EventEntry::Type::SENSOR: |
| 1882 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1883 | 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] | 1884 | return ADISPLAY_ID_NONE; |
| 1885 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1886 | } |
| 1887 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1888 | } |
| 1889 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1890 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1891 | const char* focusedWindowName) { |
| 1892 | if (mAnrTracker.empty()) { |
| 1893 | // already processed all events that we waited for |
| 1894 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1895 | return false; |
| 1896 | } |
| 1897 | |
| 1898 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1899 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1900 | // 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] | 1901 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1902 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1903 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1904 | return true; |
| 1905 | } |
| 1906 | |
| 1907 | // We still have pending events, and already started the timer |
| 1908 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1909 | return true; // Still waiting |
| 1910 | } |
| 1911 | |
| 1912 | // Waited too long, and some connection still hasn't processed all motions |
| 1913 | // Just send the key to the focused window |
| 1914 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1915 | focusedWindowName); |
| 1916 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1917 | return false; |
| 1918 | } |
| 1919 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1920 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
| 1921 | nsecs_t currentTime, const EventEntry& entry, nsecs_t* nextWakeupTime, |
| 1922 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1923 | std::string reason; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1924 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1925 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1926 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1927 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1928 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1929 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1930 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1931 | // If there is no currently focused window and no focused application |
| 1932 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1933 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1934 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1935 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1936 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1937 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1938 | } |
| 1939 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1940 | // Drop key events if requested by input feature |
| 1941 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1942 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1943 | } |
| 1944 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1945 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1946 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1947 | // start interacting with another application via touch (app switch). This code can be removed |
| 1948 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1949 | // an app is expected to have a focused window. |
| 1950 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1951 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1952 | // 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] | 1953 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1954 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1955 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1956 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1957 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1958 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1959 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1960 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1961 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1962 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 1963 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1964 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1965 | // Already raised ANR. Drop the event |
| 1966 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1967 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1968 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1969 | } else { |
| 1970 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1971 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 1972 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | // we have a valid, non-null focused window |
| 1977 | resetNoFocusedWindowTimeoutLocked(); |
| 1978 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1979 | // Verify targeted injection. |
| 1980 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 1981 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1982 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 1983 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1984 | } |
| 1985 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1986 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 1987 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1988 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1989 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 1990 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | // If the event is a key event, then we must wait for all previous events to |
| 1994 | // complete before delivering it because previous events may have the |
| 1995 | // side-effect of transferring focus to a different window and we want to |
| 1996 | // ensure that the following keys are sent to the new window. |
| 1997 | // |
| 1998 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1999 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2000 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2001 | // often anticipate pending UI changes when typing on a keyboard. |
| 2002 | // To obtain this behavior, we must serialize key events with respect to all |
| 2003 | // prior input events. |
| 2004 | if (entry.type == EventEntry::Type::KEY) { |
| 2005 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 2006 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2007 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2008 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2009 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2010 | } |
| 2011 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2012 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2013 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2014 | } |
| 2015 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2016 | /** |
| 2017 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2018 | * that are currently unresponsive. |
| 2019 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2020 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2021 | const std::vector<Monitor>& monitors) const { |
| 2022 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2023 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2024 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 2025 | sp<Connection> connection = |
| 2026 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2027 | if (connection == nullptr) { |
| 2028 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2029 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2030 | return false; |
| 2031 | } |
| 2032 | if (!connection->responsive) { |
| 2033 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2034 | connection->inputChannel->getName().c_str()); |
| 2035 | return false; |
| 2036 | } |
| 2037 | return true; |
| 2038 | }); |
| 2039 | return responsiveMonitors; |
| 2040 | } |
| 2041 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2042 | /** |
| 2043 | * In general, touch should be always split between windows. Some exceptions: |
| 2044 | * 1. Don't split touch is if we have an active pointer down, and a new pointer is going down that's |
| 2045 | * from the same device, *and* the window that's receiving the current pointer does not support |
| 2046 | * split touch. |
| 2047 | * 2. Don't split mouse events |
| 2048 | */ |
| 2049 | bool InputDispatcher::shouldSplitTouch(const TouchState& touchState, |
| 2050 | const MotionEntry& entry) const { |
| 2051 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 2052 | // We should never split mouse events |
| 2053 | return false; |
| 2054 | } |
| 2055 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 2056 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 2057 | // Spy windows should not affect whether or not touch is split. |
| 2058 | continue; |
| 2059 | } |
| 2060 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 2061 | continue; |
| 2062 | } |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 2063 | if (touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 2064 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 2065 | // Wallpaper window should not affect whether or not touch is split |
| 2066 | continue; |
| 2067 | } |
| 2068 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2069 | // Eventually, touchedWindow will contain the deviceId of each pointer that's currently |
| 2070 | // being sent there. For now, use deviceId from touch state. |
| 2071 | if (entry.deviceId == touchState.deviceId && !touchedWindow.pointerIds.isEmpty()) { |
| 2072 | return false; |
| 2073 | } |
| 2074 | } |
| 2075 | return true; |
| 2076 | } |
| 2077 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2078 | std::vector<TouchedWindow> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2079 | nsecs_t currentTime, const MotionEntry& entry, bool* outConflictingPointerActions, |
| 2080 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2081 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2082 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2083 | std::vector<TouchedWindow> touchedWindows; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2084 | // For security reasons, we defer updating the touch state until we are sure that |
| 2085 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2086 | const int32_t displayId = entry.displayId; |
| 2087 | const int32_t action = entry.action; |
| 2088 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2089 | |
| 2090 | // Update the touch state as needed based on the properties of the touch event. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2091 | outInjectionResult = InputEventInjectionResult::PENDING; |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2092 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 2093 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2094 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2095 | // Copy current touch state into tempTouchState. |
| 2096 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2097 | // 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] | 2098 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2099 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2100 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2101 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2102 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2103 | } |
| 2104 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2105 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2106 | const bool switchedDevice = (oldState != nullptr) && |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2107 | (oldState->deviceId != entry.deviceId || oldState->source != entry.source); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2108 | |
| 2109 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2110 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2111 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2112 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2113 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2114 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2115 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2116 | if (newGesture) { |
| 2117 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2118 | if (switchedDevice && tempTouchState.isDown() && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2119 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2120 | "in display %" PRId32, |
| 2121 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2122 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2123 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | f0ab2c8 | 2022-10-25 18:15:28 -0700 | [diff] [blame] | 2124 | return touchedWindows; // wrong device |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2125 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2126 | tempTouchState.reset(); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2127 | tempTouchState.deviceId = entry.deviceId; |
| 2128 | tempTouchState.source = entry.source; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2129 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2130 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2131 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2132 | "in display %" PRId32, |
| 2133 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2134 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2135 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | f0ab2c8 | 2022-10-25 18:15:28 -0700 | [diff] [blame] | 2136 | return touchedWindows; // wrong device |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2140 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2141 | const auto [x, y] = resolveTouchedPosition(entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2142 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2143 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2144 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2145 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 2146 | isStylus, isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2147 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2148 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2149 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2150 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2151 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2152 | // 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] | 2153 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2156 | // Verify targeted injection. |
| 2157 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2158 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2159 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2160 | newTouchedWindowHandle = nullptr; |
| 2161 | goto Failed; |
| 2162 | } |
| 2163 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2164 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2165 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2166 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2167 | // New window supports splitting, but we should never split mouse events. |
| 2168 | isSplit = !isFromMouse; |
| 2169 | } else if (isSplit) { |
| 2170 | // New window does not support splitting but we have already split events. |
| 2171 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2172 | newTouchedWindowHandle = nullptr; |
| 2173 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2174 | } else { |
| 2175 | // 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] | 2176 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2177 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2178 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2181 | // Update hover state. |
| 2182 | if (newTouchedWindowHandle != nullptr) { |
| 2183 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2184 | newHoverWindowHandle = nullptr; |
| 2185 | } else if (isHoverAction) { |
| 2186 | newHoverWindowHandle = newTouchedWindowHandle; |
| 2187 | } |
| 2188 | } |
| 2189 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2190 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2191 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2192 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2193 | // Process the foreground window first so that it is the first to receive the event. |
| 2194 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2195 | } |
| 2196 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2197 | if (newTouchedWindows.empty()) { |
| 2198 | ALOGI("Dropping event because there is no touchable window at (%d, %d) on display %d.", |
| 2199 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2200 | outInjectionResult = InputEventInjectionResult::FAILED; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2201 | goto Failed; |
| 2202 | } |
| 2203 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2204 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2205 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2206 | continue; |
| 2207 | } |
| 2208 | |
| 2209 | // Set target flags. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2210 | ftl::Flags<InputTarget::Flags> targetFlags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2211 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2212 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2213 | // There should only be one touched window that can be "foreground" for the pointer. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2214 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2215 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2216 | |
| 2217 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2218 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2219 | } |
| 2220 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2221 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2222 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2223 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2224 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2225 | |
| 2226 | // Update the temporary touch state. |
| 2227 | BitSet32 pointerIds; |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2228 | pointerIds.markBit(entry.pointerProperties[pointerIndex].id); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2229 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2230 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds, |
| 2231 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 2232 | |
| 2233 | // If this is the pointer going down and the touched window has a wallpaper |
| 2234 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2235 | // of the touch gesture. |
| 2236 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2237 | // engine only supports touch events. We would need to add a mechanism similar |
| 2238 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2239 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2240 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2241 | if (targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 2242 | windowHandle->getInfo()->inputConfig.test( |
| 2243 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2244 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2245 | if (wallpaper != nullptr) { |
| 2246 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2247 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 2248 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED | |
| 2249 | InputTarget::Flags::DISPATCH_AS_IS; |
| 2250 | if (isSplit) { |
| 2251 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2252 | } |
| 2253 | tempTouchState.addOrUpdateWindow(wallpaper, wallpaperFlags, pointerIds, |
| 2254 | entry.eventTime); |
| 2255 | } |
| 2256 | } |
| 2257 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2258 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2259 | |
| 2260 | // If any existing window is pilfering pointers from newly added window, remove it |
| 2261 | BitSet32 canceledPointers = BitSet32(0); |
| 2262 | for (const TouchedWindow& window : tempTouchState.windows) { |
| 2263 | if (window.isPilferingPointers) { |
| 2264 | canceledPointers |= window.pointerIds; |
| 2265 | } |
| 2266 | } |
| 2267 | tempTouchState.cancelPointersForNonPilferingWindows(canceledPointers); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2268 | } else { |
| 2269 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2270 | |
| 2271 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2272 | if (!tempTouchState.isDown()) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 2273 | ALOGD_IF(DEBUG_FOCUS, |
| 2274 | "Dropping event because the pointer is not down or we previously " |
| 2275 | "dropped the pointer down event in display %" PRId32 ": %s", |
| 2276 | displayId, entry.getDescription().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2277 | outInjectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2278 | goto Failed; |
| 2279 | } |
| 2280 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2281 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2282 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2283 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2284 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2285 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2286 | const auto [x, y] = resolveTouchedPosition(entry); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2287 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2288 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2289 | tempTouchState.getFirstForegroundWindowHandle(); |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2290 | newTouchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2291 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2292 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2293 | // Verify targeted injection. |
| 2294 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2295 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2296 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2297 | newTouchedWindowHandle = nullptr; |
| 2298 | goto Failed; |
| 2299 | } |
| 2300 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2301 | // Drop touch events if requested by input feature |
| 2302 | if (newTouchedWindowHandle != nullptr && |
| 2303 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2304 | newTouchedWindowHandle = nullptr; |
| 2305 | } |
| 2306 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2307 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2308 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2309 | if (DEBUG_FOCUS) { |
| 2310 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2311 | oldTouchedWindowHandle->getName().c_str(), |
| 2312 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2313 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2314 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2315 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2316 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2317 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2318 | |
| 2319 | // Make a slippery entrance into the new window. |
| 2320 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2321 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2322 | } |
| 2323 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2324 | ftl::Flags<InputTarget::Flags> targetFlags = |
| 2325 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2326 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2327 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2328 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2329 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2330 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2331 | } |
| 2332 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2333 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2334 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2335 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | BitSet32 pointerIds; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2339 | pointerIds.markBit(entry.pointerProperties[0].id); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2340 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds, |
| 2341 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 2342 | |
| 2343 | // Check if the wallpaper window should deliver the corresponding event. |
| 2344 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
| 2345 | tempTouchState, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2346 | } |
| 2347 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2348 | |
| 2349 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2350 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2351 | // If no split, we suppose all touched windows should receive pointer down. |
| 2352 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2353 | for (size_t i = 0; i < tempTouchState.windows.size(); i++) { |
| 2354 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2355 | // Ignore drag window for it should just track one pointer. |
| 2356 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2357 | continue; |
| 2358 | } |
| 2359 | touchedWindow.pointerIds.markBit(entry.pointerProperties[pointerIndex].id); |
| 2360 | } |
| 2361 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2362 | } |
| 2363 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2364 | // Update dispatching for hover enter and exit. |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2365 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
| 2366 | // Let the previous window know that the hover sequence is over, unless we already did |
| 2367 | // it when dispatching it as is to newTouchedWindowHandle. |
| 2368 | if (mLastHoverWindowHandle != nullptr && |
| 2369 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2370 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
| 2371 | if (DEBUG_HOVER) { |
| 2372 | ALOGD("Sending hover exit event to window %s.", |
| 2373 | mLastHoverWindowHandle->getName().c_str()); |
| 2374 | } |
| 2375 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2376 | InputTarget::Flags::DISPATCH_AS_HOVER_EXIT, |
| 2377 | BitSet32(0)); |
| 2378 | } |
| 2379 | |
| 2380 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2381 | // when dispatching it as is to newTouchedWindowHandle. |
| 2382 | if (newHoverWindowHandle != nullptr && |
| 2383 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2384 | newHoverWindowHandle != newTouchedWindowHandle)) { |
| 2385 | if (DEBUG_HOVER) { |
| 2386 | ALOGD("Sending hover enter event to window %s.", |
| 2387 | newHoverWindowHandle->getName().c_str()); |
| 2388 | } |
| 2389 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2390 | InputTarget::Flags::DISPATCH_AS_HOVER_ENTER, |
| 2391 | BitSet32(0)); |
| 2392 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2393 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2394 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2395 | // Ensure that we have at least one foreground window or at least one window that cannot be a |
| 2396 | // foreground target. If we only have windows that are not receiving foreground touches (e.g. we |
| 2397 | // only have windows getting ACTION_OUTSIDE), then drop the event, because there is no window |
| 2398 | // that is actually receiving the entire gesture. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2399 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2400 | [](const TouchedWindow& touchedWindow) { |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2401 | return !canReceiveForegroundTouches( |
| 2402 | *touchedWindow.windowHandle->getInfo()) || |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2403 | touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2404 | })) { |
Siarhei Vishniakou | 1fb1891 | 2022-03-08 10:31:39 -0800 | [diff] [blame] | 2405 | ALOGI("Dropping event because there is no touched window on display %d to receive it: %s", |
| 2406 | displayId, entry.getDescription().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2407 | outInjectionResult = InputEventInjectionResult::FAILED; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2408 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2409 | } |
| 2410 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2411 | // Ensure that all touched windows are valid for injection. |
| 2412 | if (entry.injectionState != nullptr) { |
| 2413 | std::string errs; |
| 2414 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2415 | if (touchedWindow.targetFlags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2416 | // Allow ACTION_OUTSIDE events generated by targeted injection to be |
| 2417 | // dispatched to any uid, since the coords will be zeroed out later. |
| 2418 | continue; |
| 2419 | } |
| 2420 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2421 | if (err) errs += "\n - " + *err; |
| 2422 | } |
| 2423 | if (!errs.empty()) { |
| 2424 | ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " |
| 2425 | "%d:%s", |
| 2426 | *entry.injectionState->targetUid, errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2427 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2428 | goto Failed; |
| 2429 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2430 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2431 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2432 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2433 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2434 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2435 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2436 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2437 | if (foregroundWindowHandle) { |
| 2438 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2439 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2440 | if (touchedWindow.targetFlags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2441 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2442 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2443 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2444 | InputTarget::Flags::ZERO_COORDS, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2445 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2446 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | } |
| 2450 | } |
| 2451 | |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2452 | // Success! Output targets. |
| 2453 | touchedWindows = tempTouchState.windows; |
Siarhei Vishniakou | d57302f | 2022-11-08 11:12:29 -0800 | [diff] [blame] | 2454 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2455 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2456 | // Drop the outside or hover touch windows since we will not care about them |
| 2457 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2458 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2459 | |
| 2460 | Failed: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2461 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2462 | if (switchedDevice) { |
| 2463 | if (DEBUG_FOCUS) { |
| 2464 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2465 | } |
| 2466 | *outConflictingPointerActions = true; |
| 2467 | } |
| 2468 | |
| 2469 | if (isHoverAction) { |
| 2470 | // Started hovering, therefore no longer down. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2471 | if (oldState && oldState->isDown()) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 2472 | ALOGD_IF(DEBUG_FOCUS, |
| 2473 | "Conflicting pointer actions: Hover received while pointer was down."); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2474 | *outConflictingPointerActions = true; |
| 2475 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2476 | tempTouchState.reset(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2477 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2478 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2479 | tempTouchState.deviceId = entry.deviceId; |
| 2480 | tempTouchState.source = entry.source; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2481 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2482 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2483 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2484 | // All pointers up or canceled. |
| 2485 | tempTouchState.reset(); |
| 2486 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2487 | // First pointer went down. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2488 | if (oldState && oldState->isDown()) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 2489 | ALOGD("Conflicting pointer actions: Down received while already down."); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2490 | *outConflictingPointerActions = true; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2491 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2492 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2493 | // One pointer went up. |
| 2494 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2495 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2496 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2497 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2498 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2499 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2500 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2501 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2502 | continue; |
| 2503 | } |
| 2504 | i += 1; |
| 2505 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2506 | } |
| 2507 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2508 | // Save changes unless the action was scroll in which case the temporary touch |
| 2509 | // state was only valid for this one action. |
| 2510 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2511 | if (displayId >= 0) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2512 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2513 | } else { |
| 2514 | mTouchStatesByDisplay.erase(displayId); |
| 2515 | } |
| 2516 | } |
| 2517 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2518 | if (tempTouchState.windows.empty()) { |
| 2519 | mTouchStatesByDisplay.erase(displayId); |
| 2520 | } |
| 2521 | |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2522 | // Update hover state. |
| 2523 | mLastHoverWindowHandle = newHoverWindowHandle; |
| 2524 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2525 | return touchedWindows; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2526 | } |
| 2527 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2528 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2529 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2530 | // have an explicit reason to support it. |
| 2531 | constexpr bool isStylus = false; |
| 2532 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2533 | const sp<WindowInfoHandle> dropWindow = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2534 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2535 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2536 | if (dropWindow) { |
| 2537 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2538 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2539 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2540 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2541 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2542 | } |
| 2543 | mDragState.reset(); |
| 2544 | } |
| 2545 | |
| 2546 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2547 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2548 | return; |
| 2549 | } |
| 2550 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2551 | if (!mDragState->isStartDrag) { |
| 2552 | mDragState->isStartDrag = true; |
| 2553 | mDragState->isStylusButtonDownAtStart = |
| 2554 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2555 | } |
| 2556 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2557 | // Find the pointer index by id. |
| 2558 | int32_t pointerIndex = 0; |
| 2559 | for (; static_cast<uint32_t>(pointerIndex) < entry.pointerCount; pointerIndex++) { |
| 2560 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2561 | if (pointerProperties.id == mDragState->pointerId) { |
| 2562 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2563 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2564 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2565 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2566 | if (uint32_t(pointerIndex) == entry.pointerCount) { |
| 2567 | 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] | 2568 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2569 | mDragState.reset(); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2570 | return; |
| 2571 | } |
| 2572 | |
| 2573 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2574 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2575 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2576 | |
| 2577 | switch (maskedAction) { |
| 2578 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2579 | // Handle the special case : stylus button no longer pressed. |
| 2580 | bool isStylusButtonDown = |
| 2581 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2582 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2583 | finishDragAndDrop(entry.displayId, x, y); |
| 2584 | return; |
| 2585 | } |
| 2586 | |
| 2587 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2588 | // until we have an explicit reason to support it. |
| 2589 | constexpr bool isStylus = false; |
| 2590 | |
| 2591 | const sp<WindowInfoHandle> hoverWindowHandle = |
| 2592 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
| 2593 | isStylus, false /*addOutsideTargets*/, |
| 2594 | true /*ignoreDragWindow*/); |
| 2595 | // enqueue drag exit if needed. |
| 2596 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2597 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2598 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2599 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, x, |
| 2600 | y); |
| 2601 | } |
| 2602 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2603 | } |
| 2604 | // enqueue drag location if needed. |
| 2605 | if (hoverWindowHandle != nullptr) { |
| 2606 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, x, y); |
| 2607 | } |
| 2608 | break; |
| 2609 | } |
| 2610 | |
| 2611 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 2612 | if (getMotionEventActionPointerIndex(entry.action) != pointerIndex) { |
| 2613 | break; |
| 2614 | } |
| 2615 | // The drag pointer is up. |
| 2616 | [[fallthrough]]; |
| 2617 | case AMOTION_EVENT_ACTION_UP: |
| 2618 | finishDragAndDrop(entry.displayId, x, y); |
| 2619 | break; |
| 2620 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2621 | ALOGD("Receiving cancel when drag and drop."); |
| 2622 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2623 | mDragState.reset(); |
| 2624 | break; |
| 2625 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2626 | } |
| 2627 | } |
| 2628 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2629 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2630 | ftl::Flags<InputTarget::Flags> targetFlags, |
| 2631 | BitSet32 pointerIds, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2632 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2633 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2634 | std::vector<InputTarget>::iterator it = |
| 2635 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2636 | [&windowHandle](const InputTarget& inputTarget) { |
| 2637 | return inputTarget.inputChannel->getConnectionToken() == |
| 2638 | windowHandle->getToken(); |
| 2639 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2640 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2641 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2642 | |
| 2643 | if (it == inputTargets.end()) { |
| 2644 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2645 | std::shared_ptr<InputChannel> inputChannel = |
| 2646 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2647 | if (inputChannel == nullptr) { |
| 2648 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2649 | return; |
| 2650 | } |
| 2651 | inputTarget.inputChannel = inputChannel; |
| 2652 | inputTarget.flags = targetFlags; |
| 2653 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2654 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2655 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2656 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2657 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2658 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2659 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2660 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2661 | inputTargets.push_back(inputTarget); |
| 2662 | it = inputTargets.end() - 1; |
| 2663 | } |
| 2664 | |
| 2665 | ALOG_ASSERT(it->flags == targetFlags); |
| 2666 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2667 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2668 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2669 | } |
| 2670 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2671 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2672 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2673 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2674 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2675 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2676 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2677 | InputTarget target; |
| 2678 | target.inputChannel = monitor.inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2679 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2680 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2681 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2682 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2683 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2684 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2685 | target.setDefaultPointerTransform(target.displayTransform); |
| 2686 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2687 | } |
| 2688 | } |
| 2689 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2690 | /** |
| 2691 | * Indicate whether one window handle should be considered as obscuring |
| 2692 | * another window handle. We only check a few preconditions. Actually |
| 2693 | * checking the bounds is left to the caller. |
| 2694 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2695 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2696 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2697 | // Compare by token so cloned layers aren't counted |
| 2698 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2699 | return false; |
| 2700 | } |
| 2701 | auto info = windowHandle->getInfo(); |
| 2702 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2703 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2704 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2705 | } else if (otherInfo->alpha == 0 && |
| 2706 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2707 | // Those act as if they were invisible, so we don't need to flag them. |
| 2708 | // We do want to potentially flag touchable windows even if they have 0 |
| 2709 | // opacity, since they can consume touches and alter the effects of the |
| 2710 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2711 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2712 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2713 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2714 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2715 | // If ownerUid is the same we don't generate occlusion events as there |
| 2716 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2717 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2718 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2719 | return false; |
| 2720 | } else if (otherInfo->displayId != info->displayId) { |
| 2721 | return false; |
| 2722 | } |
| 2723 | return true; |
| 2724 | } |
| 2725 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2726 | /** |
| 2727 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2728 | * untrusted, one should check: |
| 2729 | * |
| 2730 | * 1. If result.hasBlockingOcclusion is true. |
| 2731 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2732 | * BLOCK_UNTRUSTED. |
| 2733 | * |
| 2734 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2735 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2736 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2737 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2738 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2739 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2740 | * |
| 2741 | * If neither of those is true, then it means the touch can be allowed. |
| 2742 | */ |
| 2743 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2744 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2745 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2746 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2747 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2748 | TouchOcclusionInfo info; |
| 2749 | info.hasBlockingOcclusion = false; |
| 2750 | info.obscuringOpacity = 0; |
| 2751 | info.obscuringUid = -1; |
| 2752 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2753 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2754 | if (windowHandle == otherHandle) { |
| 2755 | break; // All future windows are below us. Exit early. |
| 2756 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2757 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2758 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2759 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2760 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2761 | info.debugInfo.push_back( |
| 2762 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2763 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2764 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2765 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2766 | // window's touch occlusion mode |
| 2767 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2768 | info.hasBlockingOcclusion = true; |
| 2769 | info.obscuringUid = otherInfo->ownerUid; |
| 2770 | info.obscuringPackage = otherInfo->packageName; |
| 2771 | break; |
| 2772 | } |
| 2773 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2774 | uint32_t uid = otherInfo->ownerUid; |
| 2775 | float opacity = |
| 2776 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2777 | // Given windows A and B: |
| 2778 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2779 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2780 | opacityByUid[uid] = opacity; |
| 2781 | if (opacity > info.obscuringOpacity) { |
| 2782 | info.obscuringOpacity = opacity; |
| 2783 | info.obscuringUid = uid; |
| 2784 | info.obscuringPackage = otherInfo->packageName; |
| 2785 | } |
| 2786 | } |
| 2787 | } |
| 2788 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2789 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2790 | info.debugInfo.push_back( |
| 2791 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2792 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2793 | return info; |
| 2794 | } |
| 2795 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2796 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2797 | bool isTouchedWindow) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2798 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2799 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2800 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 2801 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2802 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 2803 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 2804 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 2805 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 2806 | info->name.c_str(), info->inputConfig.string().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2807 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2808 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2811 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2812 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2813 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2814 | occlusionInfo.obscuringUid); |
| 2815 | return false; |
| 2816 | } |
| 2817 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2818 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2819 | "%.2f, maximum allowed = %.2f)", |
| 2820 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2821 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2822 | return false; |
| 2823 | } |
| 2824 | return true; |
| 2825 | } |
| 2826 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2827 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2828 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2829 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2830 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2831 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2832 | if (windowHandle == otherHandle) { |
| 2833 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2834 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2835 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2836 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2837 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2838 | return true; |
| 2839 | } |
| 2840 | } |
| 2841 | return false; |
| 2842 | } |
| 2843 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2844 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2845 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2846 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2847 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2848 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2849 | if (windowHandle == otherHandle) { |
| 2850 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2851 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2852 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2853 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2854 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2855 | return true; |
| 2856 | } |
| 2857 | } |
| 2858 | return false; |
| 2859 | } |
| 2860 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2861 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2862 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2863 | if (applicationHandle != nullptr) { |
| 2864 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2865 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2866 | } else { |
| 2867 | return applicationHandle->getName(); |
| 2868 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2869 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2870 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2871 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2872 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2873 | } |
| 2874 | } |
| 2875 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2876 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2877 | if (!isUserActivityEvent(eventEntry)) { |
| 2878 | // 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] | 2879 | return; |
| 2880 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2881 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2882 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2883 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2884 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2885 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2886 | if (DEBUG_DISPATCH_CYCLE) { |
| 2887 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2888 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2889 | return; |
| 2890 | } |
| 2891 | } |
| 2892 | |
| 2893 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2894 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2895 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2896 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2897 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2898 | return; |
| 2899 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2900 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2901 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2902 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2903 | } |
| 2904 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2905 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2906 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2907 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2908 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2909 | return; |
| 2910 | } |
| 2911 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2912 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2913 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2914 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2915 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2916 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2917 | break; |
| 2918 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2919 | } |
| 2920 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2921 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2922 | REQUIRES(mLock) { |
| 2923 | scoped_unlock unlock(mLock); |
| 2924 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2925 | }; |
| 2926 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2930 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2931 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2932 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2933 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2934 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2935 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2936 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2937 | ATRACE_NAME(message.c_str()); |
| 2938 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2939 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2940 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2941 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2942 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2943 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2944 | inputTarget.getPointerInfoString().c_str()); |
| 2945 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2946 | |
| 2947 | // Skip this event if the connection status is not normal. |
| 2948 | // 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] | 2949 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2950 | if (DEBUG_DISPATCH_CYCLE) { |
| 2951 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2952 | connection->getInputChannelName().c_str(), |
| 2953 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2954 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2955 | return; |
| 2956 | } |
| 2957 | |
| 2958 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2959 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2960 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2961 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2962 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2963 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2964 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2965 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2966 | LOG_ALWAYS_FATAL_IF(!inputTarget.firstDownTimeInTarget.has_value(), |
| 2967 | "Splitting motion events requires a down time to be set for the " |
| 2968 | "target"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2969 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2970 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 2971 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2972 | if (!splitMotionEntry) { |
| 2973 | return; // split event was dropped |
| 2974 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2975 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2976 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2977 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2978 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2979 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2980 | if (DEBUG_FOCUS) { |
| 2981 | ALOGD("channel '%s' ~ Split motion event.", |
| 2982 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2983 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2984 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2985 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2986 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2987 | return; |
| 2988 | } |
| 2989 | } |
| 2990 | |
| 2991 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2992 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2993 | } |
| 2994 | |
| 2995 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2996 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2997 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2998 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2999 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3000 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3001 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3002 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3003 | ATRACE_NAME(message.c_str()); |
| 3004 | } |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3005 | LOG_ALWAYS_FATAL_IF(!inputTarget.flags.any(InputTarget::DISPATCH_MASK), |
| 3006 | "No dispatch flags are set for %s", eventEntry->getDescription().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3007 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3008 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3009 | |
| 3010 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3011 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3012 | InputTarget::Flags::DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3013 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3014 | InputTarget::Flags::DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3015 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3016 | InputTarget::Flags::DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3017 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3018 | InputTarget::Flags::DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3019 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3020 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3021 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3022 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3023 | |
| 3024 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3025 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3026 | startDispatchCycleLocked(currentTime, connection); |
| 3027 | } |
| 3028 | } |
| 3029 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3030 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3031 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3032 | const InputTarget& inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3033 | ftl::Flags<InputTarget::Flags> dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3034 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3035 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 3036 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3037 | dispatchMode.string().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3038 | ATRACE_NAME(message.c_str()); |
| 3039 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3040 | ftl::Flags<InputTarget::Flags> inputTargetFlags = inputTarget.flags; |
| 3041 | if (!inputTargetFlags.any(dispatchMode)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3042 | return; |
| 3043 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3044 | |
| 3045 | inputTargetFlags.clear(InputTarget::DISPATCH_MASK); |
| 3046 | inputTargetFlags |= dispatchMode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3047 | |
| 3048 | // This is a new event. |
| 3049 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3050 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3051 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3052 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3053 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3054 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3055 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3056 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3057 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3058 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3059 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3060 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3061 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3062 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3063 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3064 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3065 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3066 | if (DEBUG_DISPATCH_CYCLE) { |
| 3067 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3068 | "event", |
| 3069 | connection->getInputChannelName().c_str()); |
| 3070 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3071 | return; // skip the inconsistent event |
| 3072 | } |
| 3073 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3074 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3075 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3076 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3077 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3078 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3079 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3080 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3081 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3082 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3083 | if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3084 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3085 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_EXIT)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3086 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3087 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_ENTER)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3088 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3089 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3090 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3091 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3092 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3093 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3094 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3095 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3096 | } |
| 3097 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3098 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3099 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3100 | if (DEBUG_DISPATCH_CYCLE) { |
| 3101 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3102 | "enter event", |
| 3103 | connection->getInputChannelName().c_str()); |
| 3104 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3105 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3106 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3107 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3108 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3109 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3110 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3111 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3112 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3113 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3114 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3115 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3116 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3117 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3118 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3119 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3120 | if (DEBUG_DISPATCH_CYCLE) { |
| 3121 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3122 | "event", |
| 3123 | connection->getInputChannelName().c_str()); |
| 3124 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3125 | return; // skip the inconsistent event |
| 3126 | } |
| 3127 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3128 | dispatchEntry->resolvedEventId = |
| 3129 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3130 | ? mIdGenerator.nextId() |
| 3131 | : motionEntry.id; |
| 3132 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3133 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3134 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3135 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3136 | ATRACE_NAME(message.c_str()); |
| 3137 | } |
| 3138 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3139 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3140 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3141 | // Skip reporting pointer down outside focus to the policy. |
| 3142 | break; |
| 3143 | } |
| 3144 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3145 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3146 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3147 | |
| 3148 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3149 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3150 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3151 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3152 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3153 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3154 | break; |
| 3155 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3156 | case EventEntry::Type::SENSOR: { |
| 3157 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3158 | break; |
| 3159 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3160 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3161 | case EventEntry::Type::DEVICE_RESET: { |
| 3162 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3163 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3164 | break; |
| 3165 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | // Remember that we are waiting for this dispatch to complete. |
| 3169 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3170 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3174 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3175 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3176 | } |
| 3177 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3178 | /** |
| 3179 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3180 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3181 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3182 | * We will see both launcher and wallpaper in that list. |
| 3183 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3184 | * until the set of interacted connections changes. |
| 3185 | * |
| 3186 | * The following items are skipped, to reduce the logspam: |
| 3187 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3188 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3189 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3190 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3191 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3192 | */ |
| 3193 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3194 | const std::vector<InputTarget>& targets) { |
| 3195 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3196 | if (entry.type == EventEntry::Type::KEY) { |
| 3197 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3198 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3199 | return; |
| 3200 | } |
| 3201 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3202 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3203 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3204 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3205 | return; |
| 3206 | } |
| 3207 | } else { |
| 3208 | return; // Not a key or a motion |
| 3209 | } |
| 3210 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3211 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3212 | std::vector<sp<Connection>> newConnections; |
| 3213 | for (const InputTarget& target : targets) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3214 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3215 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3216 | } |
| 3217 | |
| 3218 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3219 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3220 | if (connection == nullptr) { |
| 3221 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3222 | } |
| 3223 | newConnectionTokens.insert(std::move(token)); |
| 3224 | newConnections.emplace_back(connection); |
| 3225 | } |
| 3226 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3227 | return; // no change |
| 3228 | } |
| 3229 | mInteractionConnectionTokens = newConnectionTokens; |
| 3230 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3231 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3232 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3233 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3234 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3235 | std::string message = "Interaction with: " + targetList; |
| 3236 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3237 | message += "<none>"; |
| 3238 | } |
| 3239 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3240 | } |
| 3241 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3242 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3243 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3244 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3245 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3246 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3247 | return; |
| 3248 | } |
| 3249 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3250 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3251 | if (focusedToken == token) { |
| 3252 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3253 | return; |
| 3254 | } |
| 3255 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3256 | auto command = [this, token]() REQUIRES(mLock) { |
| 3257 | scoped_unlock unlock(mLock); |
| 3258 | mPolicy->onPointerDownOutsideFocus(token); |
| 3259 | }; |
| 3260 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3261 | } |
| 3262 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3263 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3264 | DispatchEntry& dispatchEntry) const { |
| 3265 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3266 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3267 | |
| 3268 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 3269 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
| 3270 | |
| 3271 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3272 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3273 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3274 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3275 | if (globalScaleFactor != 1.0f) { |
| 3276 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3277 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3278 | // Don't apply window scale here since we don't want scale to affect raw |
| 3279 | // coordinates. The scale will be sent back to the client and applied |
| 3280 | // later when requesting relative coordinates. |
| 3281 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3282 | 1 /* windowYScale */); |
| 3283 | } |
| 3284 | usingCoords = scaledCoords; |
| 3285 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3286 | } else if (dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS)) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3287 | // We don't want the dispatch target to know the coordinates |
| 3288 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3289 | scaledCoords[i].clear(); |
| 3290 | } |
| 3291 | usingCoords = scaledCoords; |
| 3292 | } |
| 3293 | |
| 3294 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3295 | |
| 3296 | // Publish the motion event. |
| 3297 | return connection.inputPublisher |
| 3298 | .publishMotionEvent(dispatchEntry.seq, dispatchEntry.resolvedEventId, |
| 3299 | motionEntry.deviceId, motionEntry.source, motionEntry.displayId, |
| 3300 | std::move(hmac), dispatchEntry.resolvedAction, |
| 3301 | motionEntry.actionButton, dispatchEntry.resolvedFlags, |
| 3302 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3303 | motionEntry.buttonState, motionEntry.classification, |
| 3304 | dispatchEntry.transform, motionEntry.xPrecision, |
| 3305 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 3306 | motionEntry.yCursorPosition, dispatchEntry.rawTransform, |
| 3307 | motionEntry.downTime, motionEntry.eventTime, |
| 3308 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 3309 | usingCoords); |
| 3310 | } |
| 3311 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3312 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3313 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3314 | if (ATRACE_ENABLED()) { |
| 3315 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3316 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3317 | ATRACE_NAME(message.c_str()); |
| 3318 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3319 | if (DEBUG_DISPATCH_CYCLE) { |
| 3320 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3321 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3322 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3323 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3324 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3325 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3326 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3327 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3328 | |
| 3329 | // Publish the event. |
| 3330 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3331 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3332 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3333 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3334 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3335 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3336 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3337 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3338 | status = connection->inputPublisher |
| 3339 | .publishKeyEvent(dispatchEntry->seq, |
| 3340 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3341 | keyEntry.source, keyEntry.displayId, |
| 3342 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3343 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3344 | keyEntry.scanCode, keyEntry.metaState, |
| 3345 | keyEntry.repeatCount, keyEntry.downTime, |
| 3346 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3347 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3348 | } |
| 3349 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3350 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3351 | status = publishMotionEvent(*connection, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3352 | break; |
| 3353 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3354 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3355 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3356 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3357 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3358 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3359 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3360 | break; |
| 3361 | } |
| 3362 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3363 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3364 | const TouchModeEntry& touchModeEntry = |
| 3365 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3366 | status = connection->inputPublisher |
| 3367 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3368 | touchModeEntry.inTouchMode); |
| 3369 | |
| 3370 | break; |
| 3371 | } |
| 3372 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3373 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3374 | const auto& captureEntry = |
| 3375 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3376 | status = connection->inputPublisher |
| 3377 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3378 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3379 | break; |
| 3380 | } |
| 3381 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3382 | case EventEntry::Type::DRAG: { |
| 3383 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3384 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3385 | dragEntry.id, dragEntry.x, |
| 3386 | dragEntry.y, |
| 3387 | dragEntry.isExiting); |
| 3388 | break; |
| 3389 | } |
| 3390 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3391 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3392 | case EventEntry::Type::DEVICE_RESET: |
| 3393 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3394 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3395 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3396 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3397 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | // Check the result. |
| 3401 | if (status) { |
| 3402 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3403 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3404 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3405 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3406 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3407 | "event to it, status=%s(%d)", |
| 3408 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3409 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3410 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3411 | } else { |
| 3412 | // Pipe is full and we are waiting for the app to finish process some events |
| 3413 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3414 | if (DEBUG_DISPATCH_CYCLE) { |
| 3415 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3416 | "waiting for the application to catch up", |
| 3417 | connection->getInputChannelName().c_str()); |
| 3418 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3419 | } |
| 3420 | } else { |
| 3421 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3422 | "status=%s(%d)", |
| 3423 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3424 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3425 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3426 | } |
| 3427 | return; |
| 3428 | } |
| 3429 | |
| 3430 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3431 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3432 | connection->outboundQueue.end(), |
| 3433 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3434 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3435 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3436 | if (connection->responsive) { |
| 3437 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3438 | connection->inputChannel->getConnectionToken()); |
| 3439 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3440 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3441 | } |
| 3442 | } |
| 3443 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3444 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3445 | size_t size; |
| 3446 | switch (event.type) { |
| 3447 | case VerifiedInputEvent::Type::KEY: { |
| 3448 | size = sizeof(VerifiedKeyEvent); |
| 3449 | break; |
| 3450 | } |
| 3451 | case VerifiedInputEvent::Type::MOTION: { |
| 3452 | size = sizeof(VerifiedMotionEvent); |
| 3453 | break; |
| 3454 | } |
| 3455 | } |
| 3456 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3457 | return mHmacKeyManager.sign(start, size); |
| 3458 | } |
| 3459 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3460 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3461 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3462 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3463 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3464 | // Only sign events up and down events as the purely move events |
| 3465 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3466 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3467 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3468 | |
| 3469 | VerifiedMotionEvent verifiedEvent = |
| 3470 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3471 | verifiedEvent.actionMasked = actionMasked; |
| 3472 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3473 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3474 | } |
| 3475 | |
| 3476 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3477 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3478 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3479 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3480 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3481 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3482 | } |
| 3483 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3484 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3485 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3486 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3487 | if (DEBUG_DISPATCH_CYCLE) { |
| 3488 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3489 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3490 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3491 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3492 | if (connection->status == Connection::Status::BROKEN || |
| 3493 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3494 | return; |
| 3495 | } |
| 3496 | |
| 3497 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3498 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3499 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3500 | }; |
| 3501 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3505 | const sp<Connection>& connection, |
| 3506 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3507 | if (DEBUG_DISPATCH_CYCLE) { |
| 3508 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3509 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3510 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3511 | |
| 3512 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3513 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3514 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3515 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3516 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3517 | |
| 3518 | // The connection appears to be unrecoverably broken. |
| 3519 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3520 | if (connection->status == Connection::Status::NORMAL) { |
| 3521 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3522 | |
| 3523 | if (notify) { |
| 3524 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3525 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3526 | connection->getInputChannelName().c_str()); |
| 3527 | |
| 3528 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3529 | scoped_unlock unlock(mLock); |
| 3530 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3531 | }; |
| 3532 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3533 | } |
| 3534 | } |
| 3535 | } |
| 3536 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3537 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3538 | while (!queue.empty()) { |
| 3539 | DispatchEntry* dispatchEntry = queue.front(); |
| 3540 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3541 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3542 | } |
| 3543 | } |
| 3544 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3545 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3546 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3547 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3548 | } |
| 3549 | delete dispatchEntry; |
| 3550 | } |
| 3551 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3552 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3553 | std::scoped_lock _l(mLock); |
| 3554 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3555 | if (connection == nullptr) { |
| 3556 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3557 | connectionToken.get(), events); |
| 3558 | return 0; // remove the callback |
| 3559 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3560 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3561 | bool notify; |
| 3562 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3563 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3564 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3565 | "events=0x%x", |
| 3566 | connection->getInputChannelName().c_str(), events); |
| 3567 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3568 | } |
| 3569 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3570 | nsecs_t currentTime = now(); |
| 3571 | bool gotOne = false; |
| 3572 | status_t status = OK; |
| 3573 | for (;;) { |
| 3574 | Result<InputPublisher::ConsumerResponse> result = |
| 3575 | connection->inputPublisher.receiveConsumerResponse(); |
| 3576 | if (!result.ok()) { |
| 3577 | status = result.error().code(); |
| 3578 | break; |
| 3579 | } |
| 3580 | |
| 3581 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3582 | const InputPublisher::Finished& finish = |
| 3583 | std::get<InputPublisher::Finished>(*result); |
| 3584 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3585 | finish.consumeTime); |
| 3586 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3587 | if (shouldReportMetricsForConnection(*connection)) { |
| 3588 | const InputPublisher::Timeline& timeline = |
| 3589 | std::get<InputPublisher::Timeline>(*result); |
| 3590 | mLatencyTracker |
| 3591 | .trackGraphicsLatency(timeline.inputEventId, |
| 3592 | connection->inputChannel->getConnectionToken(), |
| 3593 | std::move(timeline.graphicsTimeline)); |
| 3594 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3595 | } |
| 3596 | gotOne = true; |
| 3597 | } |
| 3598 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3599 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3600 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3601 | return 1; |
| 3602 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3603 | } |
| 3604 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3605 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3606 | if (notify) { |
| 3607 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3608 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3609 | status); |
| 3610 | } |
| 3611 | } else { |
| 3612 | // Monitor channels are never explicitly unregistered. |
| 3613 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3614 | const bool stillHaveWindowHandle = |
| 3615 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3616 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3617 | if (notify) { |
| 3618 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3619 | connection->getInputChannelName().c_str(), events); |
| 3620 | } |
| 3621 | } |
| 3622 | |
| 3623 | // Remove the channel. |
| 3624 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3625 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3626 | } |
| 3627 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3628 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3629 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3630 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3631 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3632 | } |
| 3633 | } |
| 3634 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3635 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3636 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3637 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3638 | for (const Monitor& monitor : monitors) { |
| 3639 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3640 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3641 | } |
| 3642 | } |
| 3643 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3644 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3645 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3646 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3647 | if (connection == nullptr) { |
| 3648 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3649 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3650 | |
| 3651 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3655 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3656 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3657 | return; |
| 3658 | } |
| 3659 | |
| 3660 | nsecs_t currentTime = now(); |
| 3661 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3662 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3663 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3664 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3665 | if (cancelationEvents.empty()) { |
| 3666 | return; |
| 3667 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3668 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3669 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3670 | "with reality: %s, mode=%d.", |
| 3671 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3672 | options.mode); |
| 3673 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3674 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3675 | std::string reason = std::string("reason=").append(options.reason); |
| 3676 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3677 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3678 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3679 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3680 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3681 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3682 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3683 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3684 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3685 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3686 | } |
| 3687 | target.inputChannel = connection->inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3688 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3689 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3690 | const bool wasEmpty = connection->outboundQueue.empty(); |
| 3691 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3692 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3693 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3694 | switch (cancelationEventEntry->type) { |
| 3695 | case EventEntry::Type::KEY: { |
| 3696 | logOutboundKeyDetails("cancel - ", |
| 3697 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3698 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3699 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3700 | case EventEntry::Type::MOTION: { |
| 3701 | logOutboundMotionDetails("cancel - ", |
| 3702 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3703 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3704 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3705 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3706 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3707 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3708 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3709 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3710 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3711 | break; |
| 3712 | } |
| 3713 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3714 | case EventEntry::Type::DEVICE_RESET: |
| 3715 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3716 | 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] | 3717 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3718 | break; |
| 3719 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3720 | } |
| 3721 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3722 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3723 | InputTarget::Flags::DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3724 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3725 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3726 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3727 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3728 | startDispatchCycleLocked(currentTime, connection); |
| 3729 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3730 | } |
| 3731 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3732 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 3733 | const nsecs_t downTime, const sp<Connection>& connection, |
| 3734 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3735 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3736 | return; |
| 3737 | } |
| 3738 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3739 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3740 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3741 | |
| 3742 | if (downEvents.empty()) { |
| 3743 | return; |
| 3744 | } |
| 3745 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3746 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3747 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3748 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3749 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3750 | |
| 3751 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3752 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3753 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3754 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3755 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3756 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3757 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3758 | } |
| 3759 | target.inputChannel = connection->inputChannel; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 3760 | target.flags = targetFlags; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3761 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3762 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3763 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3764 | switch (downEventEntry->type) { |
| 3765 | case EventEntry::Type::MOTION: { |
| 3766 | logOutboundMotionDetails("down - ", |
| 3767 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3768 | break; |
| 3769 | } |
| 3770 | |
| 3771 | case EventEntry::Type::KEY: |
| 3772 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3773 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3774 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3775 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3776 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3777 | case EventEntry::Type::SENSOR: |
| 3778 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3779 | 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] | 3780 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3781 | break; |
| 3782 | } |
| 3783 | } |
| 3784 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3785 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3786 | InputTarget::Flags::DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3787 | } |
| 3788 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3789 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3790 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3791 | startDispatchCycleLocked(downTime, connection); |
| 3792 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3793 | } |
| 3794 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 3795 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 3796 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options) { |
| 3797 | if (windowHandle != nullptr) { |
| 3798 | sp<Connection> wallpaperConnection = getConnectionLocked(windowHandle->getToken()); |
| 3799 | if (wallpaperConnection != nullptr) { |
| 3800 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, options); |
| 3801 | } |
| 3802 | } |
| 3803 | } |
| 3804 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3805 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3806 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds, nsecs_t splitDownTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3807 | ALOG_ASSERT(pointerIds.value != 0); |
| 3808 | |
| 3809 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3810 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3811 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3812 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3813 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3814 | uint32_t splitPointerCount = 0; |
| 3815 | |
| 3816 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3817 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3818 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3819 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3820 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3821 | if (pointerIds.hasBit(pointerId)) { |
| 3822 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3823 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3824 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3825 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3826 | splitPointerCount += 1; |
| 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | if (splitPointerCount != pointerIds.count()) { |
| 3831 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3832 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3833 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3834 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3835 | // in this way. |
| 3836 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3837 | "we expected there to be %d pointers. This probably means we received " |
| 3838 | "a broken sequence of pointer ids from the input device.", |
| 3839 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3840 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3841 | } |
| 3842 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3843 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3844 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3845 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3846 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3847 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3848 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3849 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3850 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3851 | if (pointerIds.hasBit(pointerId)) { |
| 3852 | if (pointerIds.count() == 1) { |
| 3853 | // The first/last pointer went down/up. |
| 3854 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3855 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3856 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3857 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3858 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3859 | } else { |
| 3860 | // A secondary pointer went down/up. |
| 3861 | uint32_t splitPointerIndex = 0; |
| 3862 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3863 | splitPointerIndex += 1; |
| 3864 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3865 | action = maskedAction | |
| 3866 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3867 | } |
| 3868 | } else { |
| 3869 | // An unrelated pointer changed. |
| 3870 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3871 | } |
| 3872 | } |
| 3873 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3874 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 3875 | LOG_ALWAYS_FATAL_IF(splitDownTime != originalMotionEntry.eventTime, |
| 3876 | "Split motion event has mismatching downTime and eventTime for " |
| 3877 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64 "ms", |
| 3878 | originalMotionEntry.getDescription().c_str(), ns2ms(splitDownTime)); |
| 3879 | } |
| 3880 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3881 | int32_t newId = mIdGenerator.nextId(); |
| 3882 | if (ATRACE_ENABLED()) { |
| 3883 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3884 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3885 | originalMotionEntry.id, newId); |
| 3886 | ATRACE_NAME(message.c_str()); |
| 3887 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3888 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3889 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3890 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3891 | originalMotionEntry.displayId, |
| 3892 | originalMotionEntry.policyFlags, action, |
| 3893 | originalMotionEntry.actionButton, |
| 3894 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3895 | originalMotionEntry.buttonState, |
| 3896 | originalMotionEntry.classification, |
| 3897 | originalMotionEntry.edgeFlags, |
| 3898 | originalMotionEntry.xPrecision, |
| 3899 | originalMotionEntry.yPrecision, |
| 3900 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3901 | originalMotionEntry.yCursorPosition, splitDownTime, |
| 3902 | splitPointerCount, splitPointerProperties, |
| 3903 | splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3904 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3905 | if (originalMotionEntry.injectionState) { |
| 3906 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3907 | splitMotionEntry->injectionState->refCount += 1; |
| 3908 | } |
| 3909 | |
| 3910 | return splitMotionEntry; |
| 3911 | } |
| 3912 | |
| 3913 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3914 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3915 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3916 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3917 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3918 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3919 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3920 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3921 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3922 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3923 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3924 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3925 | } // release lock |
| 3926 | |
| 3927 | if (needWake) { |
| 3928 | mLooper->wake(); |
| 3929 | } |
| 3930 | } |
| 3931 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3932 | /** |
| 3933 | * If one of the meta shortcuts is detected, process them here: |
| 3934 | * Meta + Backspace -> generate BACK |
| 3935 | * Meta + Enter -> generate HOME |
| 3936 | * This will potentially overwrite keyCode and metaState. |
| 3937 | */ |
| 3938 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3939 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3940 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3941 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3942 | if (keyCode == AKEYCODE_DEL) { |
| 3943 | newKeyCode = AKEYCODE_BACK; |
| 3944 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3945 | newKeyCode = AKEYCODE_HOME; |
| 3946 | } |
| 3947 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3948 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3949 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3950 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3951 | keyCode = newKeyCode; |
| 3952 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3953 | } |
| 3954 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3955 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3956 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3957 | // 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] | 3958 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3959 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3960 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3961 | if (replacementIt != mReplacedKeys.end()) { |
| 3962 | keyCode = replacementIt->second; |
| 3963 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3964 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3965 | } |
| 3966 | } |
| 3967 | } |
| 3968 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3969 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3970 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3971 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3972 | "policyFlags=0x%x, action=0x%x, " |
| 3973 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3974 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3975 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3976 | args->downTime); |
| 3977 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3978 | if (!validateKeyEvent(args->action)) { |
| 3979 | return; |
| 3980 | } |
| 3981 | |
| 3982 | uint32_t policyFlags = args->policyFlags; |
| 3983 | int32_t flags = args->flags; |
| 3984 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3985 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3986 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3987 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3988 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3989 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3990 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3991 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3992 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3993 | metaState |= AMETA_FUNCTION_ON; |
| 3994 | } |
| 3995 | |
| 3996 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3997 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3998 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3999 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4000 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4001 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4002 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 4003 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 4004 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4005 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4006 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4007 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4008 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4009 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4010 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4011 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4012 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4013 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4014 | { // acquire lock |
| 4015 | mLock.lock(); |
| 4016 | |
| 4017 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4018 | mLock.unlock(); |
| 4019 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4020 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4021 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4022 | return; // event was consumed by the filter |
| 4023 | } |
| 4024 | |
| 4025 | mLock.lock(); |
| 4026 | } |
| 4027 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4028 | std::unique_ptr<KeyEntry> newEntry = |
| 4029 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 4030 | args->displayId, policyFlags, args->action, flags, |
| 4031 | keyCode, args->scanCode, metaState, repeatCount, |
| 4032 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4033 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4034 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4035 | mLock.unlock(); |
| 4036 | } // release lock |
| 4037 | |
| 4038 | if (needWake) { |
| 4039 | mLooper->wake(); |
| 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 4044 | return mInputFilterEnabled; |
| 4045 | } |
| 4046 | |
| 4047 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4048 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4049 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4050 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4051 | "action=%s, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4052 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4053 | "yCursorPosition=%f, downTime=%" PRId64, |
| 4054 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4055 | args->policyFlags, MotionEvent::actionToString(args->action).c_str(), |
| 4056 | args->actionButton, args->flags, args->metaState, args->buttonState, args->edgeFlags, |
| 4057 | args->xPrecision, args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 4058 | args->downTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4059 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 4060 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 4061 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 4062 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 4063 | "orientation=%f", |
| 4064 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 4065 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4066 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4067 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4068 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4069 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4070 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4071 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4072 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4073 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 4074 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4075 | } |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 4076 | LOG_ALWAYS_FATAL_IF(!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 4077 | args->pointerProperties), |
| 4078 | "Invalid event: %s", args->dump().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4079 | |
| 4080 | uint32_t policyFlags = args->policyFlags; |
| 4081 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4082 | |
| 4083 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4084 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4085 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4086 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4087 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4088 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4089 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4090 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4091 | { // acquire lock |
| 4092 | mLock.lock(); |
| 4093 | |
| 4094 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4095 | ui::Transform displayTransform; |
| 4096 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4097 | displayTransform = it->second.transform; |
| 4098 | } |
| 4099 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4100 | mLock.unlock(); |
| 4101 | |
| 4102 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4103 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4104 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4105 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4106 | displayTransform, args->xPrecision, args->yPrecision, |
| 4107 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4108 | args->downTime, args->eventTime, args->pointerCount, |
| 4109 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4110 | |
| 4111 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4112 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4113 | return; // event was consumed by the filter |
| 4114 | } |
| 4115 | |
| 4116 | mLock.lock(); |
| 4117 | } |
| 4118 | |
| 4119 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4120 | std::unique_ptr<MotionEntry> newEntry = |
| 4121 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4122 | args->source, args->displayId, policyFlags, |
| 4123 | args->action, args->actionButton, args->flags, |
| 4124 | args->metaState, args->buttonState, |
| 4125 | args->classification, args->edgeFlags, |
| 4126 | args->xPrecision, args->yPrecision, |
| 4127 | args->xCursorPosition, args->yCursorPosition, |
| 4128 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4129 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4130 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4131 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4132 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4133 | !mInputFilterEnabled) { |
| 4134 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4135 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4136 | } |
| 4137 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4138 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4139 | mLock.unlock(); |
| 4140 | } // release lock |
| 4141 | |
| 4142 | if (needWake) { |
| 4143 | mLooper->wake(); |
| 4144 | } |
| 4145 | } |
| 4146 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4147 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4148 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4149 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4150 | " sensorType=%s", |
| 4151 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4152 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4153 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4154 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4155 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4156 | { // acquire lock |
| 4157 | mLock.lock(); |
| 4158 | |
| 4159 | // Just enqueue a new sensor event. |
| 4160 | std::unique_ptr<SensorEntry> newEntry = |
| 4161 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4162 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4163 | args->sensorType, args->accuracy, |
| 4164 | args->accuracyChanged, args->values); |
| 4165 | |
| 4166 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4167 | mLock.unlock(); |
| 4168 | } // release lock |
| 4169 | |
| 4170 | if (needWake) { |
| 4171 | mLooper->wake(); |
| 4172 | } |
| 4173 | } |
| 4174 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4175 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4176 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4177 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4178 | args->deviceId, args->isOn); |
| 4179 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4180 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4181 | } |
| 4182 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4183 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4184 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4188 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4189 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4190 | "switchMask=0x%08x", |
| 4191 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4192 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4193 | |
| 4194 | uint32_t policyFlags = args->policyFlags; |
| 4195 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4196 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4197 | } |
| 4198 | |
| 4199 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4200 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4201 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4202 | args->deviceId); |
| 4203 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4204 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4205 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4206 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4207 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4208 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4209 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4210 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4211 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4212 | } // release lock |
| 4213 | |
| 4214 | if (needWake) { |
| 4215 | mLooper->wake(); |
| 4216 | } |
| 4217 | } |
| 4218 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4219 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4220 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4221 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4222 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4223 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4224 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4225 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4226 | { // acquire lock |
| 4227 | std::scoped_lock _l(mLock); |
| 4228 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4229 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4230 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4231 | } // release lock |
| 4232 | |
| 4233 | if (needWake) { |
| 4234 | mLooper->wake(); |
| 4235 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4236 | } |
| 4237 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4238 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
| 4239 | std::optional<int32_t> targetUid, |
| 4240 | InputEventInjectionSync syncMode, |
| 4241 | std::chrono::milliseconds timeout, |
| 4242 | uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4243 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4244 | ALOGD("injectInputEvent - eventType=%d, targetUid=%s, syncMode=%d, timeout=%lld, " |
| 4245 | "policyFlags=0x%08x", |
| 4246 | event->getType(), targetUid ? std::to_string(*targetUid).c_str() : "none", syncMode, |
| 4247 | timeout.count(), policyFlags); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4248 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4249 | 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] | 4250 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4251 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4252 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4253 | // 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] | 4254 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4255 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4256 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4257 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4258 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4259 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4260 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4261 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4264 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4265 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4266 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4267 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4268 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4269 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4270 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4271 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4272 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4273 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4274 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4275 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4276 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4277 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4278 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4279 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4280 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4281 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4282 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4283 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4284 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4285 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4286 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4287 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4288 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4289 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4290 | |
| 4291 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4292 | android::base::Timer t; |
| 4293 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4294 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4295 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4296 | std::to_string(t.duration().count()).c_str()); |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4301 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4302 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4303 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4304 | incomingKey.getDisplayId(), policyFlags, action, |
| 4305 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4306 | incomingKey.getRepeatCount(), |
| 4307 | incomingKey.getDownTime()); |
| 4308 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4309 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4310 | } |
| 4311 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4312 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4313 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4314 | const int32_t action = motionEvent.getAction(); |
| 4315 | const bool isPointerEvent = |
| 4316 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4317 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4318 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4319 | ? ADISPLAY_ID_DEFAULT |
| 4320 | : event->getDisplayId(); |
| 4321 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4322 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4323 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4324 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4325 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4326 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4330 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4331 | android::base::Timer t; |
| 4332 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4333 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4334 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4335 | std::to_string(t.duration().count()).c_str()); |
| 4336 | } |
| 4337 | } |
| 4338 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4339 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4340 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4341 | } |
| 4342 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4343 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4344 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4345 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4346 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4347 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4348 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4349 | displayId, policyFlags, action, actionButton, |
| 4350 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4351 | motionEvent.getButtonState(), |
| 4352 | motionEvent.getClassification(), |
| 4353 | motionEvent.getEdgeFlags(), |
| 4354 | motionEvent.getXPrecision(), |
| 4355 | motionEvent.getYPrecision(), |
| 4356 | motionEvent.getRawXCursorPosition(), |
| 4357 | motionEvent.getRawYCursorPosition(), |
| 4358 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4359 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4360 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4361 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4362 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4363 | sampleEventTimes += 1; |
| 4364 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4365 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4366 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4367 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4368 | displayId, policyFlags, action, actionButton, |
| 4369 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4370 | motionEvent.getButtonState(), |
| 4371 | motionEvent.getClassification(), |
| 4372 | motionEvent.getEdgeFlags(), |
| 4373 | motionEvent.getXPrecision(), |
| 4374 | motionEvent.getYPrecision(), |
| 4375 | motionEvent.getRawXCursorPosition(), |
| 4376 | motionEvent.getRawYCursorPosition(), |
| 4377 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4378 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4379 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4380 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4381 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4382 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4383 | } |
| 4384 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4385 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4386 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4387 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4388 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4389 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4390 | } |
| 4391 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4392 | InjectionState* injectionState = new InjectionState(targetUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4393 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4394 | injectionState->injectionIsAsync = true; |
| 4395 | } |
| 4396 | |
| 4397 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4398 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4399 | |
| 4400 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4401 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4402 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4403 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | mLock.unlock(); |
| 4407 | |
| 4408 | if (needWake) { |
| 4409 | mLooper->wake(); |
| 4410 | } |
| 4411 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4412 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4413 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4414 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4415 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4416 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4417 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4418 | } else { |
| 4419 | for (;;) { |
| 4420 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4421 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4422 | break; |
| 4423 | } |
| 4424 | |
| 4425 | nsecs_t remainingTimeout = endTime - now(); |
| 4426 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4427 | if (DEBUG_INJECTION) { |
| 4428 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4429 | "to become available."); |
| 4430 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4431 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4432 | break; |
| 4433 | } |
| 4434 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4435 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4436 | } |
| 4437 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4438 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4439 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4440 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4441 | if (DEBUG_INJECTION) { |
| 4442 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4443 | injectionState->pendingForegroundDispatches); |
| 4444 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4445 | nsecs_t remainingTimeout = endTime - now(); |
| 4446 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4447 | if (DEBUG_INJECTION) { |
| 4448 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4449 | "dispatches to finish."); |
| 4450 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4451 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4452 | break; |
| 4453 | } |
| 4454 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4455 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4456 | } |
| 4457 | } |
| 4458 | } |
| 4459 | |
| 4460 | injectionState->release(); |
| 4461 | } // release lock |
| 4462 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4463 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4464 | ALOGD("injectInputEvent - Finished with result %d.", injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4465 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4466 | |
| 4467 | return injectionResult; |
| 4468 | } |
| 4469 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4470 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4471 | std::array<uint8_t, 32> calculatedHmac; |
| 4472 | std::unique_ptr<VerifiedInputEvent> result; |
| 4473 | switch (event.getType()) { |
| 4474 | case AINPUT_EVENT_TYPE_KEY: { |
| 4475 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4476 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4477 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4478 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4479 | break; |
| 4480 | } |
| 4481 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4482 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4483 | VerifiedMotionEvent verifiedMotionEvent = |
| 4484 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4485 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4486 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4487 | break; |
| 4488 | } |
| 4489 | default: { |
| 4490 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4491 | return nullptr; |
| 4492 | } |
| 4493 | } |
| 4494 | if (calculatedHmac == INVALID_HMAC) { |
| 4495 | return nullptr; |
| 4496 | } |
| 4497 | if (calculatedHmac != event.getHmac()) { |
| 4498 | return nullptr; |
| 4499 | } |
| 4500 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4501 | } |
| 4502 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4503 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4504 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4505 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4506 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4507 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4508 | ALOGD("Setting input event injection result to %d.", injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4509 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4510 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4511 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4512 | // Log the outcome since the injector did not wait for the injection result. |
| 4513 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4514 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4515 | ALOGV("Asynchronous input event injection succeeded."); |
| 4516 | break; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4517 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4518 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4519 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4520 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4521 | ALOGW("Asynchronous input event injection failed."); |
| 4522 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4523 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4524 | ALOGW("Asynchronous input event injection timed out."); |
| 4525 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4526 | case InputEventInjectionResult::PENDING: |
| 4527 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4528 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4529 | } |
| 4530 | } |
| 4531 | |
| 4532 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4533 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4534 | } |
| 4535 | } |
| 4536 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4537 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4538 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4539 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4540 | // display space, so we need to transform the injected events accordingly. |
| 4541 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4542 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4543 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4544 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4545 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4546 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4547 | const vec2 cursor = |
| 4548 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4549 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4550 | entry.xCursorPosition = cursor.x; |
| 4551 | entry.yCursorPosition = cursor.y; |
| 4552 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4553 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4554 | entry.pointerCoords[i] = |
| 4555 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4556 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4557 | } |
| 4558 | } |
| 4559 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4560 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4561 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4562 | if (injectionState) { |
| 4563 | injectionState->pendingForegroundDispatches += 1; |
| 4564 | } |
| 4565 | } |
| 4566 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4567 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4568 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4569 | if (injectionState) { |
| 4570 | injectionState->pendingForegroundDispatches -= 1; |
| 4571 | |
| 4572 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4573 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4574 | } |
| 4575 | } |
| 4576 | } |
| 4577 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4578 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4579 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4580 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4581 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4582 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4583 | } |
| 4584 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4585 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4586 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4587 | if (windowHandleToken == nullptr) { |
| 4588 | return nullptr; |
| 4589 | } |
| 4590 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4591 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4592 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4593 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4594 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4595 | return windowHandle; |
| 4596 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4597 | } |
| 4598 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4599 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4600 | } |
| 4601 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4602 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4603 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4604 | if (windowHandleToken == nullptr) { |
| 4605 | return nullptr; |
| 4606 | } |
| 4607 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4608 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4609 | if (windowHandle->getToken() == windowHandleToken) { |
| 4610 | return windowHandle; |
| 4611 | } |
| 4612 | } |
| 4613 | return nullptr; |
| 4614 | } |
| 4615 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4616 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4617 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4618 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4619 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4620 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4621 | if (handle->getId() == windowHandle->getId() && |
| 4622 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4623 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4624 | ALOGE("Found window %s in display %" PRId32 |
| 4625 | ", but it should belong to display %" PRId32, |
| 4626 | windowHandle->getName().c_str(), it.first, |
| 4627 | windowHandle->getInfo()->displayId); |
| 4628 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4629 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4630 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4631 | } |
| 4632 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4633 | return nullptr; |
| 4634 | } |
| 4635 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4636 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4637 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4638 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4639 | } |
| 4640 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4641 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 4642 | const MotionEntry& motionEntry) const { |
| 4643 | const WindowInfo& info = *window->getInfo(); |
| 4644 | |
| 4645 | // Skip spy window targets that are not valid for targeted injection. |
| 4646 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4647 | return false; |
| 4648 | } |
| 4649 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4650 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 4651 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 4652 | return false; |
| 4653 | } |
| 4654 | |
| 4655 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 4656 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 4657 | window->getName().c_str()); |
| 4658 | return false; |
| 4659 | } |
| 4660 | |
| 4661 | sp<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4662 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4663 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 4664 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4665 | return false; |
| 4666 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4667 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4668 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4669 | ALOGW("Not sending touch to %s because it is not responsive", window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4670 | return false; |
| 4671 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4672 | |
| 4673 | // Drop events that can't be trusted due to occlusion |
| 4674 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 4675 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 4676 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 4677 | if (DEBUG_TOUCH_OCCLUSION) { |
| 4678 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 4679 | for (const auto& log : occlusionInfo.debugInfo) { |
| 4680 | ALOGD("%s", log.c_str()); |
| 4681 | } |
| 4682 | } |
| 4683 | ALOGW("Dropping untrusted touch event due to %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 4684 | occlusionInfo.obscuringUid); |
| 4685 | return false; |
| 4686 | } |
| 4687 | |
| 4688 | // Drop touch events if requested by input feature |
| 4689 | if (shouldDropInput(motionEntry, window)) { |
| 4690 | return false; |
| 4691 | } |
| 4692 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4693 | return true; |
| 4694 | } |
| 4695 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4696 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4697 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4698 | auto connectionIt = mConnectionsByToken.find(token); |
| 4699 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4700 | return nullptr; |
| 4701 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4702 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4703 | } |
| 4704 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4705 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4706 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4707 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4708 | // Remove all handles on a display if there are no windows left. |
| 4709 | mWindowHandlesByDisplay.erase(displayId); |
| 4710 | return; |
| 4711 | } |
| 4712 | |
| 4713 | // Since we compare the pointer of input window handles across window updates, we need |
| 4714 | // 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] | 4715 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4716 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4717 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4718 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4719 | } |
| 4720 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4721 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4722 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4723 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4724 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4725 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4726 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4727 | const bool canReceiveInput = |
| 4728 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4729 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4730 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4731 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4732 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4733 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4734 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | if (info->displayId != displayId) { |
| 4738 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4739 | handle->getName().c_str(), displayId, info->displayId); |
| 4740 | continue; |
| 4741 | } |
| 4742 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4743 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4744 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4745 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4746 | oldHandle->updateFrom(handle); |
| 4747 | newHandles.push_back(oldHandle); |
| 4748 | } else { |
| 4749 | newHandles.push_back(handle); |
| 4750 | } |
| 4751 | } |
| 4752 | |
| 4753 | // Insert or replace |
| 4754 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4755 | } |
| 4756 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4757 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4758 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4759 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4760 | { // acquire lock |
| 4761 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4762 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4763 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4764 | } |
| 4765 | } |
| 4766 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4767 | mLooper->wake(); |
| 4768 | } |
| 4769 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4770 | /** |
| 4771 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4772 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4773 | * If set an empty list, remove all handles from the specific display. |
| 4774 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4775 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4776 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4777 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4778 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4779 | if (DEBUG_FOCUS) { |
| 4780 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4781 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4782 | windowList += iwh->getName() + " "; |
| 4783 | } |
| 4784 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4785 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4786 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4787 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4788 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4789 | const WindowInfo& info = *window->getInfo(); |
| 4790 | |
| 4791 | // 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] | 4792 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4793 | if (noInputWindow && window->getToken() != nullptr) { |
| 4794 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4795 | window->getName().c_str()); |
| 4796 | window->releaseChannel(); |
| 4797 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4798 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4799 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4800 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 4801 | !info.inputConfig.test( |
| 4802 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4803 | "%s has feature SPY, but is not a trusted overlay.", |
| 4804 | window->getName().c_str()); |
| 4805 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4806 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4807 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 4808 | !info.inputConfig.test( |
| 4809 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4810 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4811 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4812 | } |
| 4813 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4814 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4815 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4816 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4817 | // Save the old windows' orientation by ID before it gets updated. |
| 4818 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4819 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4820 | oldWindowOrientations.emplace(handle->getId(), |
| 4821 | handle->getInfo()->transform.getOrientation()); |
| 4822 | } |
| 4823 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4824 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4825 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4826 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 4827 | if (mLastHoverWindowHandle) { |
| 4828 | const WindowInfo* lastHoverWindowInfo = mLastHoverWindowHandle->getInfo(); |
| 4829 | if (lastHoverWindowInfo->displayId == displayId && |
| 4830 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4831 | windowHandles.end()) { |
| 4832 | mLastHoverWindowHandle = nullptr; |
| 4833 | } |
| 4834 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4835 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4836 | std::optional<FocusResolver::FocusChanges> changes = |
| 4837 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4838 | if (changes) { |
| 4839 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4840 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4841 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4842 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4843 | mTouchStatesByDisplay.find(displayId); |
| 4844 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4845 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4846 | for (size_t i = 0; i < state.windows.size();) { |
| 4847 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4848 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4849 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4850 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4851 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4852 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4853 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4854 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4855 | if (touchedInputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 4856 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4857 | "touched window was removed"); |
| 4858 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4859 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4860 | // well. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 4861 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4862 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 4863 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4864 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 4865 | synthesizeCancelationEventsForWindowLocked(wallpaper, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4866 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4867 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4868 | state.windows.erase(state.windows.begin() + i); |
| 4869 | } else { |
| 4870 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4871 | } |
| 4872 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4873 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4874 | // 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] | 4875 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 4876 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4877 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4878 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 4879 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 4880 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4881 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4882 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4883 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4884 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4885 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4886 | // pointer events if necessary. |
| 4887 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4888 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4889 | if (newWindowHandle != nullptr && |
| 4890 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4891 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4892 | std::shared_ptr<InputChannel> inputChannel = |
| 4893 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4894 | if (inputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 4895 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4896 | "touched window's orientation changed"); |
| 4897 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4898 | } |
| 4899 | } |
| 4900 | } |
| 4901 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4902 | // Release information for windows that are no longer present. |
| 4903 | // This ensures that unused input channels are released promptly. |
| 4904 | // Otherwise, they might stick around until the window handle is destroyed |
| 4905 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4906 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4907 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4908 | if (DEBUG_FOCUS) { |
| 4909 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4910 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4911 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4912 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4913 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4917 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4918 | if (DEBUG_FOCUS) { |
| 4919 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4920 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4921 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4922 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4923 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4924 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4925 | } // release lock |
| 4926 | |
| 4927 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4928 | mLooper->wake(); |
| 4929 | } |
| 4930 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4931 | void InputDispatcher::setFocusedApplicationLocked( |
| 4932 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4933 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4934 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4935 | |
| 4936 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4937 | return; // This application is already focused. No need to wake up or change anything. |
| 4938 | } |
| 4939 | |
| 4940 | // Set the new application handle. |
| 4941 | if (inputApplicationHandle != nullptr) { |
| 4942 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4943 | } else { |
| 4944 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4945 | } |
| 4946 | |
| 4947 | // No matter what the old focused application was, stop waiting on it because it is |
| 4948 | // no longer focused. |
| 4949 | resetNoFocusedWindowTimeoutLocked(); |
| 4950 | } |
| 4951 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4952 | /** |
| 4953 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4954 | * the display not specified. |
| 4955 | * |
| 4956 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4957 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4958 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4959 | * display. The display-specified events won't be affected. |
| 4960 | */ |
| 4961 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4962 | if (DEBUG_FOCUS) { |
| 4963 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4964 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4965 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4966 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4967 | |
| 4968 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4969 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4970 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4971 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4972 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4973 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4974 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4975 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 4976 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4977 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4978 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4979 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4980 | } |
| 4981 | } |
| 4982 | mFocusedDisplayId = displayId; |
| 4983 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4984 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4985 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4986 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4987 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4988 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4989 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4990 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4991 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4992 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4993 | } |
| 4994 | } |
| 4995 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4996 | } // release lock |
| 4997 | |
| 4998 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4999 | mLooper->wake(); |
| 5000 | } |
| 5001 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5002 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5003 | if (DEBUG_FOCUS) { |
| 5004 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5005 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5006 | |
| 5007 | bool changed; |
| 5008 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5009 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5010 | |
| 5011 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5012 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5013 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5014 | } |
| 5015 | |
| 5016 | if (mDispatchEnabled && !enabled) { |
| 5017 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5018 | } |
| 5019 | |
| 5020 | mDispatchEnabled = enabled; |
| 5021 | mDispatchFrozen = frozen; |
| 5022 | changed = true; |
| 5023 | } else { |
| 5024 | changed = false; |
| 5025 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5026 | } // release lock |
| 5027 | |
| 5028 | if (changed) { |
| 5029 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5030 | mLooper->wake(); |
| 5031 | } |
| 5032 | } |
| 5033 | |
| 5034 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5035 | if (DEBUG_FOCUS) { |
| 5036 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5037 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5038 | |
| 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 | |
| 5042 | if (mInputFilterEnabled == enabled) { |
| 5043 | return; |
| 5044 | } |
| 5045 | |
| 5046 | mInputFilterEnabled = enabled; |
| 5047 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5048 | } // release lock |
| 5049 | |
| 5050 | // Wake up poll loop since there might be work to do to drop everything. |
| 5051 | mLooper->wake(); |
| 5052 | } |
| 5053 | |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 5054 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission, |
| 5055 | int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5056 | bool needWake = false; |
| 5057 | { |
| 5058 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5059 | ALOGD_IF(DEBUG_TOUCH_MODE, |
| 5060 | "Request to change touch mode to %s (calling pid=%d, uid=%d, " |
| 5061 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
| 5062 | toString(inTouchMode), pid, uid, toString(hasPermission), displayId, |
| 5063 | mTouchModePerDisplay.count(displayId) == 0 |
| 5064 | ? "not set" |
| 5065 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5066 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5067 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5068 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5069 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5070 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5071 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5072 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5073 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
| 5074 | ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " |
| 5075 | "window nor none of the previously interacted window", |
| 5076 | pid, uid); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5077 | return false; |
| 5078 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5079 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5080 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5081 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5082 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5083 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5084 | } // release lock |
| 5085 | |
| 5086 | if (needWake) { |
| 5087 | mLooper->wake(); |
| 5088 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5089 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5090 | } |
| 5091 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5092 | bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { |
| 5093 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5094 | if (focusedToken == nullptr) { |
| 5095 | return false; |
| 5096 | } |
| 5097 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5098 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5099 | } |
| 5100 | |
| 5101 | bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { |
| 5102 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5103 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5104 | const sp<WindowInfoHandle> windowHandle = |
| 5105 | getWindowHandleLocked(connectionToken); |
| 5106 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5107 | }) != mInteractionConnectionTokens.end(); |
| 5108 | } |
| 5109 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5110 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5111 | if (opacity < 0 || opacity > 1) { |
| 5112 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5113 | return; |
| 5114 | } |
| 5115 | |
| 5116 | std::scoped_lock lock(mLock); |
| 5117 | mMaximumObscuringOpacityForTouch = opacity; |
| 5118 | } |
| 5119 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5120 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5121 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5122 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5123 | for (TouchedWindow& w : state.windows) { |
| 5124 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5125 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5126 | } |
| 5127 | } |
| 5128 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5129 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5130 | } |
| 5131 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5132 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5133 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5134 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5135 | if (DEBUG_FOCUS) { |
| 5136 | ALOGD("Trivial transfer to same window."); |
| 5137 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5138 | return true; |
| 5139 | } |
| 5140 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5141 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5142 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5143 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5144 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5145 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5146 | if (state == nullptr || touchedWindow == nullptr) { |
| 5147 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5148 | return false; |
| 5149 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5150 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5151 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5152 | if (toWindowHandle == nullptr) { |
| 5153 | ALOGW("Cannot transfer focus because to window not found."); |
| 5154 | return false; |
| 5155 | } |
| 5156 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5157 | if (DEBUG_FOCUS) { |
| 5158 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5159 | touchedWindow->windowHandle->getName().c_str(), |
| 5160 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5161 | } |
| 5162 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5163 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5164 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5165 | BitSet32 pointerIds = touchedWindow->pointerIds; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 5166 | sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5167 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5168 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5169 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5170 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5171 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
| 5172 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5173 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5174 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5175 | } |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5176 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5177 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5178 | // Store the dragging window. |
| 5179 | if (isDragDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5180 | if (pointerIds.count() != 1) { |
| 5181 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5182 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5183 | return false; |
| 5184 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5185 | // Track the pointer id for drag window and generate the drag state. |
| 5186 | const int32_t id = pointerIds.firstMarkedBit(); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5187 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5188 | } |
| 5189 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5190 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5191 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5192 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5193 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5194 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5195 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5196 | options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5197 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5198 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 5199 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5200 | newTargetFlags); |
| 5201 | |
| 5202 | // Check if the wallpaper window should deliver the corresponding event. |
| 5203 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
| 5204 | *state, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5205 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5206 | } // release lock |
| 5207 | |
| 5208 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5209 | mLooper->wake(); |
| 5210 | return true; |
| 5211 | } |
| 5212 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5213 | /** |
| 5214 | * Get the touched foreground window on the given display. |
| 5215 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5216 | * window is being touched. |
| 5217 | */ |
| 5218 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5219 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5220 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5221 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5222 | return nullptr; |
| 5223 | } |
| 5224 | |
| 5225 | const TouchState& state = stateIt->second; |
| 5226 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5227 | // If multiple foreground windows are touched, return nullptr |
| 5228 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5229 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5230 | if (touchedForegroundWindow != nullptr) { |
| 5231 | ALOGI("Two or more foreground windows: %s and %s", |
| 5232 | touchedForegroundWindow->getName().c_str(), |
| 5233 | window.windowHandle->getName().c_str()); |
| 5234 | return nullptr; |
| 5235 | } |
| 5236 | touchedForegroundWindow = window.windowHandle; |
| 5237 | } |
| 5238 | } |
| 5239 | return touchedForegroundWindow; |
| 5240 | } |
| 5241 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5242 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5243 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5244 | sp<IBinder> fromToken; |
| 5245 | { // acquire lock |
| 5246 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5247 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5248 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5249 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5250 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5251 | return false; |
| 5252 | } |
| 5253 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5254 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5255 | if (from == nullptr) { |
| 5256 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5257 | return false; |
| 5258 | } |
| 5259 | |
| 5260 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5261 | } // release lock |
| 5262 | |
| 5263 | return transferTouchFocus(fromToken, destChannelToken); |
| 5264 | } |
| 5265 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5266 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5267 | if (DEBUG_FOCUS) { |
| 5268 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5269 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5270 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5271 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5272 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5273 | |
| 5274 | resetKeyRepeatLocked(); |
| 5275 | releasePendingEventLocked(); |
| 5276 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5277 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5278 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5279 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5280 | mTouchStatesByDisplay.clear(); |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 5281 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5282 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5283 | } |
| 5284 | |
| 5285 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5286 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5287 | dumpDispatchStateLocked(dump); |
| 5288 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5289 | std::istringstream stream(dump); |
| 5290 | std::string line; |
| 5291 | |
| 5292 | while (std::getline(stream, line, '\n')) { |
| 5293 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5294 | } |
| 5295 | } |
| 5296 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5297 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5298 | std::string dump; |
| 5299 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5300 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5301 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5302 | |
| 5303 | std::string windowName = "None"; |
| 5304 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5305 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5306 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5307 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5308 | : "token has capture without window"; |
| 5309 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5310 | 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] | 5311 | |
| 5312 | return dump; |
| 5313 | } |
| 5314 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5315 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5316 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5317 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5318 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5319 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5320 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5321 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5322 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5323 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5324 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5325 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5326 | const std::chrono::duration timeout = |
| 5327 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5328 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5329 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5330 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5331 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5332 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5333 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5334 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5335 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5336 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5337 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5338 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5339 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5340 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5341 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5342 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5343 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5344 | } |
| 5345 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5346 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5347 | } |
| 5348 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5349 | if (mDragState) { |
| 5350 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5351 | mDragState->dump(dump, INDENT2); |
| 5352 | } |
| 5353 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5354 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5355 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5356 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5357 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5358 | const auto& displayInfo = it->second; |
| 5359 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5360 | displayInfo.logicalHeight); |
| 5361 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5362 | } else { |
| 5363 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5364 | } |
| 5365 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5366 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5367 | dump += INDENT2 "Windows:\n"; |
| 5368 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5369 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5370 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5371 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5372 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5373 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5374 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5375 | "applicationInfo.name=%s, " |
| 5376 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5377 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5378 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5379 | windowInfo->displayId, |
| 5380 | windowInfo->inputConfig.string().c_str(), |
| 5381 | windowInfo->alpha, windowInfo->frameLeft, |
| 5382 | windowInfo->frameTop, windowInfo->frameRight, |
| 5383 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5384 | windowInfo->applicationInfo.name.c_str(), |
| 5385 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5386 | dump += dumpRegion(windowInfo->touchableRegion); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5387 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5388 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5389 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5390 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5391 | millis(windowInfo->dispatchingTimeout), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5392 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5393 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5394 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5395 | } |
| 5396 | } else { |
| 5397 | dump += INDENT2 "Windows: <none>\n"; |
| 5398 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5399 | } |
| 5400 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5401 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5402 | } |
| 5403 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5404 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5405 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5406 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5407 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5408 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5409 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5410 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5411 | } |
| 5412 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5413 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5414 | |
| 5415 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5416 | if (!mRecentQueue.empty()) { |
| 5417 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5418 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5419 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5420 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5421 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5422 | } |
| 5423 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5424 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5425 | } |
| 5426 | |
| 5427 | // Dump event currently being dispatched. |
| 5428 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5429 | dump += INDENT "PendingEvent:\n"; |
| 5430 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5431 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5432 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5433 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5434 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5435 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5436 | } |
| 5437 | |
| 5438 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5439 | if (!mInboundQueue.empty()) { |
| 5440 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5441 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5442 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5443 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5444 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5445 | } |
| 5446 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5447 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5448 | } |
| 5449 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5450 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5451 | dump += INDENT "ReplacedKeys:\n"; |
Michael Wright | 3cec446 | 2022-11-24 22:05:46 +0000 | [diff] [blame] | 5452 | for (const auto& [replacement, newKeyCode] : mReplacedKeys) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5453 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5454 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5455 | } |
| 5456 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5457 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5458 | } |
| 5459 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5460 | if (!mCommandQueue.empty()) { |
| 5461 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5462 | } else { |
| 5463 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5464 | } |
| 5465 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5466 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5467 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5468 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5469 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5470 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5471 | connection->inputChannel->getFd().get(), |
| 5472 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5473 | connection->getWindowName().c_str(), |
| 5474 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5475 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5476 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5477 | if (!connection->outboundQueue.empty()) { |
| 5478 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5479 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5480 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5481 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5482 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5483 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5484 | } |
| 5485 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5486 | if (!connection->waitQueue.empty()) { |
| 5487 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5488 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5489 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5490 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5491 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5492 | } |
| 5493 | } |
| 5494 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5495 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5496 | } |
| 5497 | |
| 5498 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5499 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5500 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5501 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5502 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5503 | } |
| 5504 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5505 | if (!mTouchModePerDisplay.empty()) { |
| 5506 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5507 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5508 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5509 | std::to_string(touchMode).c_str()); |
| 5510 | } |
| 5511 | } else { |
| 5512 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5513 | } |
| 5514 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5515 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5516 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5517 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5518 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5519 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5520 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5521 | } |
| 5522 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5523 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5524 | const size_t numMonitors = monitors.size(); |
| 5525 | for (size_t i = 0; i < numMonitors; i++) { |
| 5526 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5527 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5528 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5529 | dump += "\n"; |
| 5530 | } |
| 5531 | } |
| 5532 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5533 | class LooperEventCallback : public LooperCallback { |
| 5534 | public: |
| 5535 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5536 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5537 | |
| 5538 | private: |
| 5539 | std::function<int(int events)> mCallback; |
| 5540 | }; |
| 5541 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5542 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5543 | if (DEBUG_CHANNEL_CREATION) { |
| 5544 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5545 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5546 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5547 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5548 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5549 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5550 | |
| 5551 | if (result) { |
| 5552 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5553 | } |
| 5554 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5555 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5556 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5557 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5558 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5559 | sp<Connection> connection = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5560 | sp<Connection>::make(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5561 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5562 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5563 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5564 | } |
| 5565 | mConnectionsByToken.emplace(token, connection); |
| 5566 | |
| 5567 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5568 | this, std::placeholders::_1, token); |
| 5569 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5570 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5571 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5572 | } // release lock |
| 5573 | |
| 5574 | // Wake the looper because some connections have changed. |
| 5575 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5576 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5577 | } |
| 5578 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5579 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5580 | const std::string& name, |
| 5581 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5582 | std::shared_ptr<InputChannel> serverChannel; |
| 5583 | std::unique_ptr<InputChannel> clientChannel; |
| 5584 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5585 | if (result) { |
| 5586 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5587 | } |
| 5588 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5589 | { // acquire lock |
| 5590 | std::scoped_lock _l(mLock); |
| 5591 | |
| 5592 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5593 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5594 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5595 | } |
| 5596 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5597 | sp<Connection> connection = |
| 5598 | sp<Connection>::make(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5599 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5600 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5601 | |
| 5602 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5603 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5604 | } |
| 5605 | mConnectionsByToken.emplace(token, connection); |
| 5606 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5607 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5608 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5609 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5610 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5611 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5612 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5613 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5614 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5615 | // Wake the looper because some connections have changed. |
| 5616 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5617 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5618 | } |
| 5619 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5620 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5621 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5622 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5623 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5624 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5625 | if (status) { |
| 5626 | return status; |
| 5627 | } |
| 5628 | } // release lock |
| 5629 | |
| 5630 | // Wake the poll loop because removing the connection may have changed the current |
| 5631 | // synchronization state. |
| 5632 | mLooper->wake(); |
| 5633 | return OK; |
| 5634 | } |
| 5635 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5636 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5637 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5638 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5639 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5640 | // 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] | 5641 | return BAD_VALUE; |
| 5642 | } |
| 5643 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5644 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5645 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5646 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5647 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5648 | } |
| 5649 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5650 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5651 | |
| 5652 | nsecs_t currentTime = now(); |
| 5653 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5654 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5655 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5656 | return OK; |
| 5657 | } |
| 5658 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5659 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5660 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5661 | auto& [displayId, monitors] = *it; |
| 5662 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5663 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5664 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5665 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5666 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5667 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5668 | } else { |
| 5669 | ++it; |
| 5670 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5671 | } |
| 5672 | } |
| 5673 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5674 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5675 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5676 | return pilferPointersLocked(token); |
| 5677 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5678 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5679 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5680 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5681 | if (!requestingChannel) { |
| 5682 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5683 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5684 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5685 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5686 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 5687 | if (statePtr == nullptr || windowPtr == nullptr || windowPtr->pointerIds.isEmpty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5688 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5689 | " Ignoring."); |
| 5690 | return BAD_VALUE; |
| 5691 | } |
| 5692 | |
| 5693 | TouchState& state = *statePtr; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5694 | TouchedWindow& window = *windowPtr; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5695 | // Send cancel events to all the input channels we're stealing from. |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5696 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5697 | "input channel stole pointer stream"); |
| 5698 | options.deviceId = state.deviceId; |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5699 | options.displayId = displayId; |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 5700 | options.pointerIds = window.pointerIds; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5701 | std::string canceledWindows; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5702 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5703 | const std::shared_ptr<InputChannel> channel = |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5704 | getInputChannelLocked(w.windowHandle->getToken()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5705 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5706 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5707 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5708 | canceledWindows += channel->getName(); |
| 5709 | } |
| 5710 | } |
| 5711 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5712 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5713 | canceledWindows.c_str()); |
| 5714 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5715 | // Prevent the gesture from being sent to any other windows. |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5716 | // This only blocks relevant pointers to be sent to other windows |
| 5717 | window.isPilferingPointers = true; |
| 5718 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 5719 | state.cancelPointersForWindowsExcept(window.pointerIds, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5720 | return OK; |
| 5721 | } |
| 5722 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5723 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5724 | { // acquire lock |
| 5725 | std::scoped_lock _l(mLock); |
| 5726 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5727 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5728 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5729 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5730 | : "token without window"); |
| 5731 | } |
| 5732 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5733 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5734 | if (focusedToken != windowToken) { |
| 5735 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5736 | enabled ? "enable" : "disable"); |
| 5737 | return; |
| 5738 | } |
| 5739 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5740 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5741 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5742 | "window has %s requested pointer capture.", |
| 5743 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5744 | return; |
| 5745 | } |
| 5746 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5747 | if (enabled) { |
| 5748 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5749 | mIneligibleDisplaysForPointerCapture.end(), |
| 5750 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5751 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5752 | return; |
| 5753 | } |
| 5754 | } |
| 5755 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5756 | setPointerCaptureLocked(enabled); |
| 5757 | } // release lock |
| 5758 | |
| 5759 | // Wake the thread to process command entries. |
| 5760 | mLooper->wake(); |
| 5761 | } |
| 5762 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5763 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5764 | { // acquire lock |
| 5765 | std::scoped_lock _l(mLock); |
| 5766 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5767 | if (!isEligible) { |
| 5768 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5769 | } |
| 5770 | } // release lock |
| 5771 | } |
| 5772 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5773 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5774 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5775 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5776 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5777 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5778 | } |
| 5779 | } |
| 5780 | } |
| 5781 | return std::nullopt; |
| 5782 | } |
| 5783 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5784 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5785 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5786 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5787 | } |
| 5788 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5789 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5790 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5791 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5792 | } |
| 5793 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5794 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5795 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5796 | } |
| 5797 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5798 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5799 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5800 | if (connection == nullptr) { |
| 5801 | return "<nullptr>"; |
| 5802 | } |
| 5803 | return connection->getInputChannelName(); |
| 5804 | } |
| 5805 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5806 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5807 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5808 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5809 | } |
| 5810 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5811 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5812 | const sp<Connection>& connection, uint32_t seq, |
| 5813 | bool handled, nsecs_t consumeTime) { |
| 5814 | // Handle post-event policy actions. |
| 5815 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5816 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5817 | return; |
| 5818 | } |
| 5819 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5820 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5821 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5822 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5823 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5824 | } |
| 5825 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5826 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5827 | connection->inputChannel->getConnectionToken(), |
| 5828 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5829 | } |
| 5830 | |
| 5831 | bool restartEvent; |
| 5832 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5833 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5834 | restartEvent = |
| 5835 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5836 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5837 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5838 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5839 | handled); |
| 5840 | } else { |
| 5841 | restartEvent = false; |
| 5842 | } |
| 5843 | |
| 5844 | // Dequeue the event and start the next cycle. |
| 5845 | // Because the lock might have been released, it is possible that the |
| 5846 | // contents of the wait queue to have been drained, so we need to double-check |
| 5847 | // a few things. |
| 5848 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5849 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5850 | dispatchEntry = *dispatchEntryIt; |
| 5851 | connection->waitQueue.erase(dispatchEntryIt); |
| 5852 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5853 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5854 | if (!connection->responsive) { |
| 5855 | connection->responsive = isConnectionResponsive(*connection); |
| 5856 | if (connection->responsive) { |
| 5857 | // The connection was unresponsive, and now it's responsive. |
| 5858 | processConnectionResponsiveLocked(*connection); |
| 5859 | } |
| 5860 | } |
| 5861 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5862 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5863 | connection->outboundQueue.push_front(dispatchEntry); |
| 5864 | traceOutboundQueueLength(*connection); |
| 5865 | } else { |
| 5866 | releaseDispatchEntry(dispatchEntry); |
| 5867 | } |
| 5868 | } |
| 5869 | |
| 5870 | // Start the next dispatch cycle for this connection. |
| 5871 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5872 | } |
| 5873 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5874 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5875 | const sp<IBinder>& newToken) { |
| 5876 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5877 | scoped_unlock unlock(mLock); |
| 5878 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5879 | }; |
| 5880 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5881 | } |
| 5882 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5883 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5884 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5885 | scoped_unlock unlock(mLock); |
| 5886 | mPolicy->notifyDropWindow(token, x, y); |
| 5887 | }; |
| 5888 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5889 | } |
| 5890 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5891 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5892 | if (connection == nullptr) { |
| 5893 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5894 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5895 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5896 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5897 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5898 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5899 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5900 | return; |
| 5901 | } |
| 5902 | /** |
| 5903 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5904 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5905 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5906 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5907 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5908 | * most useful. |
| 5909 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5910 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5911 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5912 | std::string reason = |
| 5913 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5914 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5915 | ns2ms(currentWait), |
| 5916 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5917 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5918 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5919 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5920 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5921 | |
| 5922 | // Stop waking up for events on this connection, it is already unresponsive |
| 5923 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5924 | } |
| 5925 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5926 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5927 | std::string reason = |
| 5928 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5929 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5930 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5931 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5932 | scoped_unlock unlock(mLock); |
| 5933 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5934 | }; |
| 5935 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5936 | } |
| 5937 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5938 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5939 | const std::string& reason) { |
| 5940 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5941 | updateLastAnrStateLocked(windowLabel, reason); |
| 5942 | } |
| 5943 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5944 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5945 | const std::string& reason) { |
| 5946 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5947 | updateLastAnrStateLocked(windowLabel, reason); |
| 5948 | } |
| 5949 | |
| 5950 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5951 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5952 | // 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] | 5953 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5954 | struct tm tm; |
| 5955 | localtime_r(&t, &tm); |
| 5956 | char timestr[64]; |
| 5957 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5958 | mLastAnrState.clear(); |
| 5959 | mLastAnrState += INDENT "ANR:\n"; |
| 5960 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5961 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5962 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5963 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5964 | } |
| 5965 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5966 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5967 | KeyEntry& entry) { |
| 5968 | const KeyEvent event = createKeyEvent(entry); |
| 5969 | nsecs_t delay = 0; |
| 5970 | { // release lock |
| 5971 | scoped_unlock unlock(mLock); |
| 5972 | android::base::Timer t; |
| 5973 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5974 | entry.policyFlags); |
| 5975 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5976 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5977 | std::to_string(t.duration().count()).c_str()); |
| 5978 | } |
| 5979 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5980 | |
| 5981 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 5982 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5983 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 5984 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5985 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 5986 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5987 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5988 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5989 | } |
| 5990 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5991 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5992 | std::optional<int32_t> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5993 | std::string reason) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5994 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5995 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5996 | mPolicy->notifyWindowUnresponsive(token, pid, reason); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5997 | }; |
| 5998 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5999 | } |
| 6000 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6001 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
| 6002 | std::optional<int32_t> pid) { |
| 6003 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6004 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6005 | mPolicy->notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6006 | }; |
| 6007 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6008 | } |
| 6009 | |
| 6010 | /** |
| 6011 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6012 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6013 | * command entry to the command queue. |
| 6014 | */ |
| 6015 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6016 | std::string reason) { |
| 6017 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6018 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6019 | if (connection.monitor) { |
| 6020 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6021 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6022 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6023 | } else { |
| 6024 | // The connection is a window |
| 6025 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6026 | reason.c_str()); |
| 6027 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6028 | if (handle != nullptr) { |
| 6029 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6030 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6031 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6032 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6033 | } |
| 6034 | |
| 6035 | /** |
| 6036 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6037 | */ |
| 6038 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 6039 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6040 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6041 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6042 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6043 | } else { |
| 6044 | // The connection is a window |
| 6045 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6046 | if (handle != nullptr) { |
| 6047 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6048 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6049 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6050 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6051 | } |
| 6052 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6053 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6054 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6055 | KeyEntry& keyEntry, bool handled) { |
| 6056 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6057 | if (!handled) { |
| 6058 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6059 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6060 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6061 | return false; |
| 6062 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6063 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6064 | // Get the fallback key state. |
| 6065 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6066 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6067 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6068 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6069 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6070 | } |
| 6071 | |
| 6072 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6073 | // If the application handles the original key for which we previously |
| 6074 | // generated a fallback or if the window is not a foreground window, |
| 6075 | // then cancel the associated fallback key, if any. |
| 6076 | if (fallbackKeyCode != -1) { |
| 6077 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6078 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6079 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6080 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6081 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6082 | keyEntry.policyFlags); |
| 6083 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6084 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6085 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6086 | |
| 6087 | mLock.unlock(); |
| 6088 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6089 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6090 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6091 | |
| 6092 | mLock.lock(); |
| 6093 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6094 | // Cancel the fallback key. |
| 6095 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6096 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6097 | "application handled the original non-fallback key " |
| 6098 | "or is no longer a foreground target, " |
| 6099 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6100 | options.keyCode = fallbackKeyCode; |
| 6101 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6102 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6103 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6104 | } |
| 6105 | } else { |
| 6106 | // If the application did not handle a non-fallback key, first check |
| 6107 | // that we are in a good state to perform unhandled key event processing |
| 6108 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6109 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6110 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6111 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6112 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6113 | "since this is not an initial down. " |
| 6114 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6115 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6116 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6117 | return false; |
| 6118 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6119 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6120 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6121 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6122 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6123 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6124 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6125 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6126 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6127 | |
| 6128 | mLock.unlock(); |
| 6129 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6130 | bool fallback = |
| 6131 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6132 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6133 | |
| 6134 | mLock.lock(); |
| 6135 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6136 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6137 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6138 | return false; |
| 6139 | } |
| 6140 | |
| 6141 | // Latch the fallback keycode for this key on an initial down. |
| 6142 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6143 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6144 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6145 | fallbackKeyCode = event.getKeyCode(); |
| 6146 | } else { |
| 6147 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6148 | } |
| 6149 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6150 | } |
| 6151 | |
| 6152 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6153 | |
| 6154 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6155 | // We will continue to dispatch the key to the policy but we will no |
| 6156 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6157 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6158 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6159 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6160 | if (fallback) { |
| 6161 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6162 | "as a fallback for %d, but on the DOWN it had requested " |
| 6163 | "to send %d instead. Fallback canceled.", |
| 6164 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6165 | } else { |
| 6166 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6167 | "but on the DOWN it had requested to send %d. " |
| 6168 | "Fallback canceled.", |
| 6169 | originalKeyCode, fallbackKeyCode); |
| 6170 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6171 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6172 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6173 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6174 | "canceling fallback, policy no longer desires it"); |
| 6175 | options.keyCode = fallbackKeyCode; |
| 6176 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6177 | |
| 6178 | fallback = false; |
| 6179 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6180 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6181 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6182 | } |
| 6183 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6184 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6185 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6186 | { |
| 6187 | std::string msg; |
| 6188 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6189 | connection->inputState.getFallbackKeys(); |
| 6190 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6191 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6192 | } |
| 6193 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6194 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6195 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6196 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6197 | |
| 6198 | if (fallback) { |
| 6199 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6200 | keyEntry.eventTime = event.getEventTime(); |
| 6201 | keyEntry.deviceId = event.getDeviceId(); |
| 6202 | keyEntry.source = event.getSource(); |
| 6203 | keyEntry.displayId = event.getDisplayId(); |
| 6204 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6205 | keyEntry.keyCode = fallbackKeyCode; |
| 6206 | keyEntry.scanCode = event.getScanCode(); |
| 6207 | keyEntry.metaState = event.getMetaState(); |
| 6208 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6209 | keyEntry.downTime = event.getDownTime(); |
| 6210 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6211 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6212 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6213 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6214 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6215 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6216 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6217 | return true; // restart the event |
| 6218 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6219 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6220 | ALOGD("Unhandled key event: No fallback key."); |
| 6221 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6222 | |
| 6223 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6224 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6225 | } |
| 6226 | } |
| 6227 | return false; |
| 6228 | } |
| 6229 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6230 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6231 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6232 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6233 | return false; |
| 6234 | } |
| 6235 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6236 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6237 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6238 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6239 | } |
| 6240 | } |
| 6241 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6242 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6243 | if (ATRACE_ENABLED()) { |
| 6244 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6245 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6246 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6247 | } |
| 6248 | } |
| 6249 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6250 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6251 | if (ATRACE_ENABLED()) { |
| 6252 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6253 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6254 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6255 | } |
| 6256 | } |
| 6257 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6258 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6259 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6260 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6261 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6262 | dumpDispatchStateLocked(dump); |
| 6263 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6264 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6265 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6266 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6267 | } |
| 6268 | } |
| 6269 | |
| 6270 | void InputDispatcher::monitor() { |
| 6271 | // 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] | 6272 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6273 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6274 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6275 | } |
| 6276 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6277 | /** |
| 6278 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6279 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6280 | * this method can be safely called from any thread, as long as you've ensured that |
| 6281 | * the work you are interested in completing has already been queued. |
| 6282 | */ |
| 6283 | bool InputDispatcher::waitForIdle() { |
| 6284 | /** |
| 6285 | * Timeout should represent the longest possible time that a device might spend processing |
| 6286 | * events and commands. |
| 6287 | */ |
| 6288 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6289 | std::unique_lock lock(mLock); |
| 6290 | mLooper->wake(); |
| 6291 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6292 | return result == std::cv_status::no_timeout; |
| 6293 | } |
| 6294 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6295 | /** |
| 6296 | * Sets focus to the window identified by the token. This must be called |
| 6297 | * after updating any input window handles. |
| 6298 | * |
| 6299 | * Params: |
| 6300 | * request.token - input channel token used to identify the window that should gain focus. |
| 6301 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6302 | * specified token does not match the currently focused window, this request will be dropped. |
| 6303 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6304 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6305 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6306 | * when requesting the focus change. This determines which request gets |
| 6307 | * precedence if there is a focus change request from another source such as pointer down. |
| 6308 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6309 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6310 | { // acquire lock |
| 6311 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6312 | std::optional<FocusResolver::FocusChanges> changes = |
| 6313 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6314 | if (changes) { |
| 6315 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6316 | } |
| 6317 | } // release lock |
| 6318 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6319 | mLooper->wake(); |
| 6320 | } |
| 6321 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6322 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6323 | if (changes.oldFocus) { |
| 6324 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6325 | if (focusedInputChannel) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6326 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6327 | "focus left window"); |
| 6328 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6329 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6330 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6331 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6332 | if (changes.newFocus) { |
| 6333 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6334 | } |
| 6335 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6336 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6337 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6338 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6339 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6340 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6341 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6342 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6343 | // front. |
| 6344 | disablePointerCaptureForcedLocked(); |
| 6345 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6346 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6347 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6348 | } |
| 6349 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6350 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6351 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6352 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6353 | return; |
| 6354 | } |
| 6355 | |
| 6356 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6357 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6358 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6359 | setPointerCaptureLocked(false); |
| 6360 | } |
| 6361 | |
| 6362 | if (!mWindowTokenWithPointerCapture) { |
| 6363 | // No need to send capture changes because no window has capture. |
| 6364 | return; |
| 6365 | } |
| 6366 | |
| 6367 | if (mPendingEvent != nullptr) { |
| 6368 | // Move the pending event to the front of the queue. This will give the chance |
| 6369 | // for the pending event to be dropped if it is a captured event. |
| 6370 | mInboundQueue.push_front(mPendingEvent); |
| 6371 | mPendingEvent = nullptr; |
| 6372 | } |
| 6373 | |
| 6374 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6375 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6376 | mInboundQueue.push_front(std::move(entry)); |
| 6377 | } |
| 6378 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6379 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6380 | mCurrentPointerCaptureRequest.enable = enable; |
| 6381 | mCurrentPointerCaptureRequest.seq++; |
| 6382 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6383 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6384 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6385 | }; |
| 6386 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6387 | } |
| 6388 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6389 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6390 | { // acquire lock |
| 6391 | std::scoped_lock _l(mLock); |
| 6392 | // Set an empty list to remove all handles from the specific display. |
| 6393 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6394 | setFocusedApplicationLocked(displayId, nullptr); |
| 6395 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6396 | // have been removed for the removed display. |
| 6397 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6398 | // Reset pointer capture eligibility, regardless of previous state. |
| 6399 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6400 | // Remove the associated touch mode state. |
| 6401 | mTouchModePerDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6402 | } // release lock |
| 6403 | |
| 6404 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6405 | mLooper->wake(); |
| 6406 | } |
| 6407 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6408 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6409 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6410 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6411 | // more convenient parsing. |
| 6412 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6413 | for (const auto& info : windowInfos) { |
| 6414 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6415 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6416 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6417 | |
| 6418 | { // acquire lock |
| 6419 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6420 | |
| 6421 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6422 | // no windows, we can tell that the windows were removed from the display. |
| 6423 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6424 | handlesPerDisplay[displayId]; |
| 6425 | } |
| 6426 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6427 | mDisplayInfos.clear(); |
| 6428 | for (const auto& displayInfo : displayInfos) { |
| 6429 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6430 | } |
| 6431 | |
| 6432 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6433 | setInputWindowsLocked(handles, displayId); |
| 6434 | } |
| 6435 | } |
| 6436 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6437 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6438 | } |
| 6439 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6440 | bool InputDispatcher::shouldDropInput( |
| 6441 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6442 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6443 | (windowHandle->getInfo()->inputConfig.test( |
| 6444 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6445 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6446 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6447 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6448 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6449 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6450 | windowHandle->getInfo()->displayId); |
| 6451 | return true; |
| 6452 | } |
| 6453 | return false; |
| 6454 | } |
| 6455 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6456 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6457 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6458 | const std::vector<DisplayInfo>& displayInfos) { |
| 6459 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6460 | } |
| 6461 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6462 | void InputDispatcher::cancelCurrentTouch() { |
| 6463 | { |
| 6464 | std::scoped_lock _l(mLock); |
| 6465 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6466 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6467 | "cancel current touch"); |
| 6468 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6469 | |
| 6470 | mTouchStatesByDisplay.clear(); |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 6471 | mLastHoverWindowHandle.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6472 | } |
| 6473 | // Wake up poll loop since there might be work to do. |
| 6474 | mLooper->wake(); |
| 6475 | } |
| 6476 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6477 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6478 | std::scoped_lock _l(mLock); |
| 6479 | mMonitorDispatchingTimeout = timeout; |
| 6480 | } |
| 6481 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 6482 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6483 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6484 | const sp<WindowInfoHandle>& newWindowHandle, |
| 6485 | TouchState& state, const BitSet32& pointerIds) { |
| 6486 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6487 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6488 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6489 | newWindowHandle->getInfo()->inputConfig.test( |
| 6490 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6491 | const sp<WindowInfoHandle> oldWallpaper = |
| 6492 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6493 | const sp<WindowInfoHandle> newWallpaper = |
| 6494 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6495 | if (oldWallpaper == newWallpaper) { |
| 6496 | return; |
| 6497 | } |
| 6498 | |
| 6499 | if (oldWallpaper != nullptr) { |
| 6500 | state.addOrUpdateWindow(oldWallpaper, InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, |
| 6501 | BitSet32(0)); |
| 6502 | } |
| 6503 | |
| 6504 | if (newWallpaper != nullptr) { |
| 6505 | state.addOrUpdateWindow(newWallpaper, |
| 6506 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER | |
| 6507 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6508 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
| 6509 | pointerIds); |
| 6510 | } |
| 6511 | } |
| 6512 | |
| 6513 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6514 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 6515 | const sp<WindowInfoHandle> fromWindowHandle, |
| 6516 | const sp<WindowInfoHandle> toWindowHandle, |
| 6517 | TouchState& state, const BitSet32& pointerIds) { |
| 6518 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6519 | fromWindowHandle->getInfo()->inputConfig.test( |
| 6520 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6521 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6522 | toWindowHandle->getInfo()->inputConfig.test( |
| 6523 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6524 | |
| 6525 | const sp<WindowInfoHandle> oldWallpaper = |
| 6526 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6527 | const sp<WindowInfoHandle> newWallpaper = |
| 6528 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 6529 | if (oldWallpaper == newWallpaper) { |
| 6530 | return; |
| 6531 | } |
| 6532 | |
| 6533 | if (oldWallpaper != nullptr) { |
| 6534 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6535 | "transferring touch focus to another window"); |
| 6536 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 6537 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 6538 | } |
| 6539 | |
| 6540 | if (newWallpaper != nullptr) { |
| 6541 | nsecs_t downTimeInTarget = now(); |
| 6542 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 6543 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
| 6544 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6545 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
| 6546 | state.addOrUpdateWindow(newWallpaper, wallpaperFlags, pointerIds, downTimeInTarget); |
| 6547 | sp<Connection> wallpaperConnection = getConnectionLocked(newWallpaper->getToken()); |
| 6548 | if (wallpaperConnection != nullptr) { |
| 6549 | sp<Connection> toConnection = getConnectionLocked(toWindowHandle->getToken()); |
| 6550 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 6551 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 6552 | wallpaperFlags); |
| 6553 | } |
| 6554 | } |
| 6555 | } |
| 6556 | |
| 6557 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 6558 | const sp<WindowInfoHandle>& windowHandle) const { |
| 6559 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 6560 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 6561 | bool foundWindow = false; |
| 6562 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 6563 | if (!foundWindow && otherHandle != windowHandle) { |
| 6564 | continue; |
| 6565 | } |
| 6566 | if (windowHandle == otherHandle) { |
| 6567 | foundWindow = true; |
| 6568 | continue; |
| 6569 | } |
| 6570 | |
| 6571 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 6572 | return otherHandle; |
| 6573 | } |
| 6574 | } |
| 6575 | return nullptr; |
| 6576 | } |
| 6577 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6578 | } // namespace android::inputdispatcher |