Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "InputDispatcher" |
| 18 | #define ATRACE_TAG ATRACE_TAG_INPUT |
| 19 | |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 20 | #define LOG_NDEBUG 1 |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 22 | #include <android-base/chrono_utils.h> |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 23 | #include <android-base/properties.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 25 | #include <android/os/IInputConstants.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 26 | #include <binder/Binder.h> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 27 | #include <ftl/enum.h> |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 28 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 29 | #include <input/InputDevice.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 30 | #include <log/log.h> |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 31 | #include <log/log_event_list.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 32 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 33 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 34 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 35 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 36 | #include <cerrno> |
| 37 | #include <cinttypes> |
| 38 | #include <climits> |
| 39 | #include <cstddef> |
| 40 | #include <ctime> |
| 41 | #include <queue> |
| 42 | #include <sstream> |
| 43 | |
| 44 | #include "Connection.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 45 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 46 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 47 | #define INDENT " " |
| 48 | #define INDENT2 " " |
| 49 | #define INDENT3 " " |
| 50 | #define INDENT4 " " |
| 51 | |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 52 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 53 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 54 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 55 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 56 | using android::gui::FocusRequest; |
| 57 | using android::gui::TouchOcclusionMode; |
| 58 | using android::gui::WindowInfo; |
| 59 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 60 | using android::os::BlockUntrustedTouchesMode; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 61 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 62 | using android::os::InputEventInjectionResult; |
| 63 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 64 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 65 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 66 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 67 | namespace { |
| 68 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 69 | // Log detailed debug messages about each inbound event notification to the dispatcher. |
| 70 | constexpr bool DEBUG_INBOUND_EVENT_DETAILS = false; |
| 71 | |
| 72 | // Log detailed debug messages about each outbound event processed by the dispatcher. |
| 73 | constexpr bool DEBUG_OUTBOUND_EVENT_DETAILS = false; |
| 74 | |
| 75 | // Log debug messages about the dispatch cycle. |
| 76 | constexpr bool DEBUG_DISPATCH_CYCLE = false; |
| 77 | |
| 78 | // Log debug messages about channel creation |
| 79 | constexpr bool DEBUG_CHANNEL_CREATION = false; |
| 80 | |
| 81 | // Log debug messages about input event injection. |
| 82 | constexpr bool DEBUG_INJECTION = false; |
| 83 | |
| 84 | // Log debug messages about input focus tracking. |
| 85 | constexpr bool DEBUG_FOCUS = false; |
| 86 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 87 | // Log debug messages about touch mode event |
| 88 | constexpr bool DEBUG_TOUCH_MODE = false; |
| 89 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 90 | // Log debug messages about touch occlusion |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 91 | constexpr bool DEBUG_TOUCH_OCCLUSION = true; |
| 92 | |
| 93 | // Log debug messages about the app switch latency optimization. |
| 94 | constexpr bool DEBUG_APP_SWITCH = false; |
| 95 | |
| 96 | // Log debug messages about hover events. |
| 97 | constexpr bool DEBUG_HOVER = false; |
| 98 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 99 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 100 | // Named to match std::scoped_lock |
| 101 | class scoped_unlock { |
| 102 | public: |
| 103 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 104 | ~scoped_unlock() { mMutex.lock(); } |
| 105 | |
| 106 | private: |
| 107 | std::mutex& mMutex; |
| 108 | }; |
| 109 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 110 | // Default input dispatching timeout if there is no focused application or paused window |
| 111 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 112 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 113 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 114 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 115 | |
| 116 | // Amount of time to allow for all pending events to be processed when an app switch |
| 117 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 118 | // 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] | 119 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 120 | |
| 121 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 122 | // before considering it stale and dropping it. |
Vadim Tryshev | 7854825 | 2022-01-27 19:32:46 +0000 | [diff] [blame] | 123 | const nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL // 10sec |
| 124 | * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 125 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 126 | // 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] | 127 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 128 | |
| 129 | // Log a warning when an interception call takes longer than this to process. |
| 130 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 132 | // Additional key latency in case a connection is still processing some motion events. |
| 133 | // This will help with the case when a user touched a button that opens a new window, |
| 134 | // and gives us the chance to dispatch the key to this new window. |
| 135 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 136 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 137 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 138 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 139 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 140 | // Event log tags. See EventLogTags.logtags for reference |
| 141 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 142 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 143 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 144 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 145 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 146 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 147 | } |
| 148 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 149 | inline const char* toString(bool value) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 150 | return value ? "true" : "false"; |
| 151 | } |
| 152 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 153 | inline const std::string toString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 154 | if (binder == nullptr) { |
| 155 | return "<null>"; |
| 156 | } |
| 157 | return StringPrintf("%p", binder.get()); |
| 158 | } |
| 159 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 160 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 161 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 162 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 165 | bool isValidKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 166 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 167 | case AKEY_EVENT_ACTION_DOWN: |
| 168 | case AKEY_EVENT_ACTION_UP: |
| 169 | return true; |
| 170 | default: |
| 171 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 175 | bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 176 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 177 | ALOGE("Key event has invalid action code 0x%x", action); |
| 178 | return false; |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 183 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 184 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 185 | case AMOTION_EVENT_ACTION_DOWN: |
| 186 | case AMOTION_EVENT_ACTION_UP: |
| 187 | case AMOTION_EVENT_ACTION_CANCEL: |
| 188 | case AMOTION_EVENT_ACTION_MOVE: |
| 189 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 190 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 191 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 192 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 193 | case AMOTION_EVENT_ACTION_SCROLL: |
| 194 | return true; |
| 195 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 196 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 197 | int32_t index = getMotionEventActionPointerIndex(action); |
| 198 | return index >= 0 && index < pointerCount; |
| 199 | } |
| 200 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 201 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 202 | return actionButton != 0; |
| 203 | default: |
| 204 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 208 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 209 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 210 | } |
| 211 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 212 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 213 | const PointerProperties* pointerProperties) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 214 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 215 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 216 | return false; |
| 217 | } |
| 218 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 219 | 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] | 220 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | BitSet32 pointerIdBits; |
| 224 | for (size_t i = 0; i < pointerCount; i++) { |
| 225 | int32_t id = pointerProperties[i].id; |
| 226 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 227 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 228 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 229 | return false; |
| 230 | } |
| 231 | if (pointerIdBits.hasBit(id)) { |
| 232 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 233 | return false; |
| 234 | } |
| 235 | pointerIdBits.markBit(id); |
| 236 | } |
| 237 | return true; |
| 238 | } |
| 239 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 240 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 241 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 242 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 245 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 246 | bool first = true; |
| 247 | Region::const_iterator cur = region.begin(); |
| 248 | Region::const_iterator const tail = region.end(); |
| 249 | while (cur != tail) { |
| 250 | if (first) { |
| 251 | first = false; |
| 252 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 253 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 254 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 255 | 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] | 256 | cur++; |
| 257 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 258 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 261 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 262 | constexpr size_t maxEntries = 50; // max events to print |
| 263 | constexpr size_t skipBegin = maxEntries / 2; |
| 264 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 265 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 266 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 267 | |
| 268 | std::string dump; |
| 269 | for (size_t i = 0; i < queue.size(); i++) { |
| 270 | const DispatchEntry& entry = *queue[i]; |
| 271 | if (i >= skipBegin && i < skipEnd) { |
| 272 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 273 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 274 | continue; |
| 275 | } |
| 276 | dump.append(INDENT4); |
| 277 | dump += entry.eventEntry->getDescription(); |
| 278 | dump += StringPrintf(", seq=%" PRIu32 |
| 279 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 280 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 281 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 282 | if (entry.deliveryTime != 0) { |
| 283 | // This entry was delivered, so add information on how long we've been waiting |
| 284 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 285 | } |
| 286 | dump.append("\n"); |
| 287 | } |
| 288 | return dump; |
| 289 | } |
| 290 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 291 | /** |
| 292 | * Find the entry in std::unordered_map by key, and return it. |
| 293 | * If the entry is not found, return a default constructed entry. |
| 294 | * |
| 295 | * Useful when the entries are vectors, since an empty vector will be returned |
| 296 | * if the entry is not found. |
| 297 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 298 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 299 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 300 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 301 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 302 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 303 | } |
| 304 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 305 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 306 | if (first == second) { |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | if (first == nullptr || second == nullptr) { |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | return first->getToken() == second->getToken(); |
| 315 | } |
| 316 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 317 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 318 | if (first == nullptr || second == nullptr) { |
| 319 | return false; |
| 320 | } |
| 321 | return first->applicationInfo.token != nullptr && |
| 322 | first->applicationInfo.token == second->applicationInfo.token; |
| 323 | } |
| 324 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 325 | bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 326 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 327 | } |
| 328 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 329 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 330 | std::shared_ptr<EventEntry> eventEntry, |
| 331 | int32_t inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 332 | if (inputTarget.useDefaultPointerTransform()) { |
| 333 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 334 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 335 | inputTarget.displayTransform, |
| 336 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 340 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 341 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 342 | std::vector<PointerCoords> pointerCoords; |
| 343 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 344 | |
| 345 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 346 | // 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] | 347 | // uses the transform for the normalized pointer. |
| 348 | const ui::Transform& firstPointerTransform = |
| 349 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 350 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 351 | |
| 352 | // Iterate through all pointers in the event to normalize against the first. |
| 353 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 354 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 355 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 356 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 357 | |
| 358 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 359 | // First, apply the current pointer's transform to update the coordinates into |
| 360 | // window space. |
| 361 | pointerCoords[pointerIndex].transform(currTransform); |
| 362 | // Next, apply the inverse transform of the normalized coordinates so the |
| 363 | // current coordinates are transformed into the normalized coordinate space. |
| 364 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 367 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 368 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 369 | motionEntry.deviceId, motionEntry.source, |
| 370 | motionEntry.displayId, motionEntry.policyFlags, |
| 371 | motionEntry.action, motionEntry.actionButton, |
| 372 | motionEntry.flags, motionEntry.metaState, |
| 373 | motionEntry.buttonState, motionEntry.classification, |
| 374 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 375 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 376 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 377 | motionEntry.pointerCount, motionEntry.pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 378 | pointerCoords.data()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 379 | |
| 380 | if (motionEntry.injectionState) { |
| 381 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 382 | combinedMotionEntry->injectionState->refCount += 1; |
| 383 | } |
| 384 | |
| 385 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 386 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 387 | firstPointerTransform, inputTarget.displayTransform, |
| 388 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 389 | return dispatchEntry; |
| 390 | } |
| 391 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 392 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 393 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 394 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 395 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 396 | |
| 397 | serverChannel = std::move(uniqueServerChannel); |
| 398 | return result; |
| 399 | } |
| 400 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 401 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 402 | 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] | 403 | if (lhs == nullptr && rhs == nullptr) { |
| 404 | return true; |
| 405 | } |
| 406 | if (lhs == nullptr || rhs == nullptr) { |
| 407 | return false; |
| 408 | } |
| 409 | return *lhs == *rhs; |
| 410 | } |
| 411 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 412 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 413 | KeyEvent event; |
| 414 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 415 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 416 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 417 | return event; |
| 418 | } |
| 419 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 420 | std::optional<int32_t> findMonitorPidByToken( |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 421 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, |
| 422 | const sp<IBinder>& token) { |
| 423 | for (const auto& it : monitorsByDisplay) { |
| 424 | const std::vector<Monitor>& monitors = it.second; |
| 425 | for (const Monitor& monitor : monitors) { |
| 426 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 427 | return monitor.pid; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | return std::nullopt; |
| 432 | } |
| 433 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 434 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 435 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 436 | // affect the statistics. |
| 437 | if (connection.monitor) { |
| 438 | return false; |
| 439 | } |
| 440 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 441 | if (!connection.responsive) { |
| 442 | return false; |
| 443 | } |
| 444 | return true; |
| 445 | } |
| 446 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 447 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 448 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 449 | const int32_t& inputEventId = eventEntry.id; |
| 450 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 451 | // Event was transmuted |
| 452 | return false; |
| 453 | } |
| 454 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 455 | return false; |
| 456 | } |
| 457 | // Only track latency for events that originated from hardware |
| 458 | if (eventEntry.isSynthesized()) { |
| 459 | return false; |
| 460 | } |
| 461 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 462 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 463 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 464 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 465 | return false; |
| 466 | } |
| 467 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 468 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 469 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 470 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 471 | return false; |
| 472 | } |
| 473 | } else { |
| 474 | // Not a key or a motion |
| 475 | return false; |
| 476 | } |
| 477 | if (!shouldReportMetricsForConnection(connection)) { |
| 478 | return false; |
| 479 | } |
| 480 | return true; |
| 481 | } |
| 482 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 483 | /** |
| 484 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 485 | * current time. |
| 486 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 487 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 488 | const nsecs_t currentTime = now(); |
| 489 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 490 | if (entry->timeoutTime < currentTime) { |
| 491 | return false; |
| 492 | } |
| 493 | } |
| 494 | return true; |
| 495 | } |
| 496 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 497 | // Returns true if the event type passed as argument represents a user activity. |
| 498 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 499 | switch (eventEntry.type) { |
| 500 | case EventEntry::Type::FOCUS: |
| 501 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 502 | case EventEntry::Type::DRAG: |
| 503 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 504 | case EventEntry::Type::SENSOR: |
| 505 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 506 | return false; |
| 507 | case EventEntry::Type::DEVICE_RESET: |
| 508 | case EventEntry::Type::KEY: |
| 509 | case EventEntry::Type::MOTION: |
| 510 | return true; |
| 511 | } |
| 512 | } |
| 513 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 514 | // 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] | 515 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, int32_t x, int32_t y, |
| 516 | bool isStylus) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 517 | if (windowInfo.displayId != displayId || !windowInfo.visible) { |
| 518 | return false; |
| 519 | } |
| 520 | const auto flags = windowInfo.flags; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 521 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
| 522 | if (flags.test(WindowInfo::Flag::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 523 | return false; |
| 524 | } |
| 525 | const bool isModalWindow = !flags.test(WindowInfo::Flag::NOT_FOCUSABLE) && |
| 526 | !flags.test(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 527 | if (!isModalWindow && !windowInfo.touchableRegionContainsPoint(x, y)) { |
| 528 | return false; |
| 529 | } |
| 530 | return true; |
| 531 | } |
| 532 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 533 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 534 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
| 535 | (entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || |
| 536 | entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_ERASER); |
| 537 | } |
| 538 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 539 | } // namespace |
| 540 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 541 | // --- InputDispatcher --- |
| 542 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 543 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 544 | : mPolicy(policy), |
| 545 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 546 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 547 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 548 | mAppSwitchSawKeyDown(false), |
| 549 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 550 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame^] | 551 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 552 | mDispatchEnabled(false), |
| 553 | mDispatchFrozen(false), |
| 554 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 555 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 556 | // To avoid leaking stack in case that call never comes, and for tests, |
| 557 | // initialize it here anyways. |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 558 | mInTouchMode(kDefaultInTouchMode), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 559 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 560 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 561 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 562 | mLatencyAggregator(), |
Siarhei Vishniakou | bd25272 | 2022-01-06 03:49:35 -0800 | [diff] [blame] | 563 | mLatencyTracker(&mLatencyAggregator) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 564 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 565 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 566 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 567 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 568 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 569 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 570 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 571 | |
| 572 | policy->getDispatcherConfiguration(&mConfig); |
| 573 | } |
| 574 | |
| 575 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 576 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 577 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 578 | resetKeyRepeatLocked(); |
| 579 | releasePendingEventLocked(); |
| 580 | drainInboundQueueLocked(); |
| 581 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 582 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 583 | while (!mConnectionsByToken.empty()) { |
| 584 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 585 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 586 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 590 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 591 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 592 | return ALREADY_EXISTS; |
| 593 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 594 | mThread = std::make_unique<InputThread>( |
| 595 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 596 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 600 | if (mThread && mThread->isCallingThread()) { |
| 601 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 602 | return INVALID_OPERATION; |
| 603 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 604 | mThread.reset(); |
| 605 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 608 | void InputDispatcher::dispatchOnce() { |
| 609 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 610 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 611 | std::scoped_lock _l(mLock); |
| 612 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 613 | |
| 614 | // Run a dispatch loop if there are no pending commands. |
| 615 | // The dispatch loop might enqueue commands to run afterwards. |
| 616 | if (!haveCommandsLocked()) { |
| 617 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 618 | } |
| 619 | |
| 620 | // Run all pending commands if there are any. |
| 621 | // 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] | 622 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | nextWakeupTime = LONG_LONG_MIN; |
| 624 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 625 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 626 | // If we are still waiting for ack on some events, |
| 627 | // we might have to wake up earlier to check if an app is anr'ing. |
| 628 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 629 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 630 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 631 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 632 | // pending or queued events |
| 633 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 634 | mDispatcherEnteredIdle.notify_all(); |
| 635 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 636 | } // release lock |
| 637 | |
| 638 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 639 | nsecs_t currentTime = now(); |
| 640 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 641 | mLooper->pollOnce(timeoutMillis); |
| 642 | } |
| 643 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 644 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 645 | * Raise ANR if there is no focused window. |
| 646 | * Before the ANR is raised, do a final state check: |
| 647 | * 1. The currently focused application must be the same one we are waiting for. |
| 648 | * 2. Ensure we still don't have a focused window. |
| 649 | */ |
| 650 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 651 | // Check if the application that we are waiting for is still focused. |
| 652 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 653 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 654 | if (focusedApplication == nullptr || |
| 655 | focusedApplication->getApplicationToken() != |
| 656 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 657 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 658 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 659 | focusedApplication->getName().c_str()); |
| 660 | return; // The focused application has changed. |
| 661 | } |
| 662 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 663 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 664 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 665 | if (focusedWindowHandle != nullptr) { |
| 666 | return; // We now have a focused window. No need for ANR. |
| 667 | } |
| 668 | onAnrLocked(mAwaitedFocusedApplication); |
| 669 | } |
| 670 | |
| 671 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 672 | * Check if any of the connections' wait queues have events that are too old. |
| 673 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 674 | * Return the time at which we should wake up next. |
| 675 | */ |
| 676 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 677 | const nsecs_t currentTime = now(); |
| 678 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 679 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 680 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 681 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 682 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 683 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 684 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 685 | return LONG_LONG_MIN; |
| 686 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 687 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 688 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | // Check if any connection ANRs are due |
| 693 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 694 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 695 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 696 | } |
| 697 | |
| 698 | // If we reached here, we have an unresponsive connection. |
| 699 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 700 | if (connection == nullptr) { |
| 701 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 702 | return nextAnrCheck; |
| 703 | } |
| 704 | connection->responsive = false; |
| 705 | // Stop waking up for this unresponsive connection |
| 706 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 707 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 708 | return LONG_LONG_MIN; |
| 709 | } |
| 710 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame^] | 711 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
| 712 | const sp<Connection>& connection) { |
| 713 | if (connection->monitor) { |
| 714 | return mMonitorDispatchingTimeout; |
| 715 | } |
| 716 | const sp<WindowInfoHandle> window = |
| 717 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 718 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 719 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 720 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 721 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 724 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 725 | nsecs_t currentTime = now(); |
| 726 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 727 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 728 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 729 | // repeat if the device is just coming out of sleep. |
| 730 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 731 | resetKeyRepeatLocked(); |
| 732 | } |
| 733 | |
| 734 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 735 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 736 | if (DEBUG_FOCUS) { |
| 737 | ALOGD("Dispatch frozen. Waiting some more."); |
| 738 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 739 | return; |
| 740 | } |
| 741 | |
| 742 | // Optimize latency of app switches. |
| 743 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 744 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 745 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 746 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 747 | *nextWakeupTime = mAppSwitchDueTime; |
| 748 | } |
| 749 | |
| 750 | // Ready to start a new event. |
| 751 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 752 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 753 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 754 | if (isAppSwitchDue) { |
| 755 | // The inbound queue is empty so the app switch key we were waiting |
| 756 | // for will never arrive. Stop waiting for it. |
| 757 | resetPendingAppSwitchLocked(false); |
| 758 | isAppSwitchDue = false; |
| 759 | } |
| 760 | |
| 761 | // Synthesize a key repeat if appropriate. |
| 762 | if (mKeyRepeatState.lastKeyEntry) { |
| 763 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 764 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 765 | } else { |
| 766 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 767 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | // Nothing to do if there is no pending event. |
| 773 | if (!mPendingEvent) { |
| 774 | return; |
| 775 | } |
| 776 | } else { |
| 777 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 778 | mPendingEvent = mInboundQueue.front(); |
| 779 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | traceInboundQueueLengthLocked(); |
| 781 | } |
| 782 | |
| 783 | // Poke user activity for this event. |
| 784 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 785 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 786 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | // Now we have an event to dispatch. |
| 790 | // 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] | 791 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 792 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 793 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 794 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 795 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 796 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 797 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 801 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 805 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 806 | const ConfigurationChangedEntry& typedEntry = |
| 807 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 808 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 809 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 810 | break; |
| 811 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 812 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 813 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 814 | const DeviceResetEntry& typedEntry = |
| 815 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 816 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 817 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 818 | break; |
| 819 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 820 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 821 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 822 | std::shared_ptr<FocusEntry> typedEntry = |
| 823 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 824 | dispatchFocusLocked(currentTime, typedEntry); |
| 825 | done = true; |
| 826 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 827 | break; |
| 828 | } |
| 829 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 830 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 831 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 832 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 833 | done = true; |
| 834 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 835 | break; |
| 836 | } |
| 837 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 838 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 839 | const auto typedEntry = |
| 840 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 841 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 842 | done = true; |
| 843 | break; |
| 844 | } |
| 845 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 846 | case EventEntry::Type::DRAG: { |
| 847 | std::shared_ptr<DragEntry> typedEntry = |
| 848 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 849 | dispatchDragLocked(currentTime, typedEntry); |
| 850 | done = true; |
| 851 | break; |
| 852 | } |
| 853 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 854 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 855 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 856 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 857 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 858 | resetPendingAppSwitchLocked(true); |
| 859 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 860 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 861 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 862 | } |
| 863 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 864 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 865 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 866 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 867 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 868 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 869 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 870 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 871 | break; |
| 872 | } |
| 873 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 874 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 875 | std::shared_ptr<MotionEntry> motionEntry = |
| 876 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 877 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 878 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 879 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 880 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 881 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 882 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 883 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 884 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 885 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 886 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 887 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 888 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 889 | |
| 890 | case EventEntry::Type::SENSOR: { |
| 891 | std::shared_ptr<SensorEntry> sensorEntry = |
| 892 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 893 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 894 | dropReason = DropReason::APP_SWITCH; |
| 895 | } |
| 896 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 897 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 898 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 899 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 900 | dropReason = DropReason::STALE; |
| 901 | } |
| 902 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 903 | done = true; |
| 904 | break; |
| 905 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 909 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 910 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 911 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 912 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 913 | |
| 914 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 915 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 919 | /** |
| 920 | * Return true if the events preceding this incoming motion event should be dropped |
| 921 | * Return false otherwise (the default behaviour) |
| 922 | */ |
| 923 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 924 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 925 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 926 | |
| 927 | // Optimize case where the current application is unresponsive and the user |
| 928 | // decides to touch a window in a different application. |
| 929 | // If the application takes too long to catch up then we drop all events preceding |
| 930 | // the touch into the other window. |
| 931 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 932 | int32_t displayId = motionEntry.displayId; |
| 933 | int32_t x = static_cast<int32_t>( |
| 934 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 935 | int32_t y = static_cast<int32_t>( |
| 936 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 937 | |
| 938 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 939 | sp<WindowInfoHandle> touchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 940 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 941 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 942 | touchedWindowHandle->getApplicationToken() != |
| 943 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 944 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 945 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 946 | "for %s", |
| 947 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 948 | return true; |
| 949 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 950 | |
| 951 | // Alternatively, maybe there's a gesture monitor that could handle this event |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 952 | for (const auto& monitor : getValueByKey(mGestureMonitorsByDisplay, displayId)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 953 | sp<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 954 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 955 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 956 | // This monitor could take more input. Drop all events preceding this |
| 957 | // event, so that gesture monitor could get a chance to receive the stream |
| 958 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 959 | "responsive gesture monitor that may handle the event", |
| 960 | mAwaitedFocusedApplication->getName().c_str()); |
| 961 | return true; |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 967 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 968 | // events to be processed before dispatching the key event. This is because these motion events |
| 969 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 970 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 971 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 972 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 973 | "just send the pending key event to the focused window."); |
| 974 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 975 | } |
| 976 | return false; |
| 977 | } |
| 978 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 979 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 980 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 981 | mInboundQueue.push_back(std::move(newEntry)); |
| 982 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 983 | traceInboundQueueLengthLocked(); |
| 984 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 985 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 986 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 987 | // Optimize app switch latency. |
| 988 | // If the application takes too long to catch up then we drop all events preceding |
| 989 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 990 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 991 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 992 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 993 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 994 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 995 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 996 | if (DEBUG_APP_SWITCH) { |
| 997 | ALOGD("App switch is pending!"); |
| 998 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 999 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1000 | mAppSwitchSawKeyDown = false; |
| 1001 | needWake = true; |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | break; |
| 1006 | } |
| 1007 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1008 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1009 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1010 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1011 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1012 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1013 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1014 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1015 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1016 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1017 | break; |
| 1018 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1019 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1020 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1021 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1022 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1023 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1024 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1025 | // nothing to do |
| 1026 | break; |
| 1027 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | return needWake; |
| 1031 | } |
| 1032 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1033 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1034 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1035 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1036 | mRecentQueue.push_back(entry); |
| 1037 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1038 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1039 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1043 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1044 | int32_t y, TouchState* touchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1045 | bool isStylus, |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1046 | bool addOutsideTargets, |
| 1047 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1048 | if (addOutsideTargets && touchState == nullptr) { |
| 1049 | 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] | 1050 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1051 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1052 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1053 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1054 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1055 | continue; |
| 1056 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1057 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1058 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1059 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1060 | return windowHandle; |
| 1061 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1062 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1063 | if (addOutsideTargets && info.flags.test(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
| 1064 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1065 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1066 | } |
| 1067 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1068 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1069 | } |
| 1070 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1071 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1072 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1073 | // Traverse windows from front to back and gather the touched spy windows. |
| 1074 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1075 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1076 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1077 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1078 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1079 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1080 | continue; |
| 1081 | } |
| 1082 | if (!info.isSpy()) { |
| 1083 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1084 | return spyWindows; |
| 1085 | } |
| 1086 | spyWindows.push_back(windowHandle); |
| 1087 | } |
| 1088 | return spyWindows; |
| 1089 | } |
| 1090 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1091 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1092 | const char* reason; |
| 1093 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1094 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1095 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1096 | ALOGD("Dropped event because policy consumed it."); |
| 1097 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1098 | reason = "inbound event was dropped because the policy consumed it"; |
| 1099 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1100 | case DropReason::DISABLED: |
| 1101 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1102 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1103 | } |
| 1104 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1105 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1106 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1107 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1108 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1109 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1110 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1111 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1112 | "has started interacting with a different application."); |
| 1113 | reason = "inbound event was dropped because the current application is not responding " |
| 1114 | "and the user has started interacting with a different application"; |
| 1115 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1116 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1117 | ALOGI("Dropped event because it is stale."); |
| 1118 | reason = "inbound event was dropped because it is stale"; |
| 1119 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1120 | case DropReason::NO_POINTER_CAPTURE: |
| 1121 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1122 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1123 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1124 | case DropReason::NOT_DROPPED: { |
| 1125 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1126 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1127 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1130 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1131 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1132 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1133 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1134 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1135 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1136 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1137 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1138 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1139 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1140 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1141 | } else { |
| 1142 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1143 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1144 | } |
| 1145 | break; |
| 1146 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1147 | case EventEntry::Type::SENSOR: { |
| 1148 | break; |
| 1149 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1150 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1151 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1152 | break; |
| 1153 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1154 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1155 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1156 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1157 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1158 | 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] | 1159 | break; |
| 1160 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1164 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1165 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1166 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1169 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1170 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1171 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1172 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1176 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1177 | } |
| 1178 | |
| 1179 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1180 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1181 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1182 | if (DEBUG_APP_SWITCH) { |
| 1183 | if (handled) { |
| 1184 | ALOGD("App switch has arrived."); |
| 1185 | } else { |
| 1186 | ALOGD("App switch was abandoned."); |
| 1187 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1188 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1189 | } |
| 1190 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1191 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1192 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1195 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1196 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1197 | return false; |
| 1198 | } |
| 1199 | |
| 1200 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1201 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1202 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1203 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1204 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1205 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1206 | return true; |
| 1207 | } |
| 1208 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1209 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1210 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1214 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1215 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1216 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1217 | releaseInboundEventLocked(entry); |
| 1218 | } |
| 1219 | traceInboundQueueLengthLocked(); |
| 1220 | } |
| 1221 | |
| 1222 | void InputDispatcher::releasePendingEventLocked() { |
| 1223 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1224 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1225 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1229 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1231 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1232 | if (DEBUG_DISPATCH_CYCLE) { |
| 1233 | ALOGD("Injected inbound event was dropped."); |
| 1234 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1235 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1236 | } |
| 1237 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1238 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1239 | } |
| 1240 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1244 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1245 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1249 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1250 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1251 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1252 | uint32_t policyFlags = entry->policyFlags & |
| 1253 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1255 | std::shared_ptr<KeyEntry> newEntry = |
| 1256 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1257 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1258 | entry->flags, entry->keyCode, entry->scanCode, |
| 1259 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1260 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1261 | newEntry->syntheticRepeat = true; |
| 1262 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1263 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1264 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1265 | } |
| 1266 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1267 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1268 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1269 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1270 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1271 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1272 | |
| 1273 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1274 | resetKeyRepeatLocked(); |
| 1275 | |
| 1276 | // 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] | 1277 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1278 | scoped_unlock unlock(mLock); |
| 1279 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1280 | }; |
| 1281 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1282 | return true; |
| 1283 | } |
| 1284 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1285 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1286 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1287 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1288 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1289 | entry.deviceId); |
| 1290 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1291 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1292 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1293 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1294 | resetKeyRepeatLocked(); |
| 1295 | } |
| 1296 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1297 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1298 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1299 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1300 | return true; |
| 1301 | } |
| 1302 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1303 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1304 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1305 | if (mPendingEvent != nullptr) { |
| 1306 | // Move the pending event to the front of the queue. This will give the chance |
| 1307 | // for the pending event to get dispatched to the newly focused window |
| 1308 | mInboundQueue.push_front(mPendingEvent); |
| 1309 | mPendingEvent = nullptr; |
| 1310 | } |
| 1311 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1312 | std::unique_ptr<FocusEntry> focusEntry = |
| 1313 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1314 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1315 | |
| 1316 | // This event should go to the front of the queue, but behind all other focus events |
| 1317 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1318 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1319 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1320 | [](const std::shared_ptr<EventEntry>& event) { |
| 1321 | return event->type == EventEntry::Type::FOCUS; |
| 1322 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1323 | |
| 1324 | // 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] | 1325 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1326 | } |
| 1327 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1328 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1329 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1330 | if (channel == nullptr) { |
| 1331 | return; // Window has gone away |
| 1332 | } |
| 1333 | InputTarget target; |
| 1334 | target.inputChannel = channel; |
| 1335 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1336 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1337 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1338 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1339 | std::string reason = std::string("reason=").append(entry->reason); |
| 1340 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1341 | dispatchEventLocked(currentTime, entry, {target}); |
| 1342 | } |
| 1343 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1344 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1345 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1346 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1347 | dropReason = DropReason::NOT_DROPPED; |
| 1348 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1349 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1350 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1351 | |
| 1352 | if (entry->pointerCaptureRequest.enable) { |
| 1353 | // Enable Pointer Capture. |
| 1354 | if (haveWindowWithPointerCapture && |
| 1355 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
| 1356 | LOG_ALWAYS_FATAL("This request to enable Pointer Capture has already been dispatched " |
| 1357 | "to the window."); |
| 1358 | } |
| 1359 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1360 | // This can happen if a window requests capture and immediately releases capture. |
| 1361 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1362 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1363 | return; |
| 1364 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1365 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1366 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1367 | return; |
| 1368 | } |
| 1369 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1370 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1371 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1372 | mWindowTokenWithPointerCapture = token; |
| 1373 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1374 | // Disable Pointer Capture. |
| 1375 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1376 | // for two reasons: |
| 1377 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1378 | // to disable capture with the same sequence number: one generated by |
| 1379 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1380 | // Capture being disabled in InputReader. |
| 1381 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1382 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1383 | // in InputReader. |
| 1384 | if (!haveWindowWithPointerCapture) { |
| 1385 | // Pointer capture was already forcefully disabled because of focus change. |
| 1386 | dropReason = DropReason::NOT_DROPPED; |
| 1387 | return; |
| 1388 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1389 | token = mWindowTokenWithPointerCapture; |
| 1390 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1391 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1392 | setPointerCaptureLocked(false); |
| 1393 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | auto channel = getInputChannelLocked(token); |
| 1397 | if (channel == nullptr) { |
| 1398 | // Window has gone away, clean up Pointer Capture state. |
| 1399 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1400 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1401 | setPointerCaptureLocked(false); |
| 1402 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1403 | return; |
| 1404 | } |
| 1405 | InputTarget target; |
| 1406 | target.inputChannel = channel; |
| 1407 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1408 | entry->dispatchInProgress = true; |
| 1409 | dispatchEventLocked(currentTime, entry, {target}); |
| 1410 | |
| 1411 | dropReason = DropReason::NOT_DROPPED; |
| 1412 | } |
| 1413 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1414 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1415 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1416 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1417 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1418 | if (windowHandles.empty()) { |
| 1419 | return; |
| 1420 | } |
| 1421 | const std::vector<InputTarget> inputTargets = |
| 1422 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1423 | if (inputTargets.empty()) { |
| 1424 | return; |
| 1425 | } |
| 1426 | entry->dispatchInProgress = true; |
| 1427 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1428 | } |
| 1429 | |
| 1430 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1431 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1432 | std::vector<InputTarget> inputTargets; |
| 1433 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1434 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1435 | const sp<IBinder>& token = handle->getToken(); |
| 1436 | if (token == nullptr) { |
| 1437 | continue; |
| 1438 | } |
| 1439 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1440 | if (channel == nullptr) { |
| 1441 | continue; // Window has gone away |
| 1442 | } |
| 1443 | InputTarget target; |
| 1444 | target.inputChannel = channel; |
| 1445 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1446 | inputTargets.push_back(target); |
| 1447 | } |
| 1448 | return inputTargets; |
| 1449 | } |
| 1450 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1451 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1452 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1453 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1454 | if (!entry->dispatchInProgress) { |
| 1455 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1456 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1457 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1458 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1459 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1460 | // We have seen two identical key downs in a row which indicates that the device |
| 1461 | // driver is automatically generating key repeats itself. We take note of the |
| 1462 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1463 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1464 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1465 | // Make sure we don't get key down from a different device. If a different |
| 1466 | // device Id has same key pressed down, the new device Id will replace the |
| 1467 | // current one to hold the key repeat with repeat count reset. |
| 1468 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1469 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1470 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1471 | resetKeyRepeatLocked(); |
| 1472 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1473 | } else { |
| 1474 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1475 | resetKeyRepeatLocked(); |
| 1476 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1477 | } |
| 1478 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1479 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1480 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1481 | // 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] | 1482 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1483 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1484 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1485 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1486 | resetKeyRepeatLocked(); |
| 1487 | } |
| 1488 | |
| 1489 | if (entry->repeatCount == 1) { |
| 1490 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1491 | } else { |
| 1492 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1493 | } |
| 1494 | |
| 1495 | entry->dispatchInProgress = true; |
| 1496 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1497 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | // Handle case where the policy asked us to try again later last time. |
| 1501 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1502 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1503 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1504 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1505 | } |
| 1506 | return false; // wait until next wakeup |
| 1507 | } |
| 1508 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1509 | entry->interceptKeyWakeupTime = 0; |
| 1510 | } |
| 1511 | |
| 1512 | // Give the policy a chance to intercept the key. |
| 1513 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1514 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1515 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1516 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1517 | |
| 1518 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1519 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1520 | }; |
| 1521 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1522 | return false; // wait for the command to run |
| 1523 | } else { |
| 1524 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1525 | } |
| 1526 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1527 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1528 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1533 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1534 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1535 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1536 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1537 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1538 | return true; |
| 1539 | } |
| 1540 | |
| 1541 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1542 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1543 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1544 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1545 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1546 | return false; |
| 1547 | } |
| 1548 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1549 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1550 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1551 | return true; |
| 1552 | } |
| 1553 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1554 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1555 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1556 | |
| 1557 | // Dispatch the key. |
| 1558 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1559 | return true; |
| 1560 | } |
| 1561 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1562 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1563 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1564 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1565 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1566 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1567 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1568 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1569 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1570 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1573 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1574 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1575 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1576 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1577 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1578 | "source=0x%x, sensorType=%s", |
| 1579 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1580 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1581 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1582 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1583 | scoped_unlock unlock(mLock); |
| 1584 | |
| 1585 | if (entry->accuracyChanged) { |
| 1586 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1587 | } |
| 1588 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1589 | entry->hwTimestamp, entry->values); |
| 1590 | }; |
| 1591 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1595 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1596 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1597 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1598 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1599 | { // acquire lock |
| 1600 | std::scoped_lock _l(mLock); |
| 1601 | |
| 1602 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1603 | std::shared_ptr<EventEntry> entry = *it; |
| 1604 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1605 | it = mInboundQueue.erase(it); |
| 1606 | releaseInboundEventLocked(entry); |
| 1607 | } |
| 1608 | } |
| 1609 | } |
| 1610 | return true; |
| 1611 | } |
| 1612 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1613 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1614 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1615 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1616 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1617 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1618 | entry->dispatchInProgress = true; |
| 1619 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1620 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1624 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1625 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1626 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1627 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1628 | return true; |
| 1629 | } |
| 1630 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1631 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1632 | |
| 1633 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1634 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1635 | |
| 1636 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1637 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1638 | if (isPointerEvent) { |
| 1639 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1640 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1641 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1642 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1643 | } else { |
| 1644 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1645 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1646 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1647 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1648 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1649 | return false; |
| 1650 | } |
| 1651 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1652 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1653 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1654 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1655 | return true; |
| 1656 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1657 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1658 | CancelationOptions::Mode mode(isPointerEvent |
| 1659 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1660 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1661 | CancelationOptions options(mode, "input event injection failed"); |
| 1662 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1663 | return true; |
| 1664 | } |
| 1665 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1666 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1667 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1668 | |
| 1669 | // Dispatch the motion. |
| 1670 | if (conflictingPointerActions) { |
| 1671 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1672 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1673 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1674 | } |
| 1675 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1676 | return true; |
| 1677 | } |
| 1678 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1679 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1680 | bool isExiting, const MotionEntry& motionEntry) { |
| 1681 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1682 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1683 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1684 | PointerCoords pointerCoords; |
| 1685 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1686 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1687 | |
| 1688 | std::unique_ptr<DragEntry> dragEntry = |
| 1689 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1690 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1691 | pointerCoords.getY()); |
| 1692 | |
| 1693 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1694 | } |
| 1695 | |
| 1696 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1697 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1698 | if (channel == nullptr) { |
| 1699 | return; // Window has gone away |
| 1700 | } |
| 1701 | InputTarget target; |
| 1702 | target.inputChannel = channel; |
| 1703 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1704 | entry->dispatchInProgress = true; |
| 1705 | dispatchEventLocked(currentTime, entry, {target}); |
| 1706 | } |
| 1707 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1708 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1709 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1710 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1711 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1712 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1713 | "metaState=0x%x, buttonState=0x%x," |
| 1714 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1715 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1716 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1717 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1718 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1719 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1720 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1721 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1722 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1723 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1724 | "orientation=%f", |
| 1725 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1726 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1727 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1728 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1729 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1730 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1731 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1732 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1733 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1734 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1735 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1736 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1739 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1740 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1741 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1742 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1743 | if (DEBUG_DISPATCH_CYCLE) { |
| 1744 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1745 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1746 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1747 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1748 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1749 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1750 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1751 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1752 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1753 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1754 | sp<Connection> connection = |
| 1755 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1756 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1757 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1758 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1759 | if (DEBUG_FOCUS) { |
| 1760 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1761 | "is no longer registered with the input dispatcher.", |
| 1762 | inputTarget.inputChannel->getName().c_str()); |
| 1763 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1764 | } |
| 1765 | } |
| 1766 | } |
| 1767 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1768 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1769 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1770 | // If the policy decides to close the app, we will get a channel removal event via |
| 1771 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1772 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1773 | // pile up. |
| 1774 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1775 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1776 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1777 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1778 | "application not responding"); |
| 1779 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1783 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1784 | if (DEBUG_FOCUS) { |
| 1785 | ALOGD("Resetting ANR timeouts."); |
| 1786 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1787 | |
| 1788 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1789 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1790 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1791 | } |
| 1792 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1793 | /** |
| 1794 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1795 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1796 | * Focused display is the display that the user most recently interacted with. |
| 1797 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1798 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1799 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1800 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1801 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1802 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1803 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1804 | break; |
| 1805 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1806 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1807 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1808 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1809 | break; |
| 1810 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1811 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1812 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1813 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1814 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1815 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1816 | case EventEntry::Type::SENSOR: |
| 1817 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1818 | 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] | 1819 | return ADISPLAY_ID_NONE; |
| 1820 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1821 | } |
| 1822 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1823 | } |
| 1824 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1825 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1826 | const char* focusedWindowName) { |
| 1827 | if (mAnrTracker.empty()) { |
| 1828 | // already processed all events that we waited for |
| 1829 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1830 | return false; |
| 1831 | } |
| 1832 | |
| 1833 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1834 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1835 | // 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] | 1836 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1837 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1838 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1839 | return true; |
| 1840 | } |
| 1841 | |
| 1842 | // We still have pending events, and already started the timer |
| 1843 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1844 | return true; // Still waiting |
| 1845 | } |
| 1846 | |
| 1847 | // Waited too long, and some connection still hasn't processed all motions |
| 1848 | // Just send the key to the focused window |
| 1849 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1850 | focusedWindowName); |
| 1851 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1852 | return false; |
| 1853 | } |
| 1854 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1855 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1856 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1857 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1858 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1859 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1860 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1861 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1862 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1863 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1864 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1865 | // If there is no currently focused window and no focused application |
| 1866 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1867 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1868 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1869 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1870 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1871 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1872 | } |
| 1873 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1874 | // Drop key events if requested by input feature |
| 1875 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1876 | return InputEventInjectionResult::FAILED; |
| 1877 | } |
| 1878 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1879 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1880 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1881 | // start interacting with another application via touch (app switch). This code can be removed |
| 1882 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1883 | // an app is expected to have a focused window. |
| 1884 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1885 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1886 | // 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] | 1887 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1888 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1889 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1890 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1891 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1892 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1893 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1894 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1895 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1896 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1897 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1898 | // Already raised ANR. Drop the event |
| 1899 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1900 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1901 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1902 | } else { |
| 1903 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1904 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | // we have a valid, non-null focused window |
| 1909 | resetNoFocusedWindowTimeoutLocked(); |
| 1910 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1911 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1912 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1913 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1914 | } |
| 1915 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1916 | if (focusedWindowHandle->getInfo()->paused) { |
| 1917 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1918 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | // If the event is a key event, then we must wait for all previous events to |
| 1922 | // complete before delivering it because previous events may have the |
| 1923 | // side-effect of transferring focus to a different window and we want to |
| 1924 | // ensure that the following keys are sent to the new window. |
| 1925 | // |
| 1926 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1927 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1928 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1929 | // often anticipate pending UI changes when typing on a keyboard. |
| 1930 | // To obtain this behavior, we must serialize key events with respect to all |
| 1931 | // prior input events. |
| 1932 | if (entry.type == EventEntry::Type::KEY) { |
| 1933 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1934 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1935 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1936 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1940 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1941 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1942 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1943 | |
| 1944 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1945 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1946 | } |
| 1947 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1948 | /** |
| 1949 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1950 | * that are currently unresponsive. |
| 1951 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1952 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1953 | const std::vector<Monitor>& monitors) const { |
| 1954 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1955 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1956 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 1957 | sp<Connection> connection = |
| 1958 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1959 | if (connection == nullptr) { |
| 1960 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1961 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1962 | return false; |
| 1963 | } |
| 1964 | if (!connection->responsive) { |
| 1965 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1966 | connection->inputChannel->getName().c_str()); |
| 1967 | return false; |
| 1968 | } |
| 1969 | return true; |
| 1970 | }); |
| 1971 | return responsiveMonitors; |
| 1972 | } |
| 1973 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1974 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1975 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1976 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1977 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1978 | enum InjectionPermission { |
| 1979 | INJECTION_PERMISSION_UNKNOWN, |
| 1980 | INJECTION_PERMISSION_GRANTED, |
| 1981 | INJECTION_PERMISSION_DENIED |
| 1982 | }; |
| 1983 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1984 | // For security reasons, we defer updating the touch state until we are sure that |
| 1985 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1986 | const int32_t displayId = entry.displayId; |
| 1987 | const int32_t action = entry.action; |
| 1988 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1989 | |
| 1990 | // Update the touch state as needed based on the properties of the touch event. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1991 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1992 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1993 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1994 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1995 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1996 | // Copy current touch state into tempTouchState. |
| 1997 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1998 | // 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] | 1999 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2000 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2001 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2002 | oldState = &(it->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2003 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2004 | } |
| 2005 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2006 | bool isSplit = tempTouchState.split; |
| 2007 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 2008 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 2009 | tempTouchState.displayId != displayId); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2010 | |
| 2011 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2012 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2013 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2014 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2015 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2016 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2017 | bool wrongDevice = false; |
| 2018 | if (newGesture) { |
| 2019 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2020 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2021 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2022 | "in display %" PRId32, |
| 2023 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2024 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2025 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2026 | switchedDevice = false; |
| 2027 | wrongDevice = true; |
| 2028 | goto Failed; |
| 2029 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2030 | tempTouchState.reset(); |
| 2031 | tempTouchState.down = down; |
| 2032 | tempTouchState.deviceId = entry.deviceId; |
| 2033 | tempTouchState.source = entry.source; |
| 2034 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2035 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2036 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2037 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2038 | "in display %" PRId32, |
| 2039 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2040 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2041 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2042 | switchedDevice = false; |
| 2043 | wrongDevice = true; |
| 2044 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2048 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2049 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2050 | int32_t x; |
| 2051 | int32_t y; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2052 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2053 | // Always dispatch mouse events to cursor position. |
| 2054 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2055 | x = int32_t(entry.xCursorPosition); |
| 2056 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2057 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2058 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2059 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2060 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2061 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2062 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2063 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2064 | isStylus, isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2065 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2066 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2067 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2068 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2069 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2070 | // 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] | 2071 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2074 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2075 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2076 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2077 | // New window supports splitting, but we should never split mouse events. |
| 2078 | isSplit = !isFromMouse; |
| 2079 | } else if (isSplit) { |
| 2080 | // New window does not support splitting but we have already split events. |
| 2081 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2082 | newTouchedWindowHandle = nullptr; |
| 2083 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2084 | } else { |
| 2085 | // 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] | 2086 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2087 | // should never be split. |
| 2088 | tempTouchState.split = isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2091 | // Update hover state. |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2092 | if (newTouchedWindowHandle != nullptr) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2093 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2094 | newHoverWindowHandle = nullptr; |
| 2095 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2096 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2097 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2098 | } |
| 2099 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2100 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2101 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2102 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2103 | // Process the foreground window first so that it is the first to receive the event. |
| 2104 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
| 2108 | const WindowInfo& info = *windowHandle->getInfo(); |
| 2109 | |
| 2110 | if (info.paused) { |
| 2111 | ALOGI("Not sending touch event to %s because it is paused", |
| 2112 | windowHandle->getName().c_str()); |
| 2113 | continue; |
| 2114 | } |
| 2115 | |
| 2116 | // Ensure the window has a connection and the connection is responsive |
| 2117 | const bool isResponsive = hasResponsiveConnectionLocked(*windowHandle); |
| 2118 | if (!isResponsive) { |
| 2119 | ALOGW("Not sending touch gesture to %s because it is not responsive", |
| 2120 | windowHandle->getName().c_str()); |
| 2121 | continue; |
| 2122 | } |
| 2123 | |
| 2124 | // Drop events that can't be trusted due to occlusion |
| 2125 | if (mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2126 | TouchOcclusionInfo occlusionInfo = |
| 2127 | computeTouchOcclusionInfoLocked(windowHandle, x, y); |
| 2128 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 2129 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2130 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2131 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2132 | ALOGD("%s", log.c_str()); |
| 2133 | } |
| 2134 | } |
| 2135 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
| 2136 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2137 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2138 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2139 | continue; |
| 2140 | } |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | // Drop touch events if requested by input feature |
| 2145 | if (shouldDropInput(entry, windowHandle)) { |
| 2146 | continue; |
| 2147 | } |
| 2148 | |
| 2149 | // Set target flags. |
| 2150 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2151 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2152 | if (!info.isSpy()) { |
| 2153 | // There should only be one new foreground (non-spy) window at this location. |
| 2154 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2155 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2156 | |
| 2157 | if (isSplit) { |
| 2158 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2159 | } |
| 2160 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
| 2161 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2162 | } else if (isWindowObscuredLocked(windowHandle)) { |
| 2163 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2164 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2165 | |
| 2166 | // Update the temporary touch state. |
| 2167 | BitSet32 pointerIds; |
| 2168 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2169 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2170 | pointerIds.markBit(pointerId); |
| 2171 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2172 | |
| 2173 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2174 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2175 | |
| 2176 | const std::vector<Monitor> newGestureMonitors = isDown |
| 2177 | ? selectResponsiveMonitorsLocked( |
| 2178 | getValueByKey(mGestureMonitorsByDisplay, displayId)) |
| 2179 | : std::vector<Monitor>{}; |
| 2180 | |
| 2181 | if (newTouchedWindows.empty() && newGestureMonitors.empty() && |
| 2182 | tempTouchState.gestureMonitors.empty()) { |
| 2183 | ALOGI("Dropping event because there is no touchable window or gesture monitor at " |
| 2184 | "(%d, %d) in display %" PRId32 ".", |
| 2185 | x, y, displayId); |
| 2186 | injectionResult = InputEventInjectionResult::FAILED; |
| 2187 | goto Failed; |
Arthur Hung | 2ea44b9 | 2021-11-23 07:42:21 +0000 | [diff] [blame] | 2188 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2189 | |
| 2190 | tempTouchState.addGestureMonitors(newGestureMonitors); |
| 2191 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2192 | } else { |
| 2193 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2194 | |
| 2195 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2196 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2197 | if (DEBUG_FOCUS) { |
| 2198 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2199 | "dropped the pointer down event in display %" PRId32, |
| 2200 | displayId); |
| 2201 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2202 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2203 | goto Failed; |
| 2204 | } |
| 2205 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2206 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2207 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2208 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2209 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2210 | tempTouchState.isSlippery()) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2211 | const int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2212 | const int32_t y = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2213 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2214 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2215 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2216 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2217 | newTouchedWindowHandle = |
| 2218 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2219 | |
| 2220 | // Drop touch events if requested by input feature |
| 2221 | if (newTouchedWindowHandle != nullptr && |
| 2222 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2223 | newTouchedWindowHandle = nullptr; |
| 2224 | } |
| 2225 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2226 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2227 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2228 | if (DEBUG_FOCUS) { |
| 2229 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2230 | oldTouchedWindowHandle->getName().c_str(), |
| 2231 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2232 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2233 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2234 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2235 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2236 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2237 | |
| 2238 | // Make a slippery entrance into the new window. |
| 2239 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2240 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2241 | } |
| 2242 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2243 | int32_t targetFlags = |
| 2244 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2245 | if (isSplit) { |
| 2246 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2247 | } |
| 2248 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2249 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2250 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2251 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | BitSet32 pointerIds; |
| 2255 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2256 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2257 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2258 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2259 | } |
| 2260 | } |
| 2261 | } |
| 2262 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2263 | // Update dispatching for hover enter and exit. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2264 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2265 | // Let the previous window know that the hover sequence is over, unless we already did |
| 2266 | // it when dispatching it as is to newTouchedWindowHandle. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2267 | if (mLastHoverWindowHandle != nullptr && |
| 2268 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2269 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2270 | if (DEBUG_HOVER) { |
| 2271 | ALOGD("Sending hover exit event to window %s.", |
| 2272 | mLastHoverWindowHandle->getName().c_str()); |
| 2273 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2274 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2275 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2276 | } |
| 2277 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2278 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2279 | // when dispatching it as is to newTouchedWindowHandle. |
| 2280 | if (newHoverWindowHandle != nullptr && |
| 2281 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2282 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2283 | if (DEBUG_HOVER) { |
| 2284 | ALOGD("Sending hover enter event to window %s.", |
| 2285 | newHoverWindowHandle->getName().c_str()); |
| 2286 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2287 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2288 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2289 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | // Check permission to inject into all touched foreground windows and ensure there |
| 2294 | // is at least one touched foreground window. |
| 2295 | { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2296 | bool haveForegroundOrSpyWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2297 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2298 | const bool isForeground = |
| 2299 | (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0; |
| 2300 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 2301 | haveForegroundOrSpyWindow = true; |
| 2302 | LOG_ALWAYS_FATAL_IF(isForeground, |
| 2303 | "Spy window cannot be dispatched as a foreground window."); |
| 2304 | } |
| 2305 | if (isForeground) { |
| 2306 | haveForegroundOrSpyWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2307 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2308 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2309 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2310 | goto Failed; |
| 2311 | } |
| 2312 | } |
| 2313 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2314 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2315 | if (!haveForegroundOrSpyWindow && !hasGestureMonitor) { |
| 2316 | ALOGI("Dropping event because there is no touched window in display " |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2317 | "%" PRId32 " or gesture monitor to receive it.", |
| 2318 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2319 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2320 | goto Failed; |
| 2321 | } |
| 2322 | |
| 2323 | // Permission granted to injection into all touched foreground windows. |
| 2324 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2325 | } |
| 2326 | |
| 2327 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2328 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2329 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2330 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2331 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2332 | if (foregroundWindowHandle) { |
| 2333 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2334 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2335 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2336 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2337 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2338 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2339 | InputTarget::FLAG_ZERO_COORDS, |
| 2340 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2341 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2342 | } |
| 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2347 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2348 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2349 | // of the touch gesture. |
| 2350 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2351 | // engine only supports touch events. We would need to add a mechanism similar |
| 2352 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2353 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2354 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2355 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2356 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2357 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2358 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2359 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2360 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2361 | if (info->displayId == displayId && |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2362 | windowHandle->getInfo()->type == WindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2363 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2364 | .addOrUpdateWindow(windowHandle, |
| 2365 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2366 | InputTarget:: |
| 2367 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2368 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2369 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2376 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2377 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2378 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2379 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2380 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2381 | } |
| 2382 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2383 | for (const auto& monitor : tempTouchState.gestureMonitors) { |
| 2384 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2387 | // Drop the outside or hover touch windows since we will not care about them |
| 2388 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2389 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2390 | |
| 2391 | Failed: |
| 2392 | // Check injection permission once and for all. |
| 2393 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2394 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2395 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2396 | } else { |
| 2397 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2398 | } |
| 2399 | } |
| 2400 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2401 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2402 | return injectionResult; |
| 2403 | } |
| 2404 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2405 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2406 | if (!wrongDevice) { |
| 2407 | if (switchedDevice) { |
| 2408 | if (DEBUG_FOCUS) { |
| 2409 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2410 | } |
| 2411 | *outConflictingPointerActions = true; |
| 2412 | } |
| 2413 | |
| 2414 | if (isHoverAction) { |
| 2415 | // Started hovering, therefore no longer down. |
| 2416 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2417 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2418 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2419 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2420 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2421 | *outConflictingPointerActions = true; |
| 2422 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2423 | tempTouchState.reset(); |
| 2424 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2425 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2426 | tempTouchState.deviceId = entry.deviceId; |
| 2427 | tempTouchState.source = entry.source; |
| 2428 | tempTouchState.displayId = displayId; |
| 2429 | } |
| 2430 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2431 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2432 | // All pointers up or canceled. |
| 2433 | tempTouchState.reset(); |
| 2434 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2435 | // First pointer went down. |
| 2436 | if (oldState && oldState->down) { |
| 2437 | if (DEBUG_FOCUS) { |
| 2438 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2439 | } |
| 2440 | *outConflictingPointerActions = true; |
| 2441 | } |
| 2442 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2443 | // One pointer went up. |
| 2444 | if (isSplit) { |
| 2445 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2446 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2447 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2448 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2449 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2450 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2451 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2452 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2453 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2454 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2455 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2456 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2457 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2458 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2459 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2460 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2461 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2462 | // Save changes unless the action was scroll in which case the temporary touch |
| 2463 | // state was only valid for this one action. |
| 2464 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2465 | if (tempTouchState.displayId >= 0) { |
| 2466 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2467 | } else { |
| 2468 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2469 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2470 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2471 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2472 | // Update hover state. |
| 2473 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2474 | } |
| 2475 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2476 | return injectionResult; |
| 2477 | } |
| 2478 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2479 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2480 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2481 | // have an explicit reason to support it. |
| 2482 | constexpr bool isStylus = false; |
| 2483 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2484 | const sp<WindowInfoHandle> dropWindow = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2485 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2486 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2487 | if (dropWindow) { |
| 2488 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2489 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2490 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2491 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2492 | } |
| 2493 | mDragState.reset(); |
| 2494 | } |
| 2495 | |
| 2496 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2497 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2498 | return; |
| 2499 | } |
| 2500 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2501 | if (!mDragState->isStartDrag) { |
| 2502 | mDragState->isStartDrag = true; |
| 2503 | mDragState->isStylusButtonDownAtStart = |
| 2504 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2505 | } |
| 2506 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2507 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2508 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2509 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2510 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2511 | // Handle the special case : stylus button no longer pressed. |
| 2512 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2513 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2514 | finishDragAndDrop(entry.displayId, x, y); |
| 2515 | return; |
| 2516 | } |
| 2517 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2518 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until |
| 2519 | // we have an explicit reason to support it. |
| 2520 | constexpr bool isStylus = false; |
| 2521 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2522 | const sp<WindowInfoHandle> hoverWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2523 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2524 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2525 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2526 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2527 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2528 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2529 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2530 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2531 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2532 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2533 | } |
| 2534 | // enqueue drag location if needed. |
| 2535 | if (hoverWindowHandle != nullptr) { |
| 2536 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2537 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2538 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2539 | finishDragAndDrop(entry.displayId, x, y); |
| 2540 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
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 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2543 | } |
| 2544 | } |
| 2545 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2546 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2547 | int32_t targetFlags, BitSet32 pointerIds, |
| 2548 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2549 | std::vector<InputTarget>::iterator it = |
| 2550 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2551 | [&windowHandle](const InputTarget& inputTarget) { |
| 2552 | return inputTarget.inputChannel->getConnectionToken() == |
| 2553 | windowHandle->getToken(); |
| 2554 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2555 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2556 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2557 | |
| 2558 | if (it == inputTargets.end()) { |
| 2559 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2560 | std::shared_ptr<InputChannel> inputChannel = |
| 2561 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2562 | if (inputChannel == nullptr) { |
| 2563 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2564 | return; |
| 2565 | } |
| 2566 | inputTarget.inputChannel = inputChannel; |
| 2567 | inputTarget.flags = targetFlags; |
| 2568 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2569 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2570 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2571 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2572 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2573 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2574 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2575 | inputTargets.push_back(inputTarget); |
| 2576 | it = inputTargets.end() - 1; |
| 2577 | } |
| 2578 | |
| 2579 | ALOG_ASSERT(it->flags == targetFlags); |
| 2580 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2581 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2582 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2583 | } |
| 2584 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2585 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2586 | int32_t displayId) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2587 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2588 | mGlobalMonitorsByDisplay.find(displayId); |
| 2589 | |
| 2590 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2591 | const std::vector<Monitor>& monitors = it->second; |
| 2592 | for (const Monitor& monitor : monitors) { |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2593 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2594 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2595 | } |
| 2596 | } |
| 2597 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2598 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, int32_t displayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2599 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2600 | InputTarget target; |
| 2601 | target.inputChannel = monitor.inputChannel; |
| 2602 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2603 | ui::Transform t; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2604 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | b5402b2 | 2021-10-04 05:52:50 -0700 | [diff] [blame] | 2605 | const auto& displayTransform = it->second.transform; |
| 2606 | target.displayTransform = displayTransform; |
| 2607 | t = displayTransform; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2608 | } |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2609 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2610 | inputTargets.push_back(target); |
| 2611 | } |
| 2612 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2613 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2614 | const InjectionState* injectionState) { |
| 2615 | if (injectionState && |
| 2616 | (windowHandle == nullptr || |
| 2617 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2618 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2619 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2620 | ALOGW("Permission denied: injecting event from pid %d uid %d to window %s " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2621 | "owned by uid %d", |
| 2622 | injectionState->injectorPid, injectionState->injectorUid, |
| 2623 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2624 | } else { |
| 2625 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2626 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2627 | } |
| 2628 | return false; |
| 2629 | } |
| 2630 | return true; |
| 2631 | } |
| 2632 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2633 | /** |
| 2634 | * Indicate whether one window handle should be considered as obscuring |
| 2635 | * another window handle. We only check a few preconditions. Actually |
| 2636 | * checking the bounds is left to the caller. |
| 2637 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2638 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2639 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2640 | // Compare by token so cloned layers aren't counted |
| 2641 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2642 | return false; |
| 2643 | } |
| 2644 | auto info = windowHandle->getInfo(); |
| 2645 | auto otherInfo = otherHandle->getInfo(); |
| 2646 | if (!otherInfo->visible) { |
| 2647 | return false; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2648 | } else if (otherInfo->alpha == 0 && otherInfo->flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2649 | // Those act as if they were invisible, so we don't need to flag them. |
| 2650 | // We do want to potentially flag touchable windows even if they have 0 |
| 2651 | // opacity, since they can consume touches and alter the effects of the |
| 2652 | // user interaction (eg. apps that rely on |
| 2653 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2654 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2655 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2656 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2657 | // If ownerUid is the same we don't generate occlusion events as there |
| 2658 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2659 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2660 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2661 | return false; |
| 2662 | } else if (otherInfo->displayId != info->displayId) { |
| 2663 | return false; |
| 2664 | } |
| 2665 | return true; |
| 2666 | } |
| 2667 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2668 | /** |
| 2669 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2670 | * untrusted, one should check: |
| 2671 | * |
| 2672 | * 1. If result.hasBlockingOcclusion is true. |
| 2673 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2674 | * BLOCK_UNTRUSTED. |
| 2675 | * |
| 2676 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2677 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2678 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2679 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2680 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2681 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2682 | * |
| 2683 | * If neither of those is true, then it means the touch can be allowed. |
| 2684 | */ |
| 2685 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2686 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2687 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2688 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2689 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2690 | TouchOcclusionInfo info; |
| 2691 | info.hasBlockingOcclusion = false; |
| 2692 | info.obscuringOpacity = 0; |
| 2693 | info.obscuringUid = -1; |
| 2694 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2695 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2696 | if (windowHandle == otherHandle) { |
| 2697 | break; // All future windows are below us. Exit early. |
| 2698 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2699 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2700 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2701 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2702 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2703 | info.debugInfo.push_back( |
| 2704 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2705 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2706 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2707 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2708 | // window's touch occlusion mode |
| 2709 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2710 | info.hasBlockingOcclusion = true; |
| 2711 | info.obscuringUid = otherInfo->ownerUid; |
| 2712 | info.obscuringPackage = otherInfo->packageName; |
| 2713 | break; |
| 2714 | } |
| 2715 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2716 | uint32_t uid = otherInfo->ownerUid; |
| 2717 | float opacity = |
| 2718 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2719 | // Given windows A and B: |
| 2720 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2721 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2722 | opacityByUid[uid] = opacity; |
| 2723 | if (opacity > info.obscuringOpacity) { |
| 2724 | info.obscuringOpacity = opacity; |
| 2725 | info.obscuringUid = uid; |
| 2726 | info.obscuringPackage = otherInfo->packageName; |
| 2727 | } |
| 2728 | } |
| 2729 | } |
| 2730 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2731 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2732 | info.debugInfo.push_back( |
| 2733 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2734 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2735 | return info; |
| 2736 | } |
| 2737 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2738 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2739 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2740 | return StringPrintf(INDENT2 |
| 2741 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2742 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2743 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2744 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2745 | isTouchedWindow ? "[TOUCHED] " : "", ftl::enum_string(info->type).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2746 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2747 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2748 | info->frameTop, info->frameRight, info->frameBottom, |
| 2749 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2750 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2751 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2752 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2755 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2756 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2757 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2758 | occlusionInfo.obscuringUid); |
| 2759 | return false; |
| 2760 | } |
| 2761 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2762 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2763 | "%.2f, maximum allowed = %.2f)", |
| 2764 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2765 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2766 | return false; |
| 2767 | } |
| 2768 | return true; |
| 2769 | } |
| 2770 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2771 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2772 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2773 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2774 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2775 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2776 | if (windowHandle == otherHandle) { |
| 2777 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2778 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2779 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2780 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2781 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2782 | return true; |
| 2783 | } |
| 2784 | } |
| 2785 | return false; |
| 2786 | } |
| 2787 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2788 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2789 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2790 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2791 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2792 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2793 | if (windowHandle == otherHandle) { |
| 2794 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2795 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2796 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2797 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2798 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2799 | return true; |
| 2800 | } |
| 2801 | } |
| 2802 | return false; |
| 2803 | } |
| 2804 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2805 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2806 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2807 | if (applicationHandle != nullptr) { |
| 2808 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2809 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2810 | } else { |
| 2811 | return applicationHandle->getName(); |
| 2812 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2813 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2814 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2815 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2816 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2817 | } |
| 2818 | } |
| 2819 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2820 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2821 | if (!isUserActivityEvent(eventEntry)) { |
| 2822 | // 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] | 2823 | return; |
| 2824 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2825 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2826 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2827 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2828 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
| 2829 | if (info->inputFeatures.test(WindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2830 | if (DEBUG_DISPATCH_CYCLE) { |
| 2831 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2832 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2833 | return; |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2838 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2839 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2840 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2841 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2842 | return; |
| 2843 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2844 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2845 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2846 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2847 | } |
| 2848 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2849 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2850 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2851 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2852 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2853 | return; |
| 2854 | } |
| 2855 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2856 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2857 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2858 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2859 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2860 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2861 | break; |
| 2862 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2863 | } |
| 2864 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2865 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2866 | REQUIRES(mLock) { |
| 2867 | scoped_unlock unlock(mLock); |
| 2868 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2869 | }; |
| 2870 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2874 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2875 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2876 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2877 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2878 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2879 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2880 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2881 | ATRACE_NAME(message.c_str()); |
| 2882 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2883 | if (DEBUG_DISPATCH_CYCLE) { |
| 2884 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2885 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2886 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2887 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2888 | inputTarget.getPointerInfoString().c_str()); |
| 2889 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2890 | |
| 2891 | // Skip this event if the connection status is not normal. |
| 2892 | // 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] | 2893 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2894 | if (DEBUG_DISPATCH_CYCLE) { |
| 2895 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2896 | connection->getInputChannelName().c_str(), |
| 2897 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2898 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2899 | return; |
| 2900 | } |
| 2901 | |
| 2902 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2903 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2904 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2905 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2906 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2907 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2908 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2909 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2910 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2911 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2912 | if (!splitMotionEntry) { |
| 2913 | return; // split event was dropped |
| 2914 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2915 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2916 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2917 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2918 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2919 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2920 | if (DEBUG_FOCUS) { |
| 2921 | ALOGD("channel '%s' ~ Split motion event.", |
| 2922 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2923 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2924 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2925 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2926 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2927 | return; |
| 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2932 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2933 | } |
| 2934 | |
| 2935 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2936 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2937 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2938 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2939 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2940 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2941 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2942 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2943 | ATRACE_NAME(message.c_str()); |
| 2944 | } |
| 2945 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2946 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2947 | |
| 2948 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2949 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2950 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2951 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2952 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2953 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2954 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2955 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2956 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2957 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2958 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2959 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2960 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2961 | |
| 2962 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2963 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2964 | startDispatchCycleLocked(currentTime, connection); |
| 2965 | } |
| 2966 | } |
| 2967 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2968 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2969 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2970 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2971 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2972 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2973 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2974 | connection->getInputChannelName().c_str(), |
| 2975 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2976 | ATRACE_NAME(message.c_str()); |
| 2977 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2978 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2979 | if (!(inputTargetFlags & dispatchMode)) { |
| 2980 | return; |
| 2981 | } |
| 2982 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2983 | |
| 2984 | // This is a new event. |
| 2985 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2986 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2987 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2988 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2989 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2990 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2991 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2992 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2993 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2994 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2995 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2996 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2997 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2998 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2999 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3000 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3001 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3002 | if (DEBUG_DISPATCH_CYCLE) { |
| 3003 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3004 | "event", |
| 3005 | connection->getInputChannelName().c_str()); |
| 3006 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3007 | return; // skip the inconsistent event |
| 3008 | } |
| 3009 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3010 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3011 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3012 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3013 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3014 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3015 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3016 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3017 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3018 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3019 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3020 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 3021 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 3022 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3023 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 3024 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3025 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 3026 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 3027 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 3028 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3029 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3030 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3031 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3032 | } |
| 3033 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3034 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3035 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3036 | if (DEBUG_DISPATCH_CYCLE) { |
| 3037 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3038 | "enter event", |
| 3039 | connection->getInputChannelName().c_str()); |
| 3040 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3041 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3042 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3043 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3044 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3045 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3046 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3047 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3048 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3049 | } |
| 3050 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3051 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3052 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3053 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3054 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3055 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3056 | if (DEBUG_DISPATCH_CYCLE) { |
| 3057 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3058 | "event", |
| 3059 | connection->getInputChannelName().c_str()); |
| 3060 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3061 | return; // skip the inconsistent event |
| 3062 | } |
| 3063 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3064 | dispatchEntry->resolvedEventId = |
| 3065 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3066 | ? mIdGenerator.nextId() |
| 3067 | : motionEntry.id; |
| 3068 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3069 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3070 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3071 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3072 | ATRACE_NAME(message.c_str()); |
| 3073 | } |
| 3074 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3075 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3076 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3077 | // Skip reporting pointer down outside focus to the policy. |
| 3078 | break; |
| 3079 | } |
| 3080 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3081 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3082 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3083 | |
| 3084 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3085 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3086 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3087 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3088 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3089 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3090 | break; |
| 3091 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3092 | case EventEntry::Type::SENSOR: { |
| 3093 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3094 | break; |
| 3095 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3096 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3097 | case EventEntry::Type::DEVICE_RESET: { |
| 3098 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3099 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3100 | break; |
| 3101 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | // Remember that we are waiting for this dispatch to complete. |
| 3105 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3106 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3110 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3111 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3112 | } |
| 3113 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3114 | /** |
| 3115 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3116 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3117 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3118 | * We will see both launcher and wallpaper in that list. |
| 3119 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3120 | * until the set of interacted connections changes. |
| 3121 | * |
| 3122 | * The following items are skipped, to reduce the logspam: |
| 3123 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3124 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3125 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3126 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3127 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3128 | */ |
| 3129 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3130 | const std::vector<InputTarget>& targets) { |
| 3131 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3132 | if (entry.type == EventEntry::Type::KEY) { |
| 3133 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3134 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3135 | return; |
| 3136 | } |
| 3137 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3138 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3139 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3140 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3141 | return; |
| 3142 | } |
| 3143 | } else { |
| 3144 | return; // Not a key or a motion |
| 3145 | } |
| 3146 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3147 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3148 | std::vector<sp<Connection>> newConnections; |
| 3149 | for (const InputTarget& target : targets) { |
| 3150 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3151 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3152 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3153 | } |
| 3154 | |
| 3155 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3156 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3157 | if (connection == nullptr) { |
| 3158 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3159 | } |
| 3160 | newConnectionTokens.insert(std::move(token)); |
| 3161 | newConnections.emplace_back(connection); |
| 3162 | } |
| 3163 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3164 | return; // no change |
| 3165 | } |
| 3166 | mInteractionConnectionTokens = newConnectionTokens; |
| 3167 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3168 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3169 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3170 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3171 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3172 | std::string message = "Interaction with: " + targetList; |
| 3173 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3174 | message += "<none>"; |
| 3175 | } |
| 3176 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3177 | } |
| 3178 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3179 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3180 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3181 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3182 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3183 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3184 | return; |
| 3185 | } |
| 3186 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3187 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3188 | if (focusedToken == token) { |
| 3189 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3190 | return; |
| 3191 | } |
| 3192 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3193 | auto command = [this, token]() REQUIRES(mLock) { |
| 3194 | scoped_unlock unlock(mLock); |
| 3195 | mPolicy->onPointerDownOutsideFocus(token); |
| 3196 | }; |
| 3197 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3201 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3202 | if (ATRACE_ENABLED()) { |
| 3203 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3204 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3205 | ATRACE_NAME(message.c_str()); |
| 3206 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3207 | if (DEBUG_DISPATCH_CYCLE) { |
| 3208 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3209 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3210 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3211 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3212 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3213 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame^] | 3214 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3215 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3216 | |
| 3217 | // Publish the event. |
| 3218 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3219 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3220 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3221 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3222 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3223 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3224 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3225 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3226 | status = connection->inputPublisher |
| 3227 | .publishKeyEvent(dispatchEntry->seq, |
| 3228 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3229 | keyEntry.source, keyEntry.displayId, |
| 3230 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3231 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3232 | keyEntry.scanCode, keyEntry.metaState, |
| 3233 | keyEntry.repeatCount, keyEntry.downTime, |
| 3234 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3235 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3236 | } |
| 3237 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3238 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3239 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3240 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3241 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3242 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3243 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3244 | // Set the X and Y offset and X and Y scale depending on the input source. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3245 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3246 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3247 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3248 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3249 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3250 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3251 | // Don't apply window scale here since we don't want scale to affect raw |
| 3252 | // coordinates. The scale will be sent back to the client and applied |
| 3253 | // later when requesting relative coordinates. |
| 3254 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3255 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3256 | } |
| 3257 | usingCoords = scaledCoords; |
| 3258 | } |
| 3259 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3260 | // We don't want the dispatch target to know. |
| 3261 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3262 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3263 | scaledCoords[i].clear(); |
| 3264 | } |
| 3265 | usingCoords = scaledCoords; |
| 3266 | } |
| 3267 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3268 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3269 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3270 | |
| 3271 | // Publish the motion event. |
| 3272 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3273 | .publishMotionEvent(dispatchEntry->seq, |
| 3274 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3275 | motionEntry.deviceId, motionEntry.source, |
| 3276 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3277 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3278 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3279 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3280 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3281 | motionEntry.buttonState, |
| 3282 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3283 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3284 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3285 | motionEntry.xCursorPosition, |
| 3286 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3287 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3288 | motionEntry.downTime, motionEntry.eventTime, |
| 3289 | motionEntry.pointerCount, |
| 3290 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3291 | break; |
| 3292 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3293 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3294 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3295 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3296 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3297 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3298 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3299 | break; |
| 3300 | } |
| 3301 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3302 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3303 | const TouchModeEntry& touchModeEntry = |
| 3304 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3305 | status = connection->inputPublisher |
| 3306 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3307 | touchModeEntry.inTouchMode); |
| 3308 | |
| 3309 | break; |
| 3310 | } |
| 3311 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3312 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3313 | const auto& captureEntry = |
| 3314 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3315 | status = connection->inputPublisher |
| 3316 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3317 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3318 | break; |
| 3319 | } |
| 3320 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3321 | case EventEntry::Type::DRAG: { |
| 3322 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3323 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3324 | dragEntry.id, dragEntry.x, |
| 3325 | dragEntry.y, |
| 3326 | dragEntry.isExiting); |
| 3327 | break; |
| 3328 | } |
| 3329 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3330 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3331 | case EventEntry::Type::DEVICE_RESET: |
| 3332 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3333 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3334 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3335 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3336 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | // Check the result. |
| 3340 | if (status) { |
| 3341 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3342 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3343 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3344 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3345 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3346 | "event to it, status=%s(%d)", |
| 3347 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3348 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3349 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3350 | } else { |
| 3351 | // Pipe is full and we are waiting for the app to finish process some events |
| 3352 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3353 | if (DEBUG_DISPATCH_CYCLE) { |
| 3354 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3355 | "waiting for the application to catch up", |
| 3356 | connection->getInputChannelName().c_str()); |
| 3357 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3358 | } |
| 3359 | } else { |
| 3360 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3361 | "status=%s(%d)", |
| 3362 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3363 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3364 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3365 | } |
| 3366 | return; |
| 3367 | } |
| 3368 | |
| 3369 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3370 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3371 | connection->outboundQueue.end(), |
| 3372 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3373 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3374 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3375 | if (connection->responsive) { |
| 3376 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3377 | connection->inputChannel->getConnectionToken()); |
| 3378 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3379 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3380 | } |
| 3381 | } |
| 3382 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3383 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3384 | size_t size; |
| 3385 | switch (event.type) { |
| 3386 | case VerifiedInputEvent::Type::KEY: { |
| 3387 | size = sizeof(VerifiedKeyEvent); |
| 3388 | break; |
| 3389 | } |
| 3390 | case VerifiedInputEvent::Type::MOTION: { |
| 3391 | size = sizeof(VerifiedMotionEvent); |
| 3392 | break; |
| 3393 | } |
| 3394 | } |
| 3395 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3396 | return mHmacKeyManager.sign(start, size); |
| 3397 | } |
| 3398 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3399 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3400 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3401 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3402 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3403 | // Only sign events up and down events as the purely move events |
| 3404 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3405 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3406 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3407 | |
| 3408 | VerifiedMotionEvent verifiedEvent = |
| 3409 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3410 | verifiedEvent.actionMasked = actionMasked; |
| 3411 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3412 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3416 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3417 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3418 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3419 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3420 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3421 | } |
| 3422 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3423 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3424 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3425 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3426 | if (DEBUG_DISPATCH_CYCLE) { |
| 3427 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3428 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3429 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3430 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3431 | if (connection->status == Connection::Status::BROKEN || |
| 3432 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3433 | return; |
| 3434 | } |
| 3435 | |
| 3436 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3437 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3438 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3439 | }; |
| 3440 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3444 | const sp<Connection>& connection, |
| 3445 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3446 | if (DEBUG_DISPATCH_CYCLE) { |
| 3447 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3448 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3449 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3450 | |
| 3451 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3452 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3453 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3454 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3455 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3456 | |
| 3457 | // The connection appears to be unrecoverably broken. |
| 3458 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3459 | if (connection->status == Connection::Status::NORMAL) { |
| 3460 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3461 | |
| 3462 | if (notify) { |
| 3463 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3464 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3465 | connection->getInputChannelName().c_str()); |
| 3466 | |
| 3467 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3468 | scoped_unlock unlock(mLock); |
| 3469 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3470 | }; |
| 3471 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3472 | } |
| 3473 | } |
| 3474 | } |
| 3475 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3476 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3477 | while (!queue.empty()) { |
| 3478 | DispatchEntry* dispatchEntry = queue.front(); |
| 3479 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3480 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3481 | } |
| 3482 | } |
| 3483 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3484 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3485 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3486 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3487 | } |
| 3488 | delete dispatchEntry; |
| 3489 | } |
| 3490 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3491 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3492 | std::scoped_lock _l(mLock); |
| 3493 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3494 | if (connection == nullptr) { |
| 3495 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3496 | connectionToken.get(), events); |
| 3497 | return 0; // remove the callback |
| 3498 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3499 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3500 | bool notify; |
| 3501 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3502 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3503 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3504 | "events=0x%x", |
| 3505 | connection->getInputChannelName().c_str(), events); |
| 3506 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3507 | } |
| 3508 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3509 | nsecs_t currentTime = now(); |
| 3510 | bool gotOne = false; |
| 3511 | status_t status = OK; |
| 3512 | for (;;) { |
| 3513 | Result<InputPublisher::ConsumerResponse> result = |
| 3514 | connection->inputPublisher.receiveConsumerResponse(); |
| 3515 | if (!result.ok()) { |
| 3516 | status = result.error().code(); |
| 3517 | break; |
| 3518 | } |
| 3519 | |
| 3520 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3521 | const InputPublisher::Finished& finish = |
| 3522 | std::get<InputPublisher::Finished>(*result); |
| 3523 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3524 | finish.consumeTime); |
| 3525 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3526 | if (shouldReportMetricsForConnection(*connection)) { |
| 3527 | const InputPublisher::Timeline& timeline = |
| 3528 | std::get<InputPublisher::Timeline>(*result); |
| 3529 | mLatencyTracker |
| 3530 | .trackGraphicsLatency(timeline.inputEventId, |
| 3531 | connection->inputChannel->getConnectionToken(), |
| 3532 | std::move(timeline.graphicsTimeline)); |
| 3533 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3534 | } |
| 3535 | gotOne = true; |
| 3536 | } |
| 3537 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3538 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3539 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3540 | return 1; |
| 3541 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3542 | } |
| 3543 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3544 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3545 | if (notify) { |
| 3546 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3547 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3548 | status); |
| 3549 | } |
| 3550 | } else { |
| 3551 | // Monitor channels are never explicitly unregistered. |
| 3552 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3553 | const bool stillHaveWindowHandle = |
| 3554 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3555 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3556 | if (notify) { |
| 3557 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3558 | connection->getInputChannelName().c_str(), events); |
| 3559 | } |
| 3560 | } |
| 3561 | |
| 3562 | // Remove the channel. |
| 3563 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3564 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3565 | } |
| 3566 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3567 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3568 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3569 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3570 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3571 | } |
| 3572 | } |
| 3573 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3574 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3575 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3576 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3577 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3578 | } |
| 3579 | |
| 3580 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3581 | const CancelationOptions& options, |
| 3582 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3583 | for (const auto& it : monitorsByDisplay) { |
| 3584 | const std::vector<Monitor>& monitors = it.second; |
| 3585 | for (const Monitor& monitor : monitors) { |
| 3586 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3587 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3588 | } |
| 3589 | } |
| 3590 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3591 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3592 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3593 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3594 | if (connection == nullptr) { |
| 3595 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3596 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3597 | |
| 3598 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3602 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3603 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3604 | return; |
| 3605 | } |
| 3606 | |
| 3607 | nsecs_t currentTime = now(); |
| 3608 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3609 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3610 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3611 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3612 | if (cancelationEvents.empty()) { |
| 3613 | return; |
| 3614 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3615 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3616 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3617 | "with reality: %s, mode=%d.", |
| 3618 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3619 | options.mode); |
| 3620 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3621 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3622 | std::string reason = std::string("reason=").append(options.reason); |
| 3623 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3624 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3625 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3626 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3627 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3628 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3629 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3630 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3631 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3632 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3633 | } |
| 3634 | target.inputChannel = connection->inputChannel; |
| 3635 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3636 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3637 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3638 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3639 | switch (cancelationEventEntry->type) { |
| 3640 | case EventEntry::Type::KEY: { |
| 3641 | logOutboundKeyDetails("cancel - ", |
| 3642 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3643 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3644 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3645 | case EventEntry::Type::MOTION: { |
| 3646 | logOutboundMotionDetails("cancel - ", |
| 3647 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3648 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3649 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3650 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3651 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3652 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3653 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3654 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3655 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3656 | break; |
| 3657 | } |
| 3658 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3659 | case EventEntry::Type::DEVICE_RESET: |
| 3660 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3661 | 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] | 3662 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3663 | break; |
| 3664 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3665 | } |
| 3666 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3667 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3668 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3669 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3670 | |
| 3671 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3672 | } |
| 3673 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3674 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3675 | const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3676 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3677 | return; |
| 3678 | } |
| 3679 | |
| 3680 | nsecs_t currentTime = now(); |
| 3681 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3682 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3683 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3684 | |
| 3685 | if (downEvents.empty()) { |
| 3686 | return; |
| 3687 | } |
| 3688 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3689 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3690 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3691 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3692 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3693 | |
| 3694 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3695 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3696 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3697 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3698 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3699 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3700 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3701 | } |
| 3702 | target.inputChannel = connection->inputChannel; |
| 3703 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3704 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3705 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3706 | switch (downEventEntry->type) { |
| 3707 | case EventEntry::Type::MOTION: { |
| 3708 | logOutboundMotionDetails("down - ", |
| 3709 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3710 | break; |
| 3711 | } |
| 3712 | |
| 3713 | case EventEntry::Type::KEY: |
| 3714 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3715 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3716 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3717 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3718 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3719 | case EventEntry::Type::SENSOR: |
| 3720 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3721 | 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] | 3722 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3723 | break; |
| 3724 | } |
| 3725 | } |
| 3726 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3727 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3728 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | startDispatchCycleLocked(currentTime, connection); |
| 3732 | } |
| 3733 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3734 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3735 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3736 | ALOG_ASSERT(pointerIds.value != 0); |
| 3737 | |
| 3738 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3739 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3740 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3741 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3742 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3743 | uint32_t splitPointerCount = 0; |
| 3744 | |
| 3745 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3746 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3747 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3748 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3749 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3750 | if (pointerIds.hasBit(pointerId)) { |
| 3751 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3752 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3753 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3754 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3755 | splitPointerCount += 1; |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | if (splitPointerCount != pointerIds.count()) { |
| 3760 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3761 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3762 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3763 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3764 | // in this way. |
| 3765 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3766 | "we expected there to be %d pointers. This probably means we received " |
| 3767 | "a broken sequence of pointer ids from the input device.", |
| 3768 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3769 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3770 | } |
| 3771 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3772 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3773 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3774 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3775 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3776 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3777 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3778 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3779 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3780 | if (pointerIds.hasBit(pointerId)) { |
| 3781 | if (pointerIds.count() == 1) { |
| 3782 | // The first/last pointer went down/up. |
| 3783 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3784 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3785 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3786 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3787 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3788 | } else { |
| 3789 | // A secondary pointer went down/up. |
| 3790 | uint32_t splitPointerIndex = 0; |
| 3791 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3792 | splitPointerIndex += 1; |
| 3793 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3794 | action = maskedAction | |
| 3795 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3796 | } |
| 3797 | } else { |
| 3798 | // An unrelated pointer changed. |
| 3799 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3800 | } |
| 3801 | } |
| 3802 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3803 | int32_t newId = mIdGenerator.nextId(); |
| 3804 | if (ATRACE_ENABLED()) { |
| 3805 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3806 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3807 | originalMotionEntry.id, newId); |
| 3808 | ATRACE_NAME(message.c_str()); |
| 3809 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3810 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3811 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3812 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3813 | originalMotionEntry.displayId, |
| 3814 | originalMotionEntry.policyFlags, action, |
| 3815 | originalMotionEntry.actionButton, |
| 3816 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3817 | originalMotionEntry.buttonState, |
| 3818 | originalMotionEntry.classification, |
| 3819 | originalMotionEntry.edgeFlags, |
| 3820 | originalMotionEntry.xPrecision, |
| 3821 | originalMotionEntry.yPrecision, |
| 3822 | originalMotionEntry.xCursorPosition, |
| 3823 | originalMotionEntry.yCursorPosition, |
| 3824 | originalMotionEntry.downTime, splitPointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3825 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3826 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3827 | if (originalMotionEntry.injectionState) { |
| 3828 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3829 | splitMotionEntry->injectionState->refCount += 1; |
| 3830 | } |
| 3831 | |
| 3832 | return splitMotionEntry; |
| 3833 | } |
| 3834 | |
| 3835 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3836 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3837 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3838 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3839 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3840 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3841 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3842 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3843 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3844 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3845 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3846 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3847 | } // release lock |
| 3848 | |
| 3849 | if (needWake) { |
| 3850 | mLooper->wake(); |
| 3851 | } |
| 3852 | } |
| 3853 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3854 | /** |
| 3855 | * If one of the meta shortcuts is detected, process them here: |
| 3856 | * Meta + Backspace -> generate BACK |
| 3857 | * Meta + Enter -> generate HOME |
| 3858 | * This will potentially overwrite keyCode and metaState. |
| 3859 | */ |
| 3860 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3861 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3862 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3863 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3864 | if (keyCode == AKEYCODE_DEL) { |
| 3865 | newKeyCode = AKEYCODE_BACK; |
| 3866 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3867 | newKeyCode = AKEYCODE_HOME; |
| 3868 | } |
| 3869 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3870 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3871 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3872 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3873 | keyCode = newKeyCode; |
| 3874 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3875 | } |
| 3876 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3877 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3878 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3879 | // 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] | 3880 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3881 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3882 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3883 | if (replacementIt != mReplacedKeys.end()) { |
| 3884 | keyCode = replacementIt->second; |
| 3885 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3886 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3887 | } |
| 3888 | } |
| 3889 | } |
| 3890 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3891 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3892 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3893 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3894 | "policyFlags=0x%x, action=0x%x, " |
| 3895 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3896 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3897 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3898 | args->downTime); |
| 3899 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3900 | if (!validateKeyEvent(args->action)) { |
| 3901 | return; |
| 3902 | } |
| 3903 | |
| 3904 | uint32_t policyFlags = args->policyFlags; |
| 3905 | int32_t flags = args->flags; |
| 3906 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3907 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3908 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3909 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3910 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3911 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3912 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3913 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3914 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3915 | metaState |= AMETA_FUNCTION_ON; |
| 3916 | } |
| 3917 | |
| 3918 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3919 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3920 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3921 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3922 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3923 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3924 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3925 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3926 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3927 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3928 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3929 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3930 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3931 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3932 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3933 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3934 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3935 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3936 | { // acquire lock |
| 3937 | mLock.lock(); |
| 3938 | |
| 3939 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3940 | mLock.unlock(); |
| 3941 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3942 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3943 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3944 | return; // event was consumed by the filter |
| 3945 | } |
| 3946 | |
| 3947 | mLock.lock(); |
| 3948 | } |
| 3949 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3950 | std::unique_ptr<KeyEntry> newEntry = |
| 3951 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3952 | args->displayId, policyFlags, args->action, flags, |
| 3953 | keyCode, args->scanCode, metaState, repeatCount, |
| 3954 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3955 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3956 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3957 | mLock.unlock(); |
| 3958 | } // release lock |
| 3959 | |
| 3960 | if (needWake) { |
| 3961 | mLooper->wake(); |
| 3962 | } |
| 3963 | } |
| 3964 | |
| 3965 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3966 | return mInputFilterEnabled; |
| 3967 | } |
| 3968 | |
| 3969 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3970 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3971 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3972 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 3973 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3974 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 3975 | "yCursorPosition=%f, downTime=%" PRId64, |
| 3976 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3977 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3978 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3979 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 3980 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3981 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 3982 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3983 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3984 | "orientation=%f", |
| 3985 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3986 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3987 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3988 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3989 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3990 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3991 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3992 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3993 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3994 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 3995 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3996 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3997 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3998 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3999 | return; |
| 4000 | } |
| 4001 | |
| 4002 | uint32_t policyFlags = args->policyFlags; |
| 4003 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4004 | |
| 4005 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4006 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4007 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4008 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4009 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4010 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4011 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4012 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4013 | { // acquire lock |
| 4014 | mLock.lock(); |
| 4015 | |
| 4016 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4017 | ui::Transform displayTransform; |
| 4018 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4019 | displayTransform = it->second.transform; |
| 4020 | } |
| 4021 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4022 | mLock.unlock(); |
| 4023 | |
| 4024 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4025 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4026 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4027 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4028 | displayTransform, args->xPrecision, args->yPrecision, |
| 4029 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4030 | args->downTime, args->eventTime, args->pointerCount, |
| 4031 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4032 | |
| 4033 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4034 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4035 | return; // event was consumed by the filter |
| 4036 | } |
| 4037 | |
| 4038 | mLock.lock(); |
| 4039 | } |
| 4040 | |
| 4041 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4042 | std::unique_ptr<MotionEntry> newEntry = |
| 4043 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4044 | args->source, args->displayId, policyFlags, |
| 4045 | args->action, args->actionButton, args->flags, |
| 4046 | args->metaState, args->buttonState, |
| 4047 | args->classification, args->edgeFlags, |
| 4048 | args->xPrecision, args->yPrecision, |
| 4049 | args->xCursorPosition, args->yCursorPosition, |
| 4050 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4051 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4052 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4053 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4054 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4055 | !mInputFilterEnabled) { |
| 4056 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4057 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4058 | } |
| 4059 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4060 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4061 | mLock.unlock(); |
| 4062 | } // release lock |
| 4063 | |
| 4064 | if (needWake) { |
| 4065 | mLooper->wake(); |
| 4066 | } |
| 4067 | } |
| 4068 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4069 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4070 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4071 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4072 | " sensorType=%s", |
| 4073 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4074 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4075 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4076 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4077 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4078 | { // acquire lock |
| 4079 | mLock.lock(); |
| 4080 | |
| 4081 | // Just enqueue a new sensor event. |
| 4082 | std::unique_ptr<SensorEntry> newEntry = |
| 4083 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4084 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4085 | args->sensorType, args->accuracy, |
| 4086 | args->accuracyChanged, args->values); |
| 4087 | |
| 4088 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4089 | mLock.unlock(); |
| 4090 | } // release lock |
| 4091 | |
| 4092 | if (needWake) { |
| 4093 | mLooper->wake(); |
| 4094 | } |
| 4095 | } |
| 4096 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4097 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4098 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4099 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4100 | args->deviceId, args->isOn); |
| 4101 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4102 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4103 | } |
| 4104 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4105 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4106 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4110 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4111 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4112 | "switchMask=0x%08x", |
| 4113 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4114 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4115 | |
| 4116 | uint32_t policyFlags = args->policyFlags; |
| 4117 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4118 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4122 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4123 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4124 | args->deviceId); |
| 4125 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4126 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4127 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4128 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4129 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4130 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4131 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4132 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4133 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4134 | } // release lock |
| 4135 | |
| 4136 | if (needWake) { |
| 4137 | mLooper->wake(); |
| 4138 | } |
| 4139 | } |
| 4140 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4141 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4142 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4143 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4144 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4145 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4146 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4147 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4148 | { // acquire lock |
| 4149 | std::scoped_lock _l(mLock); |
| 4150 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4151 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4152 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4153 | } // release lock |
| 4154 | |
| 4155 | if (needWake) { |
| 4156 | mLooper->wake(); |
| 4157 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4158 | } |
| 4159 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4160 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4161 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4162 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4163 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4164 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 4165 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4166 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
| 4167 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4168 | 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] | 4169 | |
| 4170 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4171 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4172 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4173 | } |
| 4174 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4175 | // 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] | 4176 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4177 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4178 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4179 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4180 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4181 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4182 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4183 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4186 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4187 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4188 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4189 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4190 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4191 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4192 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4193 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4194 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4195 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4196 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4197 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4198 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4199 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4200 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4201 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4202 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4203 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4204 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4205 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4206 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4207 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4208 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4209 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4210 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4211 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4212 | |
| 4213 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4214 | android::base::Timer t; |
| 4215 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4216 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4217 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4218 | std::to_string(t.duration().count()).c_str()); |
| 4219 | } |
| 4220 | } |
| 4221 | |
| 4222 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4223 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4224 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4225 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4226 | incomingKey.getDisplayId(), policyFlags, action, |
| 4227 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4228 | incomingKey.getRepeatCount(), |
| 4229 | incomingKey.getDownTime()); |
| 4230 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4231 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4232 | } |
| 4233 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4234 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4235 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4236 | const int32_t action = motionEvent.getAction(); |
| 4237 | const bool isPointerEvent = |
| 4238 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4239 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4240 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4241 | ? ADISPLAY_ID_DEFAULT |
| 4242 | : event->getDisplayId(); |
| 4243 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4244 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4245 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4246 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4247 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4248 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4252 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4253 | android::base::Timer t; |
| 4254 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4255 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4256 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4257 | std::to_string(t.duration().count()).c_str()); |
| 4258 | } |
| 4259 | } |
| 4260 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4261 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4262 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4263 | } |
| 4264 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4265 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4266 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4267 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4268 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4269 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4270 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4271 | displayId, policyFlags, action, actionButton, |
| 4272 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4273 | motionEvent.getButtonState(), |
| 4274 | motionEvent.getClassification(), |
| 4275 | motionEvent.getEdgeFlags(), |
| 4276 | motionEvent.getXPrecision(), |
| 4277 | motionEvent.getYPrecision(), |
| 4278 | motionEvent.getRawXCursorPosition(), |
| 4279 | motionEvent.getRawYCursorPosition(), |
| 4280 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4281 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4282 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4283 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4284 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4285 | sampleEventTimes += 1; |
| 4286 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4287 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4288 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4289 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4290 | displayId, policyFlags, action, actionButton, |
| 4291 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4292 | motionEvent.getButtonState(), |
| 4293 | motionEvent.getClassification(), |
| 4294 | motionEvent.getEdgeFlags(), |
| 4295 | motionEvent.getXPrecision(), |
| 4296 | motionEvent.getYPrecision(), |
| 4297 | motionEvent.getRawXCursorPosition(), |
| 4298 | motionEvent.getRawYCursorPosition(), |
| 4299 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4300 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4301 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4302 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4303 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4304 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4305 | } |
| 4306 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4307 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4308 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4309 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4310 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4311 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4312 | } |
| 4313 | |
| 4314 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4315 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4316 | injectionState->injectionIsAsync = true; |
| 4317 | } |
| 4318 | |
| 4319 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4320 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4321 | |
| 4322 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4323 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4324 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4325 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4326 | } |
| 4327 | |
| 4328 | mLock.unlock(); |
| 4329 | |
| 4330 | if (needWake) { |
| 4331 | mLooper->wake(); |
| 4332 | } |
| 4333 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4334 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4335 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4336 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4337 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4338 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4339 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4340 | } else { |
| 4341 | for (;;) { |
| 4342 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4343 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4344 | break; |
| 4345 | } |
| 4346 | |
| 4347 | nsecs_t remainingTimeout = endTime - now(); |
| 4348 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4349 | if (DEBUG_INJECTION) { |
| 4350 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4351 | "to become available."); |
| 4352 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4353 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4354 | break; |
| 4355 | } |
| 4356 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4357 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4358 | } |
| 4359 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4360 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4361 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4362 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4363 | if (DEBUG_INJECTION) { |
| 4364 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4365 | injectionState->pendingForegroundDispatches); |
| 4366 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4367 | nsecs_t remainingTimeout = endTime - now(); |
| 4368 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4369 | if (DEBUG_INJECTION) { |
| 4370 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4371 | "dispatches to finish."); |
| 4372 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4373 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4374 | break; |
| 4375 | } |
| 4376 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4377 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4378 | } |
| 4379 | } |
| 4380 | } |
| 4381 | |
| 4382 | injectionState->release(); |
| 4383 | } // release lock |
| 4384 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4385 | if (DEBUG_INJECTION) { |
| 4386 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
| 4387 | injectionResult, injectorPid, injectorUid); |
| 4388 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4389 | |
| 4390 | return injectionResult; |
| 4391 | } |
| 4392 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4393 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4394 | std::array<uint8_t, 32> calculatedHmac; |
| 4395 | std::unique_ptr<VerifiedInputEvent> result; |
| 4396 | switch (event.getType()) { |
| 4397 | case AINPUT_EVENT_TYPE_KEY: { |
| 4398 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4399 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4400 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4401 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4402 | break; |
| 4403 | } |
| 4404 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4405 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4406 | VerifiedMotionEvent verifiedMotionEvent = |
| 4407 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4408 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4409 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4410 | break; |
| 4411 | } |
| 4412 | default: { |
| 4413 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4414 | return nullptr; |
| 4415 | } |
| 4416 | } |
| 4417 | if (calculatedHmac == INVALID_HMAC) { |
| 4418 | return nullptr; |
| 4419 | } |
| 4420 | if (calculatedHmac != event.getHmac()) { |
| 4421 | return nullptr; |
| 4422 | } |
| 4423 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4424 | } |
| 4425 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4426 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4427 | return injectorUid == 0 || |
| 4428 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4429 | } |
| 4430 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4431 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4432 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4433 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4434 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4435 | if (DEBUG_INJECTION) { |
| 4436 | ALOGD("Setting input event injection result to %d. " |
| 4437 | "injectorPid=%d, injectorUid=%d", |
| 4438 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
| 4439 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4440 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4441 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4442 | // Log the outcome since the injector did not wait for the injection result. |
| 4443 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4444 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4445 | ALOGV("Asynchronous input event injection succeeded."); |
| 4446 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4447 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4448 | ALOGW("Asynchronous input event injection failed."); |
| 4449 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4450 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4451 | ALOGW("Asynchronous input event injection permission denied."); |
| 4452 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4453 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4454 | ALOGW("Asynchronous input event injection timed out."); |
| 4455 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4456 | case InputEventInjectionResult::PENDING: |
| 4457 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4458 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4459 | } |
| 4460 | } |
| 4461 | |
| 4462 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4463 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4464 | } |
| 4465 | } |
| 4466 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4467 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4468 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4469 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4470 | // display space, so we need to transform the injected events accordingly. |
| 4471 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4472 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4473 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4474 | |
| 4475 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 4476 | PointerCoords& pc = entry.pointerCoords[i]; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4477 | // Make a copy of the injected coords. We cannot change them in place because some of them |
| 4478 | // are interdependent (for example, X coordinate might depend on the Y coordinate). |
| 4479 | PointerCoords injectedCoords = entry.pointerCoords[i]; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4480 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4481 | BitSet64 bits(injectedCoords.bits); |
| 4482 | while (!bits.isEmpty()) { |
| 4483 | const auto axis = static_cast<int32_t>(bits.clearFirstMarkedBit()); |
| 4484 | const float value = |
| 4485 | MotionEvent::calculateTransformedAxisValue(axis, entry.source, |
| 4486 | transformToDisplay, injectedCoords); |
| 4487 | pc.setAxisValue(axis, value); |
| 4488 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4489 | } |
| 4490 | } |
| 4491 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4492 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4493 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4494 | if (injectionState) { |
| 4495 | injectionState->pendingForegroundDispatches += 1; |
| 4496 | } |
| 4497 | } |
| 4498 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4499 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4500 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4501 | if (injectionState) { |
| 4502 | injectionState->pendingForegroundDispatches -= 1; |
| 4503 | |
| 4504 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4505 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4506 | } |
| 4507 | } |
| 4508 | } |
| 4509 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4510 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4511 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4512 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4513 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4514 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4515 | } |
| 4516 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4517 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4518 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4519 | if (windowHandleToken == nullptr) { |
| 4520 | return nullptr; |
| 4521 | } |
| 4522 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4523 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4524 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4525 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4526 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4527 | return windowHandle; |
| 4528 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4529 | } |
| 4530 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4531 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4532 | } |
| 4533 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4534 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4535 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4536 | if (windowHandleToken == nullptr) { |
| 4537 | return nullptr; |
| 4538 | } |
| 4539 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4540 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4541 | if (windowHandle->getToken() == windowHandleToken) { |
| 4542 | return windowHandle; |
| 4543 | } |
| 4544 | } |
| 4545 | return nullptr; |
| 4546 | } |
| 4547 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4548 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4549 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4550 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4551 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4552 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4553 | if (handle->getId() == windowHandle->getId() && |
| 4554 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4555 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4556 | ALOGE("Found window %s in display %" PRId32 |
| 4557 | ", but it should belong to display %" PRId32, |
| 4558 | windowHandle->getName().c_str(), it.first, |
| 4559 | windowHandle->getInfo()->displayId); |
| 4560 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4561 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4562 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4563 | } |
| 4564 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4565 | return nullptr; |
| 4566 | } |
| 4567 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4568 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4569 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4570 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4571 | } |
| 4572 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4573 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4574 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4575 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4576 | windowHandle.getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4577 | if (connection != nullptr && noInputChannel) { |
| 4578 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4579 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4580 | return false; |
| 4581 | } |
| 4582 | |
| 4583 | if (connection == nullptr) { |
| 4584 | if (!noInputChannel) { |
| 4585 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4586 | } |
| 4587 | return false; |
| 4588 | } |
| 4589 | if (!connection->responsive) { |
| 4590 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4591 | return false; |
| 4592 | } |
| 4593 | return true; |
| 4594 | } |
| 4595 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4596 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4597 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4598 | auto connectionIt = mConnectionsByToken.find(token); |
| 4599 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4600 | return nullptr; |
| 4601 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4602 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4603 | } |
| 4604 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4605 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4606 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4607 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4608 | // Remove all handles on a display if there are no windows left. |
| 4609 | mWindowHandlesByDisplay.erase(displayId); |
| 4610 | return; |
| 4611 | } |
| 4612 | |
| 4613 | // Since we compare the pointer of input window handles across window updates, we need |
| 4614 | // 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] | 4615 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4616 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4617 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4618 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4619 | } |
| 4620 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4621 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4622 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4623 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4624 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4625 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4626 | info->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4627 | const bool canReceiveInput = !info->flags.test(WindowInfo::Flag::NOT_TOUCHABLE) || |
| 4628 | !info->flags.test(WindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4629 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4630 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4631 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4632 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4633 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | if (info->displayId != displayId) { |
| 4637 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4638 | handle->getName().c_str(), displayId, info->displayId); |
| 4639 | continue; |
| 4640 | } |
| 4641 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4642 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4643 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4644 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4645 | oldHandle->updateFrom(handle); |
| 4646 | newHandles.push_back(oldHandle); |
| 4647 | } else { |
| 4648 | newHandles.push_back(handle); |
| 4649 | } |
| 4650 | } |
| 4651 | |
| 4652 | // Insert or replace |
| 4653 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4654 | } |
| 4655 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4656 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4657 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4658 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4659 | { // acquire lock |
| 4660 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4661 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4662 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4663 | } |
| 4664 | } |
| 4665 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4666 | mLooper->wake(); |
| 4667 | } |
| 4668 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4669 | /** |
| 4670 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4671 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4672 | * If set an empty list, remove all handles from the specific display. |
| 4673 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4674 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4675 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4676 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4677 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4678 | if (DEBUG_FOCUS) { |
| 4679 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4680 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4681 | windowList += iwh->getName() + " "; |
| 4682 | } |
| 4683 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4684 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4685 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4686 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4687 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4688 | const WindowInfo& info = *window->getInfo(); |
| 4689 | |
| 4690 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 4691 | const bool noInputWindow = info.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4692 | if (noInputWindow && window->getToken() != nullptr) { |
| 4693 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4694 | window->getName().c_str()); |
| 4695 | window->releaseChannel(); |
| 4696 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4697 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4698 | // Ensure all spy windows are trusted overlays |
| 4699 | LOG_ALWAYS_FATAL_IF(info.isSpy() && !info.trustedOverlay, |
| 4700 | "%s has feature SPY, but is not a trusted overlay.", |
| 4701 | window->getName().c_str()); |
| 4702 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4703 | // Ensure all stylus interceptors are trusted overlays |
| 4704 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && !info.trustedOverlay, |
| 4705 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4706 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4707 | } |
| 4708 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4709 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4710 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4711 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4712 | // Save the old windows' orientation by ID before it gets updated. |
| 4713 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4714 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4715 | oldWindowOrientations.emplace(handle->getId(), |
| 4716 | handle->getInfo()->transform.getOrientation()); |
| 4717 | } |
| 4718 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4719 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4720 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4721 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4722 | if (mLastHoverWindowHandle && |
| 4723 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4724 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4725 | mLastHoverWindowHandle = nullptr; |
| 4726 | } |
| 4727 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4728 | std::optional<FocusResolver::FocusChanges> changes = |
| 4729 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4730 | if (changes) { |
| 4731 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4732 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4733 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4734 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4735 | mTouchStatesByDisplay.find(displayId); |
| 4736 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4737 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4738 | for (size_t i = 0; i < state.windows.size();) { |
| 4739 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4740 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4741 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4742 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4743 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4744 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4745 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4746 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4747 | if (touchedInputChannel != nullptr) { |
| 4748 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4749 | "touched window was removed"); |
| 4750 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4751 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4752 | // well. |
| 4753 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
| 4754 | touchedWindow.windowHandle->getInfo()->hasWallpaper) { |
| 4755 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4756 | if (wallpaper != nullptr) { |
| 4757 | sp<Connection> wallpaperConnection = |
| 4758 | getConnectionLocked(wallpaper->getToken()); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4759 | if (wallpaperConnection != nullptr) { |
| 4760 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4761 | options); |
| 4762 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4763 | } |
| 4764 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4765 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4766 | state.windows.erase(state.windows.begin() + i); |
| 4767 | } else { |
| 4768 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4769 | } |
| 4770 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4771 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4772 | // 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] | 4773 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4774 | if (mDragState && |
| 4775 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4776 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4777 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4778 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4779 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4780 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4781 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4782 | // pointer events if necessary. |
| 4783 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4784 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4785 | if (newWindowHandle != nullptr && |
| 4786 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4787 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4788 | std::shared_ptr<InputChannel> inputChannel = |
| 4789 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4790 | if (inputChannel != nullptr) { |
| 4791 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4792 | "touched window's orientation changed"); |
| 4793 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4794 | } |
| 4795 | } |
| 4796 | } |
| 4797 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4798 | // Release information for windows that are no longer present. |
| 4799 | // This ensures that unused input channels are released promptly. |
| 4800 | // Otherwise, they might stick around until the window handle is destroyed |
| 4801 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4802 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4803 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4804 | if (DEBUG_FOCUS) { |
| 4805 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4806 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4807 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4808 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4809 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4813 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4814 | if (DEBUG_FOCUS) { |
| 4815 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4816 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4817 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4818 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4819 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4820 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4821 | } // release lock |
| 4822 | |
| 4823 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4824 | mLooper->wake(); |
| 4825 | } |
| 4826 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4827 | void InputDispatcher::setFocusedApplicationLocked( |
| 4828 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4829 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4830 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4831 | |
| 4832 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4833 | return; // This application is already focused. No need to wake up or change anything. |
| 4834 | } |
| 4835 | |
| 4836 | // Set the new application handle. |
| 4837 | if (inputApplicationHandle != nullptr) { |
| 4838 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4839 | } else { |
| 4840 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4841 | } |
| 4842 | |
| 4843 | // No matter what the old focused application was, stop waiting on it because it is |
| 4844 | // no longer focused. |
| 4845 | resetNoFocusedWindowTimeoutLocked(); |
| 4846 | } |
| 4847 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4848 | /** |
| 4849 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4850 | * the display not specified. |
| 4851 | * |
| 4852 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4853 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4854 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4855 | * display. The display-specified events won't be affected. |
| 4856 | */ |
| 4857 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4858 | if (DEBUG_FOCUS) { |
| 4859 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4860 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4861 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4862 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4863 | |
| 4864 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4865 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4866 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4867 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4868 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4869 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4870 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4871 | CancelationOptions |
| 4872 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4873 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4874 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4875 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4876 | } |
| 4877 | } |
| 4878 | mFocusedDisplayId = displayId; |
| 4879 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4880 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4881 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4882 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4883 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4884 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4885 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4886 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4887 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4888 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4889 | } |
| 4890 | } |
| 4891 | } |
| 4892 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4893 | if (DEBUG_FOCUS) { |
| 4894 | logDispatchStateLocked(); |
| 4895 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4896 | } // release lock |
| 4897 | |
| 4898 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4899 | mLooper->wake(); |
| 4900 | } |
| 4901 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4902 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4903 | if (DEBUG_FOCUS) { |
| 4904 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4905 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4906 | |
| 4907 | bool changed; |
| 4908 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4909 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4910 | |
| 4911 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4912 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4913 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | if (mDispatchEnabled && !enabled) { |
| 4917 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4918 | } |
| 4919 | |
| 4920 | mDispatchEnabled = enabled; |
| 4921 | mDispatchFrozen = frozen; |
| 4922 | changed = true; |
| 4923 | } else { |
| 4924 | changed = false; |
| 4925 | } |
| 4926 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4927 | if (DEBUG_FOCUS) { |
| 4928 | logDispatchStateLocked(); |
| 4929 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4930 | } // release lock |
| 4931 | |
| 4932 | if (changed) { |
| 4933 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4934 | mLooper->wake(); |
| 4935 | } |
| 4936 | } |
| 4937 | |
| 4938 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4939 | if (DEBUG_FOCUS) { |
| 4940 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4941 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4942 | |
| 4943 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4944 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4945 | |
| 4946 | if (mInputFilterEnabled == enabled) { |
| 4947 | return; |
| 4948 | } |
| 4949 | |
| 4950 | mInputFilterEnabled = enabled; |
| 4951 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4952 | } // release lock |
| 4953 | |
| 4954 | // Wake up poll loop since there might be work to do to drop everything. |
| 4955 | mLooper->wake(); |
| 4956 | } |
| 4957 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4958 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4959 | bool needWake = false; |
| 4960 | { |
| 4961 | std::scoped_lock lock(mLock); |
| 4962 | if (mInTouchMode == inTouchMode) { |
| 4963 | return; |
| 4964 | } |
| 4965 | if (DEBUG_TOUCH_MODE) { |
| 4966 | ALOGD("Request to change touch mode from %s to %s", toString(mInTouchMode), |
| 4967 | toString(inTouchMode)); |
| 4968 | // TODO(b/198487159): Also print the current last interacted apps. |
| 4969 | } |
| 4970 | |
| 4971 | // TODO(b/198499018): Store touch mode per display. |
| 4972 | mInTouchMode = inTouchMode; |
| 4973 | |
| 4974 | // TODO(b/198487159): Enforce that only last interacted apps can change touch mode. |
| 4975 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 4976 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4977 | } // release lock |
| 4978 | |
| 4979 | if (needWake) { |
| 4980 | mLooper->wake(); |
| 4981 | } |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4982 | } |
| 4983 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4984 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4985 | if (opacity < 0 || opacity > 1) { |
| 4986 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4987 | return; |
| 4988 | } |
| 4989 | |
| 4990 | std::scoped_lock lock(mLock); |
| 4991 | mMaximumObscuringOpacityForTouch = opacity; |
| 4992 | } |
| 4993 | |
| 4994 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4995 | std::scoped_lock lock(mLock); |
| 4996 | mBlockUntrustedTouchesMode = mode; |
| 4997 | } |
| 4998 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4999 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 5000 | const sp<IBinder>& token) { |
| 5001 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5002 | for (TouchedWindow& w : state.windows) { |
| 5003 | if (w.windowHandle->getToken() == token) { |
| 5004 | return std::make_pair(&state, &w); |
| 5005 | } |
| 5006 | } |
| 5007 | } |
| 5008 | return std::make_pair(nullptr, nullptr); |
| 5009 | } |
| 5010 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5011 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5012 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5013 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5014 | if (DEBUG_FOCUS) { |
| 5015 | ALOGD("Trivial transfer to same window."); |
| 5016 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5017 | return true; |
| 5018 | } |
| 5019 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5020 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5021 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5022 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5023 | // Find the target touch state and touched window by fromToken. |
| 5024 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 5025 | if (state == nullptr || touchedWindow == nullptr) { |
| 5026 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5027 | return false; |
| 5028 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5029 | |
| 5030 | const int32_t displayId = state->displayId; |
| 5031 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5032 | if (toWindowHandle == nullptr) { |
| 5033 | ALOGW("Cannot transfer focus because to window not found."); |
| 5034 | return false; |
| 5035 | } |
| 5036 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5037 | if (DEBUG_FOCUS) { |
| 5038 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5039 | touchedWindow->windowHandle->getName().c_str(), |
| 5040 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5041 | } |
| 5042 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5043 | // Erase old window. |
| 5044 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 5045 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 5046 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5047 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5048 | // Add new window. |
| 5049 | int32_t newTargetFlags = oldTargetFlags & |
| 5050 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 5051 | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5052 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5053 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5054 | // Store the dragging window. |
| 5055 | if (isDragDrop) { |
| 5056 | mDragState = std::make_unique<DragState>(toWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5057 | } |
| 5058 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5059 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5060 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5061 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5062 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5063 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5064 | CancelationOptions |
| 5065 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5066 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5067 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5068 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5069 | } |
| 5070 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5071 | if (DEBUG_FOCUS) { |
| 5072 | logDispatchStateLocked(); |
| 5073 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5074 | } // release lock |
| 5075 | |
| 5076 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5077 | mLooper->wake(); |
| 5078 | return true; |
| 5079 | } |
| 5080 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5081 | // Binder call |
| 5082 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 5083 | sp<IBinder> fromToken; |
| 5084 | { // acquire lock |
| 5085 | std::scoped_lock _l(mLock); |
| 5086 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5087 | auto it = std::find_if(mTouchStatesByDisplay.begin(), mTouchStatesByDisplay.end(), |
| 5088 | [](const auto& pair) { return pair.second.windows.size() == 1; }); |
| 5089 | if (it == mTouchStatesByDisplay.end()) { |
| 5090 | ALOGW("Cannot transfer touch state because there is no exact window being touched"); |
| 5091 | return false; |
| 5092 | } |
| 5093 | const int32_t displayId = it->first; |
| 5094 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5095 | if (toWindowHandle == nullptr) { |
| 5096 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 5097 | return false; |
| 5098 | } |
| 5099 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5100 | TouchState& state = it->second; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5101 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 5102 | fromToken = touchedWindow.windowHandle->getToken(); |
| 5103 | } // release lock |
| 5104 | |
| 5105 | return transferTouchFocus(fromToken, destChannelToken); |
| 5106 | } |
| 5107 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5108 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5109 | if (DEBUG_FOCUS) { |
| 5110 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5111 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5112 | |
| 5113 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5114 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5115 | |
| 5116 | resetKeyRepeatLocked(); |
| 5117 | releasePendingEventLocked(); |
| 5118 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5119 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5120 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5121 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5122 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5123 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5124 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5125 | } |
| 5126 | |
| 5127 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5128 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5129 | dumpDispatchStateLocked(dump); |
| 5130 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5131 | std::istringstream stream(dump); |
| 5132 | std::string line; |
| 5133 | |
| 5134 | while (std::getline(stream, line, '\n')) { |
| 5135 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5136 | } |
| 5137 | } |
| 5138 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5139 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5140 | std::string dump; |
| 5141 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5142 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5143 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5144 | |
| 5145 | std::string windowName = "None"; |
| 5146 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5147 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5148 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5149 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5150 | : "token has capture without window"; |
| 5151 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5152 | 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] | 5153 | |
| 5154 | return dump; |
| 5155 | } |
| 5156 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5157 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5158 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5159 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5160 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5161 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5162 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5163 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5164 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5165 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5166 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5167 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5168 | const std::chrono::duration timeout = |
| 5169 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5170 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5171 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5172 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5173 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5174 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5175 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5176 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5177 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5178 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5179 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5180 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5181 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5182 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5183 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5184 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5185 | dump += StringPrintf(INDENT2 "%d: down=%s, split=%s, deviceId=%d, source=0x%08x\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5186 | state.displayId, toString(state.down), toString(state.split), |
| 5187 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5188 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5189 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5190 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5191 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5192 | dump += StringPrintf(INDENT4 |
| 5193 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5194 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5195 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5196 | } |
| 5197 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5198 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5199 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5200 | } |
| 5201 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5202 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5203 | } |
| 5204 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5205 | if (mDragState) { |
| 5206 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5207 | mDragState->dump(dump, INDENT2); |
| 5208 | } |
| 5209 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5210 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5211 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5212 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5213 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5214 | const auto& displayInfo = it->second; |
| 5215 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5216 | displayInfo.logicalHeight); |
| 5217 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5218 | } else { |
| 5219 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5220 | } |
| 5221 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5222 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5223 | dump += INDENT2 "Windows:\n"; |
| 5224 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5225 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5226 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5227 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5228 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5229 | "paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5230 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5231 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5232 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5233 | "applicationInfo.name=%s, " |
| 5234 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5235 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5236 | i, windowInfo->name.c_str(), windowInfo->id, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5237 | windowInfo->displayId, toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5238 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5239 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5240 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 5241 | windowInfo->flags.string().c_str(), |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 5242 | ftl::enum_string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5243 | windowInfo->frameLeft, windowInfo->frameTop, |
| 5244 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5245 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5246 | windowInfo->applicationInfo.name.c_str(), |
| 5247 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5248 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5249 | dump += StringPrintf(", inputFeatures=%s", |
| 5250 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5251 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5252 | "ms, trustedOverlay=%s, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5253 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5254 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5255 | millis(windowInfo->dispatchingTimeout), |
| 5256 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5257 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5258 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5259 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5260 | } |
| 5261 | } else { |
| 5262 | dump += INDENT2 "Windows: <none>\n"; |
| 5263 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5264 | } |
| 5265 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5266 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5267 | } |
| 5268 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5269 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5270 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5271 | const std::vector<Monitor>& monitors = it.second; |
| 5272 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5273 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5274 | } |
| 5275 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5276 | const std::vector<Monitor>& monitors = it.second; |
| 5277 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5278 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5279 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5280 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5281 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5282 | } |
| 5283 | |
| 5284 | nsecs_t currentTime = now(); |
| 5285 | |
| 5286 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5287 | if (!mRecentQueue.empty()) { |
| 5288 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5289 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5290 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5291 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5292 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5293 | } |
| 5294 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5295 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5296 | } |
| 5297 | |
| 5298 | // Dump event currently being dispatched. |
| 5299 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5300 | dump += INDENT "PendingEvent:\n"; |
| 5301 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5302 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5303 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5304 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5305 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5306 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5307 | } |
| 5308 | |
| 5309 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5310 | if (!mInboundQueue.empty()) { |
| 5311 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5312 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5313 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5314 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5315 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5316 | } |
| 5317 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5318 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5319 | } |
| 5320 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5321 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5322 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5323 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5324 | const KeyReplacement& replacement = pair.first; |
| 5325 | int32_t newKeyCode = pair.second; |
| 5326 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5327 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5328 | } |
| 5329 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5330 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5331 | } |
| 5332 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5333 | if (!mCommandQueue.empty()) { |
| 5334 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5335 | } else { |
| 5336 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5337 | } |
| 5338 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5339 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5340 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5341 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5342 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5343 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5344 | connection->inputChannel->getFd().get(), |
| 5345 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5346 | connection->getWindowName().c_str(), |
| 5347 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5348 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5349 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5350 | if (!connection->outboundQueue.empty()) { |
| 5351 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5352 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5353 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5354 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5355 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5356 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5357 | } |
| 5358 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5359 | if (!connection->waitQueue.empty()) { |
| 5360 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5361 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5362 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5363 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5364 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5365 | } |
| 5366 | } |
| 5367 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5368 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5372 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5373 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5374 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5375 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5376 | } |
| 5377 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5378 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5379 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5380 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5381 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5382 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5383 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5384 | } |
| 5385 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5386 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5387 | const size_t numMonitors = monitors.size(); |
| 5388 | for (size_t i = 0; i < numMonitors; i++) { |
| 5389 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5390 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5391 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5392 | dump += "\n"; |
| 5393 | } |
| 5394 | } |
| 5395 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5396 | class LooperEventCallback : public LooperCallback { |
| 5397 | public: |
| 5398 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5399 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5400 | |
| 5401 | private: |
| 5402 | std::function<int(int events)> mCallback; |
| 5403 | }; |
| 5404 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5405 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5406 | if (DEBUG_CHANNEL_CREATION) { |
| 5407 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5408 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5409 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5410 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5411 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5412 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5413 | |
| 5414 | if (result) { |
| 5415 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5416 | } |
| 5417 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5418 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5419 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5420 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5421 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5422 | sp<Connection> connection = |
| 5423 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5424 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5425 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5426 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5427 | } |
| 5428 | mConnectionsByToken.emplace(token, connection); |
| 5429 | |
| 5430 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5431 | this, std::placeholders::_1, token); |
| 5432 | |
| 5433 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5434 | } // release lock |
| 5435 | |
| 5436 | // Wake the looper because some connections have changed. |
| 5437 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5438 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5439 | } |
| 5440 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5441 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5442 | bool isGestureMonitor, |
| 5443 | const std::string& name, |
| 5444 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5445 | std::shared_ptr<InputChannel> serverChannel; |
| 5446 | std::unique_ptr<InputChannel> clientChannel; |
| 5447 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5448 | if (result) { |
| 5449 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5450 | } |
| 5451 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5452 | { // acquire lock |
| 5453 | std::scoped_lock _l(mLock); |
| 5454 | |
| 5455 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5456 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5457 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5458 | } |
| 5459 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5460 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5461 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5462 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5463 | |
| 5464 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5465 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5466 | } |
| 5467 | mConnectionsByToken.emplace(token, connection); |
| 5468 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5469 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5470 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5471 | auto& monitorsByDisplay = |
| 5472 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5473 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5474 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5475 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5476 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), |
| 5477 | displayId, toString(isGestureMonitor), pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5478 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5479 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5480 | // Wake the looper because some connections have changed. |
| 5481 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5482 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5483 | } |
| 5484 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5485 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5486 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5487 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5488 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5489 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5490 | if (status) { |
| 5491 | return status; |
| 5492 | } |
| 5493 | } // release lock |
| 5494 | |
| 5495 | // Wake the poll loop because removing the connection may have changed the current |
| 5496 | // synchronization state. |
| 5497 | mLooper->wake(); |
| 5498 | return OK; |
| 5499 | } |
| 5500 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5501 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5502 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5503 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5504 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5505 | // 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] | 5506 | return BAD_VALUE; |
| 5507 | } |
| 5508 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5509 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5510 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5511 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5512 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5513 | } |
| 5514 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5515 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5516 | |
| 5517 | nsecs_t currentTime = now(); |
| 5518 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5519 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5520 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5521 | return OK; |
| 5522 | } |
| 5523 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5524 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5525 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5526 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5529 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5530 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5531 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5532 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5533 | std::vector<Monitor>& monitors = it->second; |
| 5534 | const size_t numMonitors = monitors.size(); |
| 5535 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5536 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5537 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5538 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5539 | monitors.erase(monitors.begin() + i); |
| 5540 | break; |
| 5541 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5542 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5543 | if (monitors.empty()) { |
| 5544 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5545 | } else { |
| 5546 | ++it; |
| 5547 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5548 | } |
| 5549 | } |
| 5550 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5551 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5552 | { // acquire lock |
| 5553 | std::scoped_lock _l(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5554 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5555 | TouchState* statePtr = nullptr; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5556 | std::shared_ptr<InputChannel> requestingChannel; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5557 | int32_t displayId; |
| 5558 | int32_t deviceId; |
| 5559 | const std::optional<int32_t> foundGestureMonitorDisplayId = |
| 5560 | findGestureMonitorDisplayByTokenLocked(token); |
| 5561 | |
| 5562 | // TODO: Optimize this function for pilfering from windows when removing gesture monitors. |
| 5563 | if (foundGestureMonitorDisplayId) { |
| 5564 | // A gesture monitor has requested to pilfer pointers. |
| 5565 | displayId = *foundGestureMonitorDisplayId; |
| 5566 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5567 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5568 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5569 | return BAD_VALUE; |
| 5570 | } |
| 5571 | statePtr = &stateIt->second; |
| 5572 | |
| 5573 | for (const auto& monitor : statePtr->gestureMonitors) { |
| 5574 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 5575 | requestingChannel = monitor.inputChannel; |
| 5576 | deviceId = statePtr->deviceId; |
| 5577 | } |
| 5578 | } |
| 5579 | } else { |
| 5580 | // Check if a window has requested to pilfer pointers. |
| 5581 | for (auto& [curDisplayId, state] : mTouchStatesByDisplay) { |
| 5582 | const sp<WindowInfoHandle>& windowHandle = state.getWindow(token); |
| 5583 | if (windowHandle != nullptr) { |
| 5584 | displayId = curDisplayId; |
| 5585 | requestingChannel = getInputChannelLocked(token); |
| 5586 | deviceId = state.deviceId; |
| 5587 | statePtr = &state; |
| 5588 | break; |
| 5589 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5590 | } |
| 5591 | } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5592 | |
| 5593 | if (requestingChannel == nullptr) { |
| 5594 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5595 | return BAD_VALUE; |
| 5596 | } |
| 5597 | TouchState& state = *statePtr; |
| 5598 | if (!state.down) { |
| 5599 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5600 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5601 | return BAD_VALUE; |
| 5602 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5603 | |
| 5604 | // Send cancel events to all the input channels we're stealing from. |
| 5605 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5606 | "input channel stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5607 | options.deviceId = deviceId; |
| 5608 | options.displayId = displayId; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5609 | std::string canceledWindows; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5610 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5611 | std::shared_ptr<InputChannel> channel = |
| 5612 | getInputChannelLocked(window.windowHandle->getToken()); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5613 | if (channel != nullptr && channel->getConnectionToken() != token) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5614 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5615 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5616 | canceledWindows += channel->getName(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5617 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5618 | } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5619 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5620 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5621 | canceledWindows.c_str()); |
| 5622 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5623 | // Then clear the current touch state so we stop dispatching to them as well. |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 5624 | state.split = false; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5625 | state.filterWindowsExcept(token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5626 | } |
| 5627 | return OK; |
| 5628 | } |
| 5629 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5630 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5631 | { // acquire lock |
| 5632 | std::scoped_lock _l(mLock); |
| 5633 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5634 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5635 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5636 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5637 | : "token without window"); |
| 5638 | } |
| 5639 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5640 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5641 | if (focusedToken != windowToken) { |
| 5642 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5643 | enabled ? "enable" : "disable"); |
| 5644 | return; |
| 5645 | } |
| 5646 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5647 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5648 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5649 | "window has %s requested pointer capture.", |
| 5650 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5651 | return; |
| 5652 | } |
| 5653 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5654 | if (enabled) { |
| 5655 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5656 | mIneligibleDisplaysForPointerCapture.end(), |
| 5657 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5658 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5659 | return; |
| 5660 | } |
| 5661 | } |
| 5662 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5663 | setPointerCaptureLocked(enabled); |
| 5664 | } // release lock |
| 5665 | |
| 5666 | // Wake the thread to process command entries. |
| 5667 | mLooper->wake(); |
| 5668 | } |
| 5669 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5670 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5671 | { // acquire lock |
| 5672 | std::scoped_lock _l(mLock); |
| 5673 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5674 | if (!isEligible) { |
| 5675 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5676 | } |
| 5677 | } // release lock |
| 5678 | } |
| 5679 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5680 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5681 | const sp<IBinder>& token) { |
| 5682 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5683 | const std::vector<Monitor>& monitors = it.second; |
| 5684 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5685 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5686 | return it.first; |
| 5687 | } |
| 5688 | } |
| 5689 | } |
| 5690 | return std::nullopt; |
| 5691 | } |
| 5692 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5693 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5694 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5695 | if (gesturePid.has_value()) { |
| 5696 | return gesturePid; |
| 5697 | } |
| 5698 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5699 | } |
| 5700 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5701 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5702 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5703 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5704 | } |
| 5705 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5706 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5707 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5708 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5709 | } |
| 5710 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5711 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5712 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5713 | } |
| 5714 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5715 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5716 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5717 | if (connection == nullptr) { |
| 5718 | return "<nullptr>"; |
| 5719 | } |
| 5720 | return connection->getInputChannelName(); |
| 5721 | } |
| 5722 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5723 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5724 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5725 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5726 | } |
| 5727 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5728 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5729 | const sp<Connection>& connection, uint32_t seq, |
| 5730 | bool handled, nsecs_t consumeTime) { |
| 5731 | // Handle post-event policy actions. |
| 5732 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5733 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5734 | return; |
| 5735 | } |
| 5736 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5737 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5738 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5739 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5740 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5741 | } |
| 5742 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5743 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5744 | connection->inputChannel->getConnectionToken(), |
| 5745 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5746 | } |
| 5747 | |
| 5748 | bool restartEvent; |
| 5749 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5750 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5751 | restartEvent = |
| 5752 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5753 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5754 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5755 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5756 | handled); |
| 5757 | } else { |
| 5758 | restartEvent = false; |
| 5759 | } |
| 5760 | |
| 5761 | // Dequeue the event and start the next cycle. |
| 5762 | // Because the lock might have been released, it is possible that the |
| 5763 | // contents of the wait queue to have been drained, so we need to double-check |
| 5764 | // a few things. |
| 5765 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5766 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5767 | dispatchEntry = *dispatchEntryIt; |
| 5768 | connection->waitQueue.erase(dispatchEntryIt); |
| 5769 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5770 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5771 | if (!connection->responsive) { |
| 5772 | connection->responsive = isConnectionResponsive(*connection); |
| 5773 | if (connection->responsive) { |
| 5774 | // The connection was unresponsive, and now it's responsive. |
| 5775 | processConnectionResponsiveLocked(*connection); |
| 5776 | } |
| 5777 | } |
| 5778 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5779 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5780 | connection->outboundQueue.push_front(dispatchEntry); |
| 5781 | traceOutboundQueueLength(*connection); |
| 5782 | } else { |
| 5783 | releaseDispatchEntry(dispatchEntry); |
| 5784 | } |
| 5785 | } |
| 5786 | |
| 5787 | // Start the next dispatch cycle for this connection. |
| 5788 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5789 | } |
| 5790 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5791 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5792 | const sp<IBinder>& newToken) { |
| 5793 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5794 | scoped_unlock unlock(mLock); |
| 5795 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5796 | }; |
| 5797 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5798 | } |
| 5799 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5800 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5801 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5802 | scoped_unlock unlock(mLock); |
| 5803 | mPolicy->notifyDropWindow(token, x, y); |
| 5804 | }; |
| 5805 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5806 | } |
| 5807 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5808 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5809 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5810 | scoped_unlock unlock(mLock); |
| 5811 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5812 | }; |
| 5813 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5814 | } |
| 5815 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5816 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5817 | if (connection == nullptr) { |
| 5818 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5819 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5820 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5821 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5822 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5823 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5824 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5825 | return; |
| 5826 | } |
| 5827 | /** |
| 5828 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5829 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5830 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5831 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5832 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5833 | * most useful. |
| 5834 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5835 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5836 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5837 | std::string reason = |
| 5838 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5839 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5840 | ns2ms(currentWait), |
| 5841 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5842 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5843 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5844 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5845 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5846 | |
| 5847 | // Stop waking up for events on this connection, it is already unresponsive |
| 5848 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5849 | } |
| 5850 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5851 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5852 | std::string reason = |
| 5853 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5854 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5855 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5856 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5857 | scoped_unlock unlock(mLock); |
| 5858 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5859 | }; |
| 5860 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5861 | } |
| 5862 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5863 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5864 | const std::string& reason) { |
| 5865 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5866 | updateLastAnrStateLocked(windowLabel, reason); |
| 5867 | } |
| 5868 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5869 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5870 | const std::string& reason) { |
| 5871 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5872 | updateLastAnrStateLocked(windowLabel, reason); |
| 5873 | } |
| 5874 | |
| 5875 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5876 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5877 | // 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] | 5878 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5879 | struct tm tm; |
| 5880 | localtime_r(&t, &tm); |
| 5881 | char timestr[64]; |
| 5882 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5883 | mLastAnrState.clear(); |
| 5884 | mLastAnrState += INDENT "ANR:\n"; |
| 5885 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5886 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5887 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5888 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5889 | } |
| 5890 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5891 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5892 | KeyEntry& entry) { |
| 5893 | const KeyEvent event = createKeyEvent(entry); |
| 5894 | nsecs_t delay = 0; |
| 5895 | { // release lock |
| 5896 | scoped_unlock unlock(mLock); |
| 5897 | android::base::Timer t; |
| 5898 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5899 | entry.policyFlags); |
| 5900 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5901 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5902 | std::to_string(t.duration().count()).c_str()); |
| 5903 | } |
| 5904 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5905 | |
| 5906 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5907 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5908 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5909 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5910 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5911 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5912 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5913 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5914 | } |
| 5915 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5916 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5917 | auto command = [this, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5918 | scoped_unlock unlock(mLock); |
| 5919 | mPolicy->notifyMonitorUnresponsive(pid, reason); |
| 5920 | }; |
| 5921 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5922 | } |
| 5923 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5924 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5925 | std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5926 | auto command = [this, token, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5927 | scoped_unlock unlock(mLock); |
| 5928 | mPolicy->notifyWindowUnresponsive(token, reason); |
| 5929 | }; |
| 5930 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5931 | } |
| 5932 | |
| 5933 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5934 | auto command = [this, pid]() REQUIRES(mLock) { |
| 5935 | scoped_unlock unlock(mLock); |
| 5936 | mPolicy->notifyMonitorResponsive(pid); |
| 5937 | }; |
| 5938 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5939 | } |
| 5940 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5941 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken) { |
| 5942 | auto command = [this, connectionToken]() REQUIRES(mLock) { |
| 5943 | scoped_unlock unlock(mLock); |
| 5944 | mPolicy->notifyWindowResponsive(connectionToken); |
| 5945 | }; |
| 5946 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5947 | } |
| 5948 | |
| 5949 | /** |
| 5950 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5951 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5952 | * command entry to the command queue. |
| 5953 | */ |
| 5954 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5955 | std::string reason) { |
| 5956 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5957 | if (connection.monitor) { |
| 5958 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5959 | reason.c_str()); |
| 5960 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5961 | if (!pid.has_value()) { |
| 5962 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5963 | connection.inputChannel->getName().c_str()); |
| 5964 | return; |
| 5965 | } |
| 5966 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5967 | return; |
| 5968 | } |
| 5969 | // If not a monitor, must be a window |
| 5970 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5971 | reason.c_str()); |
| 5972 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5973 | } |
| 5974 | |
| 5975 | /** |
| 5976 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5977 | */ |
| 5978 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5979 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5980 | if (connection.monitor) { |
| 5981 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5982 | if (!pid.has_value()) { |
| 5983 | ALOGE("Could not find responsive monitor for connection %s", |
| 5984 | connection.inputChannel->getName().c_str()); |
| 5985 | return; |
| 5986 | } |
| 5987 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5988 | return; |
| 5989 | } |
| 5990 | // If not a monitor, must be a window |
| 5991 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5992 | } |
| 5993 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5994 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5995 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5996 | KeyEntry& keyEntry, bool handled) { |
| 5997 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5998 | if (!handled) { |
| 5999 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6000 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6001 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6002 | return false; |
| 6003 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6004 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6005 | // Get the fallback key state. |
| 6006 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6007 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6008 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6009 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6010 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6011 | } |
| 6012 | |
| 6013 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6014 | // If the application handles the original key for which we previously |
| 6015 | // generated a fallback or if the window is not a foreground window, |
| 6016 | // then cancel the associated fallback key, if any. |
| 6017 | if (fallbackKeyCode != -1) { |
| 6018 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6019 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6020 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6021 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6022 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6023 | keyEntry.policyFlags); |
| 6024 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6025 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6026 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6027 | |
| 6028 | mLock.unlock(); |
| 6029 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6030 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6031 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6032 | |
| 6033 | mLock.lock(); |
| 6034 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6035 | // Cancel the fallback key. |
| 6036 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6037 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6038 | "application handled the original non-fallback key " |
| 6039 | "or is no longer a foreground target, " |
| 6040 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6041 | options.keyCode = fallbackKeyCode; |
| 6042 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6043 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6044 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6045 | } |
| 6046 | } else { |
| 6047 | // If the application did not handle a non-fallback key, first check |
| 6048 | // that we are in a good state to perform unhandled key event processing |
| 6049 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6050 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6051 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6052 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6053 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6054 | "since this is not an initial down. " |
| 6055 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6056 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6057 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6058 | return false; |
| 6059 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6060 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6061 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6062 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6063 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6064 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6065 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6066 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6067 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6068 | |
| 6069 | mLock.unlock(); |
| 6070 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6071 | bool fallback = |
| 6072 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6073 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6074 | |
| 6075 | mLock.lock(); |
| 6076 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6077 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6078 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6079 | return false; |
| 6080 | } |
| 6081 | |
| 6082 | // Latch the fallback keycode for this key on an initial down. |
| 6083 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6084 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6085 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6086 | fallbackKeyCode = event.getKeyCode(); |
| 6087 | } else { |
| 6088 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6089 | } |
| 6090 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6091 | } |
| 6092 | |
| 6093 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6094 | |
| 6095 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6096 | // We will continue to dispatch the key to the policy but we will no |
| 6097 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6098 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6099 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6100 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6101 | if (fallback) { |
| 6102 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6103 | "as a fallback for %d, but on the DOWN it had requested " |
| 6104 | "to send %d instead. Fallback canceled.", |
| 6105 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6106 | } else { |
| 6107 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6108 | "but on the DOWN it had requested to send %d. " |
| 6109 | "Fallback canceled.", |
| 6110 | originalKeyCode, fallbackKeyCode); |
| 6111 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6112 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6113 | |
| 6114 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6115 | "canceling fallback, policy no longer desires it"); |
| 6116 | options.keyCode = fallbackKeyCode; |
| 6117 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6118 | |
| 6119 | fallback = false; |
| 6120 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6121 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6122 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6123 | } |
| 6124 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6125 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6126 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6127 | { |
| 6128 | std::string msg; |
| 6129 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6130 | connection->inputState.getFallbackKeys(); |
| 6131 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6132 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6133 | } |
| 6134 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6135 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6136 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6137 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6138 | |
| 6139 | if (fallback) { |
| 6140 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6141 | keyEntry.eventTime = event.getEventTime(); |
| 6142 | keyEntry.deviceId = event.getDeviceId(); |
| 6143 | keyEntry.source = event.getSource(); |
| 6144 | keyEntry.displayId = event.getDisplayId(); |
| 6145 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6146 | keyEntry.keyCode = fallbackKeyCode; |
| 6147 | keyEntry.scanCode = event.getScanCode(); |
| 6148 | keyEntry.metaState = event.getMetaState(); |
| 6149 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6150 | keyEntry.downTime = event.getDownTime(); |
| 6151 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6152 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6153 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6154 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6155 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6156 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6157 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6158 | return true; // restart the event |
| 6159 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6160 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6161 | ALOGD("Unhandled key event: No fallback key."); |
| 6162 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6163 | |
| 6164 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6165 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6166 | } |
| 6167 | } |
| 6168 | return false; |
| 6169 | } |
| 6170 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6171 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6172 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6173 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6174 | return false; |
| 6175 | } |
| 6176 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6177 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6178 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6179 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6180 | } |
| 6181 | } |
| 6182 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6183 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6184 | if (ATRACE_ENABLED()) { |
| 6185 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6186 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6187 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6188 | } |
| 6189 | } |
| 6190 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6191 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6192 | if (ATRACE_ENABLED()) { |
| 6193 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6194 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6195 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6196 | } |
| 6197 | } |
| 6198 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6199 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6200 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6201 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6202 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6203 | dumpDispatchStateLocked(dump); |
| 6204 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6205 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6206 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6207 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6208 | } |
| 6209 | } |
| 6210 | |
| 6211 | void InputDispatcher::monitor() { |
| 6212 | // 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] | 6213 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6214 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6215 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6216 | } |
| 6217 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6218 | /** |
| 6219 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6220 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6221 | * this method can be safely called from any thread, as long as you've ensured that |
| 6222 | * the work you are interested in completing has already been queued. |
| 6223 | */ |
| 6224 | bool InputDispatcher::waitForIdle() { |
| 6225 | /** |
| 6226 | * Timeout should represent the longest possible time that a device might spend processing |
| 6227 | * events and commands. |
| 6228 | */ |
| 6229 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6230 | std::unique_lock lock(mLock); |
| 6231 | mLooper->wake(); |
| 6232 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6233 | return result == std::cv_status::no_timeout; |
| 6234 | } |
| 6235 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6236 | /** |
| 6237 | * Sets focus to the window identified by the token. This must be called |
| 6238 | * after updating any input window handles. |
| 6239 | * |
| 6240 | * Params: |
| 6241 | * request.token - input channel token used to identify the window that should gain focus. |
| 6242 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6243 | * specified token does not match the currently focused window, this request will be dropped. |
| 6244 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6245 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6246 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6247 | * when requesting the focus change. This determines which request gets |
| 6248 | * precedence if there is a focus change request from another source such as pointer down. |
| 6249 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6250 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6251 | { // acquire lock |
| 6252 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6253 | std::optional<FocusResolver::FocusChanges> changes = |
| 6254 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6255 | if (changes) { |
| 6256 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6257 | } |
| 6258 | } // release lock |
| 6259 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6260 | mLooper->wake(); |
| 6261 | } |
| 6262 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6263 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6264 | if (changes.oldFocus) { |
| 6265 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6266 | if (focusedInputChannel) { |
| 6267 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6268 | "focus left window"); |
| 6269 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6270 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6271 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6272 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6273 | if (changes.newFocus) { |
| 6274 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6275 | } |
| 6276 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6277 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6278 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6279 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6280 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6281 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6282 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6283 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6284 | // front. |
| 6285 | disablePointerCaptureForcedLocked(); |
| 6286 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6287 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6288 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6289 | } |
| 6290 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6291 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6292 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6293 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6294 | return; |
| 6295 | } |
| 6296 | |
| 6297 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6298 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6299 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6300 | setPointerCaptureLocked(false); |
| 6301 | } |
| 6302 | |
| 6303 | if (!mWindowTokenWithPointerCapture) { |
| 6304 | // No need to send capture changes because no window has capture. |
| 6305 | return; |
| 6306 | } |
| 6307 | |
| 6308 | if (mPendingEvent != nullptr) { |
| 6309 | // Move the pending event to the front of the queue. This will give the chance |
| 6310 | // for the pending event to be dropped if it is a captured event. |
| 6311 | mInboundQueue.push_front(mPendingEvent); |
| 6312 | mPendingEvent = nullptr; |
| 6313 | } |
| 6314 | |
| 6315 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6316 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6317 | mInboundQueue.push_front(std::move(entry)); |
| 6318 | } |
| 6319 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6320 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6321 | mCurrentPointerCaptureRequest.enable = enable; |
| 6322 | mCurrentPointerCaptureRequest.seq++; |
| 6323 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6324 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6325 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6326 | }; |
| 6327 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6328 | } |
| 6329 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6330 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6331 | { // acquire lock |
| 6332 | std::scoped_lock _l(mLock); |
| 6333 | // Set an empty list to remove all handles from the specific display. |
| 6334 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6335 | setFocusedApplicationLocked(displayId, nullptr); |
| 6336 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6337 | // have been removed for the removed display. |
| 6338 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6339 | // Reset pointer capture eligibility, regardless of previous state. |
| 6340 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6341 | } // release lock |
| 6342 | |
| 6343 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6344 | mLooper->wake(); |
| 6345 | } |
| 6346 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6347 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6348 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6349 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6350 | // more convenient parsing. |
| 6351 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6352 | for (const auto& info : windowInfos) { |
| 6353 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6354 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6355 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6356 | |
| 6357 | { // acquire lock |
| 6358 | std::scoped_lock _l(mLock); |
| 6359 | mDisplayInfos.clear(); |
| 6360 | for (const auto& displayInfo : displayInfos) { |
| 6361 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6362 | } |
| 6363 | |
| 6364 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6365 | setInputWindowsLocked(handles, displayId); |
| 6366 | } |
| 6367 | } |
| 6368 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6369 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6370 | } |
| 6371 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6372 | bool InputDispatcher::shouldDropInput( |
| 6373 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
| 6374 | if (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT) || |
| 6375 | (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED) && |
| 6376 | isWindowObscuredLocked(windowHandle))) { |
| 6377 | ALOGW("Dropping %s event targeting %s as requested by input feature %s on display " |
| 6378 | "%" PRId32 ".", |
| 6379 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
| 6380 | windowHandle->getInfo()->inputFeatures.string().c_str(), |
| 6381 | windowHandle->getInfo()->displayId); |
| 6382 | return true; |
| 6383 | } |
| 6384 | return false; |
| 6385 | } |
| 6386 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6387 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6388 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6389 | const std::vector<DisplayInfo>& displayInfos) { |
| 6390 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6391 | } |
| 6392 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6393 | void InputDispatcher::cancelCurrentTouch() { |
| 6394 | { |
| 6395 | std::scoped_lock _l(mLock); |
| 6396 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
| 6397 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 6398 | "cancel current touch"); |
| 6399 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6400 | |
| 6401 | mTouchStatesByDisplay.clear(); |
| 6402 | mLastHoverWindowHandle.clear(); |
| 6403 | } |
| 6404 | // Wake up poll loop since there might be work to do. |
| 6405 | mLooper->wake(); |
| 6406 | } |
| 6407 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame^] | 6408 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6409 | std::scoped_lock _l(mLock); |
| 6410 | mMonitorDispatchingTimeout = timeout; |
| 6411 | } |
| 6412 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6413 | } // namespace android::inputdispatcher |