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, |
| 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 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [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 | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 532 | } // namespace |
| 533 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 534 | // --- InputDispatcher --- |
| 535 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 536 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 537 | : mPolicy(policy), |
| 538 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 539 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 540 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 541 | mAppSwitchSawKeyDown(false), |
| 542 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 543 | mNextUnblockedEvent(nullptr), |
| 544 | mDispatchEnabled(false), |
| 545 | mDispatchFrozen(false), |
| 546 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 547 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 548 | // To avoid leaking stack in case that call never comes, and for tests, |
| 549 | // initialize it here anyways. |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 550 | mInTouchMode(kDefaultInTouchMode), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 551 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 552 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 553 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 554 | mLatencyAggregator(), |
| 555 | mLatencyTracker(&mLatencyAggregator), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 556 | mCompatService(getCompatService()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 557 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 558 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 559 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 560 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 561 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 562 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 563 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 564 | |
| 565 | policy->getDispatcherConfiguration(&mConfig); |
| 566 | } |
| 567 | |
| 568 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 569 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 570 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 571 | resetKeyRepeatLocked(); |
| 572 | releasePendingEventLocked(); |
| 573 | drainInboundQueueLocked(); |
| 574 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 575 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 576 | while (!mConnectionsByToken.empty()) { |
| 577 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 578 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 579 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 583 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 584 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 585 | return ALREADY_EXISTS; |
| 586 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 587 | mThread = std::make_unique<InputThread>( |
| 588 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 589 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 593 | if (mThread && mThread->isCallingThread()) { |
| 594 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 595 | return INVALID_OPERATION; |
| 596 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 597 | mThread.reset(); |
| 598 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 601 | void InputDispatcher::dispatchOnce() { |
| 602 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 603 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 604 | std::scoped_lock _l(mLock); |
| 605 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 606 | |
| 607 | // Run a dispatch loop if there are no pending commands. |
| 608 | // The dispatch loop might enqueue commands to run afterwards. |
| 609 | if (!haveCommandsLocked()) { |
| 610 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 611 | } |
| 612 | |
| 613 | // Run all pending commands if there are any. |
| 614 | // If any commands were run then force the next poll to wake up immediately. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 615 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 616 | nextWakeupTime = LONG_LONG_MIN; |
| 617 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 618 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 619 | // If we are still waiting for ack on some events, |
| 620 | // we might have to wake up earlier to check if an app is anr'ing. |
| 621 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 622 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 623 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 624 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 625 | // pending or queued events |
| 626 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 627 | mDispatcherEnteredIdle.notify_all(); |
| 628 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 629 | } // release lock |
| 630 | |
| 631 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 632 | nsecs_t currentTime = now(); |
| 633 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 634 | mLooper->pollOnce(timeoutMillis); |
| 635 | } |
| 636 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 637 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 638 | * Raise ANR if there is no focused window. |
| 639 | * Before the ANR is raised, do a final state check: |
| 640 | * 1. The currently focused application must be the same one we are waiting for. |
| 641 | * 2. Ensure we still don't have a focused window. |
| 642 | */ |
| 643 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 644 | // Check if the application that we are waiting for is still focused. |
| 645 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 646 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 647 | if (focusedApplication == nullptr || |
| 648 | focusedApplication->getApplicationToken() != |
| 649 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 650 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 651 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 652 | focusedApplication->getName().c_str()); |
| 653 | return; // The focused application has changed. |
| 654 | } |
| 655 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 656 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 657 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 658 | if (focusedWindowHandle != nullptr) { |
| 659 | return; // We now have a focused window. No need for ANR. |
| 660 | } |
| 661 | onAnrLocked(mAwaitedFocusedApplication); |
| 662 | } |
| 663 | |
| 664 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 665 | * Check if any of the connections' wait queues have events that are too old. |
| 666 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 667 | * Return the time at which we should wake up next. |
| 668 | */ |
| 669 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 670 | const nsecs_t currentTime = now(); |
| 671 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 672 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 673 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 674 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 675 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 676 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 677 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 678 | return LONG_LONG_MIN; |
| 679 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 680 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 681 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | // Check if any connection ANRs are due |
| 686 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 687 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 688 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 689 | } |
| 690 | |
| 691 | // If we reached here, we have an unresponsive connection. |
| 692 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 693 | if (connection == nullptr) { |
| 694 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 695 | return nextAnrCheck; |
| 696 | } |
| 697 | connection->responsive = false; |
| 698 | // Stop waking up for this unresponsive connection |
| 699 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 700 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 701 | return LONG_LONG_MIN; |
| 702 | } |
| 703 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 704 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 705 | sp<WindowInfoHandle> window = getWindowHandleLocked(token); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 706 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 707 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 708 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 709 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 712 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 713 | nsecs_t currentTime = now(); |
| 714 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 715 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 716 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 717 | // repeat if the device is just coming out of sleep. |
| 718 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 719 | resetKeyRepeatLocked(); |
| 720 | } |
| 721 | |
| 722 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 723 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 724 | if (DEBUG_FOCUS) { |
| 725 | ALOGD("Dispatch frozen. Waiting some more."); |
| 726 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 727 | return; |
| 728 | } |
| 729 | |
| 730 | // Optimize latency of app switches. |
| 731 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 732 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 733 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 734 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 735 | *nextWakeupTime = mAppSwitchDueTime; |
| 736 | } |
| 737 | |
| 738 | // Ready to start a new event. |
| 739 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 740 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 741 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 742 | if (isAppSwitchDue) { |
| 743 | // The inbound queue is empty so the app switch key we were waiting |
| 744 | // for will never arrive. Stop waiting for it. |
| 745 | resetPendingAppSwitchLocked(false); |
| 746 | isAppSwitchDue = false; |
| 747 | } |
| 748 | |
| 749 | // Synthesize a key repeat if appropriate. |
| 750 | if (mKeyRepeatState.lastKeyEntry) { |
| 751 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 752 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 753 | } else { |
| 754 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 755 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | // Nothing to do if there is no pending event. |
| 761 | if (!mPendingEvent) { |
| 762 | return; |
| 763 | } |
| 764 | } else { |
| 765 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 766 | mPendingEvent = mInboundQueue.front(); |
| 767 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 768 | traceInboundQueueLengthLocked(); |
| 769 | } |
| 770 | |
| 771 | // Poke user activity for this event. |
| 772 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 773 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 774 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | // Now we have an event to dispatch. |
| 778 | // 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] | 779 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 781 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 782 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 783 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 784 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 785 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 789 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 793 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 794 | const ConfigurationChangedEntry& typedEntry = |
| 795 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 796 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 797 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 798 | break; |
| 799 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 800 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 801 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 802 | const DeviceResetEntry& typedEntry = |
| 803 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 804 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 805 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 806 | break; |
| 807 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 808 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 809 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 810 | std::shared_ptr<FocusEntry> typedEntry = |
| 811 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 812 | dispatchFocusLocked(currentTime, typedEntry); |
| 813 | done = true; |
| 814 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 815 | break; |
| 816 | } |
| 817 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 818 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 819 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 820 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 821 | done = true; |
| 822 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 823 | break; |
| 824 | } |
| 825 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 826 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 827 | const auto typedEntry = |
| 828 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 829 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 830 | done = true; |
| 831 | break; |
| 832 | } |
| 833 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 834 | case EventEntry::Type::DRAG: { |
| 835 | std::shared_ptr<DragEntry> typedEntry = |
| 836 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 837 | dispatchDragLocked(currentTime, typedEntry); |
| 838 | done = true; |
| 839 | break; |
| 840 | } |
| 841 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 842 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 843 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 844 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 845 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 846 | resetPendingAppSwitchLocked(true); |
| 847 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 848 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 849 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 850 | } |
| 851 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 852 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 853 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 854 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 855 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 856 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 857 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 858 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 859 | break; |
| 860 | } |
| 861 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 862 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 863 | std::shared_ptr<MotionEntry> motionEntry = |
| 864 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 865 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 866 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 867 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 868 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 869 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 870 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 871 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 872 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 873 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 874 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 875 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 876 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 877 | |
| 878 | case EventEntry::Type::SENSOR: { |
| 879 | std::shared_ptr<SensorEntry> sensorEntry = |
| 880 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 881 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 882 | dropReason = DropReason::APP_SWITCH; |
| 883 | } |
| 884 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 885 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 886 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 887 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 888 | dropReason = DropReason::STALE; |
| 889 | } |
| 890 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 891 | done = true; |
| 892 | break; |
| 893 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 897 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 898 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 899 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 900 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 901 | |
| 902 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 903 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 907 | /** |
| 908 | * Return true if the events preceding this incoming motion event should be dropped |
| 909 | * Return false otherwise (the default behaviour) |
| 910 | */ |
| 911 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 912 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 913 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 914 | |
| 915 | // Optimize case where the current application is unresponsive and the user |
| 916 | // decides to touch a window in a different application. |
| 917 | // If the application takes too long to catch up then we drop all events preceding |
| 918 | // the touch into the other window. |
| 919 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 920 | int32_t displayId = motionEntry.displayId; |
| 921 | int32_t x = static_cast<int32_t>( |
| 922 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 923 | int32_t y = static_cast<int32_t>( |
| 924 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 925 | sp<WindowInfoHandle> touchedWindowHandle = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 926 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 927 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 928 | touchedWindowHandle->getApplicationToken() != |
| 929 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 930 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 931 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 932 | "for %s", |
| 933 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 934 | return true; |
| 935 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 936 | |
| 937 | // Alternatively, maybe there's a gesture monitor that could handle this event |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 938 | for (const auto& monitor : getValueByKey(mGestureMonitorsByDisplay, displayId)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 939 | sp<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 940 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 941 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 942 | // This monitor could take more input. Drop all events preceding this |
| 943 | // event, so that gesture monitor could get a chance to receive the stream |
| 944 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 945 | "responsive gesture monitor that may handle the event", |
| 946 | mAwaitedFocusedApplication->getName().c_str()); |
| 947 | return true; |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 953 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 954 | // events to be processed before dispatching the key event. This is because these motion events |
| 955 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 956 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 957 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 958 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 959 | "just send the pending key event to the focused window."); |
| 960 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 961 | } |
| 962 | return false; |
| 963 | } |
| 964 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 965 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 966 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 967 | mInboundQueue.push_back(std::move(newEntry)); |
| 968 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 969 | traceInboundQueueLengthLocked(); |
| 970 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 971 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 972 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 973 | // Optimize app switch latency. |
| 974 | // If the application takes too long to catch up then we drop all events preceding |
| 975 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 976 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 977 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 978 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 979 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 980 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 981 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 982 | if (DEBUG_APP_SWITCH) { |
| 983 | ALOGD("App switch is pending!"); |
| 984 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 985 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 986 | mAppSwitchSawKeyDown = false; |
| 987 | needWake = true; |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | break; |
| 992 | } |
| 993 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 994 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 995 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 996 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 997 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 998 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 999 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1000 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1001 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1002 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1003 | break; |
| 1004 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1005 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1006 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1007 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1008 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1009 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1010 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1011 | // nothing to do |
| 1012 | break; |
| 1013 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | return needWake; |
| 1017 | } |
| 1018 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1019 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1020 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1021 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1022 | mRecentQueue.push_back(entry); |
| 1023 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1024 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1025 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1029 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1030 | int32_t y, TouchState* touchState, |
| 1031 | bool addOutsideTargets, |
| 1032 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1033 | if (addOutsideTargets && touchState == nullptr) { |
| 1034 | 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] | 1035 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1036 | // Traverse windows from front to back to find touched window. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1037 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 1038 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1039 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1040 | continue; |
| 1041 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1042 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1043 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1044 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1045 | |
| 1046 | if (windowInfo->visible) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1047 | if (!flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
| 1048 | bool isTouchModal = !flags.test(WindowInfo::Flag::NOT_FOCUSABLE) && |
| 1049 | !flags.test(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
| 1051 | // Found window. |
| 1052 | return windowHandle; |
| 1053 | } |
| 1054 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1055 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1056 | if (addOutsideTargets && flags.test(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1057 | touchState->addOrUpdateWindow(windowHandle, |
| 1058 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1059 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1060 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1061 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1062 | } |
| 1063 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1064 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1067 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1068 | const char* reason; |
| 1069 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1070 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1071 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1072 | ALOGD("Dropped event because policy consumed it."); |
| 1073 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1074 | reason = "inbound event was dropped because the policy consumed it"; |
| 1075 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1076 | case DropReason::DISABLED: |
| 1077 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1078 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1079 | } |
| 1080 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1081 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1082 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1083 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1084 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1085 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1086 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1087 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1088 | "has started interacting with a different application."); |
| 1089 | reason = "inbound event was dropped because the current application is not responding " |
| 1090 | "and the user has started interacting with a different application"; |
| 1091 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1092 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1093 | ALOGI("Dropped event because it is stale."); |
| 1094 | reason = "inbound event was dropped because it is stale"; |
| 1095 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1096 | case DropReason::NO_POINTER_CAPTURE: |
| 1097 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1098 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1099 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1100 | case DropReason::NOT_DROPPED: { |
| 1101 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1102 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1103 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1106 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1107 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1108 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1109 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1110 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1111 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1112 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1113 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1114 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1115 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1116 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1117 | } else { |
| 1118 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1119 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1120 | } |
| 1121 | break; |
| 1122 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1123 | case EventEntry::Type::SENSOR: { |
| 1124 | break; |
| 1125 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1126 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1127 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1128 | break; |
| 1129 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1130 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1131 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1132 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1133 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1134 | 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] | 1135 | break; |
| 1136 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1140 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1141 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1142 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1145 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1146 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1147 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1148 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1152 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1153 | } |
| 1154 | |
| 1155 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1156 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1157 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1158 | if (DEBUG_APP_SWITCH) { |
| 1159 | if (handled) { |
| 1160 | ALOGD("App switch has arrived."); |
| 1161 | } else { |
| 1162 | ALOGD("App switch was abandoned."); |
| 1163 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1164 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1167 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1168 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1171 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1172 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1173 | return false; |
| 1174 | } |
| 1175 | |
| 1176 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1177 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1178 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1179 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1180 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1181 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1182 | return true; |
| 1183 | } |
| 1184 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1185 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1186 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1190 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1191 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1192 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1193 | releaseInboundEventLocked(entry); |
| 1194 | } |
| 1195 | traceInboundQueueLengthLocked(); |
| 1196 | } |
| 1197 | |
| 1198 | void InputDispatcher::releasePendingEventLocked() { |
| 1199 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1201 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1205 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1206 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1207 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1208 | if (DEBUG_DISPATCH_CYCLE) { |
| 1209 | ALOGD("Injected inbound event was dropped."); |
| 1210 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1211 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1212 | } |
| 1213 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1214 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1215 | } |
| 1216 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1220 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1221 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1225 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1226 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1227 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1228 | uint32_t policyFlags = entry->policyFlags & |
| 1229 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1231 | std::shared_ptr<KeyEntry> newEntry = |
| 1232 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1233 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1234 | entry->flags, entry->keyCode, entry->scanCode, |
| 1235 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1236 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1237 | newEntry->syntheticRepeat = true; |
| 1238 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1239 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1240 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1243 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1244 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1245 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1246 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1247 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1248 | |
| 1249 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1250 | resetKeyRepeatLocked(); |
| 1251 | |
| 1252 | // 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] | 1253 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1254 | scoped_unlock unlock(mLock); |
| 1255 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1256 | }; |
| 1257 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1258 | return true; |
| 1259 | } |
| 1260 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1261 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1262 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1263 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1264 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1265 | entry.deviceId); |
| 1266 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1267 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1268 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1269 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1270 | resetKeyRepeatLocked(); |
| 1271 | } |
| 1272 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1273 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1274 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1275 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1276 | return true; |
| 1277 | } |
| 1278 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1279 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1280 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1281 | if (mPendingEvent != nullptr) { |
| 1282 | // Move the pending event to the front of the queue. This will give the chance |
| 1283 | // for the pending event to get dispatched to the newly focused window |
| 1284 | mInboundQueue.push_front(mPendingEvent); |
| 1285 | mPendingEvent = nullptr; |
| 1286 | } |
| 1287 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1288 | std::unique_ptr<FocusEntry> focusEntry = |
| 1289 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1290 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1291 | |
| 1292 | // This event should go to the front of the queue, but behind all other focus events |
| 1293 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1294 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1295 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1296 | [](const std::shared_ptr<EventEntry>& event) { |
| 1297 | return event->type == EventEntry::Type::FOCUS; |
| 1298 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1299 | |
| 1300 | // 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] | 1301 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1302 | } |
| 1303 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1304 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1305 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1306 | if (channel == nullptr) { |
| 1307 | return; // Window has gone away |
| 1308 | } |
| 1309 | InputTarget target; |
| 1310 | target.inputChannel = channel; |
| 1311 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1312 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1313 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1314 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1315 | std::string reason = std::string("reason=").append(entry->reason); |
| 1316 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1317 | dispatchEventLocked(currentTime, entry, {target}); |
| 1318 | } |
| 1319 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1320 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1321 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1322 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1323 | dropReason = DropReason::NOT_DROPPED; |
| 1324 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1325 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1326 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1327 | |
| 1328 | if (entry->pointerCaptureRequest.enable) { |
| 1329 | // Enable Pointer Capture. |
| 1330 | if (haveWindowWithPointerCapture && |
| 1331 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
| 1332 | LOG_ALWAYS_FATAL("This request to enable Pointer Capture has already been dispatched " |
| 1333 | "to the window."); |
| 1334 | } |
| 1335 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1336 | // This can happen if a window requests capture and immediately releases capture. |
| 1337 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1338 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1339 | return; |
| 1340 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1341 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1342 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1343 | return; |
| 1344 | } |
| 1345 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1346 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1347 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1348 | mWindowTokenWithPointerCapture = token; |
| 1349 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1350 | // Disable Pointer Capture. |
| 1351 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1352 | // for two reasons: |
| 1353 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1354 | // to disable capture with the same sequence number: one generated by |
| 1355 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1356 | // Capture being disabled in InputReader. |
| 1357 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1358 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1359 | // in InputReader. |
| 1360 | if (!haveWindowWithPointerCapture) { |
| 1361 | // Pointer capture was already forcefully disabled because of focus change. |
| 1362 | dropReason = DropReason::NOT_DROPPED; |
| 1363 | return; |
| 1364 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1365 | token = mWindowTokenWithPointerCapture; |
| 1366 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1367 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1368 | setPointerCaptureLocked(false); |
| 1369 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | auto channel = getInputChannelLocked(token); |
| 1373 | if (channel == nullptr) { |
| 1374 | // Window has gone away, clean up Pointer Capture state. |
| 1375 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1376 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1377 | setPointerCaptureLocked(false); |
| 1378 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1379 | return; |
| 1380 | } |
| 1381 | InputTarget target; |
| 1382 | target.inputChannel = channel; |
| 1383 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1384 | entry->dispatchInProgress = true; |
| 1385 | dispatchEventLocked(currentTime, entry, {target}); |
| 1386 | |
| 1387 | dropReason = DropReason::NOT_DROPPED; |
| 1388 | } |
| 1389 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1390 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1391 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1392 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1393 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1394 | if (windowHandles.empty()) { |
| 1395 | return; |
| 1396 | } |
| 1397 | const std::vector<InputTarget> inputTargets = |
| 1398 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1399 | if (inputTargets.empty()) { |
| 1400 | return; |
| 1401 | } |
| 1402 | entry->dispatchInProgress = true; |
| 1403 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1404 | } |
| 1405 | |
| 1406 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1407 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1408 | std::vector<InputTarget> inputTargets; |
| 1409 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1410 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1411 | const sp<IBinder>& token = handle->getToken(); |
| 1412 | if (token == nullptr) { |
| 1413 | continue; |
| 1414 | } |
| 1415 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1416 | if (channel == nullptr) { |
| 1417 | continue; // Window has gone away |
| 1418 | } |
| 1419 | InputTarget target; |
| 1420 | target.inputChannel = channel; |
| 1421 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1422 | inputTargets.push_back(target); |
| 1423 | } |
| 1424 | return inputTargets; |
| 1425 | } |
| 1426 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1427 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1428 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1429 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1430 | if (!entry->dispatchInProgress) { |
| 1431 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1432 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1433 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1434 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1435 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1436 | // We have seen two identical key downs in a row which indicates that the device |
| 1437 | // driver is automatically generating key repeats itself. We take note of the |
| 1438 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1439 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1440 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1441 | // Make sure we don't get key down from a different device. If a different |
| 1442 | // device Id has same key pressed down, the new device Id will replace the |
| 1443 | // current one to hold the key repeat with repeat count reset. |
| 1444 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1445 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1446 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1447 | resetKeyRepeatLocked(); |
| 1448 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1449 | } else { |
| 1450 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1451 | resetKeyRepeatLocked(); |
| 1452 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1453 | } |
| 1454 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1455 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1456 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1457 | // 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] | 1458 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1459 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1460 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1461 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1462 | resetKeyRepeatLocked(); |
| 1463 | } |
| 1464 | |
| 1465 | if (entry->repeatCount == 1) { |
| 1466 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1467 | } else { |
| 1468 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1469 | } |
| 1470 | |
| 1471 | entry->dispatchInProgress = true; |
| 1472 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1473 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | // Handle case where the policy asked us to try again later last time. |
| 1477 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1478 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1479 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1480 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1481 | } |
| 1482 | return false; // wait until next wakeup |
| 1483 | } |
| 1484 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1485 | entry->interceptKeyWakeupTime = 0; |
| 1486 | } |
| 1487 | |
| 1488 | // Give the policy a chance to intercept the key. |
| 1489 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1490 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1491 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1492 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1493 | |
| 1494 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1495 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1496 | }; |
| 1497 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1498 | return false; // wait for the command to run |
| 1499 | } else { |
| 1500 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1501 | } |
| 1502 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1503 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1504 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1509 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1510 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1511 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1512 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1513 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1514 | return true; |
| 1515 | } |
| 1516 | |
| 1517 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1518 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1519 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1520 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1521 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1522 | return false; |
| 1523 | } |
| 1524 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1525 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1526 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1527 | return true; |
| 1528 | } |
| 1529 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1530 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1531 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1532 | |
| 1533 | // Dispatch the key. |
| 1534 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1535 | return true; |
| 1536 | } |
| 1537 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1538 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1539 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1540 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1541 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1542 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1543 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1544 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1545 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1546 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1549 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1550 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1551 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1552 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1553 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1554 | "source=0x%x, sensorType=%s", |
| 1555 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1556 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1557 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1558 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1559 | scoped_unlock unlock(mLock); |
| 1560 | |
| 1561 | if (entry->accuracyChanged) { |
| 1562 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1563 | } |
| 1564 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1565 | entry->hwTimestamp, entry->values); |
| 1566 | }; |
| 1567 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1571 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1572 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1573 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1574 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1575 | { // acquire lock |
| 1576 | std::scoped_lock _l(mLock); |
| 1577 | |
| 1578 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1579 | std::shared_ptr<EventEntry> entry = *it; |
| 1580 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1581 | it = mInboundQueue.erase(it); |
| 1582 | releaseInboundEventLocked(entry); |
| 1583 | } |
| 1584 | } |
| 1585 | } |
| 1586 | return true; |
| 1587 | } |
| 1588 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1589 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1590 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1591 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1592 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1593 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1594 | entry->dispatchInProgress = true; |
| 1595 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1596 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1600 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1601 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1602 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1603 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1604 | return true; |
| 1605 | } |
| 1606 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1607 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1608 | |
| 1609 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1610 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1611 | |
| 1612 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1613 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1614 | if (isPointerEvent) { |
| 1615 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1616 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1617 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1618 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1619 | } else { |
| 1620 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1621 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1622 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1623 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1624 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1625 | return false; |
| 1626 | } |
| 1627 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1628 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1629 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1630 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1631 | return true; |
| 1632 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1633 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1634 | CancelationOptions::Mode mode(isPointerEvent |
| 1635 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1636 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1637 | CancelationOptions options(mode, "input event injection failed"); |
| 1638 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1639 | return true; |
| 1640 | } |
| 1641 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1642 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1643 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1644 | |
| 1645 | // Dispatch the motion. |
| 1646 | if (conflictingPointerActions) { |
| 1647 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1648 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1649 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1650 | } |
| 1651 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1652 | return true; |
| 1653 | } |
| 1654 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1655 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1656 | bool isExiting, const MotionEntry& motionEntry) { |
| 1657 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1658 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1659 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1660 | PointerCoords pointerCoords; |
| 1661 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1662 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1663 | |
| 1664 | std::unique_ptr<DragEntry> dragEntry = |
| 1665 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1666 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1667 | pointerCoords.getY()); |
| 1668 | |
| 1669 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1670 | } |
| 1671 | |
| 1672 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1673 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1674 | if (channel == nullptr) { |
| 1675 | return; // Window has gone away |
| 1676 | } |
| 1677 | InputTarget target; |
| 1678 | target.inputChannel = channel; |
| 1679 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1680 | entry->dispatchInProgress = true; |
| 1681 | dispatchEventLocked(currentTime, entry, {target}); |
| 1682 | } |
| 1683 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1684 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1685 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1686 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1687 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1688 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1689 | "metaState=0x%x, buttonState=0x%x," |
| 1690 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1691 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1692 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1693 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1694 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1695 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1696 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1697 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1698 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1699 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1700 | "orientation=%f", |
| 1701 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1702 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1703 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1704 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1705 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1706 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1707 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1708 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1709 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1710 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1711 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1712 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1713 | } |
| 1714 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1715 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1716 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1717 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1718 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1719 | if (DEBUG_DISPATCH_CYCLE) { |
| 1720 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1721 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1722 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1723 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1724 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1725 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1726 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1727 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1728 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1729 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1730 | sp<Connection> connection = |
| 1731 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1732 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1733 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1734 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1735 | if (DEBUG_FOCUS) { |
| 1736 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1737 | "is no longer registered with the input dispatcher.", |
| 1738 | inputTarget.inputChannel->getName().c_str()); |
| 1739 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1744 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1745 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1746 | // If the policy decides to close the app, we will get a channel removal event via |
| 1747 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1748 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1749 | // pile up. |
| 1750 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1751 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame^] | 1752 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1753 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1754 | "application not responding"); |
| 1755 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1756 | } |
| 1757 | } |
| 1758 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1759 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1760 | if (DEBUG_FOCUS) { |
| 1761 | ALOGD("Resetting ANR timeouts."); |
| 1762 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1763 | |
| 1764 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1765 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1766 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1767 | } |
| 1768 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1769 | /** |
| 1770 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1771 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1772 | * Focused display is the display that the user most recently interacted with. |
| 1773 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1774 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1775 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1776 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1777 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1778 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1779 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1780 | break; |
| 1781 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1782 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1783 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1784 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1785 | break; |
| 1786 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1787 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1788 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1789 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1790 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1791 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1792 | case EventEntry::Type::SENSOR: |
| 1793 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1794 | 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] | 1795 | return ADISPLAY_ID_NONE; |
| 1796 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1797 | } |
| 1798 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1799 | } |
| 1800 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1801 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1802 | const char* focusedWindowName) { |
| 1803 | if (mAnrTracker.empty()) { |
| 1804 | // already processed all events that we waited for |
| 1805 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1806 | return false; |
| 1807 | } |
| 1808 | |
| 1809 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1810 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1811 | // 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] | 1812 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1813 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1814 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1815 | return true; |
| 1816 | } |
| 1817 | |
| 1818 | // We still have pending events, and already started the timer |
| 1819 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1820 | return true; // Still waiting |
| 1821 | } |
| 1822 | |
| 1823 | // Waited too long, and some connection still hasn't processed all motions |
| 1824 | // Just send the key to the focused window |
| 1825 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1826 | focusedWindowName); |
| 1827 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1828 | return false; |
| 1829 | } |
| 1830 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1831 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1832 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1833 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1834 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1835 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1836 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1837 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1838 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1839 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1840 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1841 | // If there is no currently focused window and no focused application |
| 1842 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1843 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1844 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1845 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1846 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1847 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1848 | } |
| 1849 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1850 | // Drop key events if requested by input feature |
| 1851 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1852 | return InputEventInjectionResult::FAILED; |
| 1853 | } |
| 1854 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1855 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1856 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1857 | // start interacting with another application via touch (app switch). This code can be removed |
| 1858 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1859 | // an app is expected to have a focused window. |
| 1860 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1861 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1862 | // 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] | 1863 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1864 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1865 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1866 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1867 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1868 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1869 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1870 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1871 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1872 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1873 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1874 | // Already raised ANR. Drop the event |
| 1875 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1876 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1877 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1878 | } else { |
| 1879 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1880 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | // we have a valid, non-null focused window |
| 1885 | resetNoFocusedWindowTimeoutLocked(); |
| 1886 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1887 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1888 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1889 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1890 | } |
| 1891 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1892 | if (focusedWindowHandle->getInfo()->paused) { |
| 1893 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1894 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | // If the event is a key event, then we must wait for all previous events to |
| 1898 | // complete before delivering it because previous events may have the |
| 1899 | // side-effect of transferring focus to a different window and we want to |
| 1900 | // ensure that the following keys are sent to the new window. |
| 1901 | // |
| 1902 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1903 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1904 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1905 | // often anticipate pending UI changes when typing on a keyboard. |
| 1906 | // To obtain this behavior, we must serialize key events with respect to all |
| 1907 | // prior input events. |
| 1908 | if (entry.type == EventEntry::Type::KEY) { |
| 1909 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1910 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1911 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1912 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1916 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1917 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1918 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1919 | |
| 1920 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1921 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1922 | } |
| 1923 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1924 | /** |
| 1925 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1926 | * that are currently unresponsive. |
| 1927 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1928 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1929 | const std::vector<Monitor>& monitors) const { |
| 1930 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1931 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1932 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 1933 | sp<Connection> connection = |
| 1934 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1935 | if (connection == nullptr) { |
| 1936 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1937 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1938 | return false; |
| 1939 | } |
| 1940 | if (!connection->responsive) { |
| 1941 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1942 | connection->inputChannel->getName().c_str()); |
| 1943 | return false; |
| 1944 | } |
| 1945 | return true; |
| 1946 | }); |
| 1947 | return responsiveMonitors; |
| 1948 | } |
| 1949 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1950 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1951 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1952 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1953 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1954 | enum InjectionPermission { |
| 1955 | INJECTION_PERMISSION_UNKNOWN, |
| 1956 | INJECTION_PERMISSION_GRANTED, |
| 1957 | INJECTION_PERMISSION_DENIED |
| 1958 | }; |
| 1959 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1960 | // For security reasons, we defer updating the touch state until we are sure that |
| 1961 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1962 | int32_t displayId = entry.displayId; |
| 1963 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1964 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1965 | |
| 1966 | // 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] | 1967 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1968 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1969 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1970 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1971 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1972 | // Copy current touch state into tempTouchState. |
| 1973 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1974 | // 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] | 1975 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1976 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1977 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1978 | mTouchStatesByDisplay.find(displayId); |
| 1979 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1980 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1981 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1982 | } |
| 1983 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1984 | bool isSplit = tempTouchState.split; |
| 1985 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1986 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1987 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1988 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1989 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1990 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1991 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1992 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1993 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1994 | bool wrongDevice = false; |
| 1995 | if (newGesture) { |
| 1996 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1997 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1998 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1999 | "in display %" PRId32, |
| 2000 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2001 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2002 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2003 | switchedDevice = false; |
| 2004 | wrongDevice = true; |
| 2005 | goto Failed; |
| 2006 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2007 | tempTouchState.reset(); |
| 2008 | tempTouchState.down = down; |
| 2009 | tempTouchState.deviceId = entry.deviceId; |
| 2010 | tempTouchState.source = entry.source; |
| 2011 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2012 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2013 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2014 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2015 | "in display %" PRId32, |
| 2016 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2017 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2018 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2019 | switchedDevice = false; |
| 2020 | wrongDevice = true; |
| 2021 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2025 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2026 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2027 | int32_t x; |
| 2028 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2029 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2030 | // Always dispatch mouse events to cursor position. |
| 2031 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2032 | x = int32_t(entry.xCursorPosition); |
| 2033 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2034 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2035 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2036 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2037 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2038 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2039 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 2040 | isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2041 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2042 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2043 | if (newTouchedWindowHandle != nullptr && |
| 2044 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 2045 | // New window supports splitting, but we should never split mouse events. |
| 2046 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2047 | } else if (isSplit) { |
| 2048 | // New window does not support splitting but we have already split events. |
| 2049 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2050 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2054 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2055 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2056 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2057 | // 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] | 2058 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2061 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 2062 | ALOGI("Not sending touch event to %s because it is paused", |
| 2063 | newTouchedWindowHandle->getName().c_str()); |
| 2064 | newTouchedWindowHandle = nullptr; |
| 2065 | } |
| 2066 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2067 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2068 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2069 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 2070 | if (!isResponsive) { |
| 2071 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2072 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 2073 | newTouchedWindowHandle = nullptr; |
| 2074 | } |
| 2075 | } |
| 2076 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2077 | // Drop events that can't be trusted due to occlusion |
| 2078 | if (newTouchedWindowHandle != nullptr && |
| 2079 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2080 | TouchOcclusionInfo occlusionInfo = |
| 2081 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2082 | if (!isTouchTrustedLocked(occlusionInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2083 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2084 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2085 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2086 | ALOGD("%s", log.c_str()); |
| 2087 | } |
| 2088 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2089 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2090 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2091 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2092 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2093 | newTouchedWindowHandle = nullptr; |
| 2094 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2095 | } |
| 2096 | } |
| 2097 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2098 | // Drop touch events if requested by input feature |
| 2099 | if (newTouchedWindowHandle != nullptr && shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2100 | newTouchedWindowHandle = nullptr; |
| 2101 | } |
| 2102 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2103 | const std::vector<Monitor> newGestureMonitors = isDown |
| 2104 | ? selectResponsiveMonitorsLocked( |
| 2105 | getValueByKey(mGestureMonitorsByDisplay, displayId)) |
| 2106 | : std::vector<Monitor>{}; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2107 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2108 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 2109 | ALOGI("Dropping event because there is no touchable window or gesture monitor at " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2110 | "(%d, %d) in display %" PRId32 ".", |
| 2111 | x, y, displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2112 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2113 | goto Failed; |
| 2114 | } |
| 2115 | |
| 2116 | if (newTouchedWindowHandle != nullptr) { |
| 2117 | // Set target flags. |
| 2118 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 2119 | if (isSplit) { |
| 2120 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2121 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2122 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2123 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2124 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2125 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2126 | } |
| 2127 | |
| 2128 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2129 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2130 | newHoverWindowHandle = nullptr; |
| 2131 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2132 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | // Update the temporary touch state. |
| 2136 | BitSet32 pointerIds; |
| 2137 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2138 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2139 | pointerIds.markBit(pointerId); |
| 2140 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2141 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2142 | } |
| 2143 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2144 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2145 | } else { |
| 2146 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2147 | |
| 2148 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2149 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2150 | if (DEBUG_FOCUS) { |
| 2151 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2152 | "dropped the pointer down event in display %" PRId32, |
| 2153 | displayId); |
| 2154 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2155 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2156 | goto Failed; |
| 2157 | } |
| 2158 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2159 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2160 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2161 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2162 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2163 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2164 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2165 | 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] | 2166 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2167 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2168 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2169 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2170 | |
| 2171 | // Drop touch events if requested by input feature |
| 2172 | if (newTouchedWindowHandle != nullptr && |
| 2173 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2174 | newTouchedWindowHandle = nullptr; |
| 2175 | } |
| 2176 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2177 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2178 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2179 | if (DEBUG_FOCUS) { |
| 2180 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2181 | oldTouchedWindowHandle->getName().c_str(), |
| 2182 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2183 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2184 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2185 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2186 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2187 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2188 | |
| 2189 | // Make a slippery entrance into the new window. |
| 2190 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2191 | isSplit = true; |
| 2192 | } |
| 2193 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2194 | int32_t targetFlags = |
| 2195 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2196 | if (isSplit) { |
| 2197 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2198 | } |
| 2199 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2200 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2201 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2202 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | BitSet32 pointerIds; |
| 2206 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2207 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2208 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2209 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2210 | } |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2215 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 2216 | // when dispatching it as is to newTouchedWindowHandle. |
| 2217 | if (mLastHoverWindowHandle != nullptr && |
| 2218 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2219 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2220 | if (DEBUG_HOVER) { |
| 2221 | ALOGD("Sending hover exit event to window %s.", |
| 2222 | mLastHoverWindowHandle->getName().c_str()); |
| 2223 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2224 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2225 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2226 | } |
| 2227 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2228 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2229 | // when dispatching it as is to newTouchedWindowHandle. |
| 2230 | if (newHoverWindowHandle != nullptr && |
| 2231 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2232 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2233 | if (DEBUG_HOVER) { |
| 2234 | ALOGD("Sending hover enter event to window %s.", |
| 2235 | newHoverWindowHandle->getName().c_str()); |
| 2236 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2237 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2238 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2239 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | // Check permission to inject into all touched foreground windows and ensure there |
| 2244 | // is at least one touched foreground window. |
| 2245 | { |
| 2246 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2247 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2248 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 2249 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2250 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2251 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2252 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2253 | goto Failed; |
| 2254 | } |
| 2255 | } |
| 2256 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2257 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2258 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2259 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 2260 | "%" PRId32 " or gesture monitor to receive it.", |
| 2261 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2262 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2263 | goto Failed; |
| 2264 | } |
| 2265 | |
| 2266 | // Permission granted to injection into all touched foreground windows. |
| 2267 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2268 | } |
| 2269 | |
| 2270 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2271 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2272 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2273 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2274 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2275 | if (foregroundWindowHandle) { |
| 2276 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2277 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2278 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2279 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2280 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2281 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2282 | InputTarget::FLAG_ZERO_COORDS, |
| 2283 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2284 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2285 | } |
| 2286 | } |
| 2287 | } |
| 2288 | } |
| 2289 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2290 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2291 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2292 | // of the touch gesture. |
| 2293 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2294 | // engine only supports touch events. We would need to add a mechanism similar |
| 2295 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2296 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2297 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2298 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2299 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2300 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2301 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2302 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2303 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2304 | if (info->displayId == displayId && |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2305 | windowHandle->getInfo()->type == WindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2306 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2307 | .addOrUpdateWindow(windowHandle, |
| 2308 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2309 | InputTarget:: |
| 2310 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2311 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2312 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2313 | } |
| 2314 | } |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2319 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2320 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2321 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2322 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2323 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2324 | } |
| 2325 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2326 | for (const auto& monitor : tempTouchState.gestureMonitors) { |
| 2327 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2330 | // Drop the outside or hover touch windows since we will not care about them |
| 2331 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2332 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2333 | |
| 2334 | Failed: |
| 2335 | // Check injection permission once and for all. |
| 2336 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2337 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2338 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2339 | } else { |
| 2340 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2341 | } |
| 2342 | } |
| 2343 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2344 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2345 | return injectionResult; |
| 2346 | } |
| 2347 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2348 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2349 | if (!wrongDevice) { |
| 2350 | if (switchedDevice) { |
| 2351 | if (DEBUG_FOCUS) { |
| 2352 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2353 | } |
| 2354 | *outConflictingPointerActions = true; |
| 2355 | } |
| 2356 | |
| 2357 | if (isHoverAction) { |
| 2358 | // Started hovering, therefore no longer down. |
| 2359 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2360 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2361 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2362 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2363 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2364 | *outConflictingPointerActions = true; |
| 2365 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2366 | tempTouchState.reset(); |
| 2367 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2368 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2369 | tempTouchState.deviceId = entry.deviceId; |
| 2370 | tempTouchState.source = entry.source; |
| 2371 | tempTouchState.displayId = displayId; |
| 2372 | } |
| 2373 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2374 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2375 | // All pointers up or canceled. |
| 2376 | tempTouchState.reset(); |
| 2377 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2378 | // First pointer went down. |
| 2379 | if (oldState && oldState->down) { |
| 2380 | if (DEBUG_FOCUS) { |
| 2381 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2382 | } |
| 2383 | *outConflictingPointerActions = true; |
| 2384 | } |
| 2385 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2386 | // One pointer went up. |
| 2387 | if (isSplit) { |
| 2388 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2389 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2390 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2391 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2392 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2393 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2394 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2395 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2396 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2397 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2398 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2399 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2400 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2401 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2402 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2403 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2404 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2405 | // Save changes unless the action was scroll in which case the temporary touch |
| 2406 | // state was only valid for this one action. |
| 2407 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2408 | if (tempTouchState.displayId >= 0) { |
| 2409 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2410 | } else { |
| 2411 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2412 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2413 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2414 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2415 | // Update hover state. |
| 2416 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2417 | } |
| 2418 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2419 | return injectionResult; |
| 2420 | } |
| 2421 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2422 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2423 | const sp<WindowInfoHandle> dropWindow = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2424 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2425 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2426 | if (dropWindow) { |
| 2427 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2428 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2429 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2430 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2431 | } |
| 2432 | mDragState.reset(); |
| 2433 | } |
| 2434 | |
| 2435 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2436 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2437 | return; |
| 2438 | } |
| 2439 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2440 | if (!mDragState->isStartDrag) { |
| 2441 | mDragState->isStartDrag = true; |
| 2442 | mDragState->isStylusButtonDownAtStart = |
| 2443 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2444 | } |
| 2445 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2446 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2447 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2448 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2449 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2450 | // Handle the special case : stylus button no longer pressed. |
| 2451 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2452 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2453 | finishDragAndDrop(entry.displayId, x, y); |
| 2454 | return; |
| 2455 | } |
| 2456 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2457 | const sp<WindowInfoHandle> hoverWindowHandle = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2458 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2459 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2460 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2461 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2462 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2463 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2464 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2465 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2466 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2467 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2468 | } |
| 2469 | // enqueue drag location if needed. |
| 2470 | if (hoverWindowHandle != nullptr) { |
| 2471 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2472 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2473 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2474 | finishDragAndDrop(entry.displayId, x, y); |
| 2475 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2476 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2477 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2478 | } |
| 2479 | } |
| 2480 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2481 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2482 | int32_t targetFlags, BitSet32 pointerIds, |
| 2483 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2484 | std::vector<InputTarget>::iterator it = |
| 2485 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2486 | [&windowHandle](const InputTarget& inputTarget) { |
| 2487 | return inputTarget.inputChannel->getConnectionToken() == |
| 2488 | windowHandle->getToken(); |
| 2489 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2490 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2491 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2492 | |
| 2493 | if (it == inputTargets.end()) { |
| 2494 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2495 | std::shared_ptr<InputChannel> inputChannel = |
| 2496 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2497 | if (inputChannel == nullptr) { |
| 2498 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2499 | return; |
| 2500 | } |
| 2501 | inputTarget.inputChannel = inputChannel; |
| 2502 | inputTarget.flags = targetFlags; |
| 2503 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2504 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2505 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2506 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2507 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2508 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2509 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2510 | inputTargets.push_back(inputTarget); |
| 2511 | it = inputTargets.end() - 1; |
| 2512 | } |
| 2513 | |
| 2514 | ALOG_ASSERT(it->flags == targetFlags); |
| 2515 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2516 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2517 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2518 | } |
| 2519 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2520 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2521 | int32_t displayId) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2522 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2523 | mGlobalMonitorsByDisplay.find(displayId); |
| 2524 | |
| 2525 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2526 | const std::vector<Monitor>& monitors = it->second; |
| 2527 | for (const Monitor& monitor : monitors) { |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2528 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2529 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2530 | } |
| 2531 | } |
| 2532 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2533 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, int32_t displayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2534 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2535 | InputTarget target; |
| 2536 | target.inputChannel = monitor.inputChannel; |
| 2537 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2538 | ui::Transform t; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2539 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | b5402b2 | 2021-10-04 05:52:50 -0700 | [diff] [blame] | 2540 | const auto& displayTransform = it->second.transform; |
| 2541 | target.displayTransform = displayTransform; |
| 2542 | t = displayTransform; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2543 | } |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2544 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2545 | inputTargets.push_back(target); |
| 2546 | } |
| 2547 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2548 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2549 | const InjectionState* injectionState) { |
| 2550 | if (injectionState && |
| 2551 | (windowHandle == nullptr || |
| 2552 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2553 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2554 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2555 | 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] | 2556 | "owned by uid %d", |
| 2557 | injectionState->injectorPid, injectionState->injectorUid, |
| 2558 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2559 | } else { |
| 2560 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2561 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2562 | } |
| 2563 | return false; |
| 2564 | } |
| 2565 | return true; |
| 2566 | } |
| 2567 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2568 | /** |
| 2569 | * Indicate whether one window handle should be considered as obscuring |
| 2570 | * another window handle. We only check a few preconditions. Actually |
| 2571 | * checking the bounds is left to the caller. |
| 2572 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2573 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2574 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2575 | // Compare by token so cloned layers aren't counted |
| 2576 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2577 | return false; |
| 2578 | } |
| 2579 | auto info = windowHandle->getInfo(); |
| 2580 | auto otherInfo = otherHandle->getInfo(); |
| 2581 | if (!otherInfo->visible) { |
| 2582 | return false; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2583 | } else if (otherInfo->alpha == 0 && otherInfo->flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2584 | // Those act as if they were invisible, so we don't need to flag them. |
| 2585 | // We do want to potentially flag touchable windows even if they have 0 |
| 2586 | // opacity, since they can consume touches and alter the effects of the |
| 2587 | // user interaction (eg. apps that rely on |
| 2588 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2589 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2590 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2591 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2592 | // If ownerUid is the same we don't generate occlusion events as there |
| 2593 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2594 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2595 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2596 | return false; |
| 2597 | } else if (otherInfo->displayId != info->displayId) { |
| 2598 | return false; |
| 2599 | } |
| 2600 | return true; |
| 2601 | } |
| 2602 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2603 | /** |
| 2604 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2605 | * untrusted, one should check: |
| 2606 | * |
| 2607 | * 1. If result.hasBlockingOcclusion is true. |
| 2608 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2609 | * BLOCK_UNTRUSTED. |
| 2610 | * |
| 2611 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2612 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2613 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2614 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2615 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2616 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2617 | * |
| 2618 | * If neither of those is true, then it means the touch can be allowed. |
| 2619 | */ |
| 2620 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2621 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2622 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2623 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2624 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2625 | TouchOcclusionInfo info; |
| 2626 | info.hasBlockingOcclusion = false; |
| 2627 | info.obscuringOpacity = 0; |
| 2628 | info.obscuringUid = -1; |
| 2629 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2630 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2631 | if (windowHandle == otherHandle) { |
| 2632 | break; // All future windows are below us. Exit early. |
| 2633 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2634 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2635 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2636 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2637 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2638 | info.debugInfo.push_back( |
| 2639 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2640 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2641 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2642 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2643 | // window's touch occlusion mode |
| 2644 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2645 | info.hasBlockingOcclusion = true; |
| 2646 | info.obscuringUid = otherInfo->ownerUid; |
| 2647 | info.obscuringPackage = otherInfo->packageName; |
| 2648 | break; |
| 2649 | } |
| 2650 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2651 | uint32_t uid = otherInfo->ownerUid; |
| 2652 | float opacity = |
| 2653 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2654 | // Given windows A and B: |
| 2655 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2656 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2657 | opacityByUid[uid] = opacity; |
| 2658 | if (opacity > info.obscuringOpacity) { |
| 2659 | info.obscuringOpacity = opacity; |
| 2660 | info.obscuringUid = uid; |
| 2661 | info.obscuringPackage = otherInfo->packageName; |
| 2662 | } |
| 2663 | } |
| 2664 | } |
| 2665 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2666 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2667 | info.debugInfo.push_back( |
| 2668 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2669 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2670 | return info; |
| 2671 | } |
| 2672 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2673 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2674 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2675 | return StringPrintf(INDENT2 |
| 2676 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2677 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2678 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2679 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2680 | isTouchedWindow ? "[TOUCHED] " : "", ftl::enum_string(info->type).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2681 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2682 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2683 | info->frameTop, info->frameRight, info->frameBottom, |
| 2684 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2685 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2686 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2687 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2690 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2691 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2692 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2693 | occlusionInfo.obscuringUid); |
| 2694 | return false; |
| 2695 | } |
| 2696 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2697 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2698 | "%.2f, maximum allowed = %.2f)", |
| 2699 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2700 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2701 | return false; |
| 2702 | } |
| 2703 | return true; |
| 2704 | } |
| 2705 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2706 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2707 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2708 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2709 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2710 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2711 | if (windowHandle == otherHandle) { |
| 2712 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2713 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2714 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2715 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2716 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2717 | return true; |
| 2718 | } |
| 2719 | } |
| 2720 | return false; |
| 2721 | } |
| 2722 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2723 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2724 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2725 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2726 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2727 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2728 | if (windowHandle == otherHandle) { |
| 2729 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2730 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2731 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2732 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2733 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2734 | return true; |
| 2735 | } |
| 2736 | } |
| 2737 | return false; |
| 2738 | } |
| 2739 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2740 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2741 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2742 | if (applicationHandle != nullptr) { |
| 2743 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2744 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2745 | } else { |
| 2746 | return applicationHandle->getName(); |
| 2747 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2748 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2749 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2750 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2751 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2752 | } |
| 2753 | } |
| 2754 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2755 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2756 | if (!isUserActivityEvent(eventEntry)) { |
| 2757 | // 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] | 2758 | return; |
| 2759 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2760 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2761 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2762 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2763 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
| 2764 | if (info->inputFeatures.test(WindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2765 | if (DEBUG_DISPATCH_CYCLE) { |
| 2766 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2767 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2768 | return; |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2773 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2774 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2775 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2776 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2777 | return; |
| 2778 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2779 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2780 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2781 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2782 | } |
| 2783 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2784 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2785 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2786 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2787 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2788 | return; |
| 2789 | } |
| 2790 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2791 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2792 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2793 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2794 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2795 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2796 | break; |
| 2797 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2798 | } |
| 2799 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2800 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2801 | REQUIRES(mLock) { |
| 2802 | scoped_unlock unlock(mLock); |
| 2803 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2804 | }; |
| 2805 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2809 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2810 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2811 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2812 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2813 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2814 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2815 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2816 | ATRACE_NAME(message.c_str()); |
| 2817 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2818 | if (DEBUG_DISPATCH_CYCLE) { |
| 2819 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2820 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2821 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2822 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2823 | inputTarget.getPointerInfoString().c_str()); |
| 2824 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2825 | |
| 2826 | // Skip this event if the connection status is not normal. |
| 2827 | // 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^] | 2828 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2829 | if (DEBUG_DISPATCH_CYCLE) { |
| 2830 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame^] | 2831 | connection->getInputChannelName().c_str(), |
| 2832 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2833 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2834 | return; |
| 2835 | } |
| 2836 | |
| 2837 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2838 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2839 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2840 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2841 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2842 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2843 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2844 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2845 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2846 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2847 | if (!splitMotionEntry) { |
| 2848 | return; // split event was dropped |
| 2849 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2850 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2851 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2852 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2853 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2854 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2855 | if (DEBUG_FOCUS) { |
| 2856 | ALOGD("channel '%s' ~ Split motion event.", |
| 2857 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2858 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2859 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2860 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2861 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2862 | return; |
| 2863 | } |
| 2864 | } |
| 2865 | |
| 2866 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2867 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2868 | } |
| 2869 | |
| 2870 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2871 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2872 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2873 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2874 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2875 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2876 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2877 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2878 | ATRACE_NAME(message.c_str()); |
| 2879 | } |
| 2880 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2881 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2882 | |
| 2883 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2884 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2885 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2886 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2887 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2888 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2889 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2890 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2891 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2892 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2893 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2894 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2895 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2896 | |
| 2897 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2898 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2899 | startDispatchCycleLocked(currentTime, connection); |
| 2900 | } |
| 2901 | } |
| 2902 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2903 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2904 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2905 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2906 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2907 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2908 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2909 | connection->getInputChannelName().c_str(), |
| 2910 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2911 | ATRACE_NAME(message.c_str()); |
| 2912 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2913 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2914 | if (!(inputTargetFlags & dispatchMode)) { |
| 2915 | return; |
| 2916 | } |
| 2917 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2918 | |
| 2919 | // This is a new event. |
| 2920 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2921 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2922 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2923 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2924 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2925 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2926 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2927 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2928 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2929 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2930 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2931 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2932 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2933 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2934 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2935 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2936 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2937 | if (DEBUG_DISPATCH_CYCLE) { |
| 2938 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 2939 | "event", |
| 2940 | connection->getInputChannelName().c_str()); |
| 2941 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2942 | return; // skip the inconsistent event |
| 2943 | } |
| 2944 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2945 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2946 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2947 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2948 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2949 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2950 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2951 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2952 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2953 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2954 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2955 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2956 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2957 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2958 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2959 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2960 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2961 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2962 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2963 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2964 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2965 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2966 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2967 | } |
| 2968 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2969 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2970 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2971 | if (DEBUG_DISPATCH_CYCLE) { |
| 2972 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 2973 | "enter event", |
| 2974 | connection->getInputChannelName().c_str()); |
| 2975 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 2976 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 2977 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2978 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2979 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2980 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2981 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2982 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2983 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2984 | } |
| 2985 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2986 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2987 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2988 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2989 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2990 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2991 | if (DEBUG_DISPATCH_CYCLE) { |
| 2992 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2993 | "event", |
| 2994 | connection->getInputChannelName().c_str()); |
| 2995 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2996 | return; // skip the inconsistent event |
| 2997 | } |
| 2998 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2999 | dispatchEntry->resolvedEventId = |
| 3000 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3001 | ? mIdGenerator.nextId() |
| 3002 | : motionEntry.id; |
| 3003 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3004 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3005 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3006 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3007 | ATRACE_NAME(message.c_str()); |
| 3008 | } |
| 3009 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3010 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3011 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3012 | // Skip reporting pointer down outside focus to the policy. |
| 3013 | break; |
| 3014 | } |
| 3015 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3016 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3017 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3018 | |
| 3019 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3020 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3021 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3022 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3023 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3024 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3025 | break; |
| 3026 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3027 | case EventEntry::Type::SENSOR: { |
| 3028 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3029 | break; |
| 3030 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3031 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3032 | case EventEntry::Type::DEVICE_RESET: { |
| 3033 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3034 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3035 | break; |
| 3036 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | // Remember that we are waiting for this dispatch to complete. |
| 3040 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3041 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3045 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3046 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3047 | } |
| 3048 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3049 | /** |
| 3050 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3051 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3052 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3053 | * We will see both launcher and wallpaper in that list. |
| 3054 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3055 | * until the set of interacted connections changes. |
| 3056 | * |
| 3057 | * The following items are skipped, to reduce the logspam: |
| 3058 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3059 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3060 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3061 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3062 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3063 | */ |
| 3064 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3065 | const std::vector<InputTarget>& targets) { |
| 3066 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3067 | if (entry.type == EventEntry::Type::KEY) { |
| 3068 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3069 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3070 | return; |
| 3071 | } |
| 3072 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3073 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3074 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3075 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3076 | return; |
| 3077 | } |
| 3078 | } else { |
| 3079 | return; // Not a key or a motion |
| 3080 | } |
| 3081 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3082 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3083 | std::vector<sp<Connection>> newConnections; |
| 3084 | for (const InputTarget& target : targets) { |
| 3085 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3086 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3087 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3088 | } |
| 3089 | |
| 3090 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3091 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3092 | if (connection == nullptr) { |
| 3093 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3094 | } |
| 3095 | newConnectionTokens.insert(std::move(token)); |
| 3096 | newConnections.emplace_back(connection); |
| 3097 | } |
| 3098 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3099 | return; // no change |
| 3100 | } |
| 3101 | mInteractionConnectionTokens = newConnectionTokens; |
| 3102 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3103 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3104 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3105 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3106 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3107 | std::string message = "Interaction with: " + targetList; |
| 3108 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3109 | message += "<none>"; |
| 3110 | } |
| 3111 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3112 | } |
| 3113 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3114 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3115 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3116 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3117 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3118 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3119 | return; |
| 3120 | } |
| 3121 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3122 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3123 | if (focusedToken == token) { |
| 3124 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3125 | return; |
| 3126 | } |
| 3127 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3128 | auto command = [this, token]() REQUIRES(mLock) { |
| 3129 | scoped_unlock unlock(mLock); |
| 3130 | mPolicy->onPointerDownOutsideFocus(token); |
| 3131 | }; |
| 3132 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3136 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3137 | if (ATRACE_ENABLED()) { |
| 3138 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3139 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3140 | ATRACE_NAME(message.c_str()); |
| 3141 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3142 | if (DEBUG_DISPATCH_CYCLE) { |
| 3143 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3144 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3145 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame^] | 3146 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3147 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3148 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3149 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3150 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3151 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3152 | |
| 3153 | // Publish the event. |
| 3154 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3155 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3156 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3157 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3158 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3159 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3160 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3161 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3162 | status = connection->inputPublisher |
| 3163 | .publishKeyEvent(dispatchEntry->seq, |
| 3164 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3165 | keyEntry.source, keyEntry.displayId, |
| 3166 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3167 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3168 | keyEntry.scanCode, keyEntry.metaState, |
| 3169 | keyEntry.repeatCount, keyEntry.downTime, |
| 3170 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3171 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3172 | } |
| 3173 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3174 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3175 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3176 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3177 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3178 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3179 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3180 | // 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] | 3181 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3182 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3183 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3184 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3185 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3186 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3187 | // Don't apply window scale here since we don't want scale to affect raw |
| 3188 | // coordinates. The scale will be sent back to the client and applied |
| 3189 | // later when requesting relative coordinates. |
| 3190 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3191 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3192 | } |
| 3193 | usingCoords = scaledCoords; |
| 3194 | } |
| 3195 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3196 | // We don't want the dispatch target to know. |
| 3197 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3198 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3199 | scaledCoords[i].clear(); |
| 3200 | } |
| 3201 | usingCoords = scaledCoords; |
| 3202 | } |
| 3203 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3204 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3205 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3206 | |
| 3207 | // Publish the motion event. |
| 3208 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3209 | .publishMotionEvent(dispatchEntry->seq, |
| 3210 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3211 | motionEntry.deviceId, motionEntry.source, |
| 3212 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3213 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3214 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3215 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3216 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3217 | motionEntry.buttonState, |
| 3218 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3219 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3220 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3221 | motionEntry.xCursorPosition, |
| 3222 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3223 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3224 | motionEntry.downTime, motionEntry.eventTime, |
| 3225 | motionEntry.pointerCount, |
| 3226 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3227 | break; |
| 3228 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3229 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3230 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3231 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3232 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3233 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3234 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3235 | break; |
| 3236 | } |
| 3237 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3238 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3239 | const TouchModeEntry& touchModeEntry = |
| 3240 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3241 | status = connection->inputPublisher |
| 3242 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3243 | touchModeEntry.inTouchMode); |
| 3244 | |
| 3245 | break; |
| 3246 | } |
| 3247 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3248 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3249 | const auto& captureEntry = |
| 3250 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3251 | status = connection->inputPublisher |
| 3252 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3253 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3254 | break; |
| 3255 | } |
| 3256 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3257 | case EventEntry::Type::DRAG: { |
| 3258 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3259 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3260 | dragEntry.id, dragEntry.x, |
| 3261 | dragEntry.y, |
| 3262 | dragEntry.isExiting); |
| 3263 | break; |
| 3264 | } |
| 3265 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3266 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3267 | case EventEntry::Type::DEVICE_RESET: |
| 3268 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3269 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3270 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3271 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3272 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | // Check the result. |
| 3276 | if (status) { |
| 3277 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3278 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3279 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3280 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3281 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3282 | "event to it, status=%s(%d)", |
| 3283 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3284 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3285 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3286 | } else { |
| 3287 | // Pipe is full and we are waiting for the app to finish process some events |
| 3288 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3289 | if (DEBUG_DISPATCH_CYCLE) { |
| 3290 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3291 | "waiting for the application to catch up", |
| 3292 | connection->getInputChannelName().c_str()); |
| 3293 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3294 | } |
| 3295 | } else { |
| 3296 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3297 | "status=%s(%d)", |
| 3298 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3299 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3300 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3301 | } |
| 3302 | return; |
| 3303 | } |
| 3304 | |
| 3305 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3306 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3307 | connection->outboundQueue.end(), |
| 3308 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3309 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3310 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3311 | if (connection->responsive) { |
| 3312 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3313 | connection->inputChannel->getConnectionToken()); |
| 3314 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3315 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3316 | } |
| 3317 | } |
| 3318 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3319 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3320 | size_t size; |
| 3321 | switch (event.type) { |
| 3322 | case VerifiedInputEvent::Type::KEY: { |
| 3323 | size = sizeof(VerifiedKeyEvent); |
| 3324 | break; |
| 3325 | } |
| 3326 | case VerifiedInputEvent::Type::MOTION: { |
| 3327 | size = sizeof(VerifiedMotionEvent); |
| 3328 | break; |
| 3329 | } |
| 3330 | } |
| 3331 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3332 | return mHmacKeyManager.sign(start, size); |
| 3333 | } |
| 3334 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3335 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3336 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3337 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3338 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3339 | // Only sign events up and down events as the purely move events |
| 3340 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3341 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3342 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3343 | |
| 3344 | VerifiedMotionEvent verifiedEvent = |
| 3345 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3346 | verifiedEvent.actionMasked = actionMasked; |
| 3347 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3348 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3352 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3353 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3354 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3355 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3356 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3357 | } |
| 3358 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3359 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3360 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3361 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3362 | if (DEBUG_DISPATCH_CYCLE) { |
| 3363 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3364 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3365 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3366 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame^] | 3367 | if (connection->status == Connection::Status::BROKEN || |
| 3368 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3369 | return; |
| 3370 | } |
| 3371 | |
| 3372 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3373 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3374 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3375 | }; |
| 3376 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3377 | } |
| 3378 | |
| 3379 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3380 | const sp<Connection>& connection, |
| 3381 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3382 | if (DEBUG_DISPATCH_CYCLE) { |
| 3383 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3384 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3385 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3386 | |
| 3387 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3388 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3389 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3390 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3391 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3392 | |
| 3393 | // The connection appears to be unrecoverably broken. |
| 3394 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame^] | 3395 | if (connection->status == Connection::Status::NORMAL) { |
| 3396 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3397 | |
| 3398 | if (notify) { |
| 3399 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3400 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3401 | connection->getInputChannelName().c_str()); |
| 3402 | |
| 3403 | auto command = [this, connection]() REQUIRES(mLock) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame^] | 3404 | if (connection->status == Connection::Status::ZOMBIE) return; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3405 | scoped_unlock unlock(mLock); |
| 3406 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3407 | }; |
| 3408 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3409 | } |
| 3410 | } |
| 3411 | } |
| 3412 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3413 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3414 | while (!queue.empty()) { |
| 3415 | DispatchEntry* dispatchEntry = queue.front(); |
| 3416 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3417 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3418 | } |
| 3419 | } |
| 3420 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3421 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3422 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3423 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3424 | } |
| 3425 | delete dispatchEntry; |
| 3426 | } |
| 3427 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3428 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3429 | std::scoped_lock _l(mLock); |
| 3430 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3431 | if (connection == nullptr) { |
| 3432 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3433 | connectionToken.get(), events); |
| 3434 | return 0; // remove the callback |
| 3435 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3436 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3437 | bool notify; |
| 3438 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3439 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3440 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3441 | "events=0x%x", |
| 3442 | connection->getInputChannelName().c_str(), events); |
| 3443 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3444 | } |
| 3445 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3446 | nsecs_t currentTime = now(); |
| 3447 | bool gotOne = false; |
| 3448 | status_t status = OK; |
| 3449 | for (;;) { |
| 3450 | Result<InputPublisher::ConsumerResponse> result = |
| 3451 | connection->inputPublisher.receiveConsumerResponse(); |
| 3452 | if (!result.ok()) { |
| 3453 | status = result.error().code(); |
| 3454 | break; |
| 3455 | } |
| 3456 | |
| 3457 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3458 | const InputPublisher::Finished& finish = |
| 3459 | std::get<InputPublisher::Finished>(*result); |
| 3460 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3461 | finish.consumeTime); |
| 3462 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3463 | if (shouldReportMetricsForConnection(*connection)) { |
| 3464 | const InputPublisher::Timeline& timeline = |
| 3465 | std::get<InputPublisher::Timeline>(*result); |
| 3466 | mLatencyTracker |
| 3467 | .trackGraphicsLatency(timeline.inputEventId, |
| 3468 | connection->inputChannel->getConnectionToken(), |
| 3469 | std::move(timeline.graphicsTimeline)); |
| 3470 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3471 | } |
| 3472 | gotOne = true; |
| 3473 | } |
| 3474 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3475 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3476 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3477 | return 1; |
| 3478 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3479 | } |
| 3480 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3481 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3482 | if (notify) { |
| 3483 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3484 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3485 | status); |
| 3486 | } |
| 3487 | } else { |
| 3488 | // Monitor channels are never explicitly unregistered. |
| 3489 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3490 | const bool stillHaveWindowHandle = |
| 3491 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3492 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3493 | if (notify) { |
| 3494 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3495 | connection->getInputChannelName().c_str(), events); |
| 3496 | } |
| 3497 | } |
| 3498 | |
| 3499 | // Remove the channel. |
| 3500 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3501 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3502 | } |
| 3503 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3504 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3505 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3506 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3507 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3508 | } |
| 3509 | } |
| 3510 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3511 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3512 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3513 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3514 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3515 | } |
| 3516 | |
| 3517 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3518 | const CancelationOptions& options, |
| 3519 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3520 | for (const auto& it : monitorsByDisplay) { |
| 3521 | const std::vector<Monitor>& monitors = it.second; |
| 3522 | for (const Monitor& monitor : monitors) { |
| 3523 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3524 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3525 | } |
| 3526 | } |
| 3527 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3528 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3529 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3530 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3531 | if (connection == nullptr) { |
| 3532 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3533 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3534 | |
| 3535 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3539 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame^] | 3540 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3541 | return; |
| 3542 | } |
| 3543 | |
| 3544 | nsecs_t currentTime = now(); |
| 3545 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3546 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3547 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3548 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3549 | if (cancelationEvents.empty()) { |
| 3550 | return; |
| 3551 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3552 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3553 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3554 | "with reality: %s, mode=%d.", |
| 3555 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3556 | options.mode); |
| 3557 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3558 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3559 | std::string reason = std::string("reason=").append(options.reason); |
| 3560 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3561 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3562 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3563 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3564 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3565 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3566 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3567 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3568 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3569 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3570 | } |
| 3571 | target.inputChannel = connection->inputChannel; |
| 3572 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3573 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3574 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3575 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3576 | switch (cancelationEventEntry->type) { |
| 3577 | case EventEntry::Type::KEY: { |
| 3578 | logOutboundKeyDetails("cancel - ", |
| 3579 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3580 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3581 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3582 | case EventEntry::Type::MOTION: { |
| 3583 | logOutboundMotionDetails("cancel - ", |
| 3584 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3585 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3586 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3587 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3588 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3589 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3590 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3591 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3592 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3593 | break; |
| 3594 | } |
| 3595 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3596 | case EventEntry::Type::DEVICE_RESET: |
| 3597 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3598 | 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] | 3599 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3600 | break; |
| 3601 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3602 | } |
| 3603 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3604 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3605 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3606 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3607 | |
| 3608 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3609 | } |
| 3610 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3611 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3612 | const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame^] | 3613 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3614 | return; |
| 3615 | } |
| 3616 | |
| 3617 | nsecs_t currentTime = now(); |
| 3618 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3619 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3620 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3621 | |
| 3622 | if (downEvents.empty()) { |
| 3623 | return; |
| 3624 | } |
| 3625 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3626 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3627 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3628 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3629 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3630 | |
| 3631 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3632 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3633 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3634 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3635 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3636 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3637 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3638 | } |
| 3639 | target.inputChannel = connection->inputChannel; |
| 3640 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3641 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3642 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3643 | switch (downEventEntry->type) { |
| 3644 | case EventEntry::Type::MOTION: { |
| 3645 | logOutboundMotionDetails("down - ", |
| 3646 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3647 | break; |
| 3648 | } |
| 3649 | |
| 3650 | case EventEntry::Type::KEY: |
| 3651 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3652 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3653 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3654 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3655 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3656 | case EventEntry::Type::SENSOR: |
| 3657 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3658 | 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] | 3659 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3660 | break; |
| 3661 | } |
| 3662 | } |
| 3663 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3664 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3665 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | startDispatchCycleLocked(currentTime, connection); |
| 3669 | } |
| 3670 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3671 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3672 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3673 | ALOG_ASSERT(pointerIds.value != 0); |
| 3674 | |
| 3675 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3676 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3677 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3678 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3679 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3680 | uint32_t splitPointerCount = 0; |
| 3681 | |
| 3682 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3683 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3684 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3685 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3686 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3687 | if (pointerIds.hasBit(pointerId)) { |
| 3688 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3689 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3690 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3691 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3692 | splitPointerCount += 1; |
| 3693 | } |
| 3694 | } |
| 3695 | |
| 3696 | if (splitPointerCount != pointerIds.count()) { |
| 3697 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3698 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3699 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3700 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3701 | // in this way. |
| 3702 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3703 | "we expected there to be %d pointers. This probably means we received " |
| 3704 | "a broken sequence of pointer ids from the input device.", |
| 3705 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3706 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3707 | } |
| 3708 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3709 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3710 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3711 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3712 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3713 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3714 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3715 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3716 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3717 | if (pointerIds.hasBit(pointerId)) { |
| 3718 | if (pointerIds.count() == 1) { |
| 3719 | // The first/last pointer went down/up. |
| 3720 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3721 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3722 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3723 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3724 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3725 | } else { |
| 3726 | // A secondary pointer went down/up. |
| 3727 | uint32_t splitPointerIndex = 0; |
| 3728 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3729 | splitPointerIndex += 1; |
| 3730 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3731 | action = maskedAction | |
| 3732 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3733 | } |
| 3734 | } else { |
| 3735 | // An unrelated pointer changed. |
| 3736 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3737 | } |
| 3738 | } |
| 3739 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3740 | int32_t newId = mIdGenerator.nextId(); |
| 3741 | if (ATRACE_ENABLED()) { |
| 3742 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3743 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3744 | originalMotionEntry.id, newId); |
| 3745 | ATRACE_NAME(message.c_str()); |
| 3746 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3747 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3748 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3749 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3750 | originalMotionEntry.displayId, |
| 3751 | originalMotionEntry.policyFlags, action, |
| 3752 | originalMotionEntry.actionButton, |
| 3753 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3754 | originalMotionEntry.buttonState, |
| 3755 | originalMotionEntry.classification, |
| 3756 | originalMotionEntry.edgeFlags, |
| 3757 | originalMotionEntry.xPrecision, |
| 3758 | originalMotionEntry.yPrecision, |
| 3759 | originalMotionEntry.xCursorPosition, |
| 3760 | originalMotionEntry.yCursorPosition, |
| 3761 | originalMotionEntry.downTime, splitPointerCount, |
| 3762 | splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3763 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3764 | if (originalMotionEntry.injectionState) { |
| 3765 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3766 | splitMotionEntry->injectionState->refCount += 1; |
| 3767 | } |
| 3768 | |
| 3769 | return splitMotionEntry; |
| 3770 | } |
| 3771 | |
| 3772 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3773 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3774 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3775 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3776 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3777 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3778 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3779 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3780 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3781 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3782 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3783 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3784 | } // release lock |
| 3785 | |
| 3786 | if (needWake) { |
| 3787 | mLooper->wake(); |
| 3788 | } |
| 3789 | } |
| 3790 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3791 | /** |
| 3792 | * If one of the meta shortcuts is detected, process them here: |
| 3793 | * Meta + Backspace -> generate BACK |
| 3794 | * Meta + Enter -> generate HOME |
| 3795 | * This will potentially overwrite keyCode and metaState. |
| 3796 | */ |
| 3797 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3798 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3799 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3800 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3801 | if (keyCode == AKEYCODE_DEL) { |
| 3802 | newKeyCode = AKEYCODE_BACK; |
| 3803 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3804 | newKeyCode = AKEYCODE_HOME; |
| 3805 | } |
| 3806 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3807 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3808 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3809 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3810 | keyCode = newKeyCode; |
| 3811 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3812 | } |
| 3813 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3814 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3815 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3816 | // 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] | 3817 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3818 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3819 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3820 | if (replacementIt != mReplacedKeys.end()) { |
| 3821 | keyCode = replacementIt->second; |
| 3822 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3823 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3824 | } |
| 3825 | } |
| 3826 | } |
| 3827 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3828 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3829 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3830 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3831 | "policyFlags=0x%x, action=0x%x, " |
| 3832 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3833 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3834 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3835 | args->downTime); |
| 3836 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3837 | if (!validateKeyEvent(args->action)) { |
| 3838 | return; |
| 3839 | } |
| 3840 | |
| 3841 | uint32_t policyFlags = args->policyFlags; |
| 3842 | int32_t flags = args->flags; |
| 3843 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3844 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3845 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3846 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3847 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3848 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3849 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3850 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3851 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3852 | metaState |= AMETA_FUNCTION_ON; |
| 3853 | } |
| 3854 | |
| 3855 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3856 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3857 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3858 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3859 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3860 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3861 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3862 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3863 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3864 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3865 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3866 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3867 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3868 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3869 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3870 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3871 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3872 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3873 | { // acquire lock |
| 3874 | mLock.lock(); |
| 3875 | |
| 3876 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3877 | mLock.unlock(); |
| 3878 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3879 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3880 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3881 | return; // event was consumed by the filter |
| 3882 | } |
| 3883 | |
| 3884 | mLock.lock(); |
| 3885 | } |
| 3886 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3887 | std::unique_ptr<KeyEntry> newEntry = |
| 3888 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3889 | args->displayId, policyFlags, args->action, flags, |
| 3890 | keyCode, args->scanCode, metaState, repeatCount, |
| 3891 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3892 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3893 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3894 | mLock.unlock(); |
| 3895 | } // release lock |
| 3896 | |
| 3897 | if (needWake) { |
| 3898 | mLooper->wake(); |
| 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3903 | return mInputFilterEnabled; |
| 3904 | } |
| 3905 | |
| 3906 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3907 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3908 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3909 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 3910 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3911 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 3912 | "yCursorPosition=%f, downTime=%" PRId64, |
| 3913 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3914 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3915 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3916 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 3917 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3918 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 3919 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3920 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3921 | "orientation=%f", |
| 3922 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3923 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3924 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3925 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3926 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3927 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3928 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3929 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3930 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3931 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 3932 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3933 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3934 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3935 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3936 | return; |
| 3937 | } |
| 3938 | |
| 3939 | uint32_t policyFlags = args->policyFlags; |
| 3940 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3941 | |
| 3942 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3943 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3944 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3945 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3946 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3947 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3948 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3949 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3950 | { // acquire lock |
| 3951 | mLock.lock(); |
| 3952 | |
| 3953 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3954 | ui::Transform displayTransform; |
| 3955 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 3956 | displayTransform = it->second.transform; |
| 3957 | } |
| 3958 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3959 | mLock.unlock(); |
| 3960 | |
| 3961 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3962 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3963 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3964 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3965 | displayTransform, args->xPrecision, args->yPrecision, |
| 3966 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3967 | args->downTime, args->eventTime, args->pointerCount, |
| 3968 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3969 | |
| 3970 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3971 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3972 | return; // event was consumed by the filter |
| 3973 | } |
| 3974 | |
| 3975 | mLock.lock(); |
| 3976 | } |
| 3977 | |
| 3978 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3979 | std::unique_ptr<MotionEntry> newEntry = |
| 3980 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 3981 | args->source, args->displayId, policyFlags, |
| 3982 | args->action, args->actionButton, args->flags, |
| 3983 | args->metaState, args->buttonState, |
| 3984 | args->classification, args->edgeFlags, |
| 3985 | args->xPrecision, args->yPrecision, |
| 3986 | args->xCursorPosition, args->yCursorPosition, |
| 3987 | args->downTime, args->pointerCount, |
| 3988 | args->pointerProperties, args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3989 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 3990 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 3991 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 3992 | !mInputFilterEnabled) { |
| 3993 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 3994 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 3995 | } |
| 3996 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3997 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3998 | mLock.unlock(); |
| 3999 | } // release lock |
| 4000 | |
| 4001 | if (needWake) { |
| 4002 | mLooper->wake(); |
| 4003 | } |
| 4004 | } |
| 4005 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4006 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4007 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4008 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4009 | " sensorType=%s", |
| 4010 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4011 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4012 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4013 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4014 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4015 | { // acquire lock |
| 4016 | mLock.lock(); |
| 4017 | |
| 4018 | // Just enqueue a new sensor event. |
| 4019 | std::unique_ptr<SensorEntry> newEntry = |
| 4020 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4021 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4022 | args->sensorType, args->accuracy, |
| 4023 | args->accuracyChanged, args->values); |
| 4024 | |
| 4025 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4026 | mLock.unlock(); |
| 4027 | } // release lock |
| 4028 | |
| 4029 | if (needWake) { |
| 4030 | mLooper->wake(); |
| 4031 | } |
| 4032 | } |
| 4033 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4034 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4035 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4036 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4037 | args->deviceId, args->isOn); |
| 4038 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4039 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4040 | } |
| 4041 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4042 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4043 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4044 | } |
| 4045 | |
| 4046 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4047 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4048 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4049 | "switchMask=0x%08x", |
| 4050 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4051 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4052 | |
| 4053 | uint32_t policyFlags = args->policyFlags; |
| 4054 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4055 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4059 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4060 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4061 | args->deviceId); |
| 4062 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4063 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4064 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4065 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4066 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4067 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4068 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4069 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4070 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4071 | } // release lock |
| 4072 | |
| 4073 | if (needWake) { |
| 4074 | mLooper->wake(); |
| 4075 | } |
| 4076 | } |
| 4077 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4078 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4079 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4080 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4081 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4082 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4083 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4084 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4085 | { // acquire lock |
| 4086 | std::scoped_lock _l(mLock); |
| 4087 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4088 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4089 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4090 | } // release lock |
| 4091 | |
| 4092 | if (needWake) { |
| 4093 | mLooper->wake(); |
| 4094 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4095 | } |
| 4096 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4097 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4098 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4099 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4100 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4101 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 4102 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4103 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
| 4104 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4105 | 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] | 4106 | |
| 4107 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4108 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4109 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4110 | } |
| 4111 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4112 | // 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] | 4113 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4114 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4115 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4116 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4117 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4118 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4119 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4120 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4123 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4124 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4125 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4126 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4127 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4128 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4129 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4130 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4131 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4132 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4133 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4134 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4135 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4136 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4137 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4138 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4139 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4140 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4141 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4142 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4143 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4144 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4145 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4146 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4147 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4148 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4149 | |
| 4150 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4151 | android::base::Timer t; |
| 4152 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4153 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4154 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4155 | std::to_string(t.duration().count()).c_str()); |
| 4156 | } |
| 4157 | } |
| 4158 | |
| 4159 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4160 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4161 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4162 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4163 | incomingKey.getDisplayId(), policyFlags, action, |
| 4164 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4165 | incomingKey.getRepeatCount(), |
| 4166 | incomingKey.getDownTime()); |
| 4167 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4168 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4169 | } |
| 4170 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4171 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4172 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4173 | const int32_t action = motionEvent.getAction(); |
| 4174 | const bool isPointerEvent = |
| 4175 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4176 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4177 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4178 | ? ADISPLAY_ID_DEFAULT |
| 4179 | : event->getDisplayId(); |
| 4180 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4181 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4182 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4183 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4184 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4185 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4189 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4190 | android::base::Timer t; |
| 4191 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4192 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4193 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4194 | std::to_string(t.duration().count()).c_str()); |
| 4195 | } |
| 4196 | } |
| 4197 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4198 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4199 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4200 | } |
| 4201 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4202 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4203 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4204 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4205 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4206 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4207 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4208 | displayId, policyFlags, action, actionButton, |
| 4209 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4210 | motionEvent.getButtonState(), |
| 4211 | motionEvent.getClassification(), |
| 4212 | motionEvent.getEdgeFlags(), |
| 4213 | motionEvent.getXPrecision(), |
| 4214 | motionEvent.getYPrecision(), |
| 4215 | motionEvent.getRawXCursorPosition(), |
| 4216 | motionEvent.getRawYCursorPosition(), |
| 4217 | motionEvent.getDownTime(), uint32_t(pointerCount), |
| 4218 | pointerProperties, samplePointerCoords, |
| 4219 | motionEvent.getXOffset(), |
| 4220 | motionEvent.getYOffset()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4221 | transformMotionEntryForInjectionLocked(*injectedEntry); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4222 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4223 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4224 | sampleEventTimes += 1; |
| 4225 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4226 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4227 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4228 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4229 | displayId, policyFlags, action, actionButton, |
| 4230 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4231 | motionEvent.getButtonState(), |
| 4232 | motionEvent.getClassification(), |
| 4233 | motionEvent.getEdgeFlags(), |
| 4234 | motionEvent.getXPrecision(), |
| 4235 | motionEvent.getYPrecision(), |
| 4236 | motionEvent.getRawXCursorPosition(), |
| 4237 | motionEvent.getRawYCursorPosition(), |
| 4238 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4239 | uint32_t(pointerCount), pointerProperties, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4240 | samplePointerCoords, motionEvent.getXOffset(), |
| 4241 | motionEvent.getYOffset()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4242 | transformMotionEntryForInjectionLocked(*nextInjectedEntry); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4243 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4244 | } |
| 4245 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4246 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4247 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4248 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4249 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4250 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4251 | } |
| 4252 | |
| 4253 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4254 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4255 | injectionState->injectionIsAsync = true; |
| 4256 | } |
| 4257 | |
| 4258 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4259 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4260 | |
| 4261 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4262 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4263 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4264 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4265 | } |
| 4266 | |
| 4267 | mLock.unlock(); |
| 4268 | |
| 4269 | if (needWake) { |
| 4270 | mLooper->wake(); |
| 4271 | } |
| 4272 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4273 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4274 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4275 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4276 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4277 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4278 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4279 | } else { |
| 4280 | for (;;) { |
| 4281 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4282 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4283 | break; |
| 4284 | } |
| 4285 | |
| 4286 | nsecs_t remainingTimeout = endTime - now(); |
| 4287 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4288 | if (DEBUG_INJECTION) { |
| 4289 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4290 | "to become available."); |
| 4291 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4292 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4293 | break; |
| 4294 | } |
| 4295 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4296 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4297 | } |
| 4298 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4299 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4300 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4301 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4302 | if (DEBUG_INJECTION) { |
| 4303 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4304 | injectionState->pendingForegroundDispatches); |
| 4305 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4306 | nsecs_t remainingTimeout = endTime - now(); |
| 4307 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4308 | if (DEBUG_INJECTION) { |
| 4309 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4310 | "dispatches to finish."); |
| 4311 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4312 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4313 | break; |
| 4314 | } |
| 4315 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4316 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4317 | } |
| 4318 | } |
| 4319 | } |
| 4320 | |
| 4321 | injectionState->release(); |
| 4322 | } // release lock |
| 4323 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4324 | if (DEBUG_INJECTION) { |
| 4325 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
| 4326 | injectionResult, injectorPid, injectorUid); |
| 4327 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4328 | |
| 4329 | return injectionResult; |
| 4330 | } |
| 4331 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4332 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4333 | std::array<uint8_t, 32> calculatedHmac; |
| 4334 | std::unique_ptr<VerifiedInputEvent> result; |
| 4335 | switch (event.getType()) { |
| 4336 | case AINPUT_EVENT_TYPE_KEY: { |
| 4337 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4338 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4339 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4340 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4341 | break; |
| 4342 | } |
| 4343 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4344 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4345 | VerifiedMotionEvent verifiedMotionEvent = |
| 4346 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4347 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4348 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4349 | break; |
| 4350 | } |
| 4351 | default: { |
| 4352 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4353 | return nullptr; |
| 4354 | } |
| 4355 | } |
| 4356 | if (calculatedHmac == INVALID_HMAC) { |
| 4357 | return nullptr; |
| 4358 | } |
| 4359 | if (calculatedHmac != event.getHmac()) { |
| 4360 | return nullptr; |
| 4361 | } |
| 4362 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4363 | } |
| 4364 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4365 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4366 | return injectorUid == 0 || |
| 4367 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4368 | } |
| 4369 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4370 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4371 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4372 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4373 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4374 | if (DEBUG_INJECTION) { |
| 4375 | ALOGD("Setting input event injection result to %d. " |
| 4376 | "injectorPid=%d, injectorUid=%d", |
| 4377 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
| 4378 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4379 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4380 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4381 | // Log the outcome since the injector did not wait for the injection result. |
| 4382 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4383 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4384 | ALOGV("Asynchronous input event injection succeeded."); |
| 4385 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4386 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4387 | ALOGW("Asynchronous input event injection failed."); |
| 4388 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4389 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4390 | ALOGW("Asynchronous input event injection permission denied."); |
| 4391 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4392 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4393 | ALOGW("Asynchronous input event injection timed out."); |
| 4394 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4395 | case InputEventInjectionResult::PENDING: |
| 4396 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4397 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4398 | } |
| 4399 | } |
| 4400 | |
| 4401 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4402 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4406 | void InputDispatcher::transformMotionEntryForInjectionLocked(MotionEntry& entry) const { |
| 4407 | const bool isRelativeMouseEvent = isFromSource(entry.source, AINPUT_SOURCE_MOUSE_RELATIVE); |
| 4408 | if (!isRelativeMouseEvent && !isFromSource(entry.source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 4409 | return; |
| 4410 | } |
| 4411 | |
| 4412 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4413 | // display space, so we need to transform the injected events accordingly. |
| 4414 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4415 | if (it == mDisplayInfos.end()) return; |
| 4416 | const auto& transformToDisplay = it->second.transform.inverse(); |
| 4417 | |
| 4418 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 4419 | PointerCoords& pc = entry.pointerCoords[i]; |
| 4420 | const auto xy = isRelativeMouseEvent |
| 4421 | ? transformWithoutTranslation(transformToDisplay, pc.getX(), pc.getY()) |
| 4422 | : transformToDisplay.transform(pc.getXYValue()); |
| 4423 | pc.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 4424 | pc.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
| 4425 | |
| 4426 | // Axes with relative values never represent points on a screen, so they should never have |
| 4427 | // translation applied. If a device does not report relative values, these values are always |
| 4428 | // 0, and will remain unaffected by the following operation. |
| 4429 | const auto rel = |
| 4430 | transformWithoutTranslation(transformToDisplay, |
| 4431 | pc.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 4432 | pc.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)); |
| 4433 | pc.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, rel.x); |
| 4434 | pc.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, rel.y); |
| 4435 | } |
| 4436 | } |
| 4437 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4438 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4439 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4440 | if (injectionState) { |
| 4441 | injectionState->pendingForegroundDispatches += 1; |
| 4442 | } |
| 4443 | } |
| 4444 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4445 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4446 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4447 | if (injectionState) { |
| 4448 | injectionState->pendingForegroundDispatches -= 1; |
| 4449 | |
| 4450 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4451 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4452 | } |
| 4453 | } |
| 4454 | } |
| 4455 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4456 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4457 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4458 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4459 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4460 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4461 | } |
| 4462 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4463 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4464 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4465 | if (windowHandleToken == nullptr) { |
| 4466 | return nullptr; |
| 4467 | } |
| 4468 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4469 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4470 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4471 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4472 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4473 | return windowHandle; |
| 4474 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4475 | } |
| 4476 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4477 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4478 | } |
| 4479 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4480 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4481 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4482 | if (windowHandleToken == nullptr) { |
| 4483 | return nullptr; |
| 4484 | } |
| 4485 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4486 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4487 | if (windowHandle->getToken() == windowHandleToken) { |
| 4488 | return windowHandle; |
| 4489 | } |
| 4490 | } |
| 4491 | return nullptr; |
| 4492 | } |
| 4493 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4494 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4495 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4496 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4497 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4498 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4499 | if (handle->getId() == windowHandle->getId() && |
| 4500 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4501 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4502 | ALOGE("Found window %s in display %" PRId32 |
| 4503 | ", but it should belong to display %" PRId32, |
| 4504 | windowHandle->getName().c_str(), it.first, |
| 4505 | windowHandle->getInfo()->displayId); |
| 4506 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4507 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4508 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4509 | } |
| 4510 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4511 | return nullptr; |
| 4512 | } |
| 4513 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4514 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4515 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4516 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4517 | } |
| 4518 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4519 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4520 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4521 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4522 | windowHandle.getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4523 | if (connection != nullptr && noInputChannel) { |
| 4524 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4525 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4526 | return false; |
| 4527 | } |
| 4528 | |
| 4529 | if (connection == nullptr) { |
| 4530 | if (!noInputChannel) { |
| 4531 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4532 | } |
| 4533 | return false; |
| 4534 | } |
| 4535 | if (!connection->responsive) { |
| 4536 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4537 | return false; |
| 4538 | } |
| 4539 | return true; |
| 4540 | } |
| 4541 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4542 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4543 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4544 | auto connectionIt = mConnectionsByToken.find(token); |
| 4545 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4546 | return nullptr; |
| 4547 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4548 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4549 | } |
| 4550 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4551 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4552 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4553 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4554 | // Remove all handles on a display if there are no windows left. |
| 4555 | mWindowHandlesByDisplay.erase(displayId); |
| 4556 | return; |
| 4557 | } |
| 4558 | |
| 4559 | // Since we compare the pointer of input window handles across window updates, we need |
| 4560 | // 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] | 4561 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4562 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4563 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4564 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4565 | } |
| 4566 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4567 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4568 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4569 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4570 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4571 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4572 | info->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4573 | const bool canReceiveInput = !info->flags.test(WindowInfo::Flag::NOT_TOUCHABLE) || |
| 4574 | !info->flags.test(WindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4575 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4576 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4577 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4578 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4579 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | if (info->displayId != displayId) { |
| 4583 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4584 | handle->getName().c_str(), displayId, info->displayId); |
| 4585 | continue; |
| 4586 | } |
| 4587 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4588 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4589 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4590 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4591 | oldHandle->updateFrom(handle); |
| 4592 | newHandles.push_back(oldHandle); |
| 4593 | } else { |
| 4594 | newHandles.push_back(handle); |
| 4595 | } |
| 4596 | } |
| 4597 | |
| 4598 | // Insert or replace |
| 4599 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4600 | } |
| 4601 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4602 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4603 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4604 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4605 | { // acquire lock |
| 4606 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4607 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4608 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4609 | } |
| 4610 | } |
| 4611 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4612 | mLooper->wake(); |
| 4613 | } |
| 4614 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4615 | /** |
| 4616 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4617 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4618 | * If set an empty list, remove all handles from the specific display. |
| 4619 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4620 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4621 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4622 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4623 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4624 | if (DEBUG_FOCUS) { |
| 4625 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4626 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4627 | windowList += iwh->getName() + " "; |
| 4628 | } |
| 4629 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4630 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4631 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4632 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4633 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4634 | const bool noInputWindow = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4635 | window->getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4636 | if (noInputWindow && window->getToken() != nullptr) { |
| 4637 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4638 | window->getName().c_str()); |
| 4639 | window->releaseChannel(); |
| 4640 | } |
| 4641 | } |
| 4642 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4643 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4644 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4645 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4646 | // Save the old windows' orientation by ID before it gets updated. |
| 4647 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4648 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4649 | oldWindowOrientations.emplace(handle->getId(), |
| 4650 | handle->getInfo()->transform.getOrientation()); |
| 4651 | } |
| 4652 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4653 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4654 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4655 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4656 | if (mLastHoverWindowHandle && |
| 4657 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4658 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4659 | mLastHoverWindowHandle = nullptr; |
| 4660 | } |
| 4661 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4662 | std::optional<FocusResolver::FocusChanges> changes = |
| 4663 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4664 | if (changes) { |
| 4665 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4666 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4667 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4668 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4669 | mTouchStatesByDisplay.find(displayId); |
| 4670 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4671 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4672 | for (size_t i = 0; i < state.windows.size();) { |
| 4673 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4674 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4675 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4676 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4677 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4678 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4679 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4680 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4681 | if (touchedInputChannel != nullptr) { |
| 4682 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4683 | "touched window was removed"); |
| 4684 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4685 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4686 | // well. |
| 4687 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
| 4688 | touchedWindow.windowHandle->getInfo()->hasWallpaper) { |
| 4689 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4690 | if (wallpaper != nullptr) { |
| 4691 | sp<Connection> wallpaperConnection = |
| 4692 | getConnectionLocked(wallpaper->getToken()); |
| 4693 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4694 | options); |
| 4695 | } |
| 4696 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4697 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4698 | state.windows.erase(state.windows.begin() + i); |
| 4699 | } else { |
| 4700 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4701 | } |
| 4702 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4703 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4704 | // 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] | 4705 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4706 | if (mDragState && |
| 4707 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4708 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4709 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4710 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4711 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4712 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4713 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4714 | // pointer events if necessary. |
| 4715 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4716 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4717 | if (newWindowHandle != nullptr && |
| 4718 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4719 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4720 | std::shared_ptr<InputChannel> inputChannel = |
| 4721 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4722 | if (inputChannel != nullptr) { |
| 4723 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4724 | "touched window's orientation changed"); |
| 4725 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4726 | } |
| 4727 | } |
| 4728 | } |
| 4729 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4730 | // Release information for windows that are no longer present. |
| 4731 | // This ensures that unused input channels are released promptly. |
| 4732 | // Otherwise, they might stick around until the window handle is destroyed |
| 4733 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4734 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4735 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4736 | if (DEBUG_FOCUS) { |
| 4737 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4738 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4739 | oldWindowHandle->releaseChannel(); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4740 | // To avoid making too many calls into the compat framework, only |
| 4741 | // check for window flags when windows are going away. |
| 4742 | // TODO(b/157929241) : delete this. This is only needed temporarily |
| 4743 | // in order to gather some data about the flag usage |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4744 | if (oldWindowHandle->getInfo()->flags.test(WindowInfo::Flag::SLIPPERY)) { |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4745 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", |
| 4746 | oldWindowHandle->getName().c_str()); |
| 4747 | if (mCompatService != nullptr) { |
| 4748 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, |
| 4749 | oldWindowHandle->getInfo()->ownerUid); |
| 4750 | } |
| 4751 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4752 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4753 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4757 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4758 | if (DEBUG_FOCUS) { |
| 4759 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4760 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4761 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4762 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4763 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4764 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4765 | } // release lock |
| 4766 | |
| 4767 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4768 | mLooper->wake(); |
| 4769 | } |
| 4770 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4771 | void InputDispatcher::setFocusedApplicationLocked( |
| 4772 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4773 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4774 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4775 | |
| 4776 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4777 | return; // This application is already focused. No need to wake up or change anything. |
| 4778 | } |
| 4779 | |
| 4780 | // Set the new application handle. |
| 4781 | if (inputApplicationHandle != nullptr) { |
| 4782 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4783 | } else { |
| 4784 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4785 | } |
| 4786 | |
| 4787 | // No matter what the old focused application was, stop waiting on it because it is |
| 4788 | // no longer focused. |
| 4789 | resetNoFocusedWindowTimeoutLocked(); |
| 4790 | } |
| 4791 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4792 | /** |
| 4793 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4794 | * the display not specified. |
| 4795 | * |
| 4796 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4797 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4798 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4799 | * display. The display-specified events won't be affected. |
| 4800 | */ |
| 4801 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4802 | if (DEBUG_FOCUS) { |
| 4803 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4804 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4805 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4806 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4807 | |
| 4808 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4809 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4810 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4811 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4812 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4813 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4814 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4815 | CancelationOptions |
| 4816 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4817 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4818 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4819 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4820 | } |
| 4821 | } |
| 4822 | mFocusedDisplayId = displayId; |
| 4823 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4824 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4825 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4826 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4827 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4828 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4829 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4830 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4831 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4832 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4833 | } |
| 4834 | } |
| 4835 | } |
| 4836 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4837 | if (DEBUG_FOCUS) { |
| 4838 | logDispatchStateLocked(); |
| 4839 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4840 | } // release lock |
| 4841 | |
| 4842 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4843 | mLooper->wake(); |
| 4844 | } |
| 4845 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4846 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4847 | if (DEBUG_FOCUS) { |
| 4848 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4849 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4850 | |
| 4851 | bool changed; |
| 4852 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4853 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4854 | |
| 4855 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4856 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4857 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | if (mDispatchEnabled && !enabled) { |
| 4861 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4862 | } |
| 4863 | |
| 4864 | mDispatchEnabled = enabled; |
| 4865 | mDispatchFrozen = frozen; |
| 4866 | changed = true; |
| 4867 | } else { |
| 4868 | changed = false; |
| 4869 | } |
| 4870 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4871 | if (DEBUG_FOCUS) { |
| 4872 | logDispatchStateLocked(); |
| 4873 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4874 | } // release lock |
| 4875 | |
| 4876 | if (changed) { |
| 4877 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4878 | mLooper->wake(); |
| 4879 | } |
| 4880 | } |
| 4881 | |
| 4882 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4883 | if (DEBUG_FOCUS) { |
| 4884 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4885 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4886 | |
| 4887 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4888 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4889 | |
| 4890 | if (mInputFilterEnabled == enabled) { |
| 4891 | return; |
| 4892 | } |
| 4893 | |
| 4894 | mInputFilterEnabled = enabled; |
| 4895 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4896 | } // release lock |
| 4897 | |
| 4898 | // Wake up poll loop since there might be work to do to drop everything. |
| 4899 | mLooper->wake(); |
| 4900 | } |
| 4901 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4902 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4903 | bool needWake = false; |
| 4904 | { |
| 4905 | std::scoped_lock lock(mLock); |
| 4906 | if (mInTouchMode == inTouchMode) { |
| 4907 | return; |
| 4908 | } |
| 4909 | if (DEBUG_TOUCH_MODE) { |
| 4910 | ALOGD("Request to change touch mode from %s to %s", toString(mInTouchMode), |
| 4911 | toString(inTouchMode)); |
| 4912 | // TODO(b/198487159): Also print the current last interacted apps. |
| 4913 | } |
| 4914 | |
| 4915 | // TODO(b/198499018): Store touch mode per display. |
| 4916 | mInTouchMode = inTouchMode; |
| 4917 | |
| 4918 | // TODO(b/198487159): Enforce that only last interacted apps can change touch mode. |
| 4919 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 4920 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4921 | } // release lock |
| 4922 | |
| 4923 | if (needWake) { |
| 4924 | mLooper->wake(); |
| 4925 | } |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4926 | } |
| 4927 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4928 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4929 | if (opacity < 0 || opacity > 1) { |
| 4930 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4931 | return; |
| 4932 | } |
| 4933 | |
| 4934 | std::scoped_lock lock(mLock); |
| 4935 | mMaximumObscuringOpacityForTouch = opacity; |
| 4936 | } |
| 4937 | |
| 4938 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4939 | std::scoped_lock lock(mLock); |
| 4940 | mBlockUntrustedTouchesMode = mode; |
| 4941 | } |
| 4942 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4943 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 4944 | const sp<IBinder>& token) { |
| 4945 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 4946 | for (TouchedWindow& w : state.windows) { |
| 4947 | if (w.windowHandle->getToken() == token) { |
| 4948 | return std::make_pair(&state, &w); |
| 4949 | } |
| 4950 | } |
| 4951 | } |
| 4952 | return std::make_pair(nullptr, nullptr); |
| 4953 | } |
| 4954 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4955 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 4956 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4957 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4958 | if (DEBUG_FOCUS) { |
| 4959 | ALOGD("Trivial transfer to same window."); |
| 4960 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4961 | return true; |
| 4962 | } |
| 4963 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4964 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4965 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4966 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4967 | // Find the target touch state and touched window by fromToken. |
| 4968 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 4969 | if (state == nullptr || touchedWindow == nullptr) { |
| 4970 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4971 | return false; |
| 4972 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4973 | |
| 4974 | const int32_t displayId = state->displayId; |
| 4975 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 4976 | if (toWindowHandle == nullptr) { |
| 4977 | ALOGW("Cannot transfer focus because to window not found."); |
| 4978 | return false; |
| 4979 | } |
| 4980 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4981 | if (DEBUG_FOCUS) { |
| 4982 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4983 | touchedWindow->windowHandle->getName().c_str(), |
| 4984 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4985 | } |
| 4986 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4987 | // Erase old window. |
| 4988 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 4989 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 4990 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4991 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4992 | // Add new window. |
| 4993 | int32_t newTargetFlags = oldTargetFlags & |
| 4994 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4995 | InputTarget::FLAG_DISPATCH_AS_IS); |
| 4996 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4997 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4998 | // Store the dragging window. |
| 4999 | if (isDragDrop) { |
| 5000 | mDragState = std::make_unique<DragState>(toWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5001 | } |
| 5002 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5003 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5004 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5005 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5006 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5007 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5008 | CancelationOptions |
| 5009 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5010 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5011 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5012 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5013 | } |
| 5014 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5015 | if (DEBUG_FOCUS) { |
| 5016 | logDispatchStateLocked(); |
| 5017 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5018 | } // release lock |
| 5019 | |
| 5020 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5021 | mLooper->wake(); |
| 5022 | return true; |
| 5023 | } |
| 5024 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5025 | // Binder call |
| 5026 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 5027 | sp<IBinder> fromToken; |
| 5028 | { // acquire lock |
| 5029 | std::scoped_lock _l(mLock); |
| 5030 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5031 | auto it = std::find_if(mTouchStatesByDisplay.begin(), mTouchStatesByDisplay.end(), |
| 5032 | [](const auto& pair) { return pair.second.windows.size() == 1; }); |
| 5033 | if (it == mTouchStatesByDisplay.end()) { |
| 5034 | ALOGW("Cannot transfer touch state because there is no exact window being touched"); |
| 5035 | return false; |
| 5036 | } |
| 5037 | const int32_t displayId = it->first; |
| 5038 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5039 | if (toWindowHandle == nullptr) { |
| 5040 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 5041 | return false; |
| 5042 | } |
| 5043 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5044 | TouchState& state = it->second; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5045 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 5046 | fromToken = touchedWindow.windowHandle->getToken(); |
| 5047 | } // release lock |
| 5048 | |
| 5049 | return transferTouchFocus(fromToken, destChannelToken); |
| 5050 | } |
| 5051 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5052 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5053 | if (DEBUG_FOCUS) { |
| 5054 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5055 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5056 | |
| 5057 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5058 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5059 | |
| 5060 | resetKeyRepeatLocked(); |
| 5061 | releasePendingEventLocked(); |
| 5062 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5063 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5064 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5065 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5066 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5067 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5068 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5069 | } |
| 5070 | |
| 5071 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5072 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5073 | dumpDispatchStateLocked(dump); |
| 5074 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5075 | std::istringstream stream(dump); |
| 5076 | std::string line; |
| 5077 | |
| 5078 | while (std::getline(stream, line, '\n')) { |
| 5079 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5080 | } |
| 5081 | } |
| 5082 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5083 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5084 | std::string dump; |
| 5085 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5086 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5087 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5088 | |
| 5089 | std::string windowName = "None"; |
| 5090 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5091 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5092 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5093 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5094 | : "token has capture without window"; |
| 5095 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5096 | 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] | 5097 | |
| 5098 | return dump; |
| 5099 | } |
| 5100 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5101 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5102 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5103 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5104 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5105 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5106 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5107 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5108 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5109 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5110 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5111 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5112 | const std::chrono::duration timeout = |
| 5113 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5114 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5115 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5116 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5117 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5118 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5119 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5120 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5121 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5122 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5123 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5124 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5125 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5126 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5127 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5128 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5129 | 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] | 5130 | state.displayId, toString(state.down), toString(state.split), |
| 5131 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5132 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5133 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5134 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5135 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5136 | dump += StringPrintf(INDENT4 |
| 5137 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5138 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5139 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5140 | } |
| 5141 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5142 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5143 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5144 | } |
| 5145 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5146 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5147 | } |
| 5148 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5149 | if (mDragState) { |
| 5150 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5151 | mDragState->dump(dump, INDENT2); |
| 5152 | } |
| 5153 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5154 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5155 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5156 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5157 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5158 | const auto& displayInfo = it->second; |
| 5159 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5160 | displayInfo.logicalHeight); |
| 5161 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5162 | } else { |
| 5163 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5164 | } |
| 5165 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5166 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5167 | dump += INDENT2 "Windows:\n"; |
| 5168 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5169 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5170 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5171 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5172 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5173 | "paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5174 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5175 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5176 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5177 | "applicationInfo.name=%s, " |
| 5178 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5179 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5180 | i, windowInfo->name.c_str(), windowInfo->id, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5181 | windowInfo->displayId, toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5182 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5183 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5184 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 5185 | windowInfo->flags.string().c_str(), |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 5186 | ftl::enum_string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5187 | windowInfo->frameLeft, windowInfo->frameTop, |
| 5188 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5189 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5190 | windowInfo->applicationInfo.name.c_str(), |
| 5191 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5192 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5193 | dump += StringPrintf(", inputFeatures=%s", |
| 5194 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5195 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5196 | "ms, trustedOverlay=%s, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5197 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5198 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5199 | millis(windowInfo->dispatchingTimeout), |
| 5200 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5201 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5202 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5203 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5204 | } |
| 5205 | } else { |
| 5206 | dump += INDENT2 "Windows: <none>\n"; |
| 5207 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5208 | } |
| 5209 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5210 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5211 | } |
| 5212 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5213 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5214 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5215 | const std::vector<Monitor>& monitors = it.second; |
| 5216 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5217 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5218 | } |
| 5219 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5220 | const std::vector<Monitor>& monitors = it.second; |
| 5221 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5222 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5223 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5224 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5225 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5226 | } |
| 5227 | |
| 5228 | nsecs_t currentTime = now(); |
| 5229 | |
| 5230 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5231 | if (!mRecentQueue.empty()) { |
| 5232 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5233 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5234 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5235 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5236 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5237 | } |
| 5238 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5239 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5240 | } |
| 5241 | |
| 5242 | // Dump event currently being dispatched. |
| 5243 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5244 | dump += INDENT "PendingEvent:\n"; |
| 5245 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5246 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5247 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5248 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5249 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5250 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5251 | } |
| 5252 | |
| 5253 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5254 | if (!mInboundQueue.empty()) { |
| 5255 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5256 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5257 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5258 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5259 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5260 | } |
| 5261 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5262 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5263 | } |
| 5264 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5265 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5266 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5267 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5268 | const KeyReplacement& replacement = pair.first; |
| 5269 | int32_t newKeyCode = pair.second; |
| 5270 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5271 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5272 | } |
| 5273 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5274 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5275 | } |
| 5276 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5277 | if (!mCommandQueue.empty()) { |
| 5278 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5279 | } else { |
| 5280 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5281 | } |
| 5282 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5283 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5284 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5285 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5286 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5287 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5288 | connection->inputChannel->getFd().get(), |
| 5289 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame^] | 5290 | connection->getWindowName().c_str(), |
| 5291 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5292 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5293 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5294 | if (!connection->outboundQueue.empty()) { |
| 5295 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5296 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5297 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5298 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5299 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5300 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5301 | } |
| 5302 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5303 | if (!connection->waitQueue.empty()) { |
| 5304 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5305 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5306 | dump += dumpQueue(connection->waitQueue, currentTime); |
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 += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5309 | } |
| 5310 | } |
| 5311 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5312 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5313 | } |
| 5314 | |
| 5315 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5316 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5317 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5318 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5319 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5320 | } |
| 5321 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5322 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5323 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5324 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5325 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5326 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5327 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5328 | } |
| 5329 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5330 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5331 | const size_t numMonitors = monitors.size(); |
| 5332 | for (size_t i = 0; i < numMonitors; i++) { |
| 5333 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5334 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5335 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5336 | dump += "\n"; |
| 5337 | } |
| 5338 | } |
| 5339 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5340 | class LooperEventCallback : public LooperCallback { |
| 5341 | public: |
| 5342 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5343 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5344 | |
| 5345 | private: |
| 5346 | std::function<int(int events)> mCallback; |
| 5347 | }; |
| 5348 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5349 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5350 | if (DEBUG_CHANNEL_CREATION) { |
| 5351 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5352 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5353 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5354 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5355 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5356 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5357 | |
| 5358 | if (result) { |
| 5359 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5360 | } |
| 5361 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5362 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5363 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5364 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5365 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5366 | sp<Connection> connection = |
| 5367 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5368 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5369 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5370 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5371 | } |
| 5372 | mConnectionsByToken.emplace(token, connection); |
| 5373 | |
| 5374 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5375 | this, std::placeholders::_1, token); |
| 5376 | |
| 5377 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5378 | } // release lock |
| 5379 | |
| 5380 | // Wake the looper because some connections have changed. |
| 5381 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5382 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5383 | } |
| 5384 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5385 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5386 | bool isGestureMonitor, |
| 5387 | const std::string& name, |
| 5388 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5389 | std::shared_ptr<InputChannel> serverChannel; |
| 5390 | std::unique_ptr<InputChannel> clientChannel; |
| 5391 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5392 | if (result) { |
| 5393 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5394 | } |
| 5395 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5396 | { // acquire lock |
| 5397 | std::scoped_lock _l(mLock); |
| 5398 | |
| 5399 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5400 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5401 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5402 | } |
| 5403 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5404 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5405 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5406 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5407 | |
| 5408 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5409 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5410 | } |
| 5411 | mConnectionsByToken.emplace(token, connection); |
| 5412 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5413 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5414 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5415 | auto& monitorsByDisplay = |
| 5416 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5417 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5418 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5419 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5420 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), |
| 5421 | displayId, toString(isGestureMonitor), pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5422 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5423 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5424 | // Wake the looper because some connections have changed. |
| 5425 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5426 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5427 | } |
| 5428 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5429 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5430 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5431 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5432 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5433 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5434 | if (status) { |
| 5435 | return status; |
| 5436 | } |
| 5437 | } // release lock |
| 5438 | |
| 5439 | // Wake the poll loop because removing the connection may have changed the current |
| 5440 | // synchronization state. |
| 5441 | mLooper->wake(); |
| 5442 | return OK; |
| 5443 | } |
| 5444 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5445 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5446 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5447 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5448 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5449 | // 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] | 5450 | return BAD_VALUE; |
| 5451 | } |
| 5452 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5453 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5454 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5455 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5456 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5457 | } |
| 5458 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5459 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5460 | |
| 5461 | nsecs_t currentTime = now(); |
| 5462 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5463 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame^] | 5464 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5465 | return OK; |
| 5466 | } |
| 5467 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5468 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5469 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5470 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5471 | } |
| 5472 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5473 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5474 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5475 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5476 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5477 | std::vector<Monitor>& monitors = it->second; |
| 5478 | const size_t numMonitors = monitors.size(); |
| 5479 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5480 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5481 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5482 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5483 | monitors.erase(monitors.begin() + i); |
| 5484 | break; |
| 5485 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5486 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5487 | if (monitors.empty()) { |
| 5488 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5489 | } else { |
| 5490 | ++it; |
| 5491 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5492 | } |
| 5493 | } |
| 5494 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5495 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5496 | { // acquire lock |
| 5497 | std::scoped_lock _l(mLock); |
| 5498 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 5499 | |
| 5500 | if (!foundDisplayId) { |
| 5501 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 5502 | return BAD_VALUE; |
| 5503 | } |
| 5504 | int32_t displayId = foundDisplayId.value(); |
| 5505 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5506 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5507 | mTouchStatesByDisplay.find(displayId); |
| 5508 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5509 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5510 | return BAD_VALUE; |
| 5511 | } |
| 5512 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5513 | TouchState& state = stateIt->second; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5514 | std::shared_ptr<InputChannel> requestingChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5515 | std::optional<int32_t> foundDeviceId; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 5516 | for (const auto& monitor : state.gestureMonitors) { |
| 5517 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 5518 | requestingChannel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5519 | foundDeviceId = state.deviceId; |
| 5520 | } |
| 5521 | } |
| 5522 | if (!foundDeviceId || !state.down) { |
| 5523 | ALOGW("Attempted to pilfer points from a monitor without any on-going pointer streams." |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5524 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5525 | return BAD_VALUE; |
| 5526 | } |
| 5527 | int32_t deviceId = foundDeviceId.value(); |
| 5528 | |
| 5529 | // Send cancel events to all the input channels we're stealing from. |
| 5530 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5531 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5532 | options.deviceId = deviceId; |
| 5533 | options.displayId = displayId; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5534 | std::string canceledWindows = "["; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5535 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5536 | std::shared_ptr<InputChannel> channel = |
| 5537 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5538 | if (channel != nullptr) { |
| 5539 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5540 | canceledWindows += channel->getName() + ", "; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5541 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5542 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5543 | canceledWindows += "]"; |
| 5544 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5545 | canceledWindows.c_str()); |
| 5546 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5547 | // Then clear the current touch state so we stop dispatching to them as well. |
| 5548 | state.filterNonMonitors(); |
| 5549 | } |
| 5550 | return OK; |
| 5551 | } |
| 5552 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5553 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5554 | { // acquire lock |
| 5555 | std::scoped_lock _l(mLock); |
| 5556 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5557 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5558 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5559 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5560 | : "token without window"); |
| 5561 | } |
| 5562 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5563 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5564 | if (focusedToken != windowToken) { |
| 5565 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5566 | enabled ? "enable" : "disable"); |
| 5567 | return; |
| 5568 | } |
| 5569 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5570 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5571 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5572 | "window has %s requested pointer capture.", |
| 5573 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5574 | return; |
| 5575 | } |
| 5576 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5577 | setPointerCaptureLocked(enabled); |
| 5578 | } // release lock |
| 5579 | |
| 5580 | // Wake the thread to process command entries. |
| 5581 | mLooper->wake(); |
| 5582 | } |
| 5583 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5584 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5585 | const sp<IBinder>& token) { |
| 5586 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5587 | const std::vector<Monitor>& monitors = it.second; |
| 5588 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5589 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5590 | return it.first; |
| 5591 | } |
| 5592 | } |
| 5593 | } |
| 5594 | return std::nullopt; |
| 5595 | } |
| 5596 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5597 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5598 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5599 | if (gesturePid.has_value()) { |
| 5600 | return gesturePid; |
| 5601 | } |
| 5602 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5603 | } |
| 5604 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5605 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5606 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5607 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5608 | } |
| 5609 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5610 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5611 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5612 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5613 | } |
| 5614 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5615 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5616 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5617 | } |
| 5618 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5619 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5620 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5621 | if (connection == nullptr) { |
| 5622 | return "<nullptr>"; |
| 5623 | } |
| 5624 | return connection->getInputChannelName(); |
| 5625 | } |
| 5626 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5627 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5628 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5629 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5630 | } |
| 5631 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5632 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5633 | const sp<Connection>& connection, uint32_t seq, |
| 5634 | bool handled, nsecs_t consumeTime) { |
| 5635 | // Handle post-event policy actions. |
| 5636 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5637 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5638 | return; |
| 5639 | } |
| 5640 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5641 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5642 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5643 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5644 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5645 | } |
| 5646 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5647 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5648 | connection->inputChannel->getConnectionToken(), |
| 5649 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5650 | } |
| 5651 | |
| 5652 | bool restartEvent; |
| 5653 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5654 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5655 | restartEvent = |
| 5656 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5657 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5658 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5659 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5660 | handled); |
| 5661 | } else { |
| 5662 | restartEvent = false; |
| 5663 | } |
| 5664 | |
| 5665 | // Dequeue the event and start the next cycle. |
| 5666 | // Because the lock might have been released, it is possible that the |
| 5667 | // contents of the wait queue to have been drained, so we need to double-check |
| 5668 | // a few things. |
| 5669 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5670 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5671 | dispatchEntry = *dispatchEntryIt; |
| 5672 | connection->waitQueue.erase(dispatchEntryIt); |
| 5673 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5674 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5675 | if (!connection->responsive) { |
| 5676 | connection->responsive = isConnectionResponsive(*connection); |
| 5677 | if (connection->responsive) { |
| 5678 | // The connection was unresponsive, and now it's responsive. |
| 5679 | processConnectionResponsiveLocked(*connection); |
| 5680 | } |
| 5681 | } |
| 5682 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame^] | 5683 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5684 | connection->outboundQueue.push_front(dispatchEntry); |
| 5685 | traceOutboundQueueLength(*connection); |
| 5686 | } else { |
| 5687 | releaseDispatchEntry(dispatchEntry); |
| 5688 | } |
| 5689 | } |
| 5690 | |
| 5691 | // Start the next dispatch cycle for this connection. |
| 5692 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5693 | } |
| 5694 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5695 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5696 | const sp<IBinder>& newToken) { |
| 5697 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5698 | scoped_unlock unlock(mLock); |
| 5699 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5700 | }; |
| 5701 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5702 | } |
| 5703 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5704 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5705 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5706 | scoped_unlock unlock(mLock); |
| 5707 | mPolicy->notifyDropWindow(token, x, y); |
| 5708 | }; |
| 5709 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5710 | } |
| 5711 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5712 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5713 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5714 | scoped_unlock unlock(mLock); |
| 5715 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5716 | }; |
| 5717 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5718 | } |
| 5719 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5720 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5721 | if (connection == nullptr) { |
| 5722 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5723 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5724 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5725 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5726 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5727 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5728 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5729 | return; |
| 5730 | } |
| 5731 | /** |
| 5732 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5733 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5734 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5735 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5736 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5737 | * most useful. |
| 5738 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5739 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5740 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5741 | std::string reason = |
| 5742 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5743 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5744 | ns2ms(currentWait), |
| 5745 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5746 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5747 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5748 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5749 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5750 | |
| 5751 | // Stop waking up for events on this connection, it is already unresponsive |
| 5752 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5753 | } |
| 5754 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5755 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5756 | std::string reason = |
| 5757 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5758 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5759 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5760 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5761 | scoped_unlock unlock(mLock); |
| 5762 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5763 | }; |
| 5764 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5767 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5768 | const std::string& reason) { |
| 5769 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5770 | updateLastAnrStateLocked(windowLabel, reason); |
| 5771 | } |
| 5772 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5773 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5774 | const std::string& reason) { |
| 5775 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5776 | updateLastAnrStateLocked(windowLabel, reason); |
| 5777 | } |
| 5778 | |
| 5779 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5780 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5781 | // 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] | 5782 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5783 | struct tm tm; |
| 5784 | localtime_r(&t, &tm); |
| 5785 | char timestr[64]; |
| 5786 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5787 | mLastAnrState.clear(); |
| 5788 | mLastAnrState += INDENT "ANR:\n"; |
| 5789 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5790 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5791 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5792 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5793 | } |
| 5794 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5795 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5796 | KeyEntry& entry) { |
| 5797 | const KeyEvent event = createKeyEvent(entry); |
| 5798 | nsecs_t delay = 0; |
| 5799 | { // release lock |
| 5800 | scoped_unlock unlock(mLock); |
| 5801 | android::base::Timer t; |
| 5802 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5803 | entry.policyFlags); |
| 5804 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5805 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5806 | std::to_string(t.duration().count()).c_str()); |
| 5807 | } |
| 5808 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5809 | |
| 5810 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5811 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5812 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5813 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5814 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5815 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5816 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5817 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5818 | } |
| 5819 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5820 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5821 | auto command = [this, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5822 | scoped_unlock unlock(mLock); |
| 5823 | mPolicy->notifyMonitorUnresponsive(pid, reason); |
| 5824 | }; |
| 5825 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5826 | } |
| 5827 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5828 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5829 | std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5830 | auto command = [this, token, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5831 | scoped_unlock unlock(mLock); |
| 5832 | mPolicy->notifyWindowUnresponsive(token, reason); |
| 5833 | }; |
| 5834 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5835 | } |
| 5836 | |
| 5837 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5838 | auto command = [this, pid]() REQUIRES(mLock) { |
| 5839 | scoped_unlock unlock(mLock); |
| 5840 | mPolicy->notifyMonitorResponsive(pid); |
| 5841 | }; |
| 5842 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5843 | } |
| 5844 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5845 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken) { |
| 5846 | auto command = [this, connectionToken]() REQUIRES(mLock) { |
| 5847 | scoped_unlock unlock(mLock); |
| 5848 | mPolicy->notifyWindowResponsive(connectionToken); |
| 5849 | }; |
| 5850 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | /** |
| 5854 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5855 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5856 | * command entry to the command queue. |
| 5857 | */ |
| 5858 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5859 | std::string reason) { |
| 5860 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5861 | if (connection.monitor) { |
| 5862 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5863 | reason.c_str()); |
| 5864 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5865 | if (!pid.has_value()) { |
| 5866 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5867 | connection.inputChannel->getName().c_str()); |
| 5868 | return; |
| 5869 | } |
| 5870 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5871 | return; |
| 5872 | } |
| 5873 | // If not a monitor, must be a window |
| 5874 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5875 | reason.c_str()); |
| 5876 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5877 | } |
| 5878 | |
| 5879 | /** |
| 5880 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5881 | */ |
| 5882 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5883 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5884 | if (connection.monitor) { |
| 5885 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5886 | if (!pid.has_value()) { |
| 5887 | ALOGE("Could not find responsive monitor for connection %s", |
| 5888 | connection.inputChannel->getName().c_str()); |
| 5889 | return; |
| 5890 | } |
| 5891 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5892 | return; |
| 5893 | } |
| 5894 | // If not a monitor, must be a window |
| 5895 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5896 | } |
| 5897 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5898 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5899 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5900 | KeyEntry& keyEntry, bool handled) { |
| 5901 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5902 | if (!handled) { |
| 5903 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5904 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5905 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5906 | return false; |
| 5907 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5908 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5909 | // Get the fallback key state. |
| 5910 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5911 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5912 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5913 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5914 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5915 | } |
| 5916 | |
| 5917 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5918 | // If the application handles the original key for which we previously |
| 5919 | // generated a fallback or if the window is not a foreground window, |
| 5920 | // then cancel the associated fallback key, if any. |
| 5921 | if (fallbackKeyCode != -1) { |
| 5922 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5923 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5924 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 5925 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5926 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 5927 | keyEntry.policyFlags); |
| 5928 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5929 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5930 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5931 | |
| 5932 | mLock.unlock(); |
| 5933 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5934 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5935 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5936 | |
| 5937 | mLock.lock(); |
| 5938 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5939 | // Cancel the fallback key. |
| 5940 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5941 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5942 | "application handled the original non-fallback key " |
| 5943 | "or is no longer a foreground target, " |
| 5944 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5945 | options.keyCode = fallbackKeyCode; |
| 5946 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5947 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5948 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5949 | } |
| 5950 | } else { |
| 5951 | // If the application did not handle a non-fallback key, first check |
| 5952 | // that we are in a good state to perform unhandled key event processing |
| 5953 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5954 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5955 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5956 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5957 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 5958 | "since this is not an initial down. " |
| 5959 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5960 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 5961 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5962 | return false; |
| 5963 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5964 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5965 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5966 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5967 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 5968 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5969 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 5970 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5971 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5972 | |
| 5973 | mLock.unlock(); |
| 5974 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5975 | bool fallback = |
| 5976 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5977 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5978 | |
| 5979 | mLock.lock(); |
| 5980 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame^] | 5981 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5982 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5983 | return false; |
| 5984 | } |
| 5985 | |
| 5986 | // Latch the fallback keycode for this key on an initial down. |
| 5987 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5988 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5989 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5990 | fallbackKeyCode = event.getKeyCode(); |
| 5991 | } else { |
| 5992 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5993 | } |
| 5994 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5995 | } |
| 5996 | |
| 5997 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5998 | |
| 5999 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6000 | // We will continue to dispatch the key to the policy but we will no |
| 6001 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6002 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6003 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6004 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6005 | if (fallback) { |
| 6006 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6007 | "as a fallback for %d, but on the DOWN it had requested " |
| 6008 | "to send %d instead. Fallback canceled.", |
| 6009 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6010 | } else { |
| 6011 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6012 | "but on the DOWN it had requested to send %d. " |
| 6013 | "Fallback canceled.", |
| 6014 | originalKeyCode, fallbackKeyCode); |
| 6015 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6016 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6017 | |
| 6018 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6019 | "canceling fallback, policy no longer desires it"); |
| 6020 | options.keyCode = fallbackKeyCode; |
| 6021 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6022 | |
| 6023 | fallback = false; |
| 6024 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6025 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6026 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6027 | } |
| 6028 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6029 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6030 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6031 | { |
| 6032 | std::string msg; |
| 6033 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6034 | connection->inputState.getFallbackKeys(); |
| 6035 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6036 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6037 | } |
| 6038 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6039 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6040 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6041 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6042 | |
| 6043 | if (fallback) { |
| 6044 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6045 | keyEntry.eventTime = event.getEventTime(); |
| 6046 | keyEntry.deviceId = event.getDeviceId(); |
| 6047 | keyEntry.source = event.getSource(); |
| 6048 | keyEntry.displayId = event.getDisplayId(); |
| 6049 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6050 | keyEntry.keyCode = fallbackKeyCode; |
| 6051 | keyEntry.scanCode = event.getScanCode(); |
| 6052 | keyEntry.metaState = event.getMetaState(); |
| 6053 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6054 | keyEntry.downTime = event.getDownTime(); |
| 6055 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6056 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6057 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6058 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6059 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6060 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6061 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6062 | return true; // restart the event |
| 6063 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6064 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6065 | ALOGD("Unhandled key event: No fallback key."); |
| 6066 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6067 | |
| 6068 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6069 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6070 | } |
| 6071 | } |
| 6072 | return false; |
| 6073 | } |
| 6074 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6075 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6076 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6077 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6078 | return false; |
| 6079 | } |
| 6080 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6081 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6082 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6083 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6084 | } |
| 6085 | } |
| 6086 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6087 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6088 | if (ATRACE_ENABLED()) { |
| 6089 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6090 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6091 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6092 | } |
| 6093 | } |
| 6094 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6095 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6096 | if (ATRACE_ENABLED()) { |
| 6097 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6098 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6099 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6100 | } |
| 6101 | } |
| 6102 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6103 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6104 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6105 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6106 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6107 | dumpDispatchStateLocked(dump); |
| 6108 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6109 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6110 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6111 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6112 | } |
| 6113 | } |
| 6114 | |
| 6115 | void InputDispatcher::monitor() { |
| 6116 | // 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] | 6117 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6118 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6119 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6120 | } |
| 6121 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6122 | /** |
| 6123 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6124 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6125 | * this method can be safely called from any thread, as long as you've ensured that |
| 6126 | * the work you are interested in completing has already been queued. |
| 6127 | */ |
| 6128 | bool InputDispatcher::waitForIdle() { |
| 6129 | /** |
| 6130 | * Timeout should represent the longest possible time that a device might spend processing |
| 6131 | * events and commands. |
| 6132 | */ |
| 6133 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6134 | std::unique_lock lock(mLock); |
| 6135 | mLooper->wake(); |
| 6136 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6137 | return result == std::cv_status::no_timeout; |
| 6138 | } |
| 6139 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6140 | /** |
| 6141 | * Sets focus to the window identified by the token. This must be called |
| 6142 | * after updating any input window handles. |
| 6143 | * |
| 6144 | * Params: |
| 6145 | * request.token - input channel token used to identify the window that should gain focus. |
| 6146 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6147 | * specified token does not match the currently focused window, this request will be dropped. |
| 6148 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6149 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6150 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6151 | * when requesting the focus change. This determines which request gets |
| 6152 | * precedence if there is a focus change request from another source such as pointer down. |
| 6153 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6154 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6155 | { // acquire lock |
| 6156 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6157 | std::optional<FocusResolver::FocusChanges> changes = |
| 6158 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6159 | if (changes) { |
| 6160 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6161 | } |
| 6162 | } // release lock |
| 6163 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6164 | mLooper->wake(); |
| 6165 | } |
| 6166 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6167 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6168 | if (changes.oldFocus) { |
| 6169 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6170 | if (focusedInputChannel) { |
| 6171 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6172 | "focus left window"); |
| 6173 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6174 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6175 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6176 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6177 | if (changes.newFocus) { |
| 6178 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6179 | } |
| 6180 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6181 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6182 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6183 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6184 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6185 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6186 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6187 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6188 | // front. |
| 6189 | disablePointerCaptureForcedLocked(); |
| 6190 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6191 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6192 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6193 | } |
| 6194 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6195 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6196 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6197 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6198 | return; |
| 6199 | } |
| 6200 | |
| 6201 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6202 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6203 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6204 | setPointerCaptureLocked(false); |
| 6205 | } |
| 6206 | |
| 6207 | if (!mWindowTokenWithPointerCapture) { |
| 6208 | // No need to send capture changes because no window has capture. |
| 6209 | return; |
| 6210 | } |
| 6211 | |
| 6212 | if (mPendingEvent != nullptr) { |
| 6213 | // Move the pending event to the front of the queue. This will give the chance |
| 6214 | // for the pending event to be dropped if it is a captured event. |
| 6215 | mInboundQueue.push_front(mPendingEvent); |
| 6216 | mPendingEvent = nullptr; |
| 6217 | } |
| 6218 | |
| 6219 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6220 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6221 | mInboundQueue.push_front(std::move(entry)); |
| 6222 | } |
| 6223 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6224 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6225 | mCurrentPointerCaptureRequest.enable = enable; |
| 6226 | mCurrentPointerCaptureRequest.seq++; |
| 6227 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6228 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6229 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6230 | }; |
| 6231 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6232 | } |
| 6233 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6234 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6235 | { // acquire lock |
| 6236 | std::scoped_lock _l(mLock); |
| 6237 | // Set an empty list to remove all handles from the specific display. |
| 6238 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6239 | setFocusedApplicationLocked(displayId, nullptr); |
| 6240 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6241 | // have been removed for the removed display. |
| 6242 | mFocusResolver.displayRemoved(displayId); |
| 6243 | } // release lock |
| 6244 | |
| 6245 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6246 | mLooper->wake(); |
| 6247 | } |
| 6248 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6249 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6250 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6251 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6252 | // more convenient parsing. |
| 6253 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6254 | for (const auto& info : windowInfos) { |
| 6255 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6256 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6257 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6258 | |
| 6259 | { // acquire lock |
| 6260 | std::scoped_lock _l(mLock); |
| 6261 | mDisplayInfos.clear(); |
| 6262 | for (const auto& displayInfo : displayInfos) { |
| 6263 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6264 | } |
| 6265 | |
| 6266 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6267 | setInputWindowsLocked(handles, displayId); |
| 6268 | } |
| 6269 | } |
| 6270 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6271 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6272 | } |
| 6273 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6274 | bool InputDispatcher::shouldDropInput( |
| 6275 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
| 6276 | if (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT) || |
| 6277 | (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED) && |
| 6278 | isWindowObscuredLocked(windowHandle))) { |
| 6279 | ALOGW("Dropping %s event targeting %s as requested by input feature %s on display " |
| 6280 | "%" PRId32 ".", |
| 6281 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
| 6282 | windowHandle->getInfo()->inputFeatures.string().c_str(), |
| 6283 | windowHandle->getInfo()->displayId); |
| 6284 | return true; |
| 6285 | } |
| 6286 | return false; |
| 6287 | } |
| 6288 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6289 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6290 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6291 | const std::vector<DisplayInfo>& displayInfos) { |
| 6292 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6293 | } |
| 6294 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6295 | } // namespace android::inputdispatcher |