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> |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 27 | #include <binder/IServiceManager.h> |
| 28 | #include <com/android/internal/compat/IPlatformCompatNative.h> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 29 | #include <ftl/enum.h> |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 30 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 31 | #include <input/InputDevice.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 32 | #include <log/log.h> |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 33 | #include <log/log_event_list.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 34 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 35 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 36 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 38 | #include <cerrno> |
| 39 | #include <cinttypes> |
| 40 | #include <climits> |
| 41 | #include <cstddef> |
| 42 | #include <ctime> |
| 43 | #include <queue> |
| 44 | #include <sstream> |
| 45 | |
| 46 | #include "Connection.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 47 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 48 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 49 | #define INDENT " " |
| 50 | #define INDENT2 " " |
| 51 | #define INDENT3 " " |
| 52 | #define INDENT4 " " |
| 53 | |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 54 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 55 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 56 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 57 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 58 | using android::gui::FocusRequest; |
| 59 | using android::gui::TouchOcclusionMode; |
| 60 | using android::gui::WindowInfo; |
| 61 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 62 | using android::os::BlockUntrustedTouchesMode; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 63 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 64 | using android::os::InputEventInjectionResult; |
| 65 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 66 | using com::android::internal::compat::IPlatformCompatNative; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 67 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 68 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 69 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 70 | namespace { |
| 71 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 72 | // Log detailed debug messages about each inbound event notification to the dispatcher. |
| 73 | constexpr bool DEBUG_INBOUND_EVENT_DETAILS = false; |
| 74 | |
| 75 | // Log detailed debug messages about each outbound event processed by the dispatcher. |
| 76 | constexpr bool DEBUG_OUTBOUND_EVENT_DETAILS = false; |
| 77 | |
| 78 | // Log debug messages about the dispatch cycle. |
| 79 | constexpr bool DEBUG_DISPATCH_CYCLE = false; |
| 80 | |
| 81 | // Log debug messages about channel creation |
| 82 | constexpr bool DEBUG_CHANNEL_CREATION = false; |
| 83 | |
| 84 | // Log debug messages about input event injection. |
| 85 | constexpr bool DEBUG_INJECTION = false; |
| 86 | |
| 87 | // Log debug messages about input focus tracking. |
| 88 | constexpr bool DEBUG_FOCUS = false; |
| 89 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 90 | // Log debug messages about touch mode event |
| 91 | constexpr bool DEBUG_TOUCH_MODE = false; |
| 92 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 93 | // Log debug messages about touch occlusion |
| 94 | // STOPSHIP(b/169067926): Set to false |
| 95 | constexpr bool DEBUG_TOUCH_OCCLUSION = true; |
| 96 | |
| 97 | // Log debug messages about the app switch latency optimization. |
| 98 | constexpr bool DEBUG_APP_SWITCH = false; |
| 99 | |
| 100 | // Log debug messages about hover events. |
| 101 | constexpr bool DEBUG_HOVER = false; |
| 102 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 103 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 104 | // Named to match std::scoped_lock |
| 105 | class scoped_unlock { |
| 106 | public: |
| 107 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 108 | ~scoped_unlock() { mMutex.lock(); } |
| 109 | |
| 110 | private: |
| 111 | std::mutex& mMutex; |
| 112 | }; |
| 113 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 114 | // Default input dispatching timeout if there is no focused application or paused window |
| 115 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 116 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 117 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 118 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 119 | |
| 120 | // Amount of time to allow for all pending events to be processed when an app switch |
| 121 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 122 | // 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] | 123 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 124 | |
| 125 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 126 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 127 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 128 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 129 | // 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] | 130 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 131 | |
| 132 | // Log a warning when an interception call takes longer than this to process. |
| 133 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 134 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 135 | // Additional key latency in case a connection is still processing some motion events. |
| 136 | // This will help with the case when a user touched a button that opens a new window, |
| 137 | // and gives us the chance to dispatch the key to this new window. |
| 138 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 139 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 140 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 141 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 142 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 143 | // Event log tags. See EventLogTags.logtags for reference |
| 144 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 145 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 146 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 147 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 148 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 149 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 150 | } |
| 151 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 152 | inline const char* toString(bool value) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 153 | return value ? "true" : "false"; |
| 154 | } |
| 155 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 156 | inline const std::string toString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 157 | if (binder == nullptr) { |
| 158 | return "<null>"; |
| 159 | } |
| 160 | return StringPrintf("%p", binder.get()); |
| 161 | } |
| 162 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 163 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 164 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 165 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 168 | bool isValidKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 169 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 170 | case AKEY_EVENT_ACTION_DOWN: |
| 171 | case AKEY_EVENT_ACTION_UP: |
| 172 | return true; |
| 173 | default: |
| 174 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 178 | bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 179 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 180 | ALOGE("Key event has invalid action code 0x%x", action); |
| 181 | return false; |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 186 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 187 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 188 | case AMOTION_EVENT_ACTION_DOWN: |
| 189 | case AMOTION_EVENT_ACTION_UP: |
| 190 | case AMOTION_EVENT_ACTION_CANCEL: |
| 191 | case AMOTION_EVENT_ACTION_MOVE: |
| 192 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 193 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 194 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 195 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 196 | case AMOTION_EVENT_ACTION_SCROLL: |
| 197 | return true; |
| 198 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 199 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 200 | int32_t index = getMotionEventActionPointerIndex(action); |
| 201 | return index >= 0 && index < pointerCount; |
| 202 | } |
| 203 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 204 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 205 | return actionButton != 0; |
| 206 | default: |
| 207 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 211 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 212 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 213 | } |
| 214 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 215 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 216 | const PointerProperties* pointerProperties) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 217 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 218 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 219 | return false; |
| 220 | } |
| 221 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 222 | ALOGE("Motion event has invalid pointer count %zu; value must be between 1 and %d.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 223 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | BitSet32 pointerIdBits; |
| 227 | for (size_t i = 0; i < pointerCount; i++) { |
| 228 | int32_t id = pointerProperties[i].id; |
| 229 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 230 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 231 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | if (pointerIdBits.hasBit(id)) { |
| 235 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 236 | return false; |
| 237 | } |
| 238 | pointerIdBits.markBit(id); |
| 239 | } |
| 240 | return true; |
| 241 | } |
| 242 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 243 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 244 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 245 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 248 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 249 | bool first = true; |
| 250 | Region::const_iterator cur = region.begin(); |
| 251 | Region::const_iterator const tail = region.end(); |
| 252 | while (cur != tail) { |
| 253 | if (first) { |
| 254 | first = false; |
| 255 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 256 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 257 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 258 | 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] | 259 | cur++; |
| 260 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 261 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 264 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 265 | constexpr size_t maxEntries = 50; // max events to print |
| 266 | constexpr size_t skipBegin = maxEntries / 2; |
| 267 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 268 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 269 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 270 | |
| 271 | std::string dump; |
| 272 | for (size_t i = 0; i < queue.size(); i++) { |
| 273 | const DispatchEntry& entry = *queue[i]; |
| 274 | if (i >= skipBegin && i < skipEnd) { |
| 275 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 276 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 277 | continue; |
| 278 | } |
| 279 | dump.append(INDENT4); |
| 280 | dump += entry.eventEntry->getDescription(); |
| 281 | dump += StringPrintf(", seq=%" PRIu32 |
| 282 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 283 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 284 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 285 | if (entry.deliveryTime != 0) { |
| 286 | // This entry was delivered, so add information on how long we've been waiting |
| 287 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 288 | } |
| 289 | dump.append("\n"); |
| 290 | } |
| 291 | return dump; |
| 292 | } |
| 293 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 294 | /** |
| 295 | * Find the entry in std::unordered_map by key, and return it. |
| 296 | * If the entry is not found, return a default constructed entry. |
| 297 | * |
| 298 | * Useful when the entries are vectors, since an empty vector will be returned |
| 299 | * if the entry is not found. |
| 300 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 301 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 302 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 303 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 304 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 305 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 306 | } |
| 307 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 308 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 309 | if (first == second) { |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | if (first == nullptr || second == nullptr) { |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | return first->getToken() == second->getToken(); |
| 318 | } |
| 319 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 320 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 321 | if (first == nullptr || second == nullptr) { |
| 322 | return false; |
| 323 | } |
| 324 | return first->applicationInfo.token != nullptr && |
| 325 | first->applicationInfo.token == second->applicationInfo.token; |
| 326 | } |
| 327 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 328 | bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 329 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 330 | } |
| 331 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 332 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 333 | std::shared_ptr<EventEntry> eventEntry, |
| 334 | int32_t inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 335 | if (inputTarget.useDefaultPointerTransform()) { |
| 336 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 337 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 338 | inputTarget.displayTransform, |
| 339 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 343 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 344 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 345 | std::vector<PointerCoords> pointerCoords; |
| 346 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 347 | |
| 348 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 349 | // 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] | 350 | // uses the transform for the normalized pointer. |
| 351 | const ui::Transform& firstPointerTransform = |
| 352 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 353 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 354 | |
| 355 | // Iterate through all pointers in the event to normalize against the first. |
| 356 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 357 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 358 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 359 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 360 | |
| 361 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 362 | // First, apply the current pointer's transform to update the coordinates into |
| 363 | // window space. |
| 364 | pointerCoords[pointerIndex].transform(currTransform); |
| 365 | // Next, apply the inverse transform of the normalized coordinates so the |
| 366 | // current coordinates are transformed into the normalized coordinate space. |
| 367 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 370 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 371 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 372 | motionEntry.deviceId, motionEntry.source, |
| 373 | motionEntry.displayId, motionEntry.policyFlags, |
| 374 | motionEntry.action, motionEntry.actionButton, |
| 375 | motionEntry.flags, motionEntry.metaState, |
| 376 | motionEntry.buttonState, motionEntry.classification, |
| 377 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 378 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 379 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 380 | motionEntry.pointerCount, motionEntry.pointerProperties, |
Alex Chau | f7c99f3 | 2021-12-03 10:37:54 +0000 | [diff] [blame] | 381 | pointerCoords.data(), 0 /* xOffset */, 0 /* yOffset */); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 382 | |
| 383 | if (motionEntry.injectionState) { |
| 384 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 385 | combinedMotionEntry->injectionState->refCount += 1; |
| 386 | } |
| 387 | |
| 388 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 389 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 390 | firstPointerTransform, inputTarget.displayTransform, |
| 391 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 392 | return dispatchEntry; |
| 393 | } |
| 394 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 395 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 396 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 397 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 398 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 399 | |
| 400 | serverChannel = std::move(uniqueServerChannel); |
| 401 | return result; |
| 402 | } |
| 403 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 404 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 405 | 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] | 406 | if (lhs == nullptr && rhs == nullptr) { |
| 407 | return true; |
| 408 | } |
| 409 | if (lhs == nullptr || rhs == nullptr) { |
| 410 | return false; |
| 411 | } |
| 412 | return *lhs == *rhs; |
| 413 | } |
| 414 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 415 | sp<IPlatformCompatNative> getCompatService() { |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 416 | sp<IBinder> service(defaultServiceManager()->getService(String16("platform_compat_native"))); |
| 417 | if (service == nullptr) { |
| 418 | ALOGE("Failed to link to compat service"); |
| 419 | return nullptr; |
| 420 | } |
| 421 | return interface_cast<IPlatformCompatNative>(service); |
| 422 | } |
| 423 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 424 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 425 | KeyEvent event; |
| 426 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 427 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 428 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 429 | return event; |
| 430 | } |
| 431 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 432 | std::optional<int32_t> findMonitorPidByToken( |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 433 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, |
| 434 | const sp<IBinder>& token) { |
| 435 | for (const auto& it : monitorsByDisplay) { |
| 436 | const std::vector<Monitor>& monitors = it.second; |
| 437 | for (const Monitor& monitor : monitors) { |
| 438 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 439 | return monitor.pid; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | return std::nullopt; |
| 444 | } |
| 445 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 446 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 447 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 448 | // affect the statistics. |
| 449 | if (connection.monitor) { |
| 450 | return false; |
| 451 | } |
| 452 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 453 | if (!connection.responsive) { |
| 454 | return false; |
| 455 | } |
| 456 | return true; |
| 457 | } |
| 458 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 459 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 460 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 461 | const int32_t& inputEventId = eventEntry.id; |
| 462 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 463 | // Event was transmuted |
| 464 | return false; |
| 465 | } |
| 466 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 467 | return false; |
| 468 | } |
| 469 | // Only track latency for events that originated from hardware |
| 470 | if (eventEntry.isSynthesized()) { |
| 471 | return false; |
| 472 | } |
| 473 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 474 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 475 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 476 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 477 | return false; |
| 478 | } |
| 479 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 480 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 481 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 482 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 483 | return false; |
| 484 | } |
| 485 | } else { |
| 486 | // Not a key or a motion |
| 487 | return false; |
| 488 | } |
| 489 | if (!shouldReportMetricsForConnection(connection)) { |
| 490 | return false; |
| 491 | } |
| 492 | return true; |
| 493 | } |
| 494 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 495 | /** |
| 496 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 497 | * current time. |
| 498 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 499 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 500 | const nsecs_t currentTime = now(); |
| 501 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 502 | if (entry->timeoutTime < currentTime) { |
| 503 | return false; |
| 504 | } |
| 505 | } |
| 506 | return true; |
| 507 | } |
| 508 | |
Alex Chau | 663fc83 | 2021-12-03 10:37:54 +0000 | [diff] [blame] | 509 | vec2 transformWithoutTranslation(const ui::Transform& transform, float x, float y) { |
| 510 | const vec2 transformedXy = transform.transform(x, y); |
| 511 | const vec2 transformedOrigin = transform.transform(0, 0); |
| 512 | return transformedXy - transformedOrigin; |
| 513 | } |
| 514 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 515 | // Returns true if the event type passed as argument represents a user activity. |
| 516 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 517 | switch (eventEntry.type) { |
| 518 | case EventEntry::Type::FOCUS: |
| 519 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 520 | case EventEntry::Type::DRAG: |
| 521 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 522 | case EventEntry::Type::SENSOR: |
| 523 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 524 | return false; |
| 525 | case EventEntry::Type::DEVICE_RESET: |
| 526 | case EventEntry::Type::KEY: |
| 527 | case EventEntry::Type::MOTION: |
| 528 | return true; |
| 529 | } |
| 530 | } |
| 531 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 532 | // Returns true if the given window can accept pointer events at the given display location. |
| 533 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, int32_t x, int32_t y) { |
| 534 | if (windowInfo.displayId != displayId || !windowInfo.visible) { |
| 535 | return false; |
| 536 | } |
| 537 | const auto flags = windowInfo.flags; |
| 538 | if (flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
| 539 | return false; |
| 540 | } |
| 541 | const bool isModalWindow = !flags.test(WindowInfo::Flag::NOT_FOCUSABLE) && |
| 542 | !flags.test(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 543 | if (!isModalWindow && !windowInfo.touchableRegionContainsPoint(x, y)) { |
| 544 | return false; |
| 545 | } |
| 546 | return true; |
| 547 | } |
| 548 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 549 | } // namespace |
| 550 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 551 | // --- InputDispatcher --- |
| 552 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 553 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 554 | : mPolicy(policy), |
| 555 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 556 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 557 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 558 | mAppSwitchSawKeyDown(false), |
| 559 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 560 | mNextUnblockedEvent(nullptr), |
| 561 | mDispatchEnabled(false), |
| 562 | mDispatchFrozen(false), |
| 563 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 564 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 565 | // To avoid leaking stack in case that call never comes, and for tests, |
| 566 | // initialize it here anyways. |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 567 | mInTouchMode(kDefaultInTouchMode), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 568 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 569 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 570 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 571 | mLatencyAggregator(), |
| 572 | mLatencyTracker(&mLatencyAggregator), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 573 | mCompatService(getCompatService()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 574 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 575 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 576 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 577 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 578 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 579 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 580 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 581 | |
| 582 | policy->getDispatcherConfiguration(&mConfig); |
| 583 | } |
| 584 | |
| 585 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 586 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 587 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 588 | resetKeyRepeatLocked(); |
| 589 | releasePendingEventLocked(); |
| 590 | drainInboundQueueLocked(); |
| 591 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 592 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 593 | while (!mConnectionsByToken.empty()) { |
| 594 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 595 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 596 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 600 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 601 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 602 | return ALREADY_EXISTS; |
| 603 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 604 | mThread = std::make_unique<InputThread>( |
| 605 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 606 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 610 | if (mThread && mThread->isCallingThread()) { |
| 611 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 612 | return INVALID_OPERATION; |
| 613 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 614 | mThread.reset(); |
| 615 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 618 | void InputDispatcher::dispatchOnce() { |
| 619 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 620 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 621 | std::scoped_lock _l(mLock); |
| 622 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | |
| 624 | // Run a dispatch loop if there are no pending commands. |
| 625 | // The dispatch loop might enqueue commands to run afterwards. |
| 626 | if (!haveCommandsLocked()) { |
| 627 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 628 | } |
| 629 | |
| 630 | // Run all pending commands if there are any. |
| 631 | // 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] | 632 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 633 | nextWakeupTime = LONG_LONG_MIN; |
| 634 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 635 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 636 | // If we are still waiting for ack on some events, |
| 637 | // we might have to wake up earlier to check if an app is anr'ing. |
| 638 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 639 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 640 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 641 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 642 | // pending or queued events |
| 643 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 644 | mDispatcherEnteredIdle.notify_all(); |
| 645 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 646 | } // release lock |
| 647 | |
| 648 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 649 | nsecs_t currentTime = now(); |
| 650 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 651 | mLooper->pollOnce(timeoutMillis); |
| 652 | } |
| 653 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 654 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 655 | * Raise ANR if there is no focused window. |
| 656 | * Before the ANR is raised, do a final state check: |
| 657 | * 1. The currently focused application must be the same one we are waiting for. |
| 658 | * 2. Ensure we still don't have a focused window. |
| 659 | */ |
| 660 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 661 | // Check if the application that we are waiting for is still focused. |
| 662 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 663 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 664 | if (focusedApplication == nullptr || |
| 665 | focusedApplication->getApplicationToken() != |
| 666 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 667 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 668 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 669 | focusedApplication->getName().c_str()); |
| 670 | return; // The focused application has changed. |
| 671 | } |
| 672 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 673 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 674 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 675 | if (focusedWindowHandle != nullptr) { |
| 676 | return; // We now have a focused window. No need for ANR. |
| 677 | } |
| 678 | onAnrLocked(mAwaitedFocusedApplication); |
| 679 | } |
| 680 | |
| 681 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 682 | * Check if any of the connections' wait queues have events that are too old. |
| 683 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 684 | * Return the time at which we should wake up next. |
| 685 | */ |
| 686 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 687 | const nsecs_t currentTime = now(); |
| 688 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 689 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 690 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 691 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 692 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 693 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 694 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 695 | return LONG_LONG_MIN; |
| 696 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 697 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 698 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | // Check if any connection ANRs are due |
| 703 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 704 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 705 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 706 | } |
| 707 | |
| 708 | // If we reached here, we have an unresponsive connection. |
| 709 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 710 | if (connection == nullptr) { |
| 711 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 712 | return nextAnrCheck; |
| 713 | } |
| 714 | connection->responsive = false; |
| 715 | // Stop waking up for this unresponsive connection |
| 716 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 717 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 718 | return LONG_LONG_MIN; |
| 719 | } |
| 720 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 721 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 722 | sp<WindowInfoHandle> window = getWindowHandleLocked(token); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 723 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 724 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 725 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 726 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 729 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 730 | nsecs_t currentTime = now(); |
| 731 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 732 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 733 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 734 | // repeat if the device is just coming out of sleep. |
| 735 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 736 | resetKeyRepeatLocked(); |
| 737 | } |
| 738 | |
| 739 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 740 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 741 | if (DEBUG_FOCUS) { |
| 742 | ALOGD("Dispatch frozen. Waiting some more."); |
| 743 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 744 | return; |
| 745 | } |
| 746 | |
| 747 | // Optimize latency of app switches. |
| 748 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 749 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 750 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 751 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 752 | *nextWakeupTime = mAppSwitchDueTime; |
| 753 | } |
| 754 | |
| 755 | // Ready to start a new event. |
| 756 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 757 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 758 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 759 | if (isAppSwitchDue) { |
| 760 | // The inbound queue is empty so the app switch key we were waiting |
| 761 | // for will never arrive. Stop waiting for it. |
| 762 | resetPendingAppSwitchLocked(false); |
| 763 | isAppSwitchDue = false; |
| 764 | } |
| 765 | |
| 766 | // Synthesize a key repeat if appropriate. |
| 767 | if (mKeyRepeatState.lastKeyEntry) { |
| 768 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 769 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 770 | } else { |
| 771 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 772 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | // Nothing to do if there is no pending event. |
| 778 | if (!mPendingEvent) { |
| 779 | return; |
| 780 | } |
| 781 | } else { |
| 782 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 783 | mPendingEvent = mInboundQueue.front(); |
| 784 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 785 | traceInboundQueueLengthLocked(); |
| 786 | } |
| 787 | |
| 788 | // Poke user activity for this event. |
| 789 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 790 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 791 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | // Now we have an event to dispatch. |
| 795 | // 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] | 796 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 797 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 798 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 799 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 800 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 801 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 802 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 806 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 810 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 811 | const ConfigurationChangedEntry& typedEntry = |
| 812 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 813 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 814 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 815 | break; |
| 816 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 817 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 818 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 819 | const DeviceResetEntry& typedEntry = |
| 820 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 821 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 822 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 823 | break; |
| 824 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 825 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 826 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 827 | std::shared_ptr<FocusEntry> typedEntry = |
| 828 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 829 | dispatchFocusLocked(currentTime, typedEntry); |
| 830 | done = true; |
| 831 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 832 | break; |
| 833 | } |
| 834 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 835 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 836 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 837 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 838 | done = true; |
| 839 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 840 | break; |
| 841 | } |
| 842 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 843 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 844 | const auto typedEntry = |
| 845 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 846 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 847 | done = true; |
| 848 | break; |
| 849 | } |
| 850 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 851 | case EventEntry::Type::DRAG: { |
| 852 | std::shared_ptr<DragEntry> typedEntry = |
| 853 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 854 | dispatchDragLocked(currentTime, typedEntry); |
| 855 | done = true; |
| 856 | break; |
| 857 | } |
| 858 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 859 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 860 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 861 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 862 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 863 | resetPendingAppSwitchLocked(true); |
| 864 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 865 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 866 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 867 | } |
| 868 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 869 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 870 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 871 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 872 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 873 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 874 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 875 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 876 | break; |
| 877 | } |
| 878 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 879 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 880 | std::shared_ptr<MotionEntry> motionEntry = |
| 881 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 882 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 883 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 884 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 885 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 886 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 887 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 888 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 889 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 890 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 891 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 892 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 893 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 894 | |
| 895 | case EventEntry::Type::SENSOR: { |
| 896 | std::shared_ptr<SensorEntry> sensorEntry = |
| 897 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 898 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 899 | dropReason = DropReason::APP_SWITCH; |
| 900 | } |
| 901 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 902 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 903 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 904 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 905 | dropReason = DropReason::STALE; |
| 906 | } |
| 907 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 908 | done = true; |
| 909 | break; |
| 910 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 914 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 915 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 916 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 917 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 918 | |
| 919 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 920 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 924 | /** |
| 925 | * Return true if the events preceding this incoming motion event should be dropped |
| 926 | * Return false otherwise (the default behaviour) |
| 927 | */ |
| 928 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 929 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 930 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 931 | |
| 932 | // Optimize case where the current application is unresponsive and the user |
| 933 | // decides to touch a window in a different application. |
| 934 | // If the application takes too long to catch up then we drop all events preceding |
| 935 | // the touch into the other window. |
| 936 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 937 | int32_t displayId = motionEntry.displayId; |
| 938 | int32_t x = static_cast<int32_t>( |
| 939 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 940 | int32_t y = static_cast<int32_t>( |
| 941 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 942 | sp<WindowInfoHandle> touchedWindowHandle = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 943 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 944 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 945 | touchedWindowHandle->getApplicationToken() != |
| 946 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 947 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 948 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 949 | "for %s", |
| 950 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 951 | return true; |
| 952 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 953 | |
| 954 | // Alternatively, maybe there's a gesture monitor that could handle this event |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 955 | for (const auto& monitor : getValueByKey(mGestureMonitorsByDisplay, displayId)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 956 | sp<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 957 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 958 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 959 | // This monitor could take more input. Drop all events preceding this |
| 960 | // event, so that gesture monitor could get a chance to receive the stream |
| 961 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 962 | "responsive gesture monitor that may handle the event", |
| 963 | mAwaitedFocusedApplication->getName().c_str()); |
| 964 | return true; |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 970 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 971 | // events to be processed before dispatching the key event. This is because these motion events |
| 972 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 973 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 974 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 975 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 976 | "just send the pending key event to the focused window."); |
| 977 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 978 | } |
| 979 | return false; |
| 980 | } |
| 981 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 982 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 983 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 984 | mInboundQueue.push_back(std::move(newEntry)); |
| 985 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 986 | traceInboundQueueLengthLocked(); |
| 987 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 988 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 989 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 990 | // Optimize app switch latency. |
| 991 | // If the application takes too long to catch up then we drop all events preceding |
| 992 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 993 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 994 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 995 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 996 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 997 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 998 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 999 | if (DEBUG_APP_SWITCH) { |
| 1000 | ALOGD("App switch is pending!"); |
| 1001 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1002 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1003 | mAppSwitchSawKeyDown = false; |
| 1004 | needWake = true; |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | break; |
| 1009 | } |
| 1010 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1011 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1012 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1013 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1014 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1015 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1016 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1017 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1018 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1019 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1020 | break; |
| 1021 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1022 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1023 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1024 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1025 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1026 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1027 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1028 | // nothing to do |
| 1029 | break; |
| 1030 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | return needWake; |
| 1034 | } |
| 1035 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1036 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1037 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1038 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1039 | mRecentQueue.push_back(entry); |
| 1040 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1041 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1042 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1046 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1047 | int32_t y, TouchState* touchState, |
| 1048 | bool addOutsideTargets, |
| 1049 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1050 | if (addOutsideTargets && touchState == nullptr) { |
| 1051 | 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] | 1052 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1053 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 1054 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1055 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1056 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1057 | continue; |
| 1058 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1059 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1060 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 1061 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1062 | return windowHandle; |
| 1063 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1064 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1065 | if (addOutsideTargets && info.flags.test(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
| 1066 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1067 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1068 | } |
| 1069 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1070 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 1073 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked(int32_t displayId, |
| 1074 | int32_t x, |
| 1075 | int32_t y) const { |
| 1076 | // Traverse windows from front to back and gather the touched spy windows. |
| 1077 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1078 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1079 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1080 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1081 | |
| 1082 | if (!windowAcceptsTouchAt(info, displayId, x, y)) { |
| 1083 | continue; |
| 1084 | } |
| 1085 | if (!info.isSpy()) { |
| 1086 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1087 | return spyWindows; |
| 1088 | } |
| 1089 | spyWindows.push_back(windowHandle); |
| 1090 | } |
| 1091 | return spyWindows; |
| 1092 | } |
| 1093 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1094 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1095 | const char* reason; |
| 1096 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1097 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1098 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1099 | ALOGD("Dropped event because policy consumed it."); |
| 1100 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1101 | reason = "inbound event was dropped because the policy consumed it"; |
| 1102 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1103 | case DropReason::DISABLED: |
| 1104 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1105 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1106 | } |
| 1107 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1108 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1109 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1110 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1111 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1112 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1113 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1114 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1115 | "has started interacting with a different application."); |
| 1116 | reason = "inbound event was dropped because the current application is not responding " |
| 1117 | "and the user has started interacting with a different application"; |
| 1118 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1119 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1120 | ALOGI("Dropped event because it is stale."); |
| 1121 | reason = "inbound event was dropped because it is stale"; |
| 1122 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1123 | case DropReason::NO_POINTER_CAPTURE: |
| 1124 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1125 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1126 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1127 | case DropReason::NOT_DROPPED: { |
| 1128 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1129 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1130 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1133 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1134 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1135 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1136 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1137 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1138 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1139 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1140 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1141 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1142 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1143 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1144 | } else { |
| 1145 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1146 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1147 | } |
| 1148 | break; |
| 1149 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1150 | case EventEntry::Type::SENSOR: { |
| 1151 | break; |
| 1152 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1153 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1154 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1155 | break; |
| 1156 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1157 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1158 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1159 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1160 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1161 | 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] | 1162 | break; |
| 1163 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1167 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1168 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1169 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1172 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1173 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1174 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1175 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1179 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1180 | } |
| 1181 | |
| 1182 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1183 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1184 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1185 | if (DEBUG_APP_SWITCH) { |
| 1186 | if (handled) { |
| 1187 | ALOGD("App switch has arrived."); |
| 1188 | } else { |
| 1189 | ALOGD("App switch was abandoned."); |
| 1190 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1191 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1194 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1195 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1198 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1199 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | return false; |
| 1201 | } |
| 1202 | |
| 1203 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1204 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1205 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1206 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1207 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1208 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1209 | return true; |
| 1210 | } |
| 1211 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1212 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1213 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1217 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1218 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1219 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1220 | releaseInboundEventLocked(entry); |
| 1221 | } |
| 1222 | traceInboundQueueLengthLocked(); |
| 1223 | } |
| 1224 | |
| 1225 | void InputDispatcher::releasePendingEventLocked() { |
| 1226 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1227 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1228 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1232 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1233 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1234 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1235 | if (DEBUG_DISPATCH_CYCLE) { |
| 1236 | ALOGD("Injected inbound event was dropped."); |
| 1237 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1238 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1239 | } |
| 1240 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1241 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1242 | } |
| 1243 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1247 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1248 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1252 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1253 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1255 | uint32_t policyFlags = entry->policyFlags & |
| 1256 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1257 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1258 | std::shared_ptr<KeyEntry> newEntry = |
| 1259 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1260 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1261 | entry->flags, entry->keyCode, entry->scanCode, |
| 1262 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1263 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1264 | newEntry->syntheticRepeat = true; |
| 1265 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1266 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1267 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1270 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1271 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1272 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1273 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1274 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1275 | |
| 1276 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1277 | resetKeyRepeatLocked(); |
| 1278 | |
| 1279 | // 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] | 1280 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1281 | scoped_unlock unlock(mLock); |
| 1282 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1283 | }; |
| 1284 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1285 | return true; |
| 1286 | } |
| 1287 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1288 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1289 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1290 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1291 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1292 | entry.deviceId); |
| 1293 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1294 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1295 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1296 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1297 | resetKeyRepeatLocked(); |
| 1298 | } |
| 1299 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1300 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1301 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1302 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1303 | return true; |
| 1304 | } |
| 1305 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1306 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1307 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1308 | if (mPendingEvent != nullptr) { |
| 1309 | // Move the pending event to the front of the queue. This will give the chance |
| 1310 | // for the pending event to get dispatched to the newly focused window |
| 1311 | mInboundQueue.push_front(mPendingEvent); |
| 1312 | mPendingEvent = nullptr; |
| 1313 | } |
| 1314 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1315 | std::unique_ptr<FocusEntry> focusEntry = |
| 1316 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1317 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1318 | |
| 1319 | // This event should go to the front of the queue, but behind all other focus events |
| 1320 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1321 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1322 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1323 | [](const std::shared_ptr<EventEntry>& event) { |
| 1324 | return event->type == EventEntry::Type::FOCUS; |
| 1325 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1326 | |
| 1327 | // 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] | 1328 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1329 | } |
| 1330 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1331 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1332 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1333 | if (channel == nullptr) { |
| 1334 | return; // Window has gone away |
| 1335 | } |
| 1336 | InputTarget target; |
| 1337 | target.inputChannel = channel; |
| 1338 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1339 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1340 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1341 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1342 | std::string reason = std::string("reason=").append(entry->reason); |
| 1343 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1344 | dispatchEventLocked(currentTime, entry, {target}); |
| 1345 | } |
| 1346 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1347 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1348 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1349 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1350 | dropReason = DropReason::NOT_DROPPED; |
| 1351 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1352 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1353 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1354 | |
| 1355 | if (entry->pointerCaptureRequest.enable) { |
| 1356 | // Enable Pointer Capture. |
| 1357 | if (haveWindowWithPointerCapture && |
| 1358 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
| 1359 | LOG_ALWAYS_FATAL("This request to enable Pointer Capture has already been dispatched " |
| 1360 | "to the window."); |
| 1361 | } |
| 1362 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1363 | // This can happen if a window requests capture and immediately releases capture. |
| 1364 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1365 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1366 | return; |
| 1367 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1368 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1369 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1370 | return; |
| 1371 | } |
| 1372 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1373 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1374 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1375 | mWindowTokenWithPointerCapture = token; |
| 1376 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1377 | // Disable Pointer Capture. |
| 1378 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1379 | // for two reasons: |
| 1380 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1381 | // to disable capture with the same sequence number: one generated by |
| 1382 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1383 | // Capture being disabled in InputReader. |
| 1384 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1385 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1386 | // in InputReader. |
| 1387 | if (!haveWindowWithPointerCapture) { |
| 1388 | // Pointer capture was already forcefully disabled because of focus change. |
| 1389 | dropReason = DropReason::NOT_DROPPED; |
| 1390 | return; |
| 1391 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1392 | token = mWindowTokenWithPointerCapture; |
| 1393 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1394 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1395 | setPointerCaptureLocked(false); |
| 1396 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | auto channel = getInputChannelLocked(token); |
| 1400 | if (channel == nullptr) { |
| 1401 | // Window has gone away, clean up Pointer Capture state. |
| 1402 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1403 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1404 | setPointerCaptureLocked(false); |
| 1405 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1406 | return; |
| 1407 | } |
| 1408 | InputTarget target; |
| 1409 | target.inputChannel = channel; |
| 1410 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1411 | entry->dispatchInProgress = true; |
| 1412 | dispatchEventLocked(currentTime, entry, {target}); |
| 1413 | |
| 1414 | dropReason = DropReason::NOT_DROPPED; |
| 1415 | } |
| 1416 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1417 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1418 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1419 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1420 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1421 | if (windowHandles.empty()) { |
| 1422 | return; |
| 1423 | } |
| 1424 | const std::vector<InputTarget> inputTargets = |
| 1425 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1426 | if (inputTargets.empty()) { |
| 1427 | return; |
| 1428 | } |
| 1429 | entry->dispatchInProgress = true; |
| 1430 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1431 | } |
| 1432 | |
| 1433 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1434 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1435 | std::vector<InputTarget> inputTargets; |
| 1436 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1437 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1438 | const sp<IBinder>& token = handle->getToken(); |
| 1439 | if (token == nullptr) { |
| 1440 | continue; |
| 1441 | } |
| 1442 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1443 | if (channel == nullptr) { |
| 1444 | continue; // Window has gone away |
| 1445 | } |
| 1446 | InputTarget target; |
| 1447 | target.inputChannel = channel; |
| 1448 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1449 | inputTargets.push_back(target); |
| 1450 | } |
| 1451 | return inputTargets; |
| 1452 | } |
| 1453 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1454 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1455 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1456 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1457 | if (!entry->dispatchInProgress) { |
| 1458 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1459 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1460 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1461 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1462 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1463 | // We have seen two identical key downs in a row which indicates that the device |
| 1464 | // driver is automatically generating key repeats itself. We take note of the |
| 1465 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1466 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1467 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1468 | // Make sure we don't get key down from a different device. If a different |
| 1469 | // device Id has same key pressed down, the new device Id will replace the |
| 1470 | // current one to hold the key repeat with repeat count reset. |
| 1471 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1472 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1473 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1474 | resetKeyRepeatLocked(); |
| 1475 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1476 | } else { |
| 1477 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1478 | resetKeyRepeatLocked(); |
| 1479 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1480 | } |
| 1481 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1482 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1483 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1484 | // 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] | 1485 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1486 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1487 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1488 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1489 | resetKeyRepeatLocked(); |
| 1490 | } |
| 1491 | |
| 1492 | if (entry->repeatCount == 1) { |
| 1493 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1494 | } else { |
| 1495 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1496 | } |
| 1497 | |
| 1498 | entry->dispatchInProgress = true; |
| 1499 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1500 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | // Handle case where the policy asked us to try again later last time. |
| 1504 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1505 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1506 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1507 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1508 | } |
| 1509 | return false; // wait until next wakeup |
| 1510 | } |
| 1511 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1512 | entry->interceptKeyWakeupTime = 0; |
| 1513 | } |
| 1514 | |
| 1515 | // Give the policy a chance to intercept the key. |
| 1516 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1517 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1518 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1519 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1520 | |
| 1521 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1522 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1523 | }; |
| 1524 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1525 | return false; // wait for the command to run |
| 1526 | } else { |
| 1527 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1528 | } |
| 1529 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1530 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1531 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1536 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1537 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1538 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1539 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1540 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1541 | return true; |
| 1542 | } |
| 1543 | |
| 1544 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1545 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1546 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1547 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1548 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1549 | return false; |
| 1550 | } |
| 1551 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1552 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1553 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1554 | return true; |
| 1555 | } |
| 1556 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1557 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1558 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1559 | |
| 1560 | // Dispatch the key. |
| 1561 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1562 | return true; |
| 1563 | } |
| 1564 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1565 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1566 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1567 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1568 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1569 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1570 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1571 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1572 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1573 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1574 | } |
| 1575 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1576 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1577 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1578 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1579 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1580 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1581 | "source=0x%x, sensorType=%s", |
| 1582 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1583 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1584 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1585 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1586 | scoped_unlock unlock(mLock); |
| 1587 | |
| 1588 | if (entry->accuracyChanged) { |
| 1589 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1590 | } |
| 1591 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1592 | entry->hwTimestamp, entry->values); |
| 1593 | }; |
| 1594 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1598 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1599 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1600 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1601 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1602 | { // acquire lock |
| 1603 | std::scoped_lock _l(mLock); |
| 1604 | |
| 1605 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1606 | std::shared_ptr<EventEntry> entry = *it; |
| 1607 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1608 | it = mInboundQueue.erase(it); |
| 1609 | releaseInboundEventLocked(entry); |
| 1610 | } |
| 1611 | } |
| 1612 | } |
| 1613 | return true; |
| 1614 | } |
| 1615 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1616 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1617 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1618 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1619 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1620 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1621 | entry->dispatchInProgress = true; |
| 1622 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1623 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1627 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1628 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1629 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1630 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1631 | return true; |
| 1632 | } |
| 1633 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1634 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1635 | |
| 1636 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1637 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1638 | |
| 1639 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1640 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1641 | if (isPointerEvent) { |
| 1642 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1643 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1644 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1645 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1646 | } else { |
| 1647 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1648 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1649 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1650 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1651 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1652 | return false; |
| 1653 | } |
| 1654 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1655 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1656 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1657 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1658 | return true; |
| 1659 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1660 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1661 | CancelationOptions::Mode mode(isPointerEvent |
| 1662 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1663 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1664 | CancelationOptions options(mode, "input event injection failed"); |
| 1665 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1666 | return true; |
| 1667 | } |
| 1668 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1669 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1670 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1671 | |
| 1672 | // Dispatch the motion. |
| 1673 | if (conflictingPointerActions) { |
| 1674 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1675 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1676 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1677 | } |
| 1678 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1679 | return true; |
| 1680 | } |
| 1681 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1682 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1683 | bool isExiting, const MotionEntry& motionEntry) { |
| 1684 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1685 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1686 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1687 | PointerCoords pointerCoords; |
| 1688 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1689 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1690 | |
| 1691 | std::unique_ptr<DragEntry> dragEntry = |
| 1692 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1693 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1694 | pointerCoords.getY()); |
| 1695 | |
| 1696 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1697 | } |
| 1698 | |
| 1699 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1700 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1701 | if (channel == nullptr) { |
| 1702 | return; // Window has gone away |
| 1703 | } |
| 1704 | InputTarget target; |
| 1705 | target.inputChannel = channel; |
| 1706 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1707 | entry->dispatchInProgress = true; |
| 1708 | dispatchEventLocked(currentTime, entry, {target}); |
| 1709 | } |
| 1710 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1711 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1712 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1713 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1714 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1715 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1716 | "metaState=0x%x, buttonState=0x%x," |
| 1717 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1718 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1719 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1720 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1721 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1722 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1723 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1724 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1725 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1726 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1727 | "orientation=%f", |
| 1728 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1729 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1730 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1731 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1732 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1733 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1734 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1735 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1736 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1737 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1738 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1739 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1740 | } |
| 1741 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1742 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1743 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1744 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1745 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1746 | if (DEBUG_DISPATCH_CYCLE) { |
| 1747 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1748 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1749 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1750 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1751 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1752 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1753 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1754 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1755 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1756 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1757 | sp<Connection> connection = |
| 1758 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1759 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1760 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1761 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1762 | if (DEBUG_FOCUS) { |
| 1763 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1764 | "is no longer registered with the input dispatcher.", |
| 1765 | inputTarget.inputChannel->getName().c_str()); |
| 1766 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1767 | } |
| 1768 | } |
| 1769 | } |
| 1770 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1771 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1772 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1773 | // If the policy decides to close the app, we will get a channel removal event via |
| 1774 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1775 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1776 | // pile up. |
| 1777 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1778 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1779 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1780 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1781 | "application not responding"); |
| 1782 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1783 | } |
| 1784 | } |
| 1785 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1786 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1787 | if (DEBUG_FOCUS) { |
| 1788 | ALOGD("Resetting ANR timeouts."); |
| 1789 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1790 | |
| 1791 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1792 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1793 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1794 | } |
| 1795 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1796 | /** |
| 1797 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1798 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1799 | * Focused display is the display that the user most recently interacted with. |
| 1800 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1801 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1802 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1803 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1804 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1805 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1806 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1807 | break; |
| 1808 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1809 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1810 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1811 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1812 | break; |
| 1813 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1814 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1815 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1816 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1817 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1818 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1819 | case EventEntry::Type::SENSOR: |
| 1820 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1821 | 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] | 1822 | return ADISPLAY_ID_NONE; |
| 1823 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1824 | } |
| 1825 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1826 | } |
| 1827 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1828 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1829 | const char* focusedWindowName) { |
| 1830 | if (mAnrTracker.empty()) { |
| 1831 | // already processed all events that we waited for |
| 1832 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1833 | return false; |
| 1834 | } |
| 1835 | |
| 1836 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1837 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1838 | // 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] | 1839 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1840 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1841 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1842 | return true; |
| 1843 | } |
| 1844 | |
| 1845 | // We still have pending events, and already started the timer |
| 1846 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1847 | return true; // Still waiting |
| 1848 | } |
| 1849 | |
| 1850 | // Waited too long, and some connection still hasn't processed all motions |
| 1851 | // Just send the key to the focused window |
| 1852 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1853 | focusedWindowName); |
| 1854 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1855 | return false; |
| 1856 | } |
| 1857 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1858 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1859 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1860 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1861 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1862 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1863 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1864 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1865 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1866 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1867 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1868 | // If there is no currently focused window and no focused application |
| 1869 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1870 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1871 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1872 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1873 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1874 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1875 | } |
| 1876 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1877 | // Drop key events if requested by input feature |
| 1878 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1879 | return InputEventInjectionResult::FAILED; |
| 1880 | } |
| 1881 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1882 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1883 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1884 | // start interacting with another application via touch (app switch). This code can be removed |
| 1885 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1886 | // an app is expected to have a focused window. |
| 1887 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1888 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1889 | // 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] | 1890 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1891 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1892 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1893 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1894 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1895 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1896 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1897 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1898 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1899 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1900 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1901 | // Already raised ANR. Drop the event |
| 1902 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1903 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1904 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1905 | } else { |
| 1906 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1907 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | // we have a valid, non-null focused window |
| 1912 | resetNoFocusedWindowTimeoutLocked(); |
| 1913 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1914 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1915 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1916 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1917 | } |
| 1918 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1919 | if (focusedWindowHandle->getInfo()->paused) { |
| 1920 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1921 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | // If the event is a key event, then we must wait for all previous events to |
| 1925 | // complete before delivering it because previous events may have the |
| 1926 | // side-effect of transferring focus to a different window and we want to |
| 1927 | // ensure that the following keys are sent to the new window. |
| 1928 | // |
| 1929 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1930 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1931 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1932 | // often anticipate pending UI changes when typing on a keyboard. |
| 1933 | // To obtain this behavior, we must serialize key events with respect to all |
| 1934 | // prior input events. |
| 1935 | if (entry.type == EventEntry::Type::KEY) { |
| 1936 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1937 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1938 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1939 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1943 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1944 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1945 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1946 | |
| 1947 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1948 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1949 | } |
| 1950 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1951 | /** |
| 1952 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1953 | * that are currently unresponsive. |
| 1954 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1955 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1956 | const std::vector<Monitor>& monitors) const { |
| 1957 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1958 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1959 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 1960 | sp<Connection> connection = |
| 1961 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1962 | if (connection == nullptr) { |
| 1963 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1964 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1965 | return false; |
| 1966 | } |
| 1967 | if (!connection->responsive) { |
| 1968 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1969 | connection->inputChannel->getName().c_str()); |
| 1970 | return false; |
| 1971 | } |
| 1972 | return true; |
| 1973 | }); |
| 1974 | return responsiveMonitors; |
| 1975 | } |
| 1976 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1977 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1978 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1979 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1980 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1981 | enum InjectionPermission { |
| 1982 | INJECTION_PERMISSION_UNKNOWN, |
| 1983 | INJECTION_PERMISSION_GRANTED, |
| 1984 | INJECTION_PERMISSION_DENIED |
| 1985 | }; |
| 1986 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1987 | // For security reasons, we defer updating the touch state until we are sure that |
| 1988 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1989 | const int32_t displayId = entry.displayId; |
| 1990 | const int32_t action = entry.action; |
| 1991 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1992 | |
| 1993 | // 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] | 1994 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1995 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1996 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1997 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1998 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1999 | // Copy current touch state into tempTouchState. |
| 2000 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2001 | // 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] | 2002 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2003 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2004 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2005 | oldState = &(it->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2006 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2007 | } |
| 2008 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2009 | bool isSplit = tempTouchState.split; |
| 2010 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 2011 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 2012 | tempTouchState.displayId != displayId); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2013 | |
| 2014 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2015 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2016 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2017 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2018 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2019 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2020 | bool wrongDevice = false; |
| 2021 | if (newGesture) { |
| 2022 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2023 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2024 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2025 | "in display %" PRId32, |
| 2026 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2027 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2028 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2029 | switchedDevice = false; |
| 2030 | wrongDevice = true; |
| 2031 | goto Failed; |
| 2032 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2033 | tempTouchState.reset(); |
| 2034 | tempTouchState.down = down; |
| 2035 | tempTouchState.deviceId = entry.deviceId; |
| 2036 | tempTouchState.source = entry.source; |
| 2037 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2038 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2039 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2040 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2041 | "in display %" PRId32, |
| 2042 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2043 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2044 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2045 | switchedDevice = false; |
| 2046 | wrongDevice = true; |
| 2047 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2051 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2052 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2053 | int32_t x; |
| 2054 | int32_t y; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2055 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2056 | // Always dispatch mouse events to cursor position. |
| 2057 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2058 | x = int32_t(entry.xCursorPosition); |
| 2059 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2060 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2061 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2062 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2063 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2064 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2065 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 2066 | isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2067 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2068 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2069 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2070 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2071 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2072 | // 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] | 2073 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2076 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2077 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2078 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2079 | // New window supports splitting, but we should never split mouse events. |
| 2080 | isSplit = !isFromMouse; |
| 2081 | } else if (isSplit) { |
| 2082 | // New window does not support splitting but we have already split events. |
| 2083 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2084 | newTouchedWindowHandle = nullptr; |
| 2085 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2086 | } else { |
| 2087 | // 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] | 2088 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2089 | // should never be split. |
| 2090 | tempTouchState.split = isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2093 | // Update hover state. |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2094 | if (newTouchedWindowHandle != nullptr) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2095 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2096 | newHoverWindowHandle = nullptr; |
| 2097 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2098 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2099 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2100 | } |
| 2101 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 2102 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
| 2103 | findTouchedSpyWindowsAtLocked(displayId, x, y); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2104 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 2105 | // Process the foreground window first so that it is the first to receive the event. |
| 2106 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
| 2110 | const WindowInfo& info = *windowHandle->getInfo(); |
| 2111 | |
| 2112 | if (info.paused) { |
| 2113 | ALOGI("Not sending touch event to %s because it is paused", |
| 2114 | windowHandle->getName().c_str()); |
| 2115 | continue; |
| 2116 | } |
| 2117 | |
| 2118 | // Ensure the window has a connection and the connection is responsive |
| 2119 | const bool isResponsive = hasResponsiveConnectionLocked(*windowHandle); |
| 2120 | if (!isResponsive) { |
| 2121 | ALOGW("Not sending touch gesture to %s because it is not responsive", |
| 2122 | windowHandle->getName().c_str()); |
| 2123 | continue; |
| 2124 | } |
| 2125 | |
| 2126 | // Drop events that can't be trusted due to occlusion |
| 2127 | if (mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2128 | TouchOcclusionInfo occlusionInfo = |
| 2129 | computeTouchOcclusionInfoLocked(windowHandle, x, y); |
| 2130 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 2131 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2132 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2133 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2134 | ALOGD("%s", log.c_str()); |
| 2135 | } |
| 2136 | } |
| 2137 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
| 2138 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2139 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2140 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2141 | continue; |
| 2142 | } |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | // Drop touch events if requested by input feature |
| 2147 | if (shouldDropInput(entry, windowHandle)) { |
| 2148 | continue; |
| 2149 | } |
| 2150 | |
| 2151 | // Set target flags. |
| 2152 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2153 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 2154 | if (!info.isSpy()) { |
| 2155 | // There should only be one new foreground (non-spy) window at this location. |
| 2156 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2157 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2158 | |
| 2159 | if (isSplit) { |
| 2160 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2161 | } |
| 2162 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
| 2163 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2164 | } else if (isWindowObscuredLocked(windowHandle)) { |
| 2165 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2166 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2167 | |
| 2168 | // Update the temporary touch state. |
| 2169 | BitSet32 pointerIds; |
| 2170 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2171 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2172 | pointerIds.markBit(pointerId); |
| 2173 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2174 | |
| 2175 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2176 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2177 | |
| 2178 | const std::vector<Monitor> newGestureMonitors = isDown |
| 2179 | ? selectResponsiveMonitorsLocked( |
| 2180 | getValueByKey(mGestureMonitorsByDisplay, displayId)) |
| 2181 | : std::vector<Monitor>{}; |
| 2182 | |
| 2183 | if (newTouchedWindows.empty() && newGestureMonitors.empty() && |
| 2184 | tempTouchState.gestureMonitors.empty()) { |
| 2185 | ALOGI("Dropping event because there is no touchable window or gesture monitor at " |
| 2186 | "(%d, %d) in display %" PRId32 ".", |
| 2187 | x, y, displayId); |
| 2188 | injectionResult = InputEventInjectionResult::FAILED; |
| 2189 | goto Failed; |
Arthur Hung | 2ea44b9 | 2021-11-23 07:42:21 +0000 | [diff] [blame] | 2190 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2191 | |
| 2192 | tempTouchState.addGestureMonitors(newGestureMonitors); |
| 2193 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2194 | } else { |
| 2195 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2196 | |
| 2197 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2198 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2199 | if (DEBUG_FOCUS) { |
| 2200 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2201 | "dropped the pointer down event in display %" PRId32, |
| 2202 | displayId); |
| 2203 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2204 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2205 | goto Failed; |
| 2206 | } |
| 2207 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2208 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2209 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2210 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2211 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2212 | tempTouchState.isSlippery()) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2213 | const int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2214 | 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] | 2215 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2216 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2217 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2218 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
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) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2480 | const sp<WindowInfoHandle> dropWindow = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2481 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2482 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2483 | if (dropWindow) { |
| 2484 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2485 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2486 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2487 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2488 | } |
| 2489 | mDragState.reset(); |
| 2490 | } |
| 2491 | |
| 2492 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2493 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2494 | return; |
| 2495 | } |
| 2496 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2497 | if (!mDragState->isStartDrag) { |
| 2498 | mDragState->isStartDrag = true; |
| 2499 | mDragState->isStylusButtonDownAtStart = |
| 2500 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2501 | } |
| 2502 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2503 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2504 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2505 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2506 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2507 | // Handle the special case : stylus button no longer pressed. |
| 2508 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2509 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2510 | finishDragAndDrop(entry.displayId, x, y); |
| 2511 | return; |
| 2512 | } |
| 2513 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2514 | const sp<WindowInfoHandle> hoverWindowHandle = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2515 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2516 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2517 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2518 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2519 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2520 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2521 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2522 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2523 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2524 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2525 | } |
| 2526 | // enqueue drag location if needed. |
| 2527 | if (hoverWindowHandle != nullptr) { |
| 2528 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2529 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2530 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2531 | finishDragAndDrop(entry.displayId, x, y); |
| 2532 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2533 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2534 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2535 | } |
| 2536 | } |
| 2537 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2538 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2539 | int32_t targetFlags, BitSet32 pointerIds, |
| 2540 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2541 | std::vector<InputTarget>::iterator it = |
| 2542 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2543 | [&windowHandle](const InputTarget& inputTarget) { |
| 2544 | return inputTarget.inputChannel->getConnectionToken() == |
| 2545 | windowHandle->getToken(); |
| 2546 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2547 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2548 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2549 | |
| 2550 | if (it == inputTargets.end()) { |
| 2551 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2552 | std::shared_ptr<InputChannel> inputChannel = |
| 2553 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2554 | if (inputChannel == nullptr) { |
| 2555 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2556 | return; |
| 2557 | } |
| 2558 | inputTarget.inputChannel = inputChannel; |
| 2559 | inputTarget.flags = targetFlags; |
| 2560 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2561 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2562 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2563 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2564 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2565 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2566 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2567 | inputTargets.push_back(inputTarget); |
| 2568 | it = inputTargets.end() - 1; |
| 2569 | } |
| 2570 | |
| 2571 | ALOG_ASSERT(it->flags == targetFlags); |
| 2572 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2573 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2574 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2575 | } |
| 2576 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2577 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2578 | int32_t displayId) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2579 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2580 | mGlobalMonitorsByDisplay.find(displayId); |
| 2581 | |
| 2582 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2583 | const std::vector<Monitor>& monitors = it->second; |
| 2584 | for (const Monitor& monitor : monitors) { |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2585 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2586 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2587 | } |
| 2588 | } |
| 2589 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2590 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, int32_t displayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2591 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2592 | InputTarget target; |
| 2593 | target.inputChannel = monitor.inputChannel; |
| 2594 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2595 | ui::Transform t; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2596 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | b5402b2 | 2021-10-04 05:52:50 -0700 | [diff] [blame] | 2597 | const auto& displayTransform = it->second.transform; |
| 2598 | target.displayTransform = displayTransform; |
| 2599 | t = displayTransform; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2600 | } |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2601 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2602 | inputTargets.push_back(target); |
| 2603 | } |
| 2604 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2605 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2606 | const InjectionState* injectionState) { |
| 2607 | if (injectionState && |
| 2608 | (windowHandle == nullptr || |
| 2609 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2610 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2611 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2612 | 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] | 2613 | "owned by uid %d", |
| 2614 | injectionState->injectorPid, injectionState->injectorUid, |
| 2615 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2616 | } else { |
| 2617 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2618 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2619 | } |
| 2620 | return false; |
| 2621 | } |
| 2622 | return true; |
| 2623 | } |
| 2624 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2625 | /** |
| 2626 | * Indicate whether one window handle should be considered as obscuring |
| 2627 | * another window handle. We only check a few preconditions. Actually |
| 2628 | * checking the bounds is left to the caller. |
| 2629 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2630 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2631 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2632 | // Compare by token so cloned layers aren't counted |
| 2633 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2634 | return false; |
| 2635 | } |
| 2636 | auto info = windowHandle->getInfo(); |
| 2637 | auto otherInfo = otherHandle->getInfo(); |
| 2638 | if (!otherInfo->visible) { |
| 2639 | return false; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2640 | } else if (otherInfo->alpha == 0 && otherInfo->flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2641 | // Those act as if they were invisible, so we don't need to flag them. |
| 2642 | // We do want to potentially flag touchable windows even if they have 0 |
| 2643 | // opacity, since they can consume touches and alter the effects of the |
| 2644 | // user interaction (eg. apps that rely on |
| 2645 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2646 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2647 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2648 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2649 | // If ownerUid is the same we don't generate occlusion events as there |
| 2650 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2651 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2652 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2653 | return false; |
| 2654 | } else if (otherInfo->displayId != info->displayId) { |
| 2655 | return false; |
| 2656 | } |
| 2657 | return true; |
| 2658 | } |
| 2659 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2660 | /** |
| 2661 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2662 | * untrusted, one should check: |
| 2663 | * |
| 2664 | * 1. If result.hasBlockingOcclusion is true. |
| 2665 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2666 | * BLOCK_UNTRUSTED. |
| 2667 | * |
| 2668 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2669 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2670 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2671 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2672 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2673 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2674 | * |
| 2675 | * If neither of those is true, then it means the touch can be allowed. |
| 2676 | */ |
| 2677 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2678 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2679 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2680 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2681 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2682 | TouchOcclusionInfo info; |
| 2683 | info.hasBlockingOcclusion = false; |
| 2684 | info.obscuringOpacity = 0; |
| 2685 | info.obscuringUid = -1; |
| 2686 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2687 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2688 | if (windowHandle == otherHandle) { |
| 2689 | break; // All future windows are below us. Exit early. |
| 2690 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2691 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2692 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2693 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2694 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2695 | info.debugInfo.push_back( |
| 2696 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2697 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2698 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2699 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2700 | // window's touch occlusion mode |
| 2701 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2702 | info.hasBlockingOcclusion = true; |
| 2703 | info.obscuringUid = otherInfo->ownerUid; |
| 2704 | info.obscuringPackage = otherInfo->packageName; |
| 2705 | break; |
| 2706 | } |
| 2707 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2708 | uint32_t uid = otherInfo->ownerUid; |
| 2709 | float opacity = |
| 2710 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2711 | // Given windows A and B: |
| 2712 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2713 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2714 | opacityByUid[uid] = opacity; |
| 2715 | if (opacity > info.obscuringOpacity) { |
| 2716 | info.obscuringOpacity = opacity; |
| 2717 | info.obscuringUid = uid; |
| 2718 | info.obscuringPackage = otherInfo->packageName; |
| 2719 | } |
| 2720 | } |
| 2721 | } |
| 2722 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2723 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2724 | info.debugInfo.push_back( |
| 2725 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2726 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2727 | return info; |
| 2728 | } |
| 2729 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2730 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2731 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2732 | return StringPrintf(INDENT2 |
| 2733 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2734 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2735 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2736 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2737 | isTouchedWindow ? "[TOUCHED] " : "", ftl::enum_string(info->type).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2738 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2739 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2740 | info->frameTop, info->frameRight, info->frameBottom, |
| 2741 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2742 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2743 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2744 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2747 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2748 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2749 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2750 | occlusionInfo.obscuringUid); |
| 2751 | return false; |
| 2752 | } |
| 2753 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2754 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2755 | "%.2f, maximum allowed = %.2f)", |
| 2756 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2757 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2758 | return false; |
| 2759 | } |
| 2760 | return true; |
| 2761 | } |
| 2762 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2763 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2764 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2765 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2766 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2767 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2768 | if (windowHandle == otherHandle) { |
| 2769 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2770 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2771 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2772 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2773 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2774 | return true; |
| 2775 | } |
| 2776 | } |
| 2777 | return false; |
| 2778 | } |
| 2779 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2780 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2781 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2782 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2783 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2784 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2785 | if (windowHandle == otherHandle) { |
| 2786 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2787 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2788 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2789 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2790 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2791 | return true; |
| 2792 | } |
| 2793 | } |
| 2794 | return false; |
| 2795 | } |
| 2796 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2797 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2798 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2799 | if (applicationHandle != nullptr) { |
| 2800 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2801 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2802 | } else { |
| 2803 | return applicationHandle->getName(); |
| 2804 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2805 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2806 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2807 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2808 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2809 | } |
| 2810 | } |
| 2811 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2812 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2813 | if (!isUserActivityEvent(eventEntry)) { |
| 2814 | // 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] | 2815 | return; |
| 2816 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2817 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2818 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2819 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2820 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
| 2821 | if (info->inputFeatures.test(WindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2822 | if (DEBUG_DISPATCH_CYCLE) { |
| 2823 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2824 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2825 | return; |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2830 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2831 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2832 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2833 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2834 | return; |
| 2835 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2836 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2837 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2838 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2839 | } |
| 2840 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2841 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2842 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2843 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2844 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2845 | return; |
| 2846 | } |
| 2847 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2848 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2849 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2850 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2851 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2852 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2853 | break; |
| 2854 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2855 | } |
| 2856 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2857 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2858 | REQUIRES(mLock) { |
| 2859 | scoped_unlock unlock(mLock); |
| 2860 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2861 | }; |
| 2862 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2866 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2867 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2868 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2869 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2870 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2871 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2872 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2873 | ATRACE_NAME(message.c_str()); |
| 2874 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2875 | if (DEBUG_DISPATCH_CYCLE) { |
| 2876 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2877 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2878 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2879 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2880 | inputTarget.getPointerInfoString().c_str()); |
| 2881 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2882 | |
| 2883 | // Skip this event if the connection status is not normal. |
| 2884 | // 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] | 2885 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2886 | if (DEBUG_DISPATCH_CYCLE) { |
| 2887 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2888 | connection->getInputChannelName().c_str(), |
| 2889 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2890 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2891 | return; |
| 2892 | } |
| 2893 | |
| 2894 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2895 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2896 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2897 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2898 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2899 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2900 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2901 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2902 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2903 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2904 | if (!splitMotionEntry) { |
| 2905 | return; // split event was dropped |
| 2906 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2907 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2908 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2909 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2910 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2911 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2912 | if (DEBUG_FOCUS) { |
| 2913 | ALOGD("channel '%s' ~ Split motion event.", |
| 2914 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2915 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2916 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2917 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2918 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2919 | return; |
| 2920 | } |
| 2921 | } |
| 2922 | |
| 2923 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2924 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2925 | } |
| 2926 | |
| 2927 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2928 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2929 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2930 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2931 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2932 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2933 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2934 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2935 | ATRACE_NAME(message.c_str()); |
| 2936 | } |
| 2937 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2938 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2939 | |
| 2940 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2941 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2942 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2943 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2944 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2945 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2946 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2947 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2948 | InputTarget::FLAG_DISPATCH_AS_IS); |
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_SLIPPERY_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_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2953 | |
| 2954 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2955 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2956 | startDispatchCycleLocked(currentTime, connection); |
| 2957 | } |
| 2958 | } |
| 2959 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2960 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2961 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2962 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2963 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2964 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2965 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2966 | connection->getInputChannelName().c_str(), |
| 2967 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2968 | ATRACE_NAME(message.c_str()); |
| 2969 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2970 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2971 | if (!(inputTargetFlags & dispatchMode)) { |
| 2972 | return; |
| 2973 | } |
| 2974 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2975 | |
| 2976 | // This is a new event. |
| 2977 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2978 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2979 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2980 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2981 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2982 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2983 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2984 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2985 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2986 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2987 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2988 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2989 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2990 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2991 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2992 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2993 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2994 | if (DEBUG_DISPATCH_CYCLE) { |
| 2995 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 2996 | "event", |
| 2997 | connection->getInputChannelName().c_str()); |
| 2998 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2999 | return; // skip the inconsistent event |
| 3000 | } |
| 3001 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3002 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3003 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3004 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3005 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3006 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3007 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3008 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3009 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3010 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3011 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3012 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 3013 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 3014 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3015 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 3016 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3017 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 3018 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 3019 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 3020 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3021 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3022 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3023 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3024 | } |
| 3025 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3026 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3027 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3028 | if (DEBUG_DISPATCH_CYCLE) { |
| 3029 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3030 | "enter event", |
| 3031 | connection->getInputChannelName().c_str()); |
| 3032 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3033 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3034 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3035 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3036 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3037 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3038 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3039 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3040 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3041 | } |
| 3042 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3043 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3044 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3045 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3046 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3047 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3048 | if (DEBUG_DISPATCH_CYCLE) { |
| 3049 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3050 | "event", |
| 3051 | connection->getInputChannelName().c_str()); |
| 3052 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3053 | return; // skip the inconsistent event |
| 3054 | } |
| 3055 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3056 | dispatchEntry->resolvedEventId = |
| 3057 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3058 | ? mIdGenerator.nextId() |
| 3059 | : motionEntry.id; |
| 3060 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3061 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3062 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3063 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3064 | ATRACE_NAME(message.c_str()); |
| 3065 | } |
| 3066 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3067 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3068 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3069 | // Skip reporting pointer down outside focus to the policy. |
| 3070 | break; |
| 3071 | } |
| 3072 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3073 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3074 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3075 | |
| 3076 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3077 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3078 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3079 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3080 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3081 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3082 | break; |
| 3083 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3084 | case EventEntry::Type::SENSOR: { |
| 3085 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3086 | break; |
| 3087 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3088 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3089 | case EventEntry::Type::DEVICE_RESET: { |
| 3090 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3091 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3092 | break; |
| 3093 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3094 | } |
| 3095 | |
| 3096 | // Remember that we are waiting for this dispatch to complete. |
| 3097 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3098 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3102 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3103 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3104 | } |
| 3105 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3106 | /** |
| 3107 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3108 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3109 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3110 | * We will see both launcher and wallpaper in that list. |
| 3111 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3112 | * until the set of interacted connections changes. |
| 3113 | * |
| 3114 | * The following items are skipped, to reduce the logspam: |
| 3115 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3116 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3117 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3118 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3119 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3120 | */ |
| 3121 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3122 | const std::vector<InputTarget>& targets) { |
| 3123 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3124 | if (entry.type == EventEntry::Type::KEY) { |
| 3125 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3126 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3127 | return; |
| 3128 | } |
| 3129 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3130 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3131 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3132 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3133 | return; |
| 3134 | } |
| 3135 | } else { |
| 3136 | return; // Not a key or a motion |
| 3137 | } |
| 3138 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3139 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3140 | std::vector<sp<Connection>> newConnections; |
| 3141 | for (const InputTarget& target : targets) { |
| 3142 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3143 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3144 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3145 | } |
| 3146 | |
| 3147 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3148 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3149 | if (connection == nullptr) { |
| 3150 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3151 | } |
| 3152 | newConnectionTokens.insert(std::move(token)); |
| 3153 | newConnections.emplace_back(connection); |
| 3154 | } |
| 3155 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3156 | return; // no change |
| 3157 | } |
| 3158 | mInteractionConnectionTokens = newConnectionTokens; |
| 3159 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3160 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3161 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3162 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3163 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3164 | std::string message = "Interaction with: " + targetList; |
| 3165 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3166 | message += "<none>"; |
| 3167 | } |
| 3168 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3169 | } |
| 3170 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3171 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3172 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3173 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3174 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3175 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3176 | return; |
| 3177 | } |
| 3178 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3179 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3180 | if (focusedToken == token) { |
| 3181 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3182 | return; |
| 3183 | } |
| 3184 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3185 | auto command = [this, token]() REQUIRES(mLock) { |
| 3186 | scoped_unlock unlock(mLock); |
| 3187 | mPolicy->onPointerDownOutsideFocus(token); |
| 3188 | }; |
| 3189 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3193 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3194 | if (ATRACE_ENABLED()) { |
| 3195 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3196 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3197 | ATRACE_NAME(message.c_str()); |
| 3198 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3199 | if (DEBUG_DISPATCH_CYCLE) { |
| 3200 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3201 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3202 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3203 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3204 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3205 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3206 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3207 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3208 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3209 | |
| 3210 | // Publish the event. |
| 3211 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3212 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3213 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3214 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3215 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3216 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3217 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3218 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3219 | status = connection->inputPublisher |
| 3220 | .publishKeyEvent(dispatchEntry->seq, |
| 3221 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3222 | keyEntry.source, keyEntry.displayId, |
| 3223 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3224 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3225 | keyEntry.scanCode, keyEntry.metaState, |
| 3226 | keyEntry.repeatCount, keyEntry.downTime, |
| 3227 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3228 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3229 | } |
| 3230 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3231 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3232 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3233 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3234 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3235 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3236 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3237 | // 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] | 3238 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3239 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3240 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3241 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3242 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3243 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3244 | // Don't apply window scale here since we don't want scale to affect raw |
| 3245 | // coordinates. The scale will be sent back to the client and applied |
| 3246 | // later when requesting relative coordinates. |
| 3247 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3248 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3249 | } |
| 3250 | usingCoords = scaledCoords; |
| 3251 | } |
| 3252 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3253 | // We don't want the dispatch target to know. |
| 3254 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3255 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3256 | scaledCoords[i].clear(); |
| 3257 | } |
| 3258 | usingCoords = scaledCoords; |
| 3259 | } |
| 3260 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3261 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3262 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3263 | |
| 3264 | // Publish the motion event. |
| 3265 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3266 | .publishMotionEvent(dispatchEntry->seq, |
| 3267 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3268 | motionEntry.deviceId, motionEntry.source, |
| 3269 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3270 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3271 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3272 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3273 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3274 | motionEntry.buttonState, |
| 3275 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3276 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3277 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3278 | motionEntry.xCursorPosition, |
| 3279 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3280 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3281 | motionEntry.downTime, motionEntry.eventTime, |
| 3282 | motionEntry.pointerCount, |
| 3283 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3284 | break; |
| 3285 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3286 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3287 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3288 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3289 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3290 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3291 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3292 | break; |
| 3293 | } |
| 3294 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3295 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3296 | const TouchModeEntry& touchModeEntry = |
| 3297 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3298 | status = connection->inputPublisher |
| 3299 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3300 | touchModeEntry.inTouchMode); |
| 3301 | |
| 3302 | break; |
| 3303 | } |
| 3304 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3305 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3306 | const auto& captureEntry = |
| 3307 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3308 | status = connection->inputPublisher |
| 3309 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3310 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3311 | break; |
| 3312 | } |
| 3313 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3314 | case EventEntry::Type::DRAG: { |
| 3315 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3316 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3317 | dragEntry.id, dragEntry.x, |
| 3318 | dragEntry.y, |
| 3319 | dragEntry.isExiting); |
| 3320 | break; |
| 3321 | } |
| 3322 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3323 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3324 | case EventEntry::Type::DEVICE_RESET: |
| 3325 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3326 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3327 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3328 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3329 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | // Check the result. |
| 3333 | if (status) { |
| 3334 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3335 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3336 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3337 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3338 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3339 | "event to it, status=%s(%d)", |
| 3340 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3341 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3342 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3343 | } else { |
| 3344 | // Pipe is full and we are waiting for the app to finish process some events |
| 3345 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3346 | if (DEBUG_DISPATCH_CYCLE) { |
| 3347 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3348 | "waiting for the application to catch up", |
| 3349 | connection->getInputChannelName().c_str()); |
| 3350 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3351 | } |
| 3352 | } else { |
| 3353 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3354 | "status=%s(%d)", |
| 3355 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3356 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3357 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3358 | } |
| 3359 | return; |
| 3360 | } |
| 3361 | |
| 3362 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3363 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3364 | connection->outboundQueue.end(), |
| 3365 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3366 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3367 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3368 | if (connection->responsive) { |
| 3369 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3370 | connection->inputChannel->getConnectionToken()); |
| 3371 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3372 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3373 | } |
| 3374 | } |
| 3375 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3376 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3377 | size_t size; |
| 3378 | switch (event.type) { |
| 3379 | case VerifiedInputEvent::Type::KEY: { |
| 3380 | size = sizeof(VerifiedKeyEvent); |
| 3381 | break; |
| 3382 | } |
| 3383 | case VerifiedInputEvent::Type::MOTION: { |
| 3384 | size = sizeof(VerifiedMotionEvent); |
| 3385 | break; |
| 3386 | } |
| 3387 | } |
| 3388 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3389 | return mHmacKeyManager.sign(start, size); |
| 3390 | } |
| 3391 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3392 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3393 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3394 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3395 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3396 | // Only sign events up and down events as the purely move events |
| 3397 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3398 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3399 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3400 | |
| 3401 | VerifiedMotionEvent verifiedEvent = |
| 3402 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3403 | verifiedEvent.actionMasked = actionMasked; |
| 3404 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3405 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3409 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3410 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3411 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3412 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3413 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3414 | } |
| 3415 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3416 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3417 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3418 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3419 | if (DEBUG_DISPATCH_CYCLE) { |
| 3420 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3421 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3422 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3423 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3424 | if (connection->status == Connection::Status::BROKEN || |
| 3425 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3426 | return; |
| 3427 | } |
| 3428 | |
| 3429 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3430 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3431 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3432 | }; |
| 3433 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3437 | const sp<Connection>& connection, |
| 3438 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3439 | if (DEBUG_DISPATCH_CYCLE) { |
| 3440 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3441 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3442 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3443 | |
| 3444 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3445 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3446 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3447 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3448 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3449 | |
| 3450 | // The connection appears to be unrecoverably broken. |
| 3451 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3452 | if (connection->status == Connection::Status::NORMAL) { |
| 3453 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3454 | |
| 3455 | if (notify) { |
| 3456 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3457 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3458 | connection->getInputChannelName().c_str()); |
| 3459 | |
| 3460 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3461 | scoped_unlock unlock(mLock); |
| 3462 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3463 | }; |
| 3464 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3465 | } |
| 3466 | } |
| 3467 | } |
| 3468 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3469 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3470 | while (!queue.empty()) { |
| 3471 | DispatchEntry* dispatchEntry = queue.front(); |
| 3472 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3473 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3474 | } |
| 3475 | } |
| 3476 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3477 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3478 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3479 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3480 | } |
| 3481 | delete dispatchEntry; |
| 3482 | } |
| 3483 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3484 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3485 | std::scoped_lock _l(mLock); |
| 3486 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3487 | if (connection == nullptr) { |
| 3488 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3489 | connectionToken.get(), events); |
| 3490 | return 0; // remove the callback |
| 3491 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3492 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3493 | bool notify; |
| 3494 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3495 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3496 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3497 | "events=0x%x", |
| 3498 | connection->getInputChannelName().c_str(), events); |
| 3499 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3500 | } |
| 3501 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3502 | nsecs_t currentTime = now(); |
| 3503 | bool gotOne = false; |
| 3504 | status_t status = OK; |
| 3505 | for (;;) { |
| 3506 | Result<InputPublisher::ConsumerResponse> result = |
| 3507 | connection->inputPublisher.receiveConsumerResponse(); |
| 3508 | if (!result.ok()) { |
| 3509 | status = result.error().code(); |
| 3510 | break; |
| 3511 | } |
| 3512 | |
| 3513 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3514 | const InputPublisher::Finished& finish = |
| 3515 | std::get<InputPublisher::Finished>(*result); |
| 3516 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3517 | finish.consumeTime); |
| 3518 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3519 | if (shouldReportMetricsForConnection(*connection)) { |
| 3520 | const InputPublisher::Timeline& timeline = |
| 3521 | std::get<InputPublisher::Timeline>(*result); |
| 3522 | mLatencyTracker |
| 3523 | .trackGraphicsLatency(timeline.inputEventId, |
| 3524 | connection->inputChannel->getConnectionToken(), |
| 3525 | std::move(timeline.graphicsTimeline)); |
| 3526 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3527 | } |
| 3528 | gotOne = true; |
| 3529 | } |
| 3530 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3531 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3532 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3533 | return 1; |
| 3534 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3535 | } |
| 3536 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3537 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3538 | if (notify) { |
| 3539 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3540 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3541 | status); |
| 3542 | } |
| 3543 | } else { |
| 3544 | // Monitor channels are never explicitly unregistered. |
| 3545 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3546 | const bool stillHaveWindowHandle = |
| 3547 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3548 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3549 | if (notify) { |
| 3550 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3551 | connection->getInputChannelName().c_str(), events); |
| 3552 | } |
| 3553 | } |
| 3554 | |
| 3555 | // Remove the channel. |
| 3556 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3557 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3558 | } |
| 3559 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3560 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3561 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3562 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3563 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3564 | } |
| 3565 | } |
| 3566 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3567 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3568 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3569 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3570 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3571 | } |
| 3572 | |
| 3573 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3574 | const CancelationOptions& options, |
| 3575 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3576 | for (const auto& it : monitorsByDisplay) { |
| 3577 | const std::vector<Monitor>& monitors = it.second; |
| 3578 | for (const Monitor& monitor : monitors) { |
| 3579 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3580 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3581 | } |
| 3582 | } |
| 3583 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3584 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3585 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3586 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3587 | if (connection == nullptr) { |
| 3588 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3589 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3590 | |
| 3591 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3592 | } |
| 3593 | |
| 3594 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3595 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3596 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3597 | return; |
| 3598 | } |
| 3599 | |
| 3600 | nsecs_t currentTime = now(); |
| 3601 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3602 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3603 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3604 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3605 | if (cancelationEvents.empty()) { |
| 3606 | return; |
| 3607 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3608 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3609 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3610 | "with reality: %s, mode=%d.", |
| 3611 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3612 | options.mode); |
| 3613 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3614 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3615 | std::string reason = std::string("reason=").append(options.reason); |
| 3616 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3617 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3618 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3619 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3620 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3621 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3622 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3623 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3624 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3625 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3626 | } |
| 3627 | target.inputChannel = connection->inputChannel; |
| 3628 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3629 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3630 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3631 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3632 | switch (cancelationEventEntry->type) { |
| 3633 | case EventEntry::Type::KEY: { |
| 3634 | logOutboundKeyDetails("cancel - ", |
| 3635 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3636 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3637 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3638 | case EventEntry::Type::MOTION: { |
| 3639 | logOutboundMotionDetails("cancel - ", |
| 3640 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3641 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3642 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3643 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3644 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3645 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3646 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3647 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3648 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3649 | break; |
| 3650 | } |
| 3651 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3652 | case EventEntry::Type::DEVICE_RESET: |
| 3653 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3654 | 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] | 3655 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3656 | break; |
| 3657 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3658 | } |
| 3659 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3660 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3661 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3662 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3663 | |
| 3664 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3665 | } |
| 3666 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3667 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3668 | const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3669 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3670 | return; |
| 3671 | } |
| 3672 | |
| 3673 | nsecs_t currentTime = now(); |
| 3674 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3675 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3676 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3677 | |
| 3678 | if (downEvents.empty()) { |
| 3679 | return; |
| 3680 | } |
| 3681 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3682 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3683 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3684 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3685 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3686 | |
| 3687 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3688 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3689 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3690 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3691 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3692 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3693 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3694 | } |
| 3695 | target.inputChannel = connection->inputChannel; |
| 3696 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3697 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3698 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3699 | switch (downEventEntry->type) { |
| 3700 | case EventEntry::Type::MOTION: { |
| 3701 | logOutboundMotionDetails("down - ", |
| 3702 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3703 | break; |
| 3704 | } |
| 3705 | |
| 3706 | case EventEntry::Type::KEY: |
| 3707 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3708 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3709 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3710 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3711 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3712 | case EventEntry::Type::SENSOR: |
| 3713 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3714 | 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] | 3715 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3716 | break; |
| 3717 | } |
| 3718 | } |
| 3719 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3720 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3721 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3722 | } |
| 3723 | |
| 3724 | startDispatchCycleLocked(currentTime, connection); |
| 3725 | } |
| 3726 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3727 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3728 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3729 | ALOG_ASSERT(pointerIds.value != 0); |
| 3730 | |
| 3731 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3732 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3733 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3734 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3735 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3736 | uint32_t splitPointerCount = 0; |
| 3737 | |
| 3738 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3739 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3740 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3741 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3742 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3743 | if (pointerIds.hasBit(pointerId)) { |
| 3744 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3745 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3746 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3747 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3748 | splitPointerCount += 1; |
| 3749 | } |
| 3750 | } |
| 3751 | |
| 3752 | if (splitPointerCount != pointerIds.count()) { |
| 3753 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3754 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3755 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3756 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3757 | // in this way. |
| 3758 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3759 | "we expected there to be %d pointers. This probably means we received " |
| 3760 | "a broken sequence of pointer ids from the input device.", |
| 3761 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3762 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3763 | } |
| 3764 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3765 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3766 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3767 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3768 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3769 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3770 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3771 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3772 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3773 | if (pointerIds.hasBit(pointerId)) { |
| 3774 | if (pointerIds.count() == 1) { |
| 3775 | // The first/last pointer went down/up. |
| 3776 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3777 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3778 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3779 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3780 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3781 | } else { |
| 3782 | // A secondary pointer went down/up. |
| 3783 | uint32_t splitPointerIndex = 0; |
| 3784 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3785 | splitPointerIndex += 1; |
| 3786 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3787 | action = maskedAction | |
| 3788 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3789 | } |
| 3790 | } else { |
| 3791 | // An unrelated pointer changed. |
| 3792 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3793 | } |
| 3794 | } |
| 3795 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3796 | int32_t newId = mIdGenerator.nextId(); |
| 3797 | if (ATRACE_ENABLED()) { |
| 3798 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3799 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3800 | originalMotionEntry.id, newId); |
| 3801 | ATRACE_NAME(message.c_str()); |
| 3802 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3803 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3804 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3805 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3806 | originalMotionEntry.displayId, |
| 3807 | originalMotionEntry.policyFlags, action, |
| 3808 | originalMotionEntry.actionButton, |
| 3809 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3810 | originalMotionEntry.buttonState, |
| 3811 | originalMotionEntry.classification, |
| 3812 | originalMotionEntry.edgeFlags, |
| 3813 | originalMotionEntry.xPrecision, |
| 3814 | originalMotionEntry.yPrecision, |
| 3815 | originalMotionEntry.xCursorPosition, |
| 3816 | originalMotionEntry.yCursorPosition, |
| 3817 | originalMotionEntry.downTime, splitPointerCount, |
Alex Chau | f7c99f3 | 2021-12-03 10:37:54 +0000 | [diff] [blame] | 3818 | splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3819 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3820 | if (originalMotionEntry.injectionState) { |
| 3821 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3822 | splitMotionEntry->injectionState->refCount += 1; |
| 3823 | } |
| 3824 | |
| 3825 | return splitMotionEntry; |
| 3826 | } |
| 3827 | |
| 3828 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3829 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3830 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3831 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3832 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3833 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3834 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3835 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3836 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3837 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3838 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3839 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3840 | } // release lock |
| 3841 | |
| 3842 | if (needWake) { |
| 3843 | mLooper->wake(); |
| 3844 | } |
| 3845 | } |
| 3846 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3847 | /** |
| 3848 | * If one of the meta shortcuts is detected, process them here: |
| 3849 | * Meta + Backspace -> generate BACK |
| 3850 | * Meta + Enter -> generate HOME |
| 3851 | * This will potentially overwrite keyCode and metaState. |
| 3852 | */ |
| 3853 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3854 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3855 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3856 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3857 | if (keyCode == AKEYCODE_DEL) { |
| 3858 | newKeyCode = AKEYCODE_BACK; |
| 3859 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3860 | newKeyCode = AKEYCODE_HOME; |
| 3861 | } |
| 3862 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3863 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3864 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3865 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3866 | keyCode = newKeyCode; |
| 3867 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3868 | } |
| 3869 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3870 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3871 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3872 | // 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] | 3873 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3874 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3875 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3876 | if (replacementIt != mReplacedKeys.end()) { |
| 3877 | keyCode = replacementIt->second; |
| 3878 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3879 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3880 | } |
| 3881 | } |
| 3882 | } |
| 3883 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3884 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3885 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3886 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3887 | "policyFlags=0x%x, action=0x%x, " |
| 3888 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3889 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3890 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3891 | args->downTime); |
| 3892 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3893 | if (!validateKeyEvent(args->action)) { |
| 3894 | return; |
| 3895 | } |
| 3896 | |
| 3897 | uint32_t policyFlags = args->policyFlags; |
| 3898 | int32_t flags = args->flags; |
| 3899 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3900 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3901 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3902 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3903 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3904 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3905 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3906 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3907 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3908 | metaState |= AMETA_FUNCTION_ON; |
| 3909 | } |
| 3910 | |
| 3911 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3912 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3913 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3914 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3915 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3916 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3917 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3918 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3919 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3920 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3921 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3922 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3923 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3924 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3925 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3926 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3927 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3928 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3929 | { // acquire lock |
| 3930 | mLock.lock(); |
| 3931 | |
| 3932 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3933 | mLock.unlock(); |
| 3934 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3935 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3936 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3937 | return; // event was consumed by the filter |
| 3938 | } |
| 3939 | |
| 3940 | mLock.lock(); |
| 3941 | } |
| 3942 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3943 | std::unique_ptr<KeyEntry> newEntry = |
| 3944 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3945 | args->displayId, policyFlags, args->action, flags, |
| 3946 | keyCode, args->scanCode, metaState, repeatCount, |
| 3947 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3948 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3949 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3950 | mLock.unlock(); |
| 3951 | } // release lock |
| 3952 | |
| 3953 | if (needWake) { |
| 3954 | mLooper->wake(); |
| 3955 | } |
| 3956 | } |
| 3957 | |
| 3958 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3959 | return mInputFilterEnabled; |
| 3960 | } |
| 3961 | |
| 3962 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3963 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3964 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3965 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 3966 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3967 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 3968 | "yCursorPosition=%f, downTime=%" PRId64, |
| 3969 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3970 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3971 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3972 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 3973 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3974 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 3975 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3976 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3977 | "orientation=%f", |
| 3978 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3979 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3980 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3981 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3982 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3983 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3984 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3985 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3986 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3987 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 3988 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3989 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3990 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3991 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3992 | return; |
| 3993 | } |
| 3994 | |
| 3995 | uint32_t policyFlags = args->policyFlags; |
| 3996 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3997 | |
| 3998 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3999 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4000 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4001 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4002 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4003 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4004 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4005 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4006 | { // acquire lock |
| 4007 | mLock.lock(); |
| 4008 | |
| 4009 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4010 | ui::Transform displayTransform; |
| 4011 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4012 | displayTransform = it->second.transform; |
| 4013 | } |
| 4014 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4015 | mLock.unlock(); |
| 4016 | |
| 4017 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4018 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4019 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4020 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4021 | displayTransform, args->xPrecision, args->yPrecision, |
| 4022 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4023 | args->downTime, args->eventTime, args->pointerCount, |
| 4024 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4025 | |
| 4026 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4027 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4028 | return; // event was consumed by the filter |
| 4029 | } |
| 4030 | |
| 4031 | mLock.lock(); |
| 4032 | } |
| 4033 | |
| 4034 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4035 | std::unique_ptr<MotionEntry> newEntry = |
| 4036 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4037 | args->source, args->displayId, policyFlags, |
| 4038 | args->action, args->actionButton, args->flags, |
| 4039 | args->metaState, args->buttonState, |
| 4040 | args->classification, args->edgeFlags, |
| 4041 | args->xPrecision, args->yPrecision, |
| 4042 | args->xCursorPosition, args->yCursorPosition, |
| 4043 | args->downTime, args->pointerCount, |
Alex Chau | f7c99f3 | 2021-12-03 10:37:54 +0000 | [diff] [blame] | 4044 | args->pointerProperties, args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4045 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4046 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4047 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4048 | !mInputFilterEnabled) { |
| 4049 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4050 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4051 | } |
| 4052 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4053 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4054 | mLock.unlock(); |
| 4055 | } // release lock |
| 4056 | |
| 4057 | if (needWake) { |
| 4058 | mLooper->wake(); |
| 4059 | } |
| 4060 | } |
| 4061 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4062 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4063 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4064 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4065 | " sensorType=%s", |
| 4066 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4067 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4068 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4069 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4070 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4071 | { // acquire lock |
| 4072 | mLock.lock(); |
| 4073 | |
| 4074 | // Just enqueue a new sensor event. |
| 4075 | std::unique_ptr<SensorEntry> newEntry = |
| 4076 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4077 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4078 | args->sensorType, args->accuracy, |
| 4079 | args->accuracyChanged, args->values); |
| 4080 | |
| 4081 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4082 | mLock.unlock(); |
| 4083 | } // release lock |
| 4084 | |
| 4085 | if (needWake) { |
| 4086 | mLooper->wake(); |
| 4087 | } |
| 4088 | } |
| 4089 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4090 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4091 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4092 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4093 | args->deviceId, args->isOn); |
| 4094 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4095 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4096 | } |
| 4097 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4098 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4099 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4103 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4104 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4105 | "switchMask=0x%08x", |
| 4106 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4107 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4108 | |
| 4109 | uint32_t policyFlags = args->policyFlags; |
| 4110 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4111 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4115 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4116 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4117 | args->deviceId); |
| 4118 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4119 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4120 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4121 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4122 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4123 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4124 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4125 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4126 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4127 | } // release lock |
| 4128 | |
| 4129 | if (needWake) { |
| 4130 | mLooper->wake(); |
| 4131 | } |
| 4132 | } |
| 4133 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4134 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4135 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4136 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4137 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4138 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4139 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4140 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4141 | { // acquire lock |
| 4142 | std::scoped_lock _l(mLock); |
| 4143 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4144 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4145 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4146 | } // release lock |
| 4147 | |
| 4148 | if (needWake) { |
| 4149 | mLooper->wake(); |
| 4150 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4151 | } |
| 4152 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4153 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4154 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4155 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4156 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4157 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 4158 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4159 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
| 4160 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4161 | 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] | 4162 | |
| 4163 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4164 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4165 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4166 | } |
| 4167 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4168 | // 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] | 4169 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4170 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4171 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4172 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4173 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4174 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4175 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4176 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4179 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4180 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4181 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4182 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4183 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4184 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4185 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4186 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4187 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4188 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4189 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4190 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4191 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4192 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4193 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4194 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4195 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4196 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4197 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4198 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4199 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4200 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4201 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4202 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4203 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4204 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4205 | |
| 4206 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4207 | android::base::Timer t; |
| 4208 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4209 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4210 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4211 | std::to_string(t.duration().count()).c_str()); |
| 4212 | } |
| 4213 | } |
| 4214 | |
| 4215 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4216 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4217 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4218 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4219 | incomingKey.getDisplayId(), policyFlags, action, |
| 4220 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4221 | incomingKey.getRepeatCount(), |
| 4222 | incomingKey.getDownTime()); |
| 4223 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4224 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4225 | } |
| 4226 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4227 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4228 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4229 | const int32_t action = motionEvent.getAction(); |
| 4230 | const bool isPointerEvent = |
| 4231 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4232 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4233 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4234 | ? ADISPLAY_ID_DEFAULT |
| 4235 | : event->getDisplayId(); |
| 4236 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4237 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4238 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4239 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4240 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4241 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4245 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4246 | android::base::Timer t; |
| 4247 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4248 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4249 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4250 | std::to_string(t.duration().count()).c_str()); |
| 4251 | } |
| 4252 | } |
| 4253 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4254 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4255 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4256 | } |
| 4257 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4258 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4259 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4260 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4261 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4262 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4263 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4264 | displayId, policyFlags, action, actionButton, |
| 4265 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4266 | motionEvent.getButtonState(), |
| 4267 | motionEvent.getClassification(), |
| 4268 | motionEvent.getEdgeFlags(), |
| 4269 | motionEvent.getXPrecision(), |
| 4270 | motionEvent.getYPrecision(), |
| 4271 | motionEvent.getRawXCursorPosition(), |
| 4272 | motionEvent.getRawYCursorPosition(), |
| 4273 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Alex Chau | 663fc83 | 2021-12-03 10:37:54 +0000 | [diff] [blame] | 4274 | pointerProperties, samplePointerCoords, |
| 4275 | motionEvent.getXOffset(), |
| 4276 | motionEvent.getYOffset()); |
| 4277 | transformMotionEntryForInjectionLocked(*injectedEntry); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4278 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4279 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4280 | sampleEventTimes += 1; |
| 4281 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4282 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4283 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4284 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4285 | displayId, policyFlags, action, actionButton, |
| 4286 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4287 | motionEvent.getButtonState(), |
| 4288 | motionEvent.getClassification(), |
| 4289 | motionEvent.getEdgeFlags(), |
| 4290 | motionEvent.getXPrecision(), |
| 4291 | motionEvent.getYPrecision(), |
| 4292 | motionEvent.getRawXCursorPosition(), |
| 4293 | motionEvent.getRawYCursorPosition(), |
| 4294 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4295 | uint32_t(pointerCount), pointerProperties, |
Alex Chau | 663fc83 | 2021-12-03 10:37:54 +0000 | [diff] [blame] | 4296 | samplePointerCoords, motionEvent.getXOffset(), |
| 4297 | motionEvent.getYOffset()); |
| 4298 | transformMotionEntryForInjectionLocked(*nextInjectedEntry); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4299 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4300 | } |
| 4301 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4302 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4303 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4304 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4305 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4306 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4310 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4311 | injectionState->injectionIsAsync = true; |
| 4312 | } |
| 4313 | |
| 4314 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4315 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4316 | |
| 4317 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4318 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4319 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4320 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | mLock.unlock(); |
| 4324 | |
| 4325 | if (needWake) { |
| 4326 | mLooper->wake(); |
| 4327 | } |
| 4328 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4329 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4330 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4331 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4332 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4333 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4334 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4335 | } else { |
| 4336 | for (;;) { |
| 4337 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4338 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4339 | break; |
| 4340 | } |
| 4341 | |
| 4342 | nsecs_t remainingTimeout = endTime - now(); |
| 4343 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4344 | if (DEBUG_INJECTION) { |
| 4345 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4346 | "to become available."); |
| 4347 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4348 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4349 | break; |
| 4350 | } |
| 4351 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4352 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4353 | } |
| 4354 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4355 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4356 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4357 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4358 | if (DEBUG_INJECTION) { |
| 4359 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4360 | injectionState->pendingForegroundDispatches); |
| 4361 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4362 | nsecs_t remainingTimeout = endTime - now(); |
| 4363 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4364 | if (DEBUG_INJECTION) { |
| 4365 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4366 | "dispatches to finish."); |
| 4367 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4368 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4369 | break; |
| 4370 | } |
| 4371 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4372 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4373 | } |
| 4374 | } |
| 4375 | } |
| 4376 | |
| 4377 | injectionState->release(); |
| 4378 | } // release lock |
| 4379 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4380 | if (DEBUG_INJECTION) { |
| 4381 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
| 4382 | injectionResult, injectorPid, injectorUid); |
| 4383 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4384 | |
| 4385 | return injectionResult; |
| 4386 | } |
| 4387 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4388 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4389 | std::array<uint8_t, 32> calculatedHmac; |
| 4390 | std::unique_ptr<VerifiedInputEvent> result; |
| 4391 | switch (event.getType()) { |
| 4392 | case AINPUT_EVENT_TYPE_KEY: { |
| 4393 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4394 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4395 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4396 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4397 | break; |
| 4398 | } |
| 4399 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4400 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4401 | VerifiedMotionEvent verifiedMotionEvent = |
| 4402 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4403 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4404 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4405 | break; |
| 4406 | } |
| 4407 | default: { |
| 4408 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4409 | return nullptr; |
| 4410 | } |
| 4411 | } |
| 4412 | if (calculatedHmac == INVALID_HMAC) { |
| 4413 | return nullptr; |
| 4414 | } |
| 4415 | if (calculatedHmac != event.getHmac()) { |
| 4416 | return nullptr; |
| 4417 | } |
| 4418 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4419 | } |
| 4420 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4421 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4422 | return injectorUid == 0 || |
| 4423 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4424 | } |
| 4425 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4426 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4427 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4428 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4429 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4430 | if (DEBUG_INJECTION) { |
| 4431 | ALOGD("Setting input event injection result to %d. " |
| 4432 | "injectorPid=%d, injectorUid=%d", |
| 4433 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
| 4434 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4435 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4436 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4437 | // Log the outcome since the injector did not wait for the injection result. |
| 4438 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4439 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4440 | ALOGV("Asynchronous input event injection succeeded."); |
| 4441 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4442 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4443 | ALOGW("Asynchronous input event injection failed."); |
| 4444 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4445 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4446 | ALOGW("Asynchronous input event injection permission denied."); |
| 4447 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4448 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4449 | ALOGW("Asynchronous input event injection timed out."); |
| 4450 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4451 | case InputEventInjectionResult::PENDING: |
| 4452 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4453 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4454 | } |
| 4455 | } |
| 4456 | |
| 4457 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4458 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4459 | } |
| 4460 | } |
| 4461 | |
Alex Chau | 663fc83 | 2021-12-03 10:37:54 +0000 | [diff] [blame] | 4462 | void InputDispatcher::transformMotionEntryForInjectionLocked(MotionEntry& entry) const { |
| 4463 | const bool isRelativeMouseEvent = isFromSource(entry.source, AINPUT_SOURCE_MOUSE_RELATIVE); |
| 4464 | if (!isRelativeMouseEvent && !isFromSource(entry.source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 4465 | return; |
| 4466 | } |
| 4467 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4468 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4469 | // display space, so we need to transform the injected events accordingly. |
| 4470 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4471 | if (it == mDisplayInfos.end()) return; |
Alex Chau | 663fc83 | 2021-12-03 10:37:54 +0000 | [diff] [blame] | 4472 | const auto& transformToDisplay = it->second.transform.inverse(); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4473 | |
| 4474 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 4475 | PointerCoords& pc = entry.pointerCoords[i]; |
Alex Chau | 663fc83 | 2021-12-03 10:37:54 +0000 | [diff] [blame] | 4476 | const auto xy = isRelativeMouseEvent |
| 4477 | ? transformWithoutTranslation(transformToDisplay, pc.getX(), pc.getY()) |
| 4478 | : transformToDisplay.transform(pc.getXYValue()); |
| 4479 | pc.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 4480 | pc.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4481 | |
Alex Chau | 663fc83 | 2021-12-03 10:37:54 +0000 | [diff] [blame] | 4482 | // Axes with relative values never represent points on a screen, so they should never have |
| 4483 | // translation applied. If a device does not report relative values, these values are always |
| 4484 | // 0, and will remain unaffected by the following operation. |
| 4485 | const auto rel = |
| 4486 | transformWithoutTranslation(transformToDisplay, |
| 4487 | pc.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 4488 | pc.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)); |
| 4489 | pc.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, rel.x); |
| 4490 | pc.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, rel.y); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4491 | } |
| 4492 | } |
| 4493 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4494 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4495 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4496 | if (injectionState) { |
| 4497 | injectionState->pendingForegroundDispatches += 1; |
| 4498 | } |
| 4499 | } |
| 4500 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4501 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4502 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4503 | if (injectionState) { |
| 4504 | injectionState->pendingForegroundDispatches -= 1; |
| 4505 | |
| 4506 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4507 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4508 | } |
| 4509 | } |
| 4510 | } |
| 4511 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4512 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4513 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4514 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4515 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4516 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4517 | } |
| 4518 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4519 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4520 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4521 | if (windowHandleToken == nullptr) { |
| 4522 | return nullptr; |
| 4523 | } |
| 4524 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4525 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4526 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4527 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4528 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4529 | return windowHandle; |
| 4530 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4531 | } |
| 4532 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4533 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4534 | } |
| 4535 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4536 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4537 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4538 | if (windowHandleToken == nullptr) { |
| 4539 | return nullptr; |
| 4540 | } |
| 4541 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4542 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4543 | if (windowHandle->getToken() == windowHandleToken) { |
| 4544 | return windowHandle; |
| 4545 | } |
| 4546 | } |
| 4547 | return nullptr; |
| 4548 | } |
| 4549 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4550 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4551 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4552 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4553 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4554 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4555 | if (handle->getId() == windowHandle->getId() && |
| 4556 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4557 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4558 | ALOGE("Found window %s in display %" PRId32 |
| 4559 | ", but it should belong to display %" PRId32, |
| 4560 | windowHandle->getName().c_str(), it.first, |
| 4561 | windowHandle->getInfo()->displayId); |
| 4562 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4563 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4564 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4565 | } |
| 4566 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4567 | return nullptr; |
| 4568 | } |
| 4569 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4570 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4571 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4572 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4573 | } |
| 4574 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4575 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4576 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4577 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4578 | windowHandle.getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4579 | if (connection != nullptr && noInputChannel) { |
| 4580 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4581 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4582 | return false; |
| 4583 | } |
| 4584 | |
| 4585 | if (connection == nullptr) { |
| 4586 | if (!noInputChannel) { |
| 4587 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4588 | } |
| 4589 | return false; |
| 4590 | } |
| 4591 | if (!connection->responsive) { |
| 4592 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4593 | return false; |
| 4594 | } |
| 4595 | return true; |
| 4596 | } |
| 4597 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4598 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4599 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4600 | auto connectionIt = mConnectionsByToken.find(token); |
| 4601 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4602 | return nullptr; |
| 4603 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4604 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4605 | } |
| 4606 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4607 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4608 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4609 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4610 | // Remove all handles on a display if there are no windows left. |
| 4611 | mWindowHandlesByDisplay.erase(displayId); |
| 4612 | return; |
| 4613 | } |
| 4614 | |
| 4615 | // Since we compare the pointer of input window handles across window updates, we need |
| 4616 | // 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] | 4617 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4618 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4619 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4620 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4621 | } |
| 4622 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4623 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4624 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4625 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4626 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4627 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4628 | info->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4629 | const bool canReceiveInput = !info->flags.test(WindowInfo::Flag::NOT_TOUCHABLE) || |
| 4630 | !info->flags.test(WindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4631 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4632 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4633 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4634 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4635 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | if (info->displayId != displayId) { |
| 4639 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4640 | handle->getName().c_str(), displayId, info->displayId); |
| 4641 | continue; |
| 4642 | } |
| 4643 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4644 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4645 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4646 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4647 | oldHandle->updateFrom(handle); |
| 4648 | newHandles.push_back(oldHandle); |
| 4649 | } else { |
| 4650 | newHandles.push_back(handle); |
| 4651 | } |
| 4652 | } |
| 4653 | |
| 4654 | // Insert or replace |
| 4655 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4656 | } |
| 4657 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4658 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4659 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4660 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4661 | { // acquire lock |
| 4662 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4663 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4664 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4665 | } |
| 4666 | } |
| 4667 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4668 | mLooper->wake(); |
| 4669 | } |
| 4670 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4671 | /** |
| 4672 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4673 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4674 | * If set an empty list, remove all handles from the specific display. |
| 4675 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4676 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4677 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4678 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4679 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4680 | if (DEBUG_FOCUS) { |
| 4681 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4682 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4683 | windowList += iwh->getName() + " "; |
| 4684 | } |
| 4685 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4686 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4687 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4688 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4689 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4690 | const bool noInputWindow = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4691 | window->getInfo()->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 | } |
| 4697 | } |
| 4698 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4699 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4700 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4701 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4702 | // Save the old windows' orientation by ID before it gets updated. |
| 4703 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4704 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4705 | oldWindowOrientations.emplace(handle->getId(), |
| 4706 | handle->getInfo()->transform.getOrientation()); |
| 4707 | } |
| 4708 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4709 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4710 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4711 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4712 | if (mLastHoverWindowHandle && |
| 4713 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4714 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4715 | mLastHoverWindowHandle = nullptr; |
| 4716 | } |
| 4717 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4718 | std::optional<FocusResolver::FocusChanges> changes = |
| 4719 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4720 | if (changes) { |
| 4721 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4722 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4723 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4724 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4725 | mTouchStatesByDisplay.find(displayId); |
| 4726 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4727 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4728 | for (size_t i = 0; i < state.windows.size();) { |
| 4729 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4730 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4731 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4732 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4733 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4734 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4735 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4736 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4737 | if (touchedInputChannel != nullptr) { |
| 4738 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4739 | "touched window was removed"); |
| 4740 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4741 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4742 | // well. |
| 4743 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
| 4744 | touchedWindow.windowHandle->getInfo()->hasWallpaper) { |
| 4745 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4746 | if (wallpaper != nullptr) { |
| 4747 | sp<Connection> wallpaperConnection = |
| 4748 | getConnectionLocked(wallpaper->getToken()); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4749 | if (wallpaperConnection != nullptr) { |
| 4750 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4751 | options); |
| 4752 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4753 | } |
| 4754 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4755 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4756 | state.windows.erase(state.windows.begin() + i); |
| 4757 | } else { |
| 4758 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4759 | } |
| 4760 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4761 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4762 | // 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] | 4763 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4764 | if (mDragState && |
| 4765 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4766 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4767 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4768 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4769 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4770 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4771 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4772 | // pointer events if necessary. |
| 4773 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4774 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4775 | if (newWindowHandle != nullptr && |
| 4776 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4777 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4778 | std::shared_ptr<InputChannel> inputChannel = |
| 4779 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4780 | if (inputChannel != nullptr) { |
| 4781 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4782 | "touched window's orientation changed"); |
| 4783 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4784 | } |
| 4785 | } |
| 4786 | } |
| 4787 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4788 | // Release information for windows that are no longer present. |
| 4789 | // This ensures that unused input channels are released promptly. |
| 4790 | // Otherwise, they might stick around until the window handle is destroyed |
| 4791 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4792 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4793 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4794 | if (DEBUG_FOCUS) { |
| 4795 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4796 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4797 | oldWindowHandle->releaseChannel(); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4798 | // To avoid making too many calls into the compat framework, only |
| 4799 | // check for window flags when windows are going away. |
| 4800 | // TODO(b/157929241) : delete this. This is only needed temporarily |
| 4801 | // in order to gather some data about the flag usage |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4802 | if (oldWindowHandle->getInfo()->flags.test(WindowInfo::Flag::SLIPPERY)) { |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4803 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", |
| 4804 | oldWindowHandle->getName().c_str()); |
| 4805 | if (mCompatService != nullptr) { |
| 4806 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, |
| 4807 | oldWindowHandle->getInfo()->ownerUid); |
| 4808 | } |
| 4809 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4810 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4811 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4815 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4816 | if (DEBUG_FOCUS) { |
| 4817 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4818 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4819 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4820 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4821 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4822 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4823 | } // release lock |
| 4824 | |
| 4825 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4826 | mLooper->wake(); |
| 4827 | } |
| 4828 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4829 | void InputDispatcher::setFocusedApplicationLocked( |
| 4830 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4831 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4832 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4833 | |
| 4834 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4835 | return; // This application is already focused. No need to wake up or change anything. |
| 4836 | } |
| 4837 | |
| 4838 | // Set the new application handle. |
| 4839 | if (inputApplicationHandle != nullptr) { |
| 4840 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4841 | } else { |
| 4842 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4843 | } |
| 4844 | |
| 4845 | // No matter what the old focused application was, stop waiting on it because it is |
| 4846 | // no longer focused. |
| 4847 | resetNoFocusedWindowTimeoutLocked(); |
| 4848 | } |
| 4849 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4850 | /** |
| 4851 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4852 | * the display not specified. |
| 4853 | * |
| 4854 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4855 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4856 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4857 | * display. The display-specified events won't be affected. |
| 4858 | */ |
| 4859 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4860 | if (DEBUG_FOCUS) { |
| 4861 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4862 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4863 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4864 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4865 | |
| 4866 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4867 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4868 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4869 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4870 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4871 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4872 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4873 | CancelationOptions |
| 4874 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4875 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4876 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4877 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4878 | } |
| 4879 | } |
| 4880 | mFocusedDisplayId = displayId; |
| 4881 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4882 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4883 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4884 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4885 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4886 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4887 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4888 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4889 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4890 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4891 | } |
| 4892 | } |
| 4893 | } |
| 4894 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4895 | if (DEBUG_FOCUS) { |
| 4896 | logDispatchStateLocked(); |
| 4897 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4898 | } // release lock |
| 4899 | |
| 4900 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4901 | mLooper->wake(); |
| 4902 | } |
| 4903 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4904 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4905 | if (DEBUG_FOCUS) { |
| 4906 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4907 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4908 | |
| 4909 | bool changed; |
| 4910 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4911 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4912 | |
| 4913 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4914 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4915 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | if (mDispatchEnabled && !enabled) { |
| 4919 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4920 | } |
| 4921 | |
| 4922 | mDispatchEnabled = enabled; |
| 4923 | mDispatchFrozen = frozen; |
| 4924 | changed = true; |
| 4925 | } else { |
| 4926 | changed = false; |
| 4927 | } |
| 4928 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4929 | if (DEBUG_FOCUS) { |
| 4930 | logDispatchStateLocked(); |
| 4931 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4932 | } // release lock |
| 4933 | |
| 4934 | if (changed) { |
| 4935 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4936 | mLooper->wake(); |
| 4937 | } |
| 4938 | } |
| 4939 | |
| 4940 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4941 | if (DEBUG_FOCUS) { |
| 4942 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4943 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4944 | |
| 4945 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4946 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4947 | |
| 4948 | if (mInputFilterEnabled == enabled) { |
| 4949 | return; |
| 4950 | } |
| 4951 | |
| 4952 | mInputFilterEnabled = enabled; |
| 4953 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4954 | } // release lock |
| 4955 | |
| 4956 | // Wake up poll loop since there might be work to do to drop everything. |
| 4957 | mLooper->wake(); |
| 4958 | } |
| 4959 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4960 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4961 | bool needWake = false; |
| 4962 | { |
| 4963 | std::scoped_lock lock(mLock); |
| 4964 | if (mInTouchMode == inTouchMode) { |
| 4965 | return; |
| 4966 | } |
| 4967 | if (DEBUG_TOUCH_MODE) { |
| 4968 | ALOGD("Request to change touch mode from %s to %s", toString(mInTouchMode), |
| 4969 | toString(inTouchMode)); |
| 4970 | // TODO(b/198487159): Also print the current last interacted apps. |
| 4971 | } |
| 4972 | |
| 4973 | // TODO(b/198499018): Store touch mode per display. |
| 4974 | mInTouchMode = inTouchMode; |
| 4975 | |
| 4976 | // TODO(b/198487159): Enforce that only last interacted apps can change touch mode. |
| 4977 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 4978 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4979 | } // release lock |
| 4980 | |
| 4981 | if (needWake) { |
| 4982 | mLooper->wake(); |
| 4983 | } |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4984 | } |
| 4985 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4986 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4987 | if (opacity < 0 || opacity > 1) { |
| 4988 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4989 | return; |
| 4990 | } |
| 4991 | |
| 4992 | std::scoped_lock lock(mLock); |
| 4993 | mMaximumObscuringOpacityForTouch = opacity; |
| 4994 | } |
| 4995 | |
| 4996 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4997 | std::scoped_lock lock(mLock); |
| 4998 | mBlockUntrustedTouchesMode = mode; |
| 4999 | } |
| 5000 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5001 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 5002 | const sp<IBinder>& token) { |
| 5003 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5004 | for (TouchedWindow& w : state.windows) { |
| 5005 | if (w.windowHandle->getToken() == token) { |
| 5006 | return std::make_pair(&state, &w); |
| 5007 | } |
| 5008 | } |
| 5009 | } |
| 5010 | return std::make_pair(nullptr, nullptr); |
| 5011 | } |
| 5012 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5013 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5014 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5015 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5016 | if (DEBUG_FOCUS) { |
| 5017 | ALOGD("Trivial transfer to same window."); |
| 5018 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5019 | return true; |
| 5020 | } |
| 5021 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5022 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5023 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5024 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5025 | // Find the target touch state and touched window by fromToken. |
| 5026 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 5027 | if (state == nullptr || touchedWindow == nullptr) { |
| 5028 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5029 | return false; |
| 5030 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5031 | |
| 5032 | const int32_t displayId = state->displayId; |
| 5033 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5034 | if (toWindowHandle == nullptr) { |
| 5035 | ALOGW("Cannot transfer focus because to window not found."); |
| 5036 | return false; |
| 5037 | } |
| 5038 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5039 | if (DEBUG_FOCUS) { |
| 5040 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5041 | touchedWindow->windowHandle->getName().c_str(), |
| 5042 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5043 | } |
| 5044 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5045 | // Erase old window. |
| 5046 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 5047 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 5048 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5049 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5050 | // Add new window. |
| 5051 | int32_t newTargetFlags = oldTargetFlags & |
| 5052 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 5053 | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5054 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5055 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5056 | // Store the dragging window. |
| 5057 | if (isDragDrop) { |
| 5058 | mDragState = std::make_unique<DragState>(toWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5059 | } |
| 5060 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5061 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5062 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5063 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5064 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5065 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5066 | CancelationOptions |
| 5067 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5068 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5069 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5070 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5071 | } |
| 5072 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5073 | if (DEBUG_FOCUS) { |
| 5074 | logDispatchStateLocked(); |
| 5075 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5076 | } // release lock |
| 5077 | |
| 5078 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5079 | mLooper->wake(); |
| 5080 | return true; |
| 5081 | } |
| 5082 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5083 | // Binder call |
| 5084 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 5085 | sp<IBinder> fromToken; |
| 5086 | { // acquire lock |
| 5087 | std::scoped_lock _l(mLock); |
| 5088 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5089 | auto it = std::find_if(mTouchStatesByDisplay.begin(), mTouchStatesByDisplay.end(), |
| 5090 | [](const auto& pair) { return pair.second.windows.size() == 1; }); |
| 5091 | if (it == mTouchStatesByDisplay.end()) { |
| 5092 | ALOGW("Cannot transfer touch state because there is no exact window being touched"); |
| 5093 | return false; |
| 5094 | } |
| 5095 | const int32_t displayId = it->first; |
| 5096 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5097 | if (toWindowHandle == nullptr) { |
| 5098 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 5099 | return false; |
| 5100 | } |
| 5101 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5102 | TouchState& state = it->second; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5103 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 5104 | fromToken = touchedWindow.windowHandle->getToken(); |
| 5105 | } // release lock |
| 5106 | |
| 5107 | return transferTouchFocus(fromToken, destChannelToken); |
| 5108 | } |
| 5109 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5110 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5111 | if (DEBUG_FOCUS) { |
| 5112 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5113 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5114 | |
| 5115 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5116 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5117 | |
| 5118 | resetKeyRepeatLocked(); |
| 5119 | releasePendingEventLocked(); |
| 5120 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5121 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5122 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5123 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5124 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5125 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5126 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5130 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5131 | dumpDispatchStateLocked(dump); |
| 5132 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5133 | std::istringstream stream(dump); |
| 5134 | std::string line; |
| 5135 | |
| 5136 | while (std::getline(stream, line, '\n')) { |
| 5137 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5138 | } |
| 5139 | } |
| 5140 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5141 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5142 | std::string dump; |
| 5143 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5144 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5145 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5146 | |
| 5147 | std::string windowName = "None"; |
| 5148 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5149 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5150 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5151 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5152 | : "token has capture without window"; |
| 5153 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5154 | 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] | 5155 | |
| 5156 | return dump; |
| 5157 | } |
| 5158 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5159 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5160 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5161 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5162 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5163 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5164 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5165 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5166 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5167 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5168 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5169 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5170 | const std::chrono::duration timeout = |
| 5171 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5172 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5173 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5174 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5175 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5176 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5177 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5178 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5179 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5180 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5181 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5182 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5183 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5184 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5185 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5186 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5187 | 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] | 5188 | state.displayId, toString(state.down), toString(state.split), |
| 5189 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5190 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5191 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5192 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5193 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5194 | dump += StringPrintf(INDENT4 |
| 5195 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5196 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5197 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5198 | } |
| 5199 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5200 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5201 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5202 | } |
| 5203 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5204 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5205 | } |
| 5206 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5207 | if (mDragState) { |
| 5208 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5209 | mDragState->dump(dump, INDENT2); |
| 5210 | } |
| 5211 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5212 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5213 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5214 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5215 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5216 | const auto& displayInfo = it->second; |
| 5217 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5218 | displayInfo.logicalHeight); |
| 5219 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5220 | } else { |
| 5221 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5222 | } |
| 5223 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5224 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5225 | dump += INDENT2 "Windows:\n"; |
| 5226 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5227 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5228 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5229 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5230 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5231 | "paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5232 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5233 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5234 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5235 | "applicationInfo.name=%s, " |
| 5236 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5237 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5238 | i, windowInfo->name.c_str(), windowInfo->id, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5239 | windowInfo->displayId, toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5240 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5241 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5242 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 5243 | windowInfo->flags.string().c_str(), |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 5244 | ftl::enum_string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5245 | windowInfo->frameLeft, windowInfo->frameTop, |
| 5246 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5247 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5248 | windowInfo->applicationInfo.name.c_str(), |
| 5249 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5250 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5251 | dump += StringPrintf(", inputFeatures=%s", |
| 5252 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5253 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5254 | "ms, trustedOverlay=%s, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5255 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5256 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5257 | millis(windowInfo->dispatchingTimeout), |
| 5258 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5259 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5260 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5261 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5262 | } |
| 5263 | } else { |
| 5264 | dump += INDENT2 "Windows: <none>\n"; |
| 5265 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5266 | } |
| 5267 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5268 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5269 | } |
| 5270 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5271 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5272 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5273 | const std::vector<Monitor>& monitors = it.second; |
| 5274 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5275 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5276 | } |
| 5277 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5278 | const std::vector<Monitor>& monitors = it.second; |
| 5279 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5280 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5281 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5282 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5283 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | nsecs_t currentTime = now(); |
| 5287 | |
| 5288 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5289 | if (!mRecentQueue.empty()) { |
| 5290 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5291 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5292 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5293 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5294 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5295 | } |
| 5296 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5297 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | // Dump event currently being dispatched. |
| 5301 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5302 | dump += INDENT "PendingEvent:\n"; |
| 5303 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5304 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5305 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5306 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5307 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5308 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5312 | if (!mInboundQueue.empty()) { |
| 5313 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5314 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5315 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5316 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5317 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5318 | } |
| 5319 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5320 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5321 | } |
| 5322 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5323 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5324 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5325 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5326 | const KeyReplacement& replacement = pair.first; |
| 5327 | int32_t newKeyCode = pair.second; |
| 5328 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5329 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5330 | } |
| 5331 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5332 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5333 | } |
| 5334 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5335 | if (!mCommandQueue.empty()) { |
| 5336 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5337 | } else { |
| 5338 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5339 | } |
| 5340 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5341 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5342 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5343 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5344 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5345 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5346 | connection->inputChannel->getFd().get(), |
| 5347 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5348 | connection->getWindowName().c_str(), |
| 5349 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5350 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5351 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5352 | if (!connection->outboundQueue.empty()) { |
| 5353 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5354 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5355 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5356 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5357 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5358 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5359 | } |
| 5360 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5361 | if (!connection->waitQueue.empty()) { |
| 5362 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5363 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5364 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5365 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5366 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5367 | } |
| 5368 | } |
| 5369 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5370 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5371 | } |
| 5372 | |
| 5373 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5374 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5375 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5376 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5377 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5378 | } |
| 5379 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5380 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5381 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5382 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5383 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5384 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5385 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5386 | } |
| 5387 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5388 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5389 | const size_t numMonitors = monitors.size(); |
| 5390 | for (size_t i = 0; i < numMonitors; i++) { |
| 5391 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5392 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5393 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5394 | dump += "\n"; |
| 5395 | } |
| 5396 | } |
| 5397 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5398 | class LooperEventCallback : public LooperCallback { |
| 5399 | public: |
| 5400 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5401 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5402 | |
| 5403 | private: |
| 5404 | std::function<int(int events)> mCallback; |
| 5405 | }; |
| 5406 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5407 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5408 | if (DEBUG_CHANNEL_CREATION) { |
| 5409 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5410 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5411 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5412 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5413 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5414 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5415 | |
| 5416 | if (result) { |
| 5417 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5418 | } |
| 5419 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5420 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5421 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5422 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5423 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5424 | sp<Connection> connection = |
| 5425 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5426 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5427 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5428 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5429 | } |
| 5430 | mConnectionsByToken.emplace(token, connection); |
| 5431 | |
| 5432 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5433 | this, std::placeholders::_1, token); |
| 5434 | |
| 5435 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5436 | } // release lock |
| 5437 | |
| 5438 | // Wake the looper because some connections have changed. |
| 5439 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5440 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5441 | } |
| 5442 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5443 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5444 | bool isGestureMonitor, |
| 5445 | const std::string& name, |
| 5446 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5447 | std::shared_ptr<InputChannel> serverChannel; |
| 5448 | std::unique_ptr<InputChannel> clientChannel; |
| 5449 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5450 | if (result) { |
| 5451 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5452 | } |
| 5453 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5454 | { // acquire lock |
| 5455 | std::scoped_lock _l(mLock); |
| 5456 | |
| 5457 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5458 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5459 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5460 | } |
| 5461 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5462 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5463 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5464 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5465 | |
| 5466 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5467 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5468 | } |
| 5469 | mConnectionsByToken.emplace(token, connection); |
| 5470 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5471 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5472 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5473 | auto& monitorsByDisplay = |
| 5474 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5475 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5476 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5477 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5478 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), |
| 5479 | displayId, toString(isGestureMonitor), pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5480 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5481 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5482 | // Wake the looper because some connections have changed. |
| 5483 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5484 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5485 | } |
| 5486 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5487 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5488 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5489 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5490 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5491 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5492 | if (status) { |
| 5493 | return status; |
| 5494 | } |
| 5495 | } // release lock |
| 5496 | |
| 5497 | // Wake the poll loop because removing the connection may have changed the current |
| 5498 | // synchronization state. |
| 5499 | mLooper->wake(); |
| 5500 | return OK; |
| 5501 | } |
| 5502 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5503 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5504 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5505 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5506 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5507 | // 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] | 5508 | return BAD_VALUE; |
| 5509 | } |
| 5510 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5511 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5512 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5513 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5514 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5515 | } |
| 5516 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5517 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5518 | |
| 5519 | nsecs_t currentTime = now(); |
| 5520 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5521 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5522 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5523 | return OK; |
| 5524 | } |
| 5525 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5526 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5527 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5528 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5531 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5532 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5533 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5534 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5535 | std::vector<Monitor>& monitors = it->second; |
| 5536 | const size_t numMonitors = monitors.size(); |
| 5537 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5538 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5539 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5540 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5541 | monitors.erase(monitors.begin() + i); |
| 5542 | break; |
| 5543 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5544 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5545 | if (monitors.empty()) { |
| 5546 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5547 | } else { |
| 5548 | ++it; |
| 5549 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5550 | } |
| 5551 | } |
| 5552 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5553 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5554 | { // acquire lock |
| 5555 | std::scoped_lock _l(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5556 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 5557 | TouchState* statePtr = nullptr; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5558 | std::shared_ptr<InputChannel> requestingChannel; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 5559 | int32_t displayId; |
| 5560 | int32_t deviceId; |
| 5561 | const std::optional<int32_t> foundGestureMonitorDisplayId = |
| 5562 | findGestureMonitorDisplayByTokenLocked(token); |
| 5563 | |
| 5564 | // TODO: Optimize this function for pilfering from windows when removing gesture monitors. |
| 5565 | if (foundGestureMonitorDisplayId) { |
| 5566 | // A gesture monitor has requested to pilfer pointers. |
| 5567 | displayId = *foundGestureMonitorDisplayId; |
| 5568 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5569 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5570 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5571 | return BAD_VALUE; |
| 5572 | } |
| 5573 | statePtr = &stateIt->second; |
| 5574 | |
| 5575 | for (const auto& monitor : statePtr->gestureMonitors) { |
| 5576 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 5577 | requestingChannel = monitor.inputChannel; |
| 5578 | deviceId = statePtr->deviceId; |
| 5579 | } |
| 5580 | } |
| 5581 | } else { |
| 5582 | // Check if a window has requested to pilfer pointers. |
| 5583 | for (auto& [curDisplayId, state] : mTouchStatesByDisplay) { |
| 5584 | const sp<WindowInfoHandle>& windowHandle = state.getWindow(token); |
| 5585 | if (windowHandle != nullptr) { |
| 5586 | displayId = curDisplayId; |
| 5587 | requestingChannel = getInputChannelLocked(token); |
| 5588 | deviceId = state.deviceId; |
| 5589 | statePtr = &state; |
| 5590 | break; |
| 5591 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5592 | } |
| 5593 | } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 5594 | |
| 5595 | if (requestingChannel == nullptr) { |
| 5596 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5597 | return BAD_VALUE; |
| 5598 | } |
| 5599 | TouchState& state = *statePtr; |
| 5600 | if (!state.down) { |
| 5601 | 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] | 5602 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5603 | return BAD_VALUE; |
| 5604 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5605 | |
| 5606 | // Send cancel events to all the input channels we're stealing from. |
| 5607 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 5608 | "input channel stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5609 | options.deviceId = deviceId; |
| 5610 | options.displayId = displayId; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 5611 | std::string canceledWindows; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5612 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5613 | std::shared_ptr<InputChannel> channel = |
| 5614 | getInputChannelLocked(window.windowHandle->getToken()); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 5615 | if (channel != nullptr && channel->getConnectionToken() != token) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5616 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 5617 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5618 | canceledWindows += channel->getName(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5619 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5620 | } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 5621 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5622 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5623 | canceledWindows.c_str()); |
| 5624 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5625 | // 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] | 5626 | state.split = false; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame^] | 5627 | state.filterWindowsExcept(token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5628 | } |
| 5629 | return OK; |
| 5630 | } |
| 5631 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5632 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5633 | { // acquire lock |
| 5634 | std::scoped_lock _l(mLock); |
| 5635 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5636 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5637 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5638 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5639 | : "token without window"); |
| 5640 | } |
| 5641 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5642 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5643 | if (focusedToken != windowToken) { |
| 5644 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5645 | enabled ? "enable" : "disable"); |
| 5646 | return; |
| 5647 | } |
| 5648 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5649 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5650 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5651 | "window has %s requested pointer capture.", |
| 5652 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5653 | return; |
| 5654 | } |
| 5655 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5656 | setPointerCaptureLocked(enabled); |
| 5657 | } // release lock |
| 5658 | |
| 5659 | // Wake the thread to process command entries. |
| 5660 | mLooper->wake(); |
| 5661 | } |
| 5662 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5663 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5664 | const sp<IBinder>& token) { |
| 5665 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5666 | const std::vector<Monitor>& monitors = it.second; |
| 5667 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5668 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5669 | return it.first; |
| 5670 | } |
| 5671 | } |
| 5672 | } |
| 5673 | return std::nullopt; |
| 5674 | } |
| 5675 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5676 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5677 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5678 | if (gesturePid.has_value()) { |
| 5679 | return gesturePid; |
| 5680 | } |
| 5681 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5682 | } |
| 5683 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5684 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5685 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5686 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5687 | } |
| 5688 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5689 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5690 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5691 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5692 | } |
| 5693 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5694 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5695 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5696 | } |
| 5697 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5698 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5699 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5700 | if (connection == nullptr) { |
| 5701 | return "<nullptr>"; |
| 5702 | } |
| 5703 | return connection->getInputChannelName(); |
| 5704 | } |
| 5705 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5706 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5707 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5708 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5709 | } |
| 5710 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5711 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5712 | const sp<Connection>& connection, uint32_t seq, |
| 5713 | bool handled, nsecs_t consumeTime) { |
| 5714 | // Handle post-event policy actions. |
| 5715 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5716 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5717 | return; |
| 5718 | } |
| 5719 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5720 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5721 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5722 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5723 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5724 | } |
| 5725 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5726 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5727 | connection->inputChannel->getConnectionToken(), |
| 5728 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5729 | } |
| 5730 | |
| 5731 | bool restartEvent; |
| 5732 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5733 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5734 | restartEvent = |
| 5735 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5736 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5737 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5738 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5739 | handled); |
| 5740 | } else { |
| 5741 | restartEvent = false; |
| 5742 | } |
| 5743 | |
| 5744 | // Dequeue the event and start the next cycle. |
| 5745 | // Because the lock might have been released, it is possible that the |
| 5746 | // contents of the wait queue to have been drained, so we need to double-check |
| 5747 | // a few things. |
| 5748 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5749 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5750 | dispatchEntry = *dispatchEntryIt; |
| 5751 | connection->waitQueue.erase(dispatchEntryIt); |
| 5752 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5753 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5754 | if (!connection->responsive) { |
| 5755 | connection->responsive = isConnectionResponsive(*connection); |
| 5756 | if (connection->responsive) { |
| 5757 | // The connection was unresponsive, and now it's responsive. |
| 5758 | processConnectionResponsiveLocked(*connection); |
| 5759 | } |
| 5760 | } |
| 5761 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5762 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5763 | connection->outboundQueue.push_front(dispatchEntry); |
| 5764 | traceOutboundQueueLength(*connection); |
| 5765 | } else { |
| 5766 | releaseDispatchEntry(dispatchEntry); |
| 5767 | } |
| 5768 | } |
| 5769 | |
| 5770 | // Start the next dispatch cycle for this connection. |
| 5771 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5772 | } |
| 5773 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5774 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5775 | const sp<IBinder>& newToken) { |
| 5776 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5777 | scoped_unlock unlock(mLock); |
| 5778 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5779 | }; |
| 5780 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5781 | } |
| 5782 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5783 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5784 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5785 | scoped_unlock unlock(mLock); |
| 5786 | mPolicy->notifyDropWindow(token, x, y); |
| 5787 | }; |
| 5788 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5789 | } |
| 5790 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5791 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5792 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5793 | scoped_unlock unlock(mLock); |
| 5794 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5795 | }; |
| 5796 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5797 | } |
| 5798 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5799 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5800 | if (connection == nullptr) { |
| 5801 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5802 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5803 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5804 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5805 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5806 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5807 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5808 | return; |
| 5809 | } |
| 5810 | /** |
| 5811 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5812 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5813 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5814 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5815 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5816 | * most useful. |
| 5817 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5818 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5819 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5820 | std::string reason = |
| 5821 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5822 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5823 | ns2ms(currentWait), |
| 5824 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5825 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5826 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5827 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5828 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5829 | |
| 5830 | // Stop waking up for events on this connection, it is already unresponsive |
| 5831 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5832 | } |
| 5833 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5834 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5835 | std::string reason = |
| 5836 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5837 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5838 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5839 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5840 | scoped_unlock unlock(mLock); |
| 5841 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5842 | }; |
| 5843 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5844 | } |
| 5845 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5846 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5847 | const std::string& reason) { |
| 5848 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5849 | updateLastAnrStateLocked(windowLabel, reason); |
| 5850 | } |
| 5851 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5852 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5853 | const std::string& reason) { |
| 5854 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5855 | updateLastAnrStateLocked(windowLabel, reason); |
| 5856 | } |
| 5857 | |
| 5858 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5859 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5860 | // 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] | 5861 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5862 | struct tm tm; |
| 5863 | localtime_r(&t, &tm); |
| 5864 | char timestr[64]; |
| 5865 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5866 | mLastAnrState.clear(); |
| 5867 | mLastAnrState += INDENT "ANR:\n"; |
| 5868 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5869 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5870 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5871 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5872 | } |
| 5873 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5874 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5875 | KeyEntry& entry) { |
| 5876 | const KeyEvent event = createKeyEvent(entry); |
| 5877 | nsecs_t delay = 0; |
| 5878 | { // release lock |
| 5879 | scoped_unlock unlock(mLock); |
| 5880 | android::base::Timer t; |
| 5881 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5882 | entry.policyFlags); |
| 5883 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5884 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5885 | std::to_string(t.duration().count()).c_str()); |
| 5886 | } |
| 5887 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5888 | |
| 5889 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5890 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5891 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5892 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5893 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5894 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5895 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5896 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5897 | } |
| 5898 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5899 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5900 | auto command = [this, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5901 | scoped_unlock unlock(mLock); |
| 5902 | mPolicy->notifyMonitorUnresponsive(pid, reason); |
| 5903 | }; |
| 5904 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5905 | } |
| 5906 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5907 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5908 | std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5909 | auto command = [this, token, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5910 | scoped_unlock unlock(mLock); |
| 5911 | mPolicy->notifyWindowUnresponsive(token, reason); |
| 5912 | }; |
| 5913 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5914 | } |
| 5915 | |
| 5916 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5917 | auto command = [this, pid]() REQUIRES(mLock) { |
| 5918 | scoped_unlock unlock(mLock); |
| 5919 | mPolicy->notifyMonitorResponsive(pid); |
| 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::sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken) { |
| 5925 | auto command = [this, connectionToken]() REQUIRES(mLock) { |
| 5926 | scoped_unlock unlock(mLock); |
| 5927 | mPolicy->notifyWindowResponsive(connectionToken); |
| 5928 | }; |
| 5929 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5930 | } |
| 5931 | |
| 5932 | /** |
| 5933 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5934 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5935 | * command entry to the command queue. |
| 5936 | */ |
| 5937 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5938 | std::string reason) { |
| 5939 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5940 | if (connection.monitor) { |
| 5941 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5942 | reason.c_str()); |
| 5943 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5944 | if (!pid.has_value()) { |
| 5945 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5946 | connection.inputChannel->getName().c_str()); |
| 5947 | return; |
| 5948 | } |
| 5949 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5950 | return; |
| 5951 | } |
| 5952 | // If not a monitor, must be a window |
| 5953 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5954 | reason.c_str()); |
| 5955 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5956 | } |
| 5957 | |
| 5958 | /** |
| 5959 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5960 | */ |
| 5961 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5962 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5963 | if (connection.monitor) { |
| 5964 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5965 | if (!pid.has_value()) { |
| 5966 | ALOGE("Could not find responsive monitor for connection %s", |
| 5967 | connection.inputChannel->getName().c_str()); |
| 5968 | return; |
| 5969 | } |
| 5970 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5971 | return; |
| 5972 | } |
| 5973 | // If not a monitor, must be a window |
| 5974 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5975 | } |
| 5976 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5977 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5978 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5979 | KeyEntry& keyEntry, bool handled) { |
| 5980 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5981 | if (!handled) { |
| 5982 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5983 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5984 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5985 | return false; |
| 5986 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5987 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5988 | // Get the fallback key state. |
| 5989 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5990 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5991 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5992 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5993 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5994 | } |
| 5995 | |
| 5996 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5997 | // If the application handles the original key for which we previously |
| 5998 | // generated a fallback or if the window is not a foreground window, |
| 5999 | // then cancel the associated fallback key, if any. |
| 6000 | if (fallbackKeyCode != -1) { |
| 6001 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6002 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6003 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6004 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6005 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6006 | keyEntry.policyFlags); |
| 6007 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6008 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6009 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6010 | |
| 6011 | mLock.unlock(); |
| 6012 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6013 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6014 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6015 | |
| 6016 | mLock.lock(); |
| 6017 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6018 | // Cancel the fallback key. |
| 6019 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6020 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6021 | "application handled the original non-fallback key " |
| 6022 | "or is no longer a foreground target, " |
| 6023 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6024 | options.keyCode = fallbackKeyCode; |
| 6025 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6026 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6027 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6028 | } |
| 6029 | } else { |
| 6030 | // If the application did not handle a non-fallback key, first check |
| 6031 | // that we are in a good state to perform unhandled key event processing |
| 6032 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6033 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6034 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6035 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6036 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6037 | "since this is not an initial down. " |
| 6038 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6039 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6040 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6041 | return false; |
| 6042 | } |
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 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6045 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6046 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6047 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6048 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6049 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6050 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6051 | |
| 6052 | mLock.unlock(); |
| 6053 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6054 | bool fallback = |
| 6055 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6056 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6057 | |
| 6058 | mLock.lock(); |
| 6059 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6060 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6061 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6062 | return false; |
| 6063 | } |
| 6064 | |
| 6065 | // Latch the fallback keycode for this key on an initial down. |
| 6066 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6067 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6068 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6069 | fallbackKeyCode = event.getKeyCode(); |
| 6070 | } else { |
| 6071 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6072 | } |
| 6073 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6074 | } |
| 6075 | |
| 6076 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6077 | |
| 6078 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6079 | // We will continue to dispatch the key to the policy but we will no |
| 6080 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6081 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6082 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6083 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6084 | if (fallback) { |
| 6085 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6086 | "as a fallback for %d, but on the DOWN it had requested " |
| 6087 | "to send %d instead. Fallback canceled.", |
| 6088 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6089 | } else { |
| 6090 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6091 | "but on the DOWN it had requested to send %d. " |
| 6092 | "Fallback canceled.", |
| 6093 | originalKeyCode, fallbackKeyCode); |
| 6094 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6095 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6096 | |
| 6097 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6098 | "canceling fallback, policy no longer desires it"); |
| 6099 | options.keyCode = fallbackKeyCode; |
| 6100 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6101 | |
| 6102 | fallback = false; |
| 6103 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6104 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6105 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6106 | } |
| 6107 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6108 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6109 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6110 | { |
| 6111 | std::string msg; |
| 6112 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6113 | connection->inputState.getFallbackKeys(); |
| 6114 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6115 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6116 | } |
| 6117 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6118 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6119 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6120 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6121 | |
| 6122 | if (fallback) { |
| 6123 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6124 | keyEntry.eventTime = event.getEventTime(); |
| 6125 | keyEntry.deviceId = event.getDeviceId(); |
| 6126 | keyEntry.source = event.getSource(); |
| 6127 | keyEntry.displayId = event.getDisplayId(); |
| 6128 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6129 | keyEntry.keyCode = fallbackKeyCode; |
| 6130 | keyEntry.scanCode = event.getScanCode(); |
| 6131 | keyEntry.metaState = event.getMetaState(); |
| 6132 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6133 | keyEntry.downTime = event.getDownTime(); |
| 6134 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6135 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6136 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6137 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6138 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6139 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6140 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6141 | return true; // restart the event |
| 6142 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6143 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6144 | ALOGD("Unhandled key event: No fallback key."); |
| 6145 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6146 | |
| 6147 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6148 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6149 | } |
| 6150 | } |
| 6151 | return false; |
| 6152 | } |
| 6153 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6154 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6155 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6156 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6157 | return false; |
| 6158 | } |
| 6159 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6160 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6161 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6162 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6163 | } |
| 6164 | } |
| 6165 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6166 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6167 | if (ATRACE_ENABLED()) { |
| 6168 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6169 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6170 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6171 | } |
| 6172 | } |
| 6173 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6174 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6175 | if (ATRACE_ENABLED()) { |
| 6176 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6177 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6178 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6179 | } |
| 6180 | } |
| 6181 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6182 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6183 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6184 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6185 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6186 | dumpDispatchStateLocked(dump); |
| 6187 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6188 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6189 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6190 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6191 | } |
| 6192 | } |
| 6193 | |
| 6194 | void InputDispatcher::monitor() { |
| 6195 | // 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] | 6196 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6197 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6198 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6199 | } |
| 6200 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6201 | /** |
| 6202 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6203 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6204 | * this method can be safely called from any thread, as long as you've ensured that |
| 6205 | * the work you are interested in completing has already been queued. |
| 6206 | */ |
| 6207 | bool InputDispatcher::waitForIdle() { |
| 6208 | /** |
| 6209 | * Timeout should represent the longest possible time that a device might spend processing |
| 6210 | * events and commands. |
| 6211 | */ |
| 6212 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6213 | std::unique_lock lock(mLock); |
| 6214 | mLooper->wake(); |
| 6215 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6216 | return result == std::cv_status::no_timeout; |
| 6217 | } |
| 6218 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6219 | /** |
| 6220 | * Sets focus to the window identified by the token. This must be called |
| 6221 | * after updating any input window handles. |
| 6222 | * |
| 6223 | * Params: |
| 6224 | * request.token - input channel token used to identify the window that should gain focus. |
| 6225 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6226 | * specified token does not match the currently focused window, this request will be dropped. |
| 6227 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6228 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6229 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6230 | * when requesting the focus change. This determines which request gets |
| 6231 | * precedence if there is a focus change request from another source such as pointer down. |
| 6232 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6233 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6234 | { // acquire lock |
| 6235 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6236 | std::optional<FocusResolver::FocusChanges> changes = |
| 6237 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6238 | if (changes) { |
| 6239 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6240 | } |
| 6241 | } // release lock |
| 6242 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6243 | mLooper->wake(); |
| 6244 | } |
| 6245 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6246 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6247 | if (changes.oldFocus) { |
| 6248 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6249 | if (focusedInputChannel) { |
| 6250 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6251 | "focus left window"); |
| 6252 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6253 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6254 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6255 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6256 | if (changes.newFocus) { |
| 6257 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6258 | } |
| 6259 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6260 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6261 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6262 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6263 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6264 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6265 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6266 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6267 | // front. |
| 6268 | disablePointerCaptureForcedLocked(); |
| 6269 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6270 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6271 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6272 | } |
| 6273 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6274 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6275 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6276 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6277 | return; |
| 6278 | } |
| 6279 | |
| 6280 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6281 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6282 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6283 | setPointerCaptureLocked(false); |
| 6284 | } |
| 6285 | |
| 6286 | if (!mWindowTokenWithPointerCapture) { |
| 6287 | // No need to send capture changes because no window has capture. |
| 6288 | return; |
| 6289 | } |
| 6290 | |
| 6291 | if (mPendingEvent != nullptr) { |
| 6292 | // Move the pending event to the front of the queue. This will give the chance |
| 6293 | // for the pending event to be dropped if it is a captured event. |
| 6294 | mInboundQueue.push_front(mPendingEvent); |
| 6295 | mPendingEvent = nullptr; |
| 6296 | } |
| 6297 | |
| 6298 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6299 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6300 | mInboundQueue.push_front(std::move(entry)); |
| 6301 | } |
| 6302 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6303 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6304 | mCurrentPointerCaptureRequest.enable = enable; |
| 6305 | mCurrentPointerCaptureRequest.seq++; |
| 6306 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6307 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6308 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6309 | }; |
| 6310 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6311 | } |
| 6312 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6313 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6314 | { // acquire lock |
| 6315 | std::scoped_lock _l(mLock); |
| 6316 | // Set an empty list to remove all handles from the specific display. |
| 6317 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6318 | setFocusedApplicationLocked(displayId, nullptr); |
| 6319 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6320 | // have been removed for the removed display. |
| 6321 | mFocusResolver.displayRemoved(displayId); |
| 6322 | } // release lock |
| 6323 | |
| 6324 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6325 | mLooper->wake(); |
| 6326 | } |
| 6327 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6328 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6329 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6330 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6331 | // more convenient parsing. |
| 6332 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6333 | for (const auto& info : windowInfos) { |
| 6334 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6335 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6336 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6337 | |
| 6338 | { // acquire lock |
| 6339 | std::scoped_lock _l(mLock); |
| 6340 | mDisplayInfos.clear(); |
| 6341 | for (const auto& displayInfo : displayInfos) { |
| 6342 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6343 | } |
| 6344 | |
| 6345 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6346 | setInputWindowsLocked(handles, displayId); |
| 6347 | } |
| 6348 | } |
| 6349 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6350 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6351 | } |
| 6352 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6353 | bool InputDispatcher::shouldDropInput( |
| 6354 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
| 6355 | if (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT) || |
| 6356 | (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED) && |
| 6357 | isWindowObscuredLocked(windowHandle))) { |
| 6358 | ALOGW("Dropping %s event targeting %s as requested by input feature %s on display " |
| 6359 | "%" PRId32 ".", |
| 6360 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
| 6361 | windowHandle->getInfo()->inputFeatures.string().c_str(), |
| 6362 | windowHandle->getInfo()->displayId); |
| 6363 | return true; |
| 6364 | } |
| 6365 | return false; |
| 6366 | } |
| 6367 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6368 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6369 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6370 | const std::vector<DisplayInfo>& displayInfos) { |
| 6371 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6372 | } |
| 6373 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6374 | } // namespace android::inputdispatcher |