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, |
Prabir Pradhan | d6a7f22 | 2021-11-24 15:36:43 +0000 | [diff] [blame^] | 381 | pointerCoords.data()); |
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 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 509 | // Returns true if the event type passed as argument represents a user activity. |
| 510 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 511 | switch (eventEntry.type) { |
| 512 | case EventEntry::Type::FOCUS: |
| 513 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 514 | case EventEntry::Type::DRAG: |
| 515 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 516 | case EventEntry::Type::SENSOR: |
| 517 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 518 | return false; |
| 519 | case EventEntry::Type::DEVICE_RESET: |
| 520 | case EventEntry::Type::KEY: |
| 521 | case EventEntry::Type::MOTION: |
| 522 | return true; |
| 523 | } |
| 524 | } |
| 525 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 526 | } // namespace |
| 527 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 528 | // --- InputDispatcher --- |
| 529 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 530 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 531 | : mPolicy(policy), |
| 532 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 533 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 534 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 535 | mAppSwitchSawKeyDown(false), |
| 536 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 537 | mNextUnblockedEvent(nullptr), |
| 538 | mDispatchEnabled(false), |
| 539 | mDispatchFrozen(false), |
| 540 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 541 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 542 | // To avoid leaking stack in case that call never comes, and for tests, |
| 543 | // initialize it here anyways. |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 544 | mInTouchMode(kDefaultInTouchMode), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 545 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 546 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 547 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 548 | mLatencyAggregator(), |
| 549 | mLatencyTracker(&mLatencyAggregator), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 550 | mCompatService(getCompatService()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 551 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 552 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 553 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 554 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 555 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 556 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 557 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 558 | |
| 559 | policy->getDispatcherConfiguration(&mConfig); |
| 560 | } |
| 561 | |
| 562 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 563 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 564 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 565 | resetKeyRepeatLocked(); |
| 566 | releasePendingEventLocked(); |
| 567 | drainInboundQueueLocked(); |
| 568 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 569 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 570 | while (!mConnectionsByToken.empty()) { |
| 571 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 572 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 573 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 577 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 578 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 579 | return ALREADY_EXISTS; |
| 580 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 581 | mThread = std::make_unique<InputThread>( |
| 582 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 583 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 587 | if (mThread && mThread->isCallingThread()) { |
| 588 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 589 | return INVALID_OPERATION; |
| 590 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 591 | mThread.reset(); |
| 592 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 595 | void InputDispatcher::dispatchOnce() { |
| 596 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 597 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 598 | std::scoped_lock _l(mLock); |
| 599 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 600 | |
| 601 | // Run a dispatch loop if there are no pending commands. |
| 602 | // The dispatch loop might enqueue commands to run afterwards. |
| 603 | if (!haveCommandsLocked()) { |
| 604 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 605 | } |
| 606 | |
| 607 | // Run all pending commands if there are any. |
| 608 | // 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] | 609 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 610 | nextWakeupTime = LONG_LONG_MIN; |
| 611 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 612 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 613 | // If we are still waiting for ack on some events, |
| 614 | // we might have to wake up earlier to check if an app is anr'ing. |
| 615 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 616 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 617 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 618 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 619 | // pending or queued events |
| 620 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 621 | mDispatcherEnteredIdle.notify_all(); |
| 622 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | } // release lock |
| 624 | |
| 625 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 626 | nsecs_t currentTime = now(); |
| 627 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 628 | mLooper->pollOnce(timeoutMillis); |
| 629 | } |
| 630 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 631 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 632 | * Raise ANR if there is no focused window. |
| 633 | * Before the ANR is raised, do a final state check: |
| 634 | * 1. The currently focused application must be the same one we are waiting for. |
| 635 | * 2. Ensure we still don't have a focused window. |
| 636 | */ |
| 637 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 638 | // Check if the application that we are waiting for is still focused. |
| 639 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 640 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 641 | if (focusedApplication == nullptr || |
| 642 | focusedApplication->getApplicationToken() != |
| 643 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 644 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 645 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 646 | focusedApplication->getName().c_str()); |
| 647 | return; // The focused application has changed. |
| 648 | } |
| 649 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 650 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 651 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 652 | if (focusedWindowHandle != nullptr) { |
| 653 | return; // We now have a focused window. No need for ANR. |
| 654 | } |
| 655 | onAnrLocked(mAwaitedFocusedApplication); |
| 656 | } |
| 657 | |
| 658 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 659 | * Check if any of the connections' wait queues have events that are too old. |
| 660 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 661 | * Return the time at which we should wake up next. |
| 662 | */ |
| 663 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 664 | const nsecs_t currentTime = now(); |
| 665 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 666 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 667 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 668 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 669 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 670 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 671 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 672 | return LONG_LONG_MIN; |
| 673 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 674 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 675 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | // Check if any connection ANRs are due |
| 680 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 681 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 682 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 683 | } |
| 684 | |
| 685 | // If we reached here, we have an unresponsive connection. |
| 686 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 687 | if (connection == nullptr) { |
| 688 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 689 | return nextAnrCheck; |
| 690 | } |
| 691 | connection->responsive = false; |
| 692 | // Stop waking up for this unresponsive connection |
| 693 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 694 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 695 | return LONG_LONG_MIN; |
| 696 | } |
| 697 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 698 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 699 | sp<WindowInfoHandle> window = getWindowHandleLocked(token); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 700 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 701 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 702 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 703 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 706 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 707 | nsecs_t currentTime = now(); |
| 708 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 709 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 710 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 711 | // repeat if the device is just coming out of sleep. |
| 712 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 713 | resetKeyRepeatLocked(); |
| 714 | } |
| 715 | |
| 716 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 717 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 718 | if (DEBUG_FOCUS) { |
| 719 | ALOGD("Dispatch frozen. Waiting some more."); |
| 720 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 721 | return; |
| 722 | } |
| 723 | |
| 724 | // Optimize latency of app switches. |
| 725 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 726 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 727 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 728 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 729 | *nextWakeupTime = mAppSwitchDueTime; |
| 730 | } |
| 731 | |
| 732 | // Ready to start a new event. |
| 733 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 734 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 735 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 736 | if (isAppSwitchDue) { |
| 737 | // The inbound queue is empty so the app switch key we were waiting |
| 738 | // for will never arrive. Stop waiting for it. |
| 739 | resetPendingAppSwitchLocked(false); |
| 740 | isAppSwitchDue = false; |
| 741 | } |
| 742 | |
| 743 | // Synthesize a key repeat if appropriate. |
| 744 | if (mKeyRepeatState.lastKeyEntry) { |
| 745 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 746 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 747 | } else { |
| 748 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 749 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | // Nothing to do if there is no pending event. |
| 755 | if (!mPendingEvent) { |
| 756 | return; |
| 757 | } |
| 758 | } else { |
| 759 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 760 | mPendingEvent = mInboundQueue.front(); |
| 761 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 762 | traceInboundQueueLengthLocked(); |
| 763 | } |
| 764 | |
| 765 | // Poke user activity for this event. |
| 766 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 767 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 768 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | // Now we have an event to dispatch. |
| 772 | // 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] | 773 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 774 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 775 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 776 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 777 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 778 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 779 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 783 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 787 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 788 | const ConfigurationChangedEntry& typedEntry = |
| 789 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 790 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 791 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 792 | break; |
| 793 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 794 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 795 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 796 | const DeviceResetEntry& typedEntry = |
| 797 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 798 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 799 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 800 | break; |
| 801 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 802 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 803 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 804 | std::shared_ptr<FocusEntry> typedEntry = |
| 805 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 806 | dispatchFocusLocked(currentTime, typedEntry); |
| 807 | done = true; |
| 808 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 809 | break; |
| 810 | } |
| 811 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 812 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 813 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 814 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 815 | done = true; |
| 816 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 817 | break; |
| 818 | } |
| 819 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 820 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 821 | const auto typedEntry = |
| 822 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 823 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 824 | done = true; |
| 825 | break; |
| 826 | } |
| 827 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 828 | case EventEntry::Type::DRAG: { |
| 829 | std::shared_ptr<DragEntry> typedEntry = |
| 830 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 831 | dispatchDragLocked(currentTime, typedEntry); |
| 832 | done = true; |
| 833 | break; |
| 834 | } |
| 835 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 836 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 837 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 838 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 839 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 840 | resetPendingAppSwitchLocked(true); |
| 841 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 842 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 843 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 844 | } |
| 845 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 846 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 847 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 848 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 849 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 850 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 851 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 852 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 853 | break; |
| 854 | } |
| 855 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 856 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 857 | std::shared_ptr<MotionEntry> motionEntry = |
| 858 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 859 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 860 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 861 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 862 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 863 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 864 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 865 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 866 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 867 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 868 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 869 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 870 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 871 | |
| 872 | case EventEntry::Type::SENSOR: { |
| 873 | std::shared_ptr<SensorEntry> sensorEntry = |
| 874 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 875 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 876 | dropReason = DropReason::APP_SWITCH; |
| 877 | } |
| 878 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 879 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 880 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 881 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 882 | dropReason = DropReason::STALE; |
| 883 | } |
| 884 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 885 | done = true; |
| 886 | break; |
| 887 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 891 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 892 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 893 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 894 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 895 | |
| 896 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 897 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 901 | /** |
| 902 | * Return true if the events preceding this incoming motion event should be dropped |
| 903 | * Return false otherwise (the default behaviour) |
| 904 | */ |
| 905 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 906 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 907 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 908 | |
| 909 | // Optimize case where the current application is unresponsive and the user |
| 910 | // decides to touch a window in a different application. |
| 911 | // If the application takes too long to catch up then we drop all events preceding |
| 912 | // the touch into the other window. |
| 913 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 914 | int32_t displayId = motionEntry.displayId; |
| 915 | int32_t x = static_cast<int32_t>( |
| 916 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 917 | int32_t y = static_cast<int32_t>( |
| 918 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 919 | sp<WindowInfoHandle> touchedWindowHandle = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 920 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 921 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 922 | touchedWindowHandle->getApplicationToken() != |
| 923 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 924 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 925 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 926 | "for %s", |
| 927 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 928 | return true; |
| 929 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 930 | |
| 931 | // Alternatively, maybe there's a gesture monitor that could handle this event |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 932 | for (const auto& monitor : getValueByKey(mGestureMonitorsByDisplay, displayId)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 933 | sp<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 934 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 935 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 936 | // This monitor could take more input. Drop all events preceding this |
| 937 | // event, so that gesture monitor could get a chance to receive the stream |
| 938 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 939 | "responsive gesture monitor that may handle the event", |
| 940 | mAwaitedFocusedApplication->getName().c_str()); |
| 941 | return true; |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 947 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 948 | // events to be processed before dispatching the key event. This is because these motion events |
| 949 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 950 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 951 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 952 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 953 | "just send the pending key event to the focused window."); |
| 954 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 955 | } |
| 956 | return false; |
| 957 | } |
| 958 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 959 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 960 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 961 | mInboundQueue.push_back(std::move(newEntry)); |
| 962 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 963 | traceInboundQueueLengthLocked(); |
| 964 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 965 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 966 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 967 | // Optimize app switch latency. |
| 968 | // If the application takes too long to catch up then we drop all events preceding |
| 969 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 970 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 971 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 972 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 973 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 974 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 975 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 976 | if (DEBUG_APP_SWITCH) { |
| 977 | ALOGD("App switch is pending!"); |
| 978 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 979 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 980 | mAppSwitchSawKeyDown = false; |
| 981 | needWake = true; |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | break; |
| 986 | } |
| 987 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 988 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 989 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 990 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 991 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 992 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 993 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 994 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 995 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 996 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 997 | break; |
| 998 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 999 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1000 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1001 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1002 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1003 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1004 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1005 | // nothing to do |
| 1006 | break; |
| 1007 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | return needWake; |
| 1011 | } |
| 1012 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1013 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1014 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1015 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1016 | mRecentQueue.push_back(entry); |
| 1017 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1018 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1019 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1023 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1024 | int32_t y, TouchState* touchState, |
| 1025 | bool addOutsideTargets, |
| 1026 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1027 | if (addOutsideTargets && touchState == nullptr) { |
| 1028 | 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] | 1029 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1030 | // Traverse windows from front to back to find touched window. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1031 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 1032 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1033 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1034 | continue; |
| 1035 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1036 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1037 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1038 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1039 | |
| 1040 | if (windowInfo->visible) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1041 | if (!flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
| 1042 | bool isTouchModal = !flags.test(WindowInfo::Flag::NOT_FOCUSABLE) && |
| 1043 | !flags.test(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1044 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
| 1045 | // Found window. |
| 1046 | return windowHandle; |
| 1047 | } |
| 1048 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1049 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1050 | if (addOutsideTargets && flags.test(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1051 | touchState->addOrUpdateWindow(windowHandle, |
| 1052 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1053 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1054 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1055 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1056 | } |
| 1057 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1058 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1061 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1062 | const char* reason; |
| 1063 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1064 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1065 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1066 | ALOGD("Dropped event because policy consumed it."); |
| 1067 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1068 | reason = "inbound event was dropped because the policy consumed it"; |
| 1069 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1070 | case DropReason::DISABLED: |
| 1071 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1072 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1073 | } |
| 1074 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1075 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1076 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1077 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1078 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1079 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1080 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1081 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1082 | "has started interacting with a different application."); |
| 1083 | reason = "inbound event was dropped because the current application is not responding " |
| 1084 | "and the user has started interacting with a different application"; |
| 1085 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1086 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1087 | ALOGI("Dropped event because it is stale."); |
| 1088 | reason = "inbound event was dropped because it is stale"; |
| 1089 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1090 | case DropReason::NO_POINTER_CAPTURE: |
| 1091 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1092 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1093 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1094 | case DropReason::NOT_DROPPED: { |
| 1095 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1096 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1097 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1098 | } |
| 1099 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1100 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1101 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1102 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1103 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1104 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1105 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1106 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1107 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1108 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1109 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1110 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1111 | } else { |
| 1112 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1113 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1114 | } |
| 1115 | break; |
| 1116 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1117 | case EventEntry::Type::SENSOR: { |
| 1118 | break; |
| 1119 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1120 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1121 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1122 | break; |
| 1123 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1124 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1125 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1126 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1127 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1128 | 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] | 1129 | break; |
| 1130 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1134 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1135 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1136 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1139 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1140 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1141 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1142 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1146 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1147 | } |
| 1148 | |
| 1149 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1150 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1151 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1152 | if (DEBUG_APP_SWITCH) { |
| 1153 | if (handled) { |
| 1154 | ALOGD("App switch has arrived."); |
| 1155 | } else { |
| 1156 | ALOGD("App switch was abandoned."); |
| 1157 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1158 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1161 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1162 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1165 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1166 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1167 | return false; |
| 1168 | } |
| 1169 | |
| 1170 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1171 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1172 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1173 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1174 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1175 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1176 | return true; |
| 1177 | } |
| 1178 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1179 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1180 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1184 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1185 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1186 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1187 | releaseInboundEventLocked(entry); |
| 1188 | } |
| 1189 | traceInboundQueueLengthLocked(); |
| 1190 | } |
| 1191 | |
| 1192 | void InputDispatcher::releasePendingEventLocked() { |
| 1193 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1194 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1195 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1199 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1201 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1202 | if (DEBUG_DISPATCH_CYCLE) { |
| 1203 | ALOGD("Injected inbound event was dropped."); |
| 1204 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1205 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1206 | } |
| 1207 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1208 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1209 | } |
| 1210 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1214 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1215 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1219 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1220 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1221 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1222 | uint32_t policyFlags = entry->policyFlags & |
| 1223 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1224 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1225 | std::shared_ptr<KeyEntry> newEntry = |
| 1226 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1227 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1228 | entry->flags, entry->keyCode, entry->scanCode, |
| 1229 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
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 | newEntry->syntheticRepeat = true; |
| 1232 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1233 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1234 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1237 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1238 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1239 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1240 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1241 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1242 | |
| 1243 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1244 | resetKeyRepeatLocked(); |
| 1245 | |
| 1246 | // 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] | 1247 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1248 | scoped_unlock unlock(mLock); |
| 1249 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1250 | }; |
| 1251 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1252 | return true; |
| 1253 | } |
| 1254 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1255 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1256 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1257 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1258 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1259 | entry.deviceId); |
| 1260 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1261 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1262 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1263 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1264 | resetKeyRepeatLocked(); |
| 1265 | } |
| 1266 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1267 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1268 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1269 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1270 | return true; |
| 1271 | } |
| 1272 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1273 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1274 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1275 | if (mPendingEvent != nullptr) { |
| 1276 | // Move the pending event to the front of the queue. This will give the chance |
| 1277 | // for the pending event to get dispatched to the newly focused window |
| 1278 | mInboundQueue.push_front(mPendingEvent); |
| 1279 | mPendingEvent = nullptr; |
| 1280 | } |
| 1281 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1282 | std::unique_ptr<FocusEntry> focusEntry = |
| 1283 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1284 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1285 | |
| 1286 | // This event should go to the front of the queue, but behind all other focus events |
| 1287 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1288 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1289 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1290 | [](const std::shared_ptr<EventEntry>& event) { |
| 1291 | return event->type == EventEntry::Type::FOCUS; |
| 1292 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1293 | |
| 1294 | // 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] | 1295 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1296 | } |
| 1297 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1298 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1299 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1300 | if (channel == nullptr) { |
| 1301 | return; // Window has gone away |
| 1302 | } |
| 1303 | InputTarget target; |
| 1304 | target.inputChannel = channel; |
| 1305 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1306 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1307 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1308 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1309 | std::string reason = std::string("reason=").append(entry->reason); |
| 1310 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1311 | dispatchEventLocked(currentTime, entry, {target}); |
| 1312 | } |
| 1313 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1314 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1315 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1316 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1317 | dropReason = DropReason::NOT_DROPPED; |
| 1318 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1319 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1320 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1321 | |
| 1322 | if (entry->pointerCaptureRequest.enable) { |
| 1323 | // Enable Pointer Capture. |
| 1324 | if (haveWindowWithPointerCapture && |
| 1325 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
| 1326 | LOG_ALWAYS_FATAL("This request to enable Pointer Capture has already been dispatched " |
| 1327 | "to the window."); |
| 1328 | } |
| 1329 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1330 | // This can happen if a window requests capture and immediately releases capture. |
| 1331 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1332 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1333 | return; |
| 1334 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1335 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1336 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1337 | return; |
| 1338 | } |
| 1339 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1340 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1341 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1342 | mWindowTokenWithPointerCapture = token; |
| 1343 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1344 | // Disable Pointer Capture. |
| 1345 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1346 | // for two reasons: |
| 1347 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1348 | // to disable capture with the same sequence number: one generated by |
| 1349 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1350 | // Capture being disabled in InputReader. |
| 1351 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1352 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1353 | // in InputReader. |
| 1354 | if (!haveWindowWithPointerCapture) { |
| 1355 | // Pointer capture was already forcefully disabled because of focus change. |
| 1356 | dropReason = DropReason::NOT_DROPPED; |
| 1357 | return; |
| 1358 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1359 | token = mWindowTokenWithPointerCapture; |
| 1360 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1361 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1362 | setPointerCaptureLocked(false); |
| 1363 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | auto channel = getInputChannelLocked(token); |
| 1367 | if (channel == nullptr) { |
| 1368 | // Window has gone away, clean up Pointer Capture state. |
| 1369 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1370 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1371 | setPointerCaptureLocked(false); |
| 1372 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1373 | return; |
| 1374 | } |
| 1375 | InputTarget target; |
| 1376 | target.inputChannel = channel; |
| 1377 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1378 | entry->dispatchInProgress = true; |
| 1379 | dispatchEventLocked(currentTime, entry, {target}); |
| 1380 | |
| 1381 | dropReason = DropReason::NOT_DROPPED; |
| 1382 | } |
| 1383 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1384 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1385 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1386 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1387 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1388 | if (windowHandles.empty()) { |
| 1389 | return; |
| 1390 | } |
| 1391 | const std::vector<InputTarget> inputTargets = |
| 1392 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1393 | if (inputTargets.empty()) { |
| 1394 | return; |
| 1395 | } |
| 1396 | entry->dispatchInProgress = true; |
| 1397 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1398 | } |
| 1399 | |
| 1400 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1401 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1402 | std::vector<InputTarget> inputTargets; |
| 1403 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1404 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1405 | const sp<IBinder>& token = handle->getToken(); |
| 1406 | if (token == nullptr) { |
| 1407 | continue; |
| 1408 | } |
| 1409 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1410 | if (channel == nullptr) { |
| 1411 | continue; // Window has gone away |
| 1412 | } |
| 1413 | InputTarget target; |
| 1414 | target.inputChannel = channel; |
| 1415 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1416 | inputTargets.push_back(target); |
| 1417 | } |
| 1418 | return inputTargets; |
| 1419 | } |
| 1420 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1421 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1422 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1423 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1424 | if (!entry->dispatchInProgress) { |
| 1425 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1426 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1427 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1428 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1429 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1430 | // We have seen two identical key downs in a row which indicates that the device |
| 1431 | // driver is automatically generating key repeats itself. We take note of the |
| 1432 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1433 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1434 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1435 | // Make sure we don't get key down from a different device. If a different |
| 1436 | // device Id has same key pressed down, the new device Id will replace the |
| 1437 | // current one to hold the key repeat with repeat count reset. |
| 1438 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1439 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1440 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1441 | resetKeyRepeatLocked(); |
| 1442 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1443 | } else { |
| 1444 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1445 | resetKeyRepeatLocked(); |
| 1446 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1447 | } |
| 1448 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1449 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1450 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1451 | // 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] | 1452 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1453 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1454 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1455 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1456 | resetKeyRepeatLocked(); |
| 1457 | } |
| 1458 | |
| 1459 | if (entry->repeatCount == 1) { |
| 1460 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1461 | } else { |
| 1462 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1463 | } |
| 1464 | |
| 1465 | entry->dispatchInProgress = true; |
| 1466 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1467 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | // Handle case where the policy asked us to try again later last time. |
| 1471 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1472 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1473 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1474 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1475 | } |
| 1476 | return false; // wait until next wakeup |
| 1477 | } |
| 1478 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1479 | entry->interceptKeyWakeupTime = 0; |
| 1480 | } |
| 1481 | |
| 1482 | // Give the policy a chance to intercept the key. |
| 1483 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1484 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1485 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1486 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1487 | |
| 1488 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1489 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1490 | }; |
| 1491 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1492 | return false; // wait for the command to run |
| 1493 | } else { |
| 1494 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1495 | } |
| 1496 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1497 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1498 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1503 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1504 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1505 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1506 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1507 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1508 | return true; |
| 1509 | } |
| 1510 | |
| 1511 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1512 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1513 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1514 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1515 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1516 | return false; |
| 1517 | } |
| 1518 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1519 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1520 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1521 | return true; |
| 1522 | } |
| 1523 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1524 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1525 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1526 | |
| 1527 | // Dispatch the key. |
| 1528 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1529 | return true; |
| 1530 | } |
| 1531 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1532 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1533 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1534 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1535 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1536 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1537 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1538 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1539 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1540 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1541 | } |
| 1542 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1543 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1544 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1545 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1546 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1547 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1548 | "source=0x%x, sensorType=%s", |
| 1549 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1550 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1551 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1552 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1553 | scoped_unlock unlock(mLock); |
| 1554 | |
| 1555 | if (entry->accuracyChanged) { |
| 1556 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1557 | } |
| 1558 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1559 | entry->hwTimestamp, entry->values); |
| 1560 | }; |
| 1561 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1565 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1566 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1567 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1568 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1569 | { // acquire lock |
| 1570 | std::scoped_lock _l(mLock); |
| 1571 | |
| 1572 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1573 | std::shared_ptr<EventEntry> entry = *it; |
| 1574 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1575 | it = mInboundQueue.erase(it); |
| 1576 | releaseInboundEventLocked(entry); |
| 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | return true; |
| 1581 | } |
| 1582 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1583 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1584 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1585 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1586 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1587 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1588 | entry->dispatchInProgress = true; |
| 1589 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1590 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1594 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1595 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1596 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1597 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1598 | return true; |
| 1599 | } |
| 1600 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1601 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1602 | |
| 1603 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1604 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1605 | |
| 1606 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1607 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1608 | if (isPointerEvent) { |
| 1609 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1610 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1611 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1612 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1613 | } else { |
| 1614 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1615 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1616 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1617 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1618 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1619 | return false; |
| 1620 | } |
| 1621 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1622 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1623 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1624 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1625 | return true; |
| 1626 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1627 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1628 | CancelationOptions::Mode mode(isPointerEvent |
| 1629 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1630 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1631 | CancelationOptions options(mode, "input event injection failed"); |
| 1632 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1633 | return true; |
| 1634 | } |
| 1635 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1636 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1637 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1638 | |
| 1639 | // Dispatch the motion. |
| 1640 | if (conflictingPointerActions) { |
| 1641 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1642 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1643 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1644 | } |
| 1645 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1646 | return true; |
| 1647 | } |
| 1648 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1649 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1650 | bool isExiting, const MotionEntry& motionEntry) { |
| 1651 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1652 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1653 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1654 | PointerCoords pointerCoords; |
| 1655 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1656 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1657 | |
| 1658 | std::unique_ptr<DragEntry> dragEntry = |
| 1659 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1660 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1661 | pointerCoords.getY()); |
| 1662 | |
| 1663 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1664 | } |
| 1665 | |
| 1666 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1667 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1668 | if (channel == nullptr) { |
| 1669 | return; // Window has gone away |
| 1670 | } |
| 1671 | InputTarget target; |
| 1672 | target.inputChannel = channel; |
| 1673 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1674 | entry->dispatchInProgress = true; |
| 1675 | dispatchEventLocked(currentTime, entry, {target}); |
| 1676 | } |
| 1677 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1678 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1679 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1680 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1681 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1682 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1683 | "metaState=0x%x, buttonState=0x%x," |
| 1684 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1685 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1686 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1687 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1688 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1689 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1690 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1691 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1692 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1693 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1694 | "orientation=%f", |
| 1695 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1696 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1697 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1698 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1699 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1700 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1701 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1702 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1703 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1704 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1705 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1706 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1707 | } |
| 1708 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1709 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1710 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1711 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1712 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1713 | if (DEBUG_DISPATCH_CYCLE) { |
| 1714 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1715 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1716 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1717 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1718 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1719 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1720 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1721 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1722 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1723 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1724 | sp<Connection> connection = |
| 1725 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1726 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1727 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1728 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1729 | if (DEBUG_FOCUS) { |
| 1730 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1731 | "is no longer registered with the input dispatcher.", |
| 1732 | inputTarget.inputChannel->getName().c_str()); |
| 1733 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1734 | } |
| 1735 | } |
| 1736 | } |
| 1737 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1738 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1739 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1740 | // If the policy decides to close the app, we will get a channel removal event via |
| 1741 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1742 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1743 | // pile up. |
| 1744 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1745 | connection->inputChannel->getName().c_str()); |
| 1746 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1747 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1748 | "application not responding"); |
| 1749 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1750 | } |
| 1751 | } |
| 1752 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1753 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1754 | if (DEBUG_FOCUS) { |
| 1755 | ALOGD("Resetting ANR timeouts."); |
| 1756 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1757 | |
| 1758 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1759 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1760 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1761 | } |
| 1762 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1763 | /** |
| 1764 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1765 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1766 | * Focused display is the display that the user most recently interacted with. |
| 1767 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1768 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1769 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1770 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1771 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1772 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1773 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1774 | break; |
| 1775 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1776 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1777 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1778 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1779 | break; |
| 1780 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1781 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1782 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1783 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1784 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1785 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1786 | case EventEntry::Type::SENSOR: |
| 1787 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1788 | 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] | 1789 | return ADISPLAY_ID_NONE; |
| 1790 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1791 | } |
| 1792 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1793 | } |
| 1794 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1795 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1796 | const char* focusedWindowName) { |
| 1797 | if (mAnrTracker.empty()) { |
| 1798 | // already processed all events that we waited for |
| 1799 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1800 | return false; |
| 1801 | } |
| 1802 | |
| 1803 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1804 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1805 | // 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] | 1806 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1807 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1808 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1809 | return true; |
| 1810 | } |
| 1811 | |
| 1812 | // We still have pending events, and already started the timer |
| 1813 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1814 | return true; // Still waiting |
| 1815 | } |
| 1816 | |
| 1817 | // Waited too long, and some connection still hasn't processed all motions |
| 1818 | // Just send the key to the focused window |
| 1819 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1820 | focusedWindowName); |
| 1821 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1822 | return false; |
| 1823 | } |
| 1824 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1825 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1826 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1827 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1828 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1829 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1830 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1831 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1832 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1833 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1834 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1835 | // If there is no currently focused window and no focused application |
| 1836 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1837 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1838 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1839 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1840 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1841 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1842 | } |
| 1843 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1844 | // Drop key events if requested by input feature |
| 1845 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1846 | return InputEventInjectionResult::FAILED; |
| 1847 | } |
| 1848 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1849 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1850 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1851 | // start interacting with another application via touch (app switch). This code can be removed |
| 1852 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1853 | // an app is expected to have a focused window. |
| 1854 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1855 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1856 | // 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] | 1857 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1858 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1859 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1860 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1861 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1862 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1863 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1864 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1865 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1866 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1867 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1868 | // Already raised ANR. Drop the event |
| 1869 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1870 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1871 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1872 | } else { |
| 1873 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1874 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | // we have a valid, non-null focused window |
| 1879 | resetNoFocusedWindowTimeoutLocked(); |
| 1880 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1881 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1882 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1883 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1884 | } |
| 1885 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1886 | if (focusedWindowHandle->getInfo()->paused) { |
| 1887 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1888 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | // If the event is a key event, then we must wait for all previous events to |
| 1892 | // complete before delivering it because previous events may have the |
| 1893 | // side-effect of transferring focus to a different window and we want to |
| 1894 | // ensure that the following keys are sent to the new window. |
| 1895 | // |
| 1896 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1897 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1898 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1899 | // often anticipate pending UI changes when typing on a keyboard. |
| 1900 | // To obtain this behavior, we must serialize key events with respect to all |
| 1901 | // prior input events. |
| 1902 | if (entry.type == EventEntry::Type::KEY) { |
| 1903 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1904 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1905 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1906 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1910 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1911 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1912 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1913 | |
| 1914 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1915 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1916 | } |
| 1917 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1918 | /** |
| 1919 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1920 | * that are currently unresponsive. |
| 1921 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1922 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1923 | const std::vector<Monitor>& monitors) const { |
| 1924 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1925 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1926 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 1927 | sp<Connection> connection = |
| 1928 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1929 | if (connection == nullptr) { |
| 1930 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1931 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1932 | return false; |
| 1933 | } |
| 1934 | if (!connection->responsive) { |
| 1935 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1936 | connection->inputChannel->getName().c_str()); |
| 1937 | return false; |
| 1938 | } |
| 1939 | return true; |
| 1940 | }); |
| 1941 | return responsiveMonitors; |
| 1942 | } |
| 1943 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1944 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1945 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1946 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1947 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1948 | enum InjectionPermission { |
| 1949 | INJECTION_PERMISSION_UNKNOWN, |
| 1950 | INJECTION_PERMISSION_GRANTED, |
| 1951 | INJECTION_PERMISSION_DENIED |
| 1952 | }; |
| 1953 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1954 | // For security reasons, we defer updating the touch state until we are sure that |
| 1955 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1956 | int32_t displayId = entry.displayId; |
| 1957 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1958 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1959 | |
| 1960 | // 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] | 1961 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1962 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1963 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1964 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1965 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1966 | // Copy current touch state into tempTouchState. |
| 1967 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1968 | // 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] | 1969 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1970 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1971 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1972 | mTouchStatesByDisplay.find(displayId); |
| 1973 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1974 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1975 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1976 | } |
| 1977 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1978 | bool isSplit = tempTouchState.split; |
| 1979 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1980 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1981 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1982 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1983 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1984 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1985 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1986 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1987 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1988 | bool wrongDevice = false; |
| 1989 | if (newGesture) { |
| 1990 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1991 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1992 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1993 | "in display %" PRId32, |
| 1994 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1995 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1996 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1997 | switchedDevice = false; |
| 1998 | wrongDevice = true; |
| 1999 | goto Failed; |
| 2000 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2001 | tempTouchState.reset(); |
| 2002 | tempTouchState.down = down; |
| 2003 | tempTouchState.deviceId = entry.deviceId; |
| 2004 | tempTouchState.source = entry.source; |
| 2005 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2006 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2007 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2008 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2009 | "in display %" PRId32, |
| 2010 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2011 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2012 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2013 | switchedDevice = false; |
| 2014 | wrongDevice = true; |
| 2015 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2019 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2020 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2021 | int32_t x; |
| 2022 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2023 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2024 | // Always dispatch mouse events to cursor position. |
| 2025 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2026 | x = int32_t(entry.xCursorPosition); |
| 2027 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2028 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2029 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2030 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2031 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2032 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2033 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 2034 | isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2035 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2036 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2037 | if (newTouchedWindowHandle != nullptr && |
| 2038 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 2039 | // New window supports splitting, but we should never split mouse events. |
| 2040 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2041 | } else if (isSplit) { |
| 2042 | // New window does not support splitting but we have already split events. |
| 2043 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2044 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2048 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2049 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2050 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2051 | // 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] | 2052 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2055 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 2056 | ALOGI("Not sending touch event to %s because it is paused", |
| 2057 | newTouchedWindowHandle->getName().c_str()); |
| 2058 | newTouchedWindowHandle = nullptr; |
| 2059 | } |
| 2060 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2061 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2062 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2063 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 2064 | if (!isResponsive) { |
| 2065 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2066 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 2067 | newTouchedWindowHandle = nullptr; |
| 2068 | } |
| 2069 | } |
| 2070 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2071 | // Drop events that can't be trusted due to occlusion |
| 2072 | if (newTouchedWindowHandle != nullptr && |
| 2073 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2074 | TouchOcclusionInfo occlusionInfo = |
| 2075 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2076 | if (!isTouchTrustedLocked(occlusionInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2077 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2078 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2079 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2080 | ALOGD("%s", log.c_str()); |
| 2081 | } |
| 2082 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2083 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2084 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2085 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2086 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2087 | newTouchedWindowHandle = nullptr; |
| 2088 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2089 | } |
| 2090 | } |
| 2091 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2092 | // Drop touch events if requested by input feature |
| 2093 | if (newTouchedWindowHandle != nullptr && shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2094 | newTouchedWindowHandle = nullptr; |
| 2095 | } |
| 2096 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2097 | const std::vector<Monitor> newGestureMonitors = isDown |
| 2098 | ? selectResponsiveMonitorsLocked( |
| 2099 | getValueByKey(mGestureMonitorsByDisplay, displayId)) |
| 2100 | : std::vector<Monitor>{}; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2101 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2102 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 2103 | 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] | 2104 | "(%d, %d) in display %" PRId32 ".", |
| 2105 | x, y, displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2106 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2107 | goto Failed; |
| 2108 | } |
| 2109 | |
| 2110 | if (newTouchedWindowHandle != nullptr) { |
| 2111 | // Set target flags. |
| 2112 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 2113 | if (isSplit) { |
| 2114 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2115 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2116 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2117 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2118 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2119 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2120 | } |
| 2121 | |
| 2122 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2123 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2124 | newHoverWindowHandle = nullptr; |
| 2125 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2126 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | // Update the temporary touch state. |
| 2130 | BitSet32 pointerIds; |
| 2131 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2132 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2133 | pointerIds.markBit(pointerId); |
| 2134 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2135 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2136 | } |
| 2137 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2138 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2139 | } else { |
| 2140 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2141 | |
| 2142 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2143 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2144 | if (DEBUG_FOCUS) { |
| 2145 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2146 | "dropped the pointer down event in display %" PRId32, |
| 2147 | displayId); |
| 2148 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2149 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2150 | goto Failed; |
| 2151 | } |
| 2152 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2153 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2154 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2155 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2156 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2157 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2158 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2159 | 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] | 2160 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2161 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2162 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2163 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2164 | |
| 2165 | // Drop touch events if requested by input feature |
| 2166 | if (newTouchedWindowHandle != nullptr && |
| 2167 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2168 | newTouchedWindowHandle = nullptr; |
| 2169 | } |
| 2170 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2171 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2172 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2173 | if (DEBUG_FOCUS) { |
| 2174 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2175 | oldTouchedWindowHandle->getName().c_str(), |
| 2176 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2177 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2178 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2179 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2180 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2181 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2182 | |
| 2183 | // Make a slippery entrance into the new window. |
| 2184 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2185 | isSplit = true; |
| 2186 | } |
| 2187 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2188 | int32_t targetFlags = |
| 2189 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2190 | if (isSplit) { |
| 2191 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2192 | } |
| 2193 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2194 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2195 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2196 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | BitSet32 pointerIds; |
| 2200 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2201 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2202 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2203 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2204 | } |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2209 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 2210 | // when dispatching it as is to newTouchedWindowHandle. |
| 2211 | if (mLastHoverWindowHandle != nullptr && |
| 2212 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2213 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2214 | if (DEBUG_HOVER) { |
| 2215 | ALOGD("Sending hover exit event to window %s.", |
| 2216 | mLastHoverWindowHandle->getName().c_str()); |
| 2217 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2218 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2219 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2220 | } |
| 2221 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2222 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2223 | // when dispatching it as is to newTouchedWindowHandle. |
| 2224 | if (newHoverWindowHandle != nullptr && |
| 2225 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2226 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2227 | if (DEBUG_HOVER) { |
| 2228 | ALOGD("Sending hover enter event to window %s.", |
| 2229 | newHoverWindowHandle->getName().c_str()); |
| 2230 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2231 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2232 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2233 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | // Check permission to inject into all touched foreground windows and ensure there |
| 2238 | // is at least one touched foreground window. |
| 2239 | { |
| 2240 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2241 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2242 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 2243 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2244 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2245 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2246 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2247 | goto Failed; |
| 2248 | } |
| 2249 | } |
| 2250 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2251 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2252 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2253 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 2254 | "%" PRId32 " or gesture monitor to receive it.", |
| 2255 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2256 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2257 | goto Failed; |
| 2258 | } |
| 2259 | |
| 2260 | // Permission granted to injection into all touched foreground windows. |
| 2261 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2262 | } |
| 2263 | |
| 2264 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2265 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2266 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2267 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2268 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2269 | if (foregroundWindowHandle) { |
| 2270 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2271 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2272 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2273 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2274 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2275 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2276 | InputTarget::FLAG_ZERO_COORDS, |
| 2277 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2278 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2279 | } |
| 2280 | } |
| 2281 | } |
| 2282 | } |
| 2283 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2284 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2285 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2286 | // of the touch gesture. |
| 2287 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2288 | // engine only supports touch events. We would need to add a mechanism similar |
| 2289 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2290 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2291 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2292 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2293 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2294 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2295 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2296 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2297 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2298 | if (info->displayId == displayId && |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2299 | windowHandle->getInfo()->type == WindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2300 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2301 | .addOrUpdateWindow(windowHandle, |
| 2302 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2303 | InputTarget:: |
| 2304 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2305 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2306 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2307 | } |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2313 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2314 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2315 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2316 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2317 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2318 | } |
| 2319 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2320 | for (const auto& monitor : tempTouchState.gestureMonitors) { |
| 2321 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2324 | // Drop the outside or hover touch windows since we will not care about them |
| 2325 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2326 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2327 | |
| 2328 | Failed: |
| 2329 | // Check injection permission once and for all. |
| 2330 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2331 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2332 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2333 | } else { |
| 2334 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2335 | } |
| 2336 | } |
| 2337 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2338 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2339 | return injectionResult; |
| 2340 | } |
| 2341 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2342 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2343 | if (!wrongDevice) { |
| 2344 | if (switchedDevice) { |
| 2345 | if (DEBUG_FOCUS) { |
| 2346 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2347 | } |
| 2348 | *outConflictingPointerActions = true; |
| 2349 | } |
| 2350 | |
| 2351 | if (isHoverAction) { |
| 2352 | // Started hovering, therefore no longer down. |
| 2353 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2354 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2355 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2356 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2357 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2358 | *outConflictingPointerActions = true; |
| 2359 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2360 | tempTouchState.reset(); |
| 2361 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2362 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2363 | tempTouchState.deviceId = entry.deviceId; |
| 2364 | tempTouchState.source = entry.source; |
| 2365 | tempTouchState.displayId = displayId; |
| 2366 | } |
| 2367 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2368 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2369 | // All pointers up or canceled. |
| 2370 | tempTouchState.reset(); |
| 2371 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2372 | // First pointer went down. |
| 2373 | if (oldState && oldState->down) { |
| 2374 | if (DEBUG_FOCUS) { |
| 2375 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2376 | } |
| 2377 | *outConflictingPointerActions = true; |
| 2378 | } |
| 2379 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2380 | // One pointer went up. |
| 2381 | if (isSplit) { |
| 2382 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2383 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2384 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2385 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2386 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2387 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2388 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2389 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2390 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2391 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2392 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2393 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2394 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2395 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2396 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2397 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2398 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2399 | // Save changes unless the action was scroll in which case the temporary touch |
| 2400 | // state was only valid for this one action. |
| 2401 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2402 | if (tempTouchState.displayId >= 0) { |
| 2403 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2404 | } else { |
| 2405 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2406 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2407 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2408 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2409 | // Update hover state. |
| 2410 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2411 | } |
| 2412 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2413 | return injectionResult; |
| 2414 | } |
| 2415 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2416 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2417 | const sp<WindowInfoHandle> dropWindow = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2418 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2419 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2420 | if (dropWindow) { |
| 2421 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2422 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2423 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2424 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2425 | } |
| 2426 | mDragState.reset(); |
| 2427 | } |
| 2428 | |
| 2429 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2430 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2431 | return; |
| 2432 | } |
| 2433 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2434 | if (!mDragState->isStartDrag) { |
| 2435 | mDragState->isStartDrag = true; |
| 2436 | mDragState->isStylusButtonDownAtStart = |
| 2437 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2438 | } |
| 2439 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2440 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2441 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2442 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2443 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2444 | // Handle the special case : stylus button no longer pressed. |
| 2445 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2446 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2447 | finishDragAndDrop(entry.displayId, x, y); |
| 2448 | return; |
| 2449 | } |
| 2450 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2451 | const sp<WindowInfoHandle> hoverWindowHandle = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2452 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2453 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2454 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2455 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2456 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2457 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2458 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2459 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2460 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2461 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2462 | } |
| 2463 | // enqueue drag location if needed. |
| 2464 | if (hoverWindowHandle != nullptr) { |
| 2465 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2466 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2467 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2468 | finishDragAndDrop(entry.displayId, x, y); |
| 2469 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2470 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2471 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2472 | } |
| 2473 | } |
| 2474 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2475 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2476 | int32_t targetFlags, BitSet32 pointerIds, |
| 2477 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2478 | std::vector<InputTarget>::iterator it = |
| 2479 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2480 | [&windowHandle](const InputTarget& inputTarget) { |
| 2481 | return inputTarget.inputChannel->getConnectionToken() == |
| 2482 | windowHandle->getToken(); |
| 2483 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2484 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2485 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2486 | |
| 2487 | if (it == inputTargets.end()) { |
| 2488 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2489 | std::shared_ptr<InputChannel> inputChannel = |
| 2490 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2491 | if (inputChannel == nullptr) { |
| 2492 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2493 | return; |
| 2494 | } |
| 2495 | inputTarget.inputChannel = inputChannel; |
| 2496 | inputTarget.flags = targetFlags; |
| 2497 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2498 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2499 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2500 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2501 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2502 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2503 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2504 | inputTargets.push_back(inputTarget); |
| 2505 | it = inputTargets.end() - 1; |
| 2506 | } |
| 2507 | |
| 2508 | ALOG_ASSERT(it->flags == targetFlags); |
| 2509 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2510 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2511 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2512 | } |
| 2513 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2514 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2515 | int32_t displayId) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2516 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2517 | mGlobalMonitorsByDisplay.find(displayId); |
| 2518 | |
| 2519 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2520 | const std::vector<Monitor>& monitors = it->second; |
| 2521 | for (const Monitor& monitor : monitors) { |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2522 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2523 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2524 | } |
| 2525 | } |
| 2526 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2527 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, int32_t displayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2528 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2529 | InputTarget target; |
| 2530 | target.inputChannel = monitor.inputChannel; |
| 2531 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2532 | ui::Transform t; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2533 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | b5402b2 | 2021-10-04 05:52:50 -0700 | [diff] [blame] | 2534 | const auto& displayTransform = it->second.transform; |
| 2535 | target.displayTransform = displayTransform; |
| 2536 | t = displayTransform; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2537 | } |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2538 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2539 | inputTargets.push_back(target); |
| 2540 | } |
| 2541 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2542 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2543 | const InjectionState* injectionState) { |
| 2544 | if (injectionState && |
| 2545 | (windowHandle == nullptr || |
| 2546 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2547 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2548 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2549 | 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] | 2550 | "owned by uid %d", |
| 2551 | injectionState->injectorPid, injectionState->injectorUid, |
| 2552 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2553 | } else { |
| 2554 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2555 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2556 | } |
| 2557 | return false; |
| 2558 | } |
| 2559 | return true; |
| 2560 | } |
| 2561 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2562 | /** |
| 2563 | * Indicate whether one window handle should be considered as obscuring |
| 2564 | * another window handle. We only check a few preconditions. Actually |
| 2565 | * checking the bounds is left to the caller. |
| 2566 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2567 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2568 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2569 | // Compare by token so cloned layers aren't counted |
| 2570 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2571 | return false; |
| 2572 | } |
| 2573 | auto info = windowHandle->getInfo(); |
| 2574 | auto otherInfo = otherHandle->getInfo(); |
| 2575 | if (!otherInfo->visible) { |
| 2576 | return false; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2577 | } else if (otherInfo->alpha == 0 && otherInfo->flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2578 | // Those act as if they were invisible, so we don't need to flag them. |
| 2579 | // We do want to potentially flag touchable windows even if they have 0 |
| 2580 | // opacity, since they can consume touches and alter the effects of the |
| 2581 | // user interaction (eg. apps that rely on |
| 2582 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2583 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2584 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2585 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2586 | // If ownerUid is the same we don't generate occlusion events as there |
| 2587 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2588 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2589 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2590 | return false; |
| 2591 | } else if (otherInfo->displayId != info->displayId) { |
| 2592 | return false; |
| 2593 | } |
| 2594 | return true; |
| 2595 | } |
| 2596 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2597 | /** |
| 2598 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2599 | * untrusted, one should check: |
| 2600 | * |
| 2601 | * 1. If result.hasBlockingOcclusion is true. |
| 2602 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2603 | * BLOCK_UNTRUSTED. |
| 2604 | * |
| 2605 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2606 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2607 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2608 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2609 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2610 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2611 | * |
| 2612 | * If neither of those is true, then it means the touch can be allowed. |
| 2613 | */ |
| 2614 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2615 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2616 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2617 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2618 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2619 | TouchOcclusionInfo info; |
| 2620 | info.hasBlockingOcclusion = false; |
| 2621 | info.obscuringOpacity = 0; |
| 2622 | info.obscuringUid = -1; |
| 2623 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2624 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2625 | if (windowHandle == otherHandle) { |
| 2626 | break; // All future windows are below us. Exit early. |
| 2627 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2628 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2629 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2630 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2631 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2632 | info.debugInfo.push_back( |
| 2633 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2634 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2635 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2636 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2637 | // window's touch occlusion mode |
| 2638 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2639 | info.hasBlockingOcclusion = true; |
| 2640 | info.obscuringUid = otherInfo->ownerUid; |
| 2641 | info.obscuringPackage = otherInfo->packageName; |
| 2642 | break; |
| 2643 | } |
| 2644 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2645 | uint32_t uid = otherInfo->ownerUid; |
| 2646 | float opacity = |
| 2647 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2648 | // Given windows A and B: |
| 2649 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2650 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2651 | opacityByUid[uid] = opacity; |
| 2652 | if (opacity > info.obscuringOpacity) { |
| 2653 | info.obscuringOpacity = opacity; |
| 2654 | info.obscuringUid = uid; |
| 2655 | info.obscuringPackage = otherInfo->packageName; |
| 2656 | } |
| 2657 | } |
| 2658 | } |
| 2659 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2660 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2661 | info.debugInfo.push_back( |
| 2662 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2663 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2664 | return info; |
| 2665 | } |
| 2666 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2667 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2668 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2669 | return StringPrintf(INDENT2 |
| 2670 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2671 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2672 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2673 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2674 | isTouchedWindow ? "[TOUCHED] " : "", ftl::enum_string(info->type).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2675 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2676 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2677 | info->frameTop, info->frameRight, info->frameBottom, |
| 2678 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2679 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2680 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2681 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2684 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2685 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2686 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2687 | occlusionInfo.obscuringUid); |
| 2688 | return false; |
| 2689 | } |
| 2690 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2691 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2692 | "%.2f, maximum allowed = %.2f)", |
| 2693 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2694 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2695 | return false; |
| 2696 | } |
| 2697 | return true; |
| 2698 | } |
| 2699 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2700 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2701 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2702 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2703 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2704 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2705 | if (windowHandle == otherHandle) { |
| 2706 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2707 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2708 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2709 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2710 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2711 | return true; |
| 2712 | } |
| 2713 | } |
| 2714 | return false; |
| 2715 | } |
| 2716 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2717 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2718 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2719 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2720 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2721 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2722 | if (windowHandle == otherHandle) { |
| 2723 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2724 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2725 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2726 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2727 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2728 | return true; |
| 2729 | } |
| 2730 | } |
| 2731 | return false; |
| 2732 | } |
| 2733 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2734 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2735 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2736 | if (applicationHandle != nullptr) { |
| 2737 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2738 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2739 | } else { |
| 2740 | return applicationHandle->getName(); |
| 2741 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2742 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2743 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2744 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2745 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2746 | } |
| 2747 | } |
| 2748 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2749 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2750 | if (!isUserActivityEvent(eventEntry)) { |
| 2751 | // 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] | 2752 | return; |
| 2753 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2754 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2755 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2756 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2757 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
| 2758 | if (info->inputFeatures.test(WindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2759 | if (DEBUG_DISPATCH_CYCLE) { |
| 2760 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2761 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2762 | return; |
| 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2767 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2768 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2769 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2770 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2771 | return; |
| 2772 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2773 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2774 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2775 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2776 | } |
| 2777 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2778 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2779 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2780 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2781 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2782 | return; |
| 2783 | } |
| 2784 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2785 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2786 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2787 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2788 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2789 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2790 | break; |
| 2791 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2792 | } |
| 2793 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2794 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2795 | REQUIRES(mLock) { |
| 2796 | scoped_unlock unlock(mLock); |
| 2797 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2798 | }; |
| 2799 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2803 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2804 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2805 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2806 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2807 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2808 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2809 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2810 | ATRACE_NAME(message.c_str()); |
| 2811 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2812 | if (DEBUG_DISPATCH_CYCLE) { |
| 2813 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2814 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2815 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2816 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2817 | inputTarget.getPointerInfoString().c_str()); |
| 2818 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2819 | |
| 2820 | // Skip this event if the connection status is not normal. |
| 2821 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2822 | if (connection->status != Connection::STATUS_NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2823 | if (DEBUG_DISPATCH_CYCLE) { |
| 2824 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
| 2825 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
| 2826 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2827 | return; |
| 2828 | } |
| 2829 | |
| 2830 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2831 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2832 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2833 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2834 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2835 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2836 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2837 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2838 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2839 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2840 | if (!splitMotionEntry) { |
| 2841 | return; // split event was dropped |
| 2842 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2843 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2844 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2845 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2846 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2847 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2848 | if (DEBUG_FOCUS) { |
| 2849 | ALOGD("channel '%s' ~ Split motion event.", |
| 2850 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2851 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2852 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2853 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2854 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2855 | return; |
| 2856 | } |
| 2857 | } |
| 2858 | |
| 2859 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2860 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2861 | } |
| 2862 | |
| 2863 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2864 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2865 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2866 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2867 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2868 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2869 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2870 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2871 | ATRACE_NAME(message.c_str()); |
| 2872 | } |
| 2873 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2874 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2875 | |
| 2876 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2877 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2878 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2879 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2880 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2881 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2882 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2883 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2884 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2885 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2886 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2887 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2888 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2889 | |
| 2890 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2891 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2892 | startDispatchCycleLocked(currentTime, connection); |
| 2893 | } |
| 2894 | } |
| 2895 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2896 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2897 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2898 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2899 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2900 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2901 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2902 | connection->getInputChannelName().c_str(), |
| 2903 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2904 | ATRACE_NAME(message.c_str()); |
| 2905 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2906 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2907 | if (!(inputTargetFlags & dispatchMode)) { |
| 2908 | return; |
| 2909 | } |
| 2910 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2911 | |
| 2912 | // This is a new event. |
| 2913 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2914 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2915 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2916 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2917 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2918 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2919 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2920 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2921 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2922 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2923 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2924 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2925 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2926 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2927 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2928 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2929 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2930 | if (DEBUG_DISPATCH_CYCLE) { |
| 2931 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 2932 | "event", |
| 2933 | connection->getInputChannelName().c_str()); |
| 2934 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2935 | return; // skip the inconsistent event |
| 2936 | } |
| 2937 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2938 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2939 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2940 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2941 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2942 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2943 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2944 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2945 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2946 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2947 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2948 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2949 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2950 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2951 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2952 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2953 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2954 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2955 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2956 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2957 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2958 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2959 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2960 | } |
| 2961 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2962 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2963 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2964 | if (DEBUG_DISPATCH_CYCLE) { |
| 2965 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 2966 | "enter event", |
| 2967 | connection->getInputChannelName().c_str()); |
| 2968 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 2969 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 2970 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2971 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2972 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2973 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2974 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2975 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2976 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2977 | } |
| 2978 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2979 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2980 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2981 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2982 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2983 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2984 | if (DEBUG_DISPATCH_CYCLE) { |
| 2985 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2986 | "event", |
| 2987 | connection->getInputChannelName().c_str()); |
| 2988 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2989 | return; // skip the inconsistent event |
| 2990 | } |
| 2991 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2992 | dispatchEntry->resolvedEventId = |
| 2993 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2994 | ? mIdGenerator.nextId() |
| 2995 | : motionEntry.id; |
| 2996 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2997 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2998 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2999 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3000 | ATRACE_NAME(message.c_str()); |
| 3001 | } |
| 3002 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3003 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3004 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3005 | // Skip reporting pointer down outside focus to the policy. |
| 3006 | break; |
| 3007 | } |
| 3008 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3009 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3010 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3011 | |
| 3012 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3013 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3014 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3015 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3016 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3017 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3018 | break; |
| 3019 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3020 | case EventEntry::Type::SENSOR: { |
| 3021 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3022 | break; |
| 3023 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3024 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3025 | case EventEntry::Type::DEVICE_RESET: { |
| 3026 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3027 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3028 | break; |
| 3029 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | // Remember that we are waiting for this dispatch to complete. |
| 3033 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3034 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3038 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3039 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3040 | } |
| 3041 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3042 | /** |
| 3043 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3044 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3045 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3046 | * We will see both launcher and wallpaper in that list. |
| 3047 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3048 | * until the set of interacted connections changes. |
| 3049 | * |
| 3050 | * The following items are skipped, to reduce the logspam: |
| 3051 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3052 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3053 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3054 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3055 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3056 | */ |
| 3057 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3058 | const std::vector<InputTarget>& targets) { |
| 3059 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3060 | if (entry.type == EventEntry::Type::KEY) { |
| 3061 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3062 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3063 | return; |
| 3064 | } |
| 3065 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3066 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3067 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3068 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3069 | return; |
| 3070 | } |
| 3071 | } else { |
| 3072 | return; // Not a key or a motion |
| 3073 | } |
| 3074 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3075 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3076 | std::vector<sp<Connection>> newConnections; |
| 3077 | for (const InputTarget& target : targets) { |
| 3078 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3079 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3080 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3081 | } |
| 3082 | |
| 3083 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3084 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3085 | if (connection == nullptr) { |
| 3086 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3087 | } |
| 3088 | newConnectionTokens.insert(std::move(token)); |
| 3089 | newConnections.emplace_back(connection); |
| 3090 | } |
| 3091 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3092 | return; // no change |
| 3093 | } |
| 3094 | mInteractionConnectionTokens = newConnectionTokens; |
| 3095 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3096 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3097 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3098 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3099 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3100 | std::string message = "Interaction with: " + targetList; |
| 3101 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3102 | message += "<none>"; |
| 3103 | } |
| 3104 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3105 | } |
| 3106 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3107 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3108 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3109 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3110 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3111 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3112 | return; |
| 3113 | } |
| 3114 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3115 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3116 | if (focusedToken == token) { |
| 3117 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3118 | return; |
| 3119 | } |
| 3120 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3121 | auto command = [this, token]() REQUIRES(mLock) { |
| 3122 | scoped_unlock unlock(mLock); |
| 3123 | mPolicy->onPointerDownOutsideFocus(token); |
| 3124 | }; |
| 3125 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3129 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3130 | if (ATRACE_ENABLED()) { |
| 3131 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3132 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3133 | ATRACE_NAME(message.c_str()); |
| 3134 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3135 | if (DEBUG_DISPATCH_CYCLE) { |
| 3136 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3137 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3138 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3139 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 3140 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3141 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3142 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3143 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3144 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3145 | |
| 3146 | // Publish the event. |
| 3147 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3148 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3149 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3150 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3151 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3152 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3153 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3154 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3155 | status = connection->inputPublisher |
| 3156 | .publishKeyEvent(dispatchEntry->seq, |
| 3157 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3158 | keyEntry.source, keyEntry.displayId, |
| 3159 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3160 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3161 | keyEntry.scanCode, keyEntry.metaState, |
| 3162 | keyEntry.repeatCount, keyEntry.downTime, |
| 3163 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3164 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3165 | } |
| 3166 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3167 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3168 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3169 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3170 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3171 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3172 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3173 | // 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] | 3174 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3175 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3176 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3177 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3178 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3179 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3180 | // Don't apply window scale here since we don't want scale to affect raw |
| 3181 | // coordinates. The scale will be sent back to the client and applied |
| 3182 | // later when requesting relative coordinates. |
| 3183 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3184 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3185 | } |
| 3186 | usingCoords = scaledCoords; |
| 3187 | } |
| 3188 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3189 | // We don't want the dispatch target to know. |
| 3190 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3191 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3192 | scaledCoords[i].clear(); |
| 3193 | } |
| 3194 | usingCoords = scaledCoords; |
| 3195 | } |
| 3196 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3197 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3198 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3199 | |
| 3200 | // Publish the motion event. |
| 3201 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3202 | .publishMotionEvent(dispatchEntry->seq, |
| 3203 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3204 | motionEntry.deviceId, motionEntry.source, |
| 3205 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3206 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3207 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3208 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3209 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3210 | motionEntry.buttonState, |
| 3211 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3212 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3213 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3214 | motionEntry.xCursorPosition, |
| 3215 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3216 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3217 | motionEntry.downTime, motionEntry.eventTime, |
| 3218 | motionEntry.pointerCount, |
| 3219 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3220 | break; |
| 3221 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3222 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3223 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3224 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3225 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3226 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3227 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3228 | break; |
| 3229 | } |
| 3230 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3231 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3232 | const TouchModeEntry& touchModeEntry = |
| 3233 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3234 | status = connection->inputPublisher |
| 3235 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3236 | touchModeEntry.inTouchMode); |
| 3237 | |
| 3238 | break; |
| 3239 | } |
| 3240 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3241 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3242 | const auto& captureEntry = |
| 3243 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3244 | status = connection->inputPublisher |
| 3245 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3246 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3247 | break; |
| 3248 | } |
| 3249 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3250 | case EventEntry::Type::DRAG: { |
| 3251 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3252 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3253 | dragEntry.id, dragEntry.x, |
| 3254 | dragEntry.y, |
| 3255 | dragEntry.isExiting); |
| 3256 | break; |
| 3257 | } |
| 3258 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3259 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3260 | case EventEntry::Type::DEVICE_RESET: |
| 3261 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3262 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3263 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3264 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3265 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | // Check the result. |
| 3269 | if (status) { |
| 3270 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3271 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3272 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3273 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3274 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3275 | "event to it, status=%s(%d)", |
| 3276 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3277 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3278 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3279 | } else { |
| 3280 | // Pipe is full and we are waiting for the app to finish process some events |
| 3281 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3282 | if (DEBUG_DISPATCH_CYCLE) { |
| 3283 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3284 | "waiting for the application to catch up", |
| 3285 | connection->getInputChannelName().c_str()); |
| 3286 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3287 | } |
| 3288 | } else { |
| 3289 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3290 | "status=%s(%d)", |
| 3291 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3292 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3293 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3294 | } |
| 3295 | return; |
| 3296 | } |
| 3297 | |
| 3298 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3299 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3300 | connection->outboundQueue.end(), |
| 3301 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3302 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3303 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3304 | if (connection->responsive) { |
| 3305 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3306 | connection->inputChannel->getConnectionToken()); |
| 3307 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3308 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3309 | } |
| 3310 | } |
| 3311 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3312 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3313 | size_t size; |
| 3314 | switch (event.type) { |
| 3315 | case VerifiedInputEvent::Type::KEY: { |
| 3316 | size = sizeof(VerifiedKeyEvent); |
| 3317 | break; |
| 3318 | } |
| 3319 | case VerifiedInputEvent::Type::MOTION: { |
| 3320 | size = sizeof(VerifiedMotionEvent); |
| 3321 | break; |
| 3322 | } |
| 3323 | } |
| 3324 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3325 | return mHmacKeyManager.sign(start, size); |
| 3326 | } |
| 3327 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3328 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3329 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3330 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3331 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3332 | // Only sign events up and down events as the purely move events |
| 3333 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3334 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3335 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3336 | |
| 3337 | VerifiedMotionEvent verifiedEvent = |
| 3338 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3339 | verifiedEvent.actionMasked = actionMasked; |
| 3340 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3341 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3345 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3346 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3347 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3348 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3349 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3350 | } |
| 3351 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3352 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3353 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3354 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3355 | if (DEBUG_DISPATCH_CYCLE) { |
| 3356 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3357 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3358 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3359 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3360 | if (connection->status == Connection::STATUS_BROKEN || |
| 3361 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3362 | return; |
| 3363 | } |
| 3364 | |
| 3365 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3366 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3367 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3368 | }; |
| 3369 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3373 | const sp<Connection>& connection, |
| 3374 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3375 | if (DEBUG_DISPATCH_CYCLE) { |
| 3376 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3377 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3378 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3379 | |
| 3380 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3381 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3382 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3383 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3384 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3385 | |
| 3386 | // The connection appears to be unrecoverably broken. |
| 3387 | // Ignore already broken or zombie connections. |
| 3388 | if (connection->status == Connection::STATUS_NORMAL) { |
| 3389 | connection->status = Connection::STATUS_BROKEN; |
| 3390 | |
| 3391 | if (notify) { |
| 3392 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3393 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3394 | connection->getInputChannelName().c_str()); |
| 3395 | |
| 3396 | auto command = [this, connection]() REQUIRES(mLock) { |
| 3397 | if (connection->status == Connection::STATUS_ZOMBIE) return; |
| 3398 | scoped_unlock unlock(mLock); |
| 3399 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3400 | }; |
| 3401 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3402 | } |
| 3403 | } |
| 3404 | } |
| 3405 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3406 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3407 | while (!queue.empty()) { |
| 3408 | DispatchEntry* dispatchEntry = queue.front(); |
| 3409 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3410 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3411 | } |
| 3412 | } |
| 3413 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3414 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3415 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3416 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3417 | } |
| 3418 | delete dispatchEntry; |
| 3419 | } |
| 3420 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3421 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3422 | std::scoped_lock _l(mLock); |
| 3423 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3424 | if (connection == nullptr) { |
| 3425 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3426 | connectionToken.get(), events); |
| 3427 | return 0; // remove the callback |
| 3428 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3429 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3430 | bool notify; |
| 3431 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3432 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3433 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3434 | "events=0x%x", |
| 3435 | connection->getInputChannelName().c_str(), events); |
| 3436 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3437 | } |
| 3438 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3439 | nsecs_t currentTime = now(); |
| 3440 | bool gotOne = false; |
| 3441 | status_t status = OK; |
| 3442 | for (;;) { |
| 3443 | Result<InputPublisher::ConsumerResponse> result = |
| 3444 | connection->inputPublisher.receiveConsumerResponse(); |
| 3445 | if (!result.ok()) { |
| 3446 | status = result.error().code(); |
| 3447 | break; |
| 3448 | } |
| 3449 | |
| 3450 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3451 | const InputPublisher::Finished& finish = |
| 3452 | std::get<InputPublisher::Finished>(*result); |
| 3453 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3454 | finish.consumeTime); |
| 3455 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3456 | if (shouldReportMetricsForConnection(*connection)) { |
| 3457 | const InputPublisher::Timeline& timeline = |
| 3458 | std::get<InputPublisher::Timeline>(*result); |
| 3459 | mLatencyTracker |
| 3460 | .trackGraphicsLatency(timeline.inputEventId, |
| 3461 | connection->inputChannel->getConnectionToken(), |
| 3462 | std::move(timeline.graphicsTimeline)); |
| 3463 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3464 | } |
| 3465 | gotOne = true; |
| 3466 | } |
| 3467 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3468 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3469 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3470 | return 1; |
| 3471 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3472 | } |
| 3473 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3474 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3475 | if (notify) { |
| 3476 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3477 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3478 | status); |
| 3479 | } |
| 3480 | } else { |
| 3481 | // Monitor channels are never explicitly unregistered. |
| 3482 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3483 | const bool stillHaveWindowHandle = |
| 3484 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3485 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3486 | if (notify) { |
| 3487 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3488 | connection->getInputChannelName().c_str(), events); |
| 3489 | } |
| 3490 | } |
| 3491 | |
| 3492 | // Remove the channel. |
| 3493 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3494 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3495 | } |
| 3496 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3497 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3498 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3499 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3500 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3501 | } |
| 3502 | } |
| 3503 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3504 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3505 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3506 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3507 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3508 | } |
| 3509 | |
| 3510 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3511 | const CancelationOptions& options, |
| 3512 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3513 | for (const auto& it : monitorsByDisplay) { |
| 3514 | const std::vector<Monitor>& monitors = it.second; |
| 3515 | for (const Monitor& monitor : monitors) { |
| 3516 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3517 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3518 | } |
| 3519 | } |
| 3520 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3521 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3522 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3523 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3524 | if (connection == nullptr) { |
| 3525 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3526 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3527 | |
| 3528 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3532 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 3533 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3534 | return; |
| 3535 | } |
| 3536 | |
| 3537 | nsecs_t currentTime = now(); |
| 3538 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3539 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3540 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3541 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3542 | if (cancelationEvents.empty()) { |
| 3543 | return; |
| 3544 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3545 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3546 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3547 | "with reality: %s, mode=%d.", |
| 3548 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3549 | options.mode); |
| 3550 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3551 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3552 | std::string reason = std::string("reason=").append(options.reason); |
| 3553 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3554 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3555 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3556 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3557 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3558 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3559 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3560 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3561 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3562 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3563 | } |
| 3564 | target.inputChannel = connection->inputChannel; |
| 3565 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3566 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3567 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3568 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3569 | switch (cancelationEventEntry->type) { |
| 3570 | case EventEntry::Type::KEY: { |
| 3571 | logOutboundKeyDetails("cancel - ", |
| 3572 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3573 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3574 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3575 | case EventEntry::Type::MOTION: { |
| 3576 | logOutboundMotionDetails("cancel - ", |
| 3577 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3578 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3579 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3580 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3581 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3582 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3583 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3584 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3585 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3586 | break; |
| 3587 | } |
| 3588 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3589 | case EventEntry::Type::DEVICE_RESET: |
| 3590 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3591 | 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] | 3592 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3593 | break; |
| 3594 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3595 | } |
| 3596 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3597 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3598 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3599 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3600 | |
| 3601 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3602 | } |
| 3603 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3604 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3605 | const sp<Connection>& connection) { |
| 3606 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3607 | return; |
| 3608 | } |
| 3609 | |
| 3610 | nsecs_t currentTime = now(); |
| 3611 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3612 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3613 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3614 | |
| 3615 | if (downEvents.empty()) { |
| 3616 | return; |
| 3617 | } |
| 3618 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3619 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3620 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3621 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3622 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3623 | |
| 3624 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3625 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3626 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3627 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3628 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3629 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3630 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3631 | } |
| 3632 | target.inputChannel = connection->inputChannel; |
| 3633 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3634 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3635 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3636 | switch (downEventEntry->type) { |
| 3637 | case EventEntry::Type::MOTION: { |
| 3638 | logOutboundMotionDetails("down - ", |
| 3639 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3640 | break; |
| 3641 | } |
| 3642 | |
| 3643 | case EventEntry::Type::KEY: |
| 3644 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3645 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3646 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3647 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3648 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3649 | case EventEntry::Type::SENSOR: |
| 3650 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3651 | 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] | 3652 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3653 | break; |
| 3654 | } |
| 3655 | } |
| 3656 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3657 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3658 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | startDispatchCycleLocked(currentTime, connection); |
| 3662 | } |
| 3663 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3664 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3665 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3666 | ALOG_ASSERT(pointerIds.value != 0); |
| 3667 | |
| 3668 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3669 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3670 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3671 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3672 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3673 | uint32_t splitPointerCount = 0; |
| 3674 | |
| 3675 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3676 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3677 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3678 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3679 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3680 | if (pointerIds.hasBit(pointerId)) { |
| 3681 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3682 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3683 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3684 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3685 | splitPointerCount += 1; |
| 3686 | } |
| 3687 | } |
| 3688 | |
| 3689 | if (splitPointerCount != pointerIds.count()) { |
| 3690 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3691 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3692 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3693 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3694 | // in this way. |
| 3695 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3696 | "we expected there to be %d pointers. This probably means we received " |
| 3697 | "a broken sequence of pointer ids from the input device.", |
| 3698 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3699 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3700 | } |
| 3701 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3702 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3703 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3704 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3705 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3706 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3707 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3708 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3709 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3710 | if (pointerIds.hasBit(pointerId)) { |
| 3711 | if (pointerIds.count() == 1) { |
| 3712 | // The first/last pointer went down/up. |
| 3713 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3714 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3715 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3716 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3717 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3718 | } else { |
| 3719 | // A secondary pointer went down/up. |
| 3720 | uint32_t splitPointerIndex = 0; |
| 3721 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3722 | splitPointerIndex += 1; |
| 3723 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3724 | action = maskedAction | |
| 3725 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3726 | } |
| 3727 | } else { |
| 3728 | // An unrelated pointer changed. |
| 3729 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3730 | } |
| 3731 | } |
| 3732 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3733 | int32_t newId = mIdGenerator.nextId(); |
| 3734 | if (ATRACE_ENABLED()) { |
| 3735 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3736 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3737 | originalMotionEntry.id, newId); |
| 3738 | ATRACE_NAME(message.c_str()); |
| 3739 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3740 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3741 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3742 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3743 | originalMotionEntry.displayId, |
| 3744 | originalMotionEntry.policyFlags, action, |
| 3745 | originalMotionEntry.actionButton, |
| 3746 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3747 | originalMotionEntry.buttonState, |
| 3748 | originalMotionEntry.classification, |
| 3749 | originalMotionEntry.edgeFlags, |
| 3750 | originalMotionEntry.xPrecision, |
| 3751 | originalMotionEntry.yPrecision, |
| 3752 | originalMotionEntry.xCursorPosition, |
| 3753 | originalMotionEntry.yCursorPosition, |
| 3754 | originalMotionEntry.downTime, splitPointerCount, |
Prabir Pradhan | d6a7f22 | 2021-11-24 15:36:43 +0000 | [diff] [blame^] | 3755 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3756 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3757 | if (originalMotionEntry.injectionState) { |
| 3758 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3759 | splitMotionEntry->injectionState->refCount += 1; |
| 3760 | } |
| 3761 | |
| 3762 | return splitMotionEntry; |
| 3763 | } |
| 3764 | |
| 3765 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3766 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3767 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3768 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3769 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3770 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3771 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3772 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3773 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3774 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3775 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3776 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3777 | } // release lock |
| 3778 | |
| 3779 | if (needWake) { |
| 3780 | mLooper->wake(); |
| 3781 | } |
| 3782 | } |
| 3783 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3784 | /** |
| 3785 | * If one of the meta shortcuts is detected, process them here: |
| 3786 | * Meta + Backspace -> generate BACK |
| 3787 | * Meta + Enter -> generate HOME |
| 3788 | * This will potentially overwrite keyCode and metaState. |
| 3789 | */ |
| 3790 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3791 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3792 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3793 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3794 | if (keyCode == AKEYCODE_DEL) { |
| 3795 | newKeyCode = AKEYCODE_BACK; |
| 3796 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3797 | newKeyCode = AKEYCODE_HOME; |
| 3798 | } |
| 3799 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3800 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3801 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3802 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3803 | keyCode = newKeyCode; |
| 3804 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3805 | } |
| 3806 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3807 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3808 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3809 | // 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] | 3810 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3811 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3812 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3813 | if (replacementIt != mReplacedKeys.end()) { |
| 3814 | keyCode = replacementIt->second; |
| 3815 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3816 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3817 | } |
| 3818 | } |
| 3819 | } |
| 3820 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3821 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3822 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3823 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3824 | "policyFlags=0x%x, action=0x%x, " |
| 3825 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3826 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3827 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3828 | args->downTime); |
| 3829 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3830 | if (!validateKeyEvent(args->action)) { |
| 3831 | return; |
| 3832 | } |
| 3833 | |
| 3834 | uint32_t policyFlags = args->policyFlags; |
| 3835 | int32_t flags = args->flags; |
| 3836 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3837 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3838 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3839 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3840 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3841 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3842 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3843 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3844 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3845 | metaState |= AMETA_FUNCTION_ON; |
| 3846 | } |
| 3847 | |
| 3848 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3849 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3850 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3851 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3852 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3853 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3854 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3855 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3856 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3857 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3858 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3859 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3860 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3861 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3862 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3863 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3864 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3865 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3866 | { // acquire lock |
| 3867 | mLock.lock(); |
| 3868 | |
| 3869 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3870 | mLock.unlock(); |
| 3871 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3872 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3873 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3874 | return; // event was consumed by the filter |
| 3875 | } |
| 3876 | |
| 3877 | mLock.lock(); |
| 3878 | } |
| 3879 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3880 | std::unique_ptr<KeyEntry> newEntry = |
| 3881 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3882 | args->displayId, policyFlags, args->action, flags, |
| 3883 | keyCode, args->scanCode, metaState, repeatCount, |
| 3884 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3885 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3886 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3887 | mLock.unlock(); |
| 3888 | } // release lock |
| 3889 | |
| 3890 | if (needWake) { |
| 3891 | mLooper->wake(); |
| 3892 | } |
| 3893 | } |
| 3894 | |
| 3895 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3896 | return mInputFilterEnabled; |
| 3897 | } |
| 3898 | |
| 3899 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3900 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3901 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3902 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 3903 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3904 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 3905 | "yCursorPosition=%f, downTime=%" PRId64, |
| 3906 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3907 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3908 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3909 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 3910 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3911 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 3912 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3913 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3914 | "orientation=%f", |
| 3915 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3916 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3917 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3918 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3919 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3920 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3921 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3922 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3923 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3924 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 3925 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3926 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3927 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3928 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3929 | return; |
| 3930 | } |
| 3931 | |
| 3932 | uint32_t policyFlags = args->policyFlags; |
| 3933 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3934 | |
| 3935 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3936 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3937 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3938 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3939 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3940 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3941 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3942 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3943 | { // acquire lock |
| 3944 | mLock.lock(); |
| 3945 | |
| 3946 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3947 | ui::Transform displayTransform; |
| 3948 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 3949 | displayTransform = it->second.transform; |
| 3950 | } |
| 3951 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3952 | mLock.unlock(); |
| 3953 | |
| 3954 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3955 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3956 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3957 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3958 | displayTransform, args->xPrecision, args->yPrecision, |
| 3959 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3960 | args->downTime, args->eventTime, args->pointerCount, |
| 3961 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3962 | |
| 3963 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3964 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3965 | return; // event was consumed by the filter |
| 3966 | } |
| 3967 | |
| 3968 | mLock.lock(); |
| 3969 | } |
| 3970 | |
| 3971 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3972 | std::unique_ptr<MotionEntry> newEntry = |
| 3973 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 3974 | args->source, args->displayId, policyFlags, |
| 3975 | args->action, args->actionButton, args->flags, |
| 3976 | args->metaState, args->buttonState, |
| 3977 | args->classification, args->edgeFlags, |
| 3978 | args->xPrecision, args->yPrecision, |
| 3979 | args->xCursorPosition, args->yCursorPosition, |
| 3980 | args->downTime, args->pointerCount, |
Prabir Pradhan | d6a7f22 | 2021-11-24 15:36:43 +0000 | [diff] [blame^] | 3981 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3982 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 3983 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 3984 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 3985 | !mInputFilterEnabled) { |
| 3986 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 3987 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 3988 | } |
| 3989 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3990 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3991 | mLock.unlock(); |
| 3992 | } // release lock |
| 3993 | |
| 3994 | if (needWake) { |
| 3995 | mLooper->wake(); |
| 3996 | } |
| 3997 | } |
| 3998 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3999 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4000 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4001 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4002 | " sensorType=%s", |
| 4003 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4004 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4005 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4006 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4007 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4008 | { // acquire lock |
| 4009 | mLock.lock(); |
| 4010 | |
| 4011 | // Just enqueue a new sensor event. |
| 4012 | std::unique_ptr<SensorEntry> newEntry = |
| 4013 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4014 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4015 | args->sensorType, args->accuracy, |
| 4016 | args->accuracyChanged, args->values); |
| 4017 | |
| 4018 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4019 | mLock.unlock(); |
| 4020 | } // release lock |
| 4021 | |
| 4022 | if (needWake) { |
| 4023 | mLooper->wake(); |
| 4024 | } |
| 4025 | } |
| 4026 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4027 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4028 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4029 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4030 | args->deviceId, args->isOn); |
| 4031 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4032 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4033 | } |
| 4034 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4035 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4036 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4040 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4041 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4042 | "switchMask=0x%08x", |
| 4043 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4044 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4045 | |
| 4046 | uint32_t policyFlags = args->policyFlags; |
| 4047 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4048 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4049 | } |
| 4050 | |
| 4051 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4052 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4053 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4054 | args->deviceId); |
| 4055 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4056 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4057 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4058 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4059 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4060 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4061 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4062 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4063 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4064 | } // release lock |
| 4065 | |
| 4066 | if (needWake) { |
| 4067 | mLooper->wake(); |
| 4068 | } |
| 4069 | } |
| 4070 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4071 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4072 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4073 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4074 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4075 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4076 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4077 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4078 | { // acquire lock |
| 4079 | std::scoped_lock _l(mLock); |
| 4080 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4081 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4082 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4083 | } // release lock |
| 4084 | |
| 4085 | if (needWake) { |
| 4086 | mLooper->wake(); |
| 4087 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4088 | } |
| 4089 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4090 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4091 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4092 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4093 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4094 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 4095 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4096 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
| 4097 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4098 | 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] | 4099 | |
| 4100 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4101 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4102 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4103 | } |
| 4104 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4105 | // 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] | 4106 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4107 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4108 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4109 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4110 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4111 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4112 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4113 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4116 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4117 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4118 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4119 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4120 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4121 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4122 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4123 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4124 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4125 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4126 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4127 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4128 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4129 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4130 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4131 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4132 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4133 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4134 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4135 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4136 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4137 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4138 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4139 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4140 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4141 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4142 | |
| 4143 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4144 | android::base::Timer t; |
| 4145 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4146 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4147 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4148 | std::to_string(t.duration().count()).c_str()); |
| 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4153 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4154 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4155 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4156 | incomingKey.getDisplayId(), policyFlags, action, |
| 4157 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4158 | incomingKey.getRepeatCount(), |
| 4159 | incomingKey.getDownTime()); |
| 4160 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4161 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4162 | } |
| 4163 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4164 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4165 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4166 | const int32_t action = motionEvent.getAction(); |
| 4167 | const bool isPointerEvent = |
| 4168 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4169 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4170 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4171 | ? ADISPLAY_ID_DEFAULT |
| 4172 | : event->getDisplayId(); |
| 4173 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4174 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4175 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4176 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4177 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4178 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4182 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4183 | android::base::Timer t; |
| 4184 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4185 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4186 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4187 | std::to_string(t.duration().count()).c_str()); |
| 4188 | } |
| 4189 | } |
| 4190 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4191 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4192 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4193 | } |
| 4194 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4195 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4196 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4197 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4198 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4199 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4200 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4201 | displayId, policyFlags, action, actionButton, |
| 4202 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4203 | motionEvent.getButtonState(), |
| 4204 | motionEvent.getClassification(), |
| 4205 | motionEvent.getEdgeFlags(), |
| 4206 | motionEvent.getXPrecision(), |
| 4207 | motionEvent.getYPrecision(), |
| 4208 | motionEvent.getRawXCursorPosition(), |
| 4209 | motionEvent.getRawYCursorPosition(), |
| 4210 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | d6a7f22 | 2021-11-24 15:36:43 +0000 | [diff] [blame^] | 4211 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | 923373d | 2021-11-24 15:36:43 +0000 | [diff] [blame] | 4212 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4213 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4214 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4215 | sampleEventTimes += 1; |
| 4216 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4217 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4218 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4219 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4220 | displayId, policyFlags, action, actionButton, |
| 4221 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4222 | motionEvent.getButtonState(), |
| 4223 | motionEvent.getClassification(), |
| 4224 | motionEvent.getEdgeFlags(), |
| 4225 | motionEvent.getXPrecision(), |
| 4226 | motionEvent.getYPrecision(), |
| 4227 | motionEvent.getRawXCursorPosition(), |
| 4228 | motionEvent.getRawYCursorPosition(), |
| 4229 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4230 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | d6a7f22 | 2021-11-24 15:36:43 +0000 | [diff] [blame^] | 4231 | samplePointerCoords); |
Prabir Pradhan | 923373d | 2021-11-24 15:36:43 +0000 | [diff] [blame] | 4232 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4233 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4234 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4235 | } |
| 4236 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4237 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4238 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4239 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4240 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4241 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4245 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4246 | injectionState->injectionIsAsync = true; |
| 4247 | } |
| 4248 | |
| 4249 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4250 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4251 | |
| 4252 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4253 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4254 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4255 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | mLock.unlock(); |
| 4259 | |
| 4260 | if (needWake) { |
| 4261 | mLooper->wake(); |
| 4262 | } |
| 4263 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4264 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4265 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4266 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4267 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4268 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4269 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4270 | } else { |
| 4271 | for (;;) { |
| 4272 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4273 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4274 | break; |
| 4275 | } |
| 4276 | |
| 4277 | nsecs_t remainingTimeout = endTime - now(); |
| 4278 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4279 | if (DEBUG_INJECTION) { |
| 4280 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4281 | "to become available."); |
| 4282 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4283 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4284 | break; |
| 4285 | } |
| 4286 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4287 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4288 | } |
| 4289 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4290 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4291 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4292 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4293 | if (DEBUG_INJECTION) { |
| 4294 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4295 | injectionState->pendingForegroundDispatches); |
| 4296 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4297 | nsecs_t remainingTimeout = endTime - now(); |
| 4298 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4299 | if (DEBUG_INJECTION) { |
| 4300 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4301 | "dispatches to finish."); |
| 4302 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4303 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4304 | break; |
| 4305 | } |
| 4306 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4307 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4308 | } |
| 4309 | } |
| 4310 | } |
| 4311 | |
| 4312 | injectionState->release(); |
| 4313 | } // release lock |
| 4314 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4315 | if (DEBUG_INJECTION) { |
| 4316 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
| 4317 | injectionResult, injectorPid, injectorUid); |
| 4318 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4319 | |
| 4320 | return injectionResult; |
| 4321 | } |
| 4322 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4323 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4324 | std::array<uint8_t, 32> calculatedHmac; |
| 4325 | std::unique_ptr<VerifiedInputEvent> result; |
| 4326 | switch (event.getType()) { |
| 4327 | case AINPUT_EVENT_TYPE_KEY: { |
| 4328 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4329 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4330 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4331 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4332 | break; |
| 4333 | } |
| 4334 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4335 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4336 | VerifiedMotionEvent verifiedMotionEvent = |
| 4337 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4338 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4339 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4340 | break; |
| 4341 | } |
| 4342 | default: { |
| 4343 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4344 | return nullptr; |
| 4345 | } |
| 4346 | } |
| 4347 | if (calculatedHmac == INVALID_HMAC) { |
| 4348 | return nullptr; |
| 4349 | } |
| 4350 | if (calculatedHmac != event.getHmac()) { |
| 4351 | return nullptr; |
| 4352 | } |
| 4353 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4354 | } |
| 4355 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4356 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4357 | return injectorUid == 0 || |
| 4358 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4359 | } |
| 4360 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4361 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4362 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4363 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4364 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4365 | if (DEBUG_INJECTION) { |
| 4366 | ALOGD("Setting input event injection result to %d. " |
| 4367 | "injectorPid=%d, injectorUid=%d", |
| 4368 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
| 4369 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4370 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4371 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4372 | // Log the outcome since the injector did not wait for the injection result. |
| 4373 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4374 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4375 | ALOGV("Asynchronous input event injection succeeded."); |
| 4376 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4377 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4378 | ALOGW("Asynchronous input event injection failed."); |
| 4379 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4380 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4381 | ALOGW("Asynchronous input event injection permission denied."); |
| 4382 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4383 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4384 | ALOGW("Asynchronous input event injection timed out."); |
| 4385 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4386 | case InputEventInjectionResult::PENDING: |
| 4387 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4388 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4389 | } |
| 4390 | } |
| 4391 | |
| 4392 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4393 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4394 | } |
| 4395 | } |
| 4396 | |
Prabir Pradhan | 923373d | 2021-11-24 15:36:43 +0000 | [diff] [blame] | 4397 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4398 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4399 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4400 | // display space, so we need to transform the injected events accordingly. |
| 4401 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4402 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | 923373d | 2021-11-24 15:36:43 +0000 | [diff] [blame] | 4403 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4404 | |
| 4405 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 4406 | PointerCoords& pc = entry.pointerCoords[i]; |
Prabir Pradhan | 923373d | 2021-11-24 15:36:43 +0000 | [diff] [blame] | 4407 | // Make a copy of the injected coords. We cannot change them in place because some of them |
| 4408 | // are interdependent (for example, X coordinate might depend on the Y coordinate). |
| 4409 | PointerCoords injectedCoords = entry.pointerCoords[i]; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4410 | |
Prabir Pradhan | 923373d | 2021-11-24 15:36:43 +0000 | [diff] [blame] | 4411 | BitSet64 bits(injectedCoords.bits); |
| 4412 | while (!bits.isEmpty()) { |
| 4413 | const auto axis = static_cast<int32_t>(bits.clearFirstMarkedBit()); |
| 4414 | const float value = |
| 4415 | MotionEvent::calculateTransformedAxisValue(axis, entry.source, |
| 4416 | transformToDisplay, injectedCoords); |
| 4417 | pc.setAxisValue(axis, value); |
| 4418 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4419 | } |
| 4420 | } |
| 4421 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4422 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4423 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4424 | if (injectionState) { |
| 4425 | injectionState->pendingForegroundDispatches += 1; |
| 4426 | } |
| 4427 | } |
| 4428 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4429 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4430 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4431 | if (injectionState) { |
| 4432 | injectionState->pendingForegroundDispatches -= 1; |
| 4433 | |
| 4434 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4435 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4436 | } |
| 4437 | } |
| 4438 | } |
| 4439 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4440 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4441 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4442 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4443 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4444 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4445 | } |
| 4446 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4447 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4448 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4449 | if (windowHandleToken == nullptr) { |
| 4450 | return nullptr; |
| 4451 | } |
| 4452 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4453 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4454 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4455 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4456 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4457 | return windowHandle; |
| 4458 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4459 | } |
| 4460 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4461 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4462 | } |
| 4463 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4464 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4465 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4466 | if (windowHandleToken == nullptr) { |
| 4467 | return nullptr; |
| 4468 | } |
| 4469 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4470 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4471 | if (windowHandle->getToken() == windowHandleToken) { |
| 4472 | return windowHandle; |
| 4473 | } |
| 4474 | } |
| 4475 | return nullptr; |
| 4476 | } |
| 4477 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4478 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4479 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4480 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4481 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4482 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4483 | if (handle->getId() == windowHandle->getId() && |
| 4484 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4485 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4486 | ALOGE("Found window %s in display %" PRId32 |
| 4487 | ", but it should belong to display %" PRId32, |
| 4488 | windowHandle->getName().c_str(), it.first, |
| 4489 | windowHandle->getInfo()->displayId); |
| 4490 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4491 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4492 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4493 | } |
| 4494 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4495 | return nullptr; |
| 4496 | } |
| 4497 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4498 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4499 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4500 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4501 | } |
| 4502 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4503 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4504 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4505 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4506 | windowHandle.getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4507 | if (connection != nullptr && noInputChannel) { |
| 4508 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4509 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4510 | return false; |
| 4511 | } |
| 4512 | |
| 4513 | if (connection == nullptr) { |
| 4514 | if (!noInputChannel) { |
| 4515 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4516 | } |
| 4517 | return false; |
| 4518 | } |
| 4519 | if (!connection->responsive) { |
| 4520 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4521 | return false; |
| 4522 | } |
| 4523 | return true; |
| 4524 | } |
| 4525 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4526 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4527 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4528 | auto connectionIt = mConnectionsByToken.find(token); |
| 4529 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4530 | return nullptr; |
| 4531 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4532 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4533 | } |
| 4534 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4535 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4536 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4537 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4538 | // Remove all handles on a display if there are no windows left. |
| 4539 | mWindowHandlesByDisplay.erase(displayId); |
| 4540 | return; |
| 4541 | } |
| 4542 | |
| 4543 | // Since we compare the pointer of input window handles across window updates, we need |
| 4544 | // 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] | 4545 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4546 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4547 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4548 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4549 | } |
| 4550 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4551 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4552 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4553 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4554 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4555 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4556 | info->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4557 | const bool canReceiveInput = !info->flags.test(WindowInfo::Flag::NOT_TOUCHABLE) || |
| 4558 | !info->flags.test(WindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4559 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4560 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4561 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4562 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4563 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | if (info->displayId != displayId) { |
| 4567 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4568 | handle->getName().c_str(), displayId, info->displayId); |
| 4569 | continue; |
| 4570 | } |
| 4571 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4572 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4573 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4574 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4575 | oldHandle->updateFrom(handle); |
| 4576 | newHandles.push_back(oldHandle); |
| 4577 | } else { |
| 4578 | newHandles.push_back(handle); |
| 4579 | } |
| 4580 | } |
| 4581 | |
| 4582 | // Insert or replace |
| 4583 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4584 | } |
| 4585 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4586 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4587 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4588 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4589 | { // acquire lock |
| 4590 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4591 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4592 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4593 | } |
| 4594 | } |
| 4595 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4596 | mLooper->wake(); |
| 4597 | } |
| 4598 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4599 | /** |
| 4600 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4601 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4602 | * If set an empty list, remove all handles from the specific display. |
| 4603 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4604 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4605 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4606 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4607 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4608 | if (DEBUG_FOCUS) { |
| 4609 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4610 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4611 | windowList += iwh->getName() + " "; |
| 4612 | } |
| 4613 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4614 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4615 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4616 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4617 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4618 | const bool noInputWindow = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4619 | window->getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4620 | if (noInputWindow && window->getToken() != nullptr) { |
| 4621 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4622 | window->getName().c_str()); |
| 4623 | window->releaseChannel(); |
| 4624 | } |
| 4625 | } |
| 4626 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4627 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4628 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4629 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4630 | // Save the old windows' orientation by ID before it gets updated. |
| 4631 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4632 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4633 | oldWindowOrientations.emplace(handle->getId(), |
| 4634 | handle->getInfo()->transform.getOrientation()); |
| 4635 | } |
| 4636 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4637 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4638 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4639 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4640 | if (mLastHoverWindowHandle && |
| 4641 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4642 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4643 | mLastHoverWindowHandle = nullptr; |
| 4644 | } |
| 4645 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4646 | std::optional<FocusResolver::FocusChanges> changes = |
| 4647 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4648 | if (changes) { |
| 4649 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4650 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4651 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4652 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4653 | mTouchStatesByDisplay.find(displayId); |
| 4654 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4655 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4656 | for (size_t i = 0; i < state.windows.size();) { |
| 4657 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4658 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4659 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4660 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4661 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4662 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4663 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4664 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4665 | if (touchedInputChannel != nullptr) { |
| 4666 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4667 | "touched window was removed"); |
| 4668 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4669 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4670 | // well. |
| 4671 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
| 4672 | touchedWindow.windowHandle->getInfo()->hasWallpaper) { |
| 4673 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4674 | if (wallpaper != nullptr) { |
| 4675 | sp<Connection> wallpaperConnection = |
| 4676 | getConnectionLocked(wallpaper->getToken()); |
| 4677 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4678 | options); |
| 4679 | } |
| 4680 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4681 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4682 | state.windows.erase(state.windows.begin() + i); |
| 4683 | } else { |
| 4684 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4685 | } |
| 4686 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4687 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4688 | // 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] | 4689 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4690 | if (mDragState && |
| 4691 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4692 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4693 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4694 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4695 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4696 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4697 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4698 | // pointer events if necessary. |
| 4699 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4700 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4701 | if (newWindowHandle != nullptr && |
| 4702 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4703 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4704 | std::shared_ptr<InputChannel> inputChannel = |
| 4705 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4706 | if (inputChannel != nullptr) { |
| 4707 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4708 | "touched window's orientation changed"); |
| 4709 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4710 | } |
| 4711 | } |
| 4712 | } |
| 4713 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4714 | // Release information for windows that are no longer present. |
| 4715 | // This ensures that unused input channels are released promptly. |
| 4716 | // Otherwise, they might stick around until the window handle is destroyed |
| 4717 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4718 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4719 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4720 | if (DEBUG_FOCUS) { |
| 4721 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4722 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4723 | oldWindowHandle->releaseChannel(); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4724 | // To avoid making too many calls into the compat framework, only |
| 4725 | // check for window flags when windows are going away. |
| 4726 | // TODO(b/157929241) : delete this. This is only needed temporarily |
| 4727 | // in order to gather some data about the flag usage |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4728 | if (oldWindowHandle->getInfo()->flags.test(WindowInfo::Flag::SLIPPERY)) { |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4729 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", |
| 4730 | oldWindowHandle->getName().c_str()); |
| 4731 | if (mCompatService != nullptr) { |
| 4732 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, |
| 4733 | oldWindowHandle->getInfo()->ownerUid); |
| 4734 | } |
| 4735 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4736 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4737 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4741 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4742 | if (DEBUG_FOCUS) { |
| 4743 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4744 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4745 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4746 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4747 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4748 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4749 | } // release lock |
| 4750 | |
| 4751 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4752 | mLooper->wake(); |
| 4753 | } |
| 4754 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4755 | void InputDispatcher::setFocusedApplicationLocked( |
| 4756 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4757 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4758 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4759 | |
| 4760 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4761 | return; // This application is already focused. No need to wake up or change anything. |
| 4762 | } |
| 4763 | |
| 4764 | // Set the new application handle. |
| 4765 | if (inputApplicationHandle != nullptr) { |
| 4766 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4767 | } else { |
| 4768 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4769 | } |
| 4770 | |
| 4771 | // No matter what the old focused application was, stop waiting on it because it is |
| 4772 | // no longer focused. |
| 4773 | resetNoFocusedWindowTimeoutLocked(); |
| 4774 | } |
| 4775 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4776 | /** |
| 4777 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4778 | * the display not specified. |
| 4779 | * |
| 4780 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4781 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4782 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4783 | * display. The display-specified events won't be affected. |
| 4784 | */ |
| 4785 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4786 | if (DEBUG_FOCUS) { |
| 4787 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4788 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4789 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4790 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4791 | |
| 4792 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4793 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4794 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4795 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4796 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4797 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4798 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4799 | CancelationOptions |
| 4800 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4801 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4802 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4803 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4804 | } |
| 4805 | } |
| 4806 | mFocusedDisplayId = displayId; |
| 4807 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4808 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4809 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4810 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4811 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4812 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4813 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4814 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4815 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4816 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4817 | } |
| 4818 | } |
| 4819 | } |
| 4820 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4821 | if (DEBUG_FOCUS) { |
| 4822 | logDispatchStateLocked(); |
| 4823 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4824 | } // release lock |
| 4825 | |
| 4826 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4827 | mLooper->wake(); |
| 4828 | } |
| 4829 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4830 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4831 | if (DEBUG_FOCUS) { |
| 4832 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4833 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4834 | |
| 4835 | bool changed; |
| 4836 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4837 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4838 | |
| 4839 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4840 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4841 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4842 | } |
| 4843 | |
| 4844 | if (mDispatchEnabled && !enabled) { |
| 4845 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4846 | } |
| 4847 | |
| 4848 | mDispatchEnabled = enabled; |
| 4849 | mDispatchFrozen = frozen; |
| 4850 | changed = true; |
| 4851 | } else { |
| 4852 | changed = false; |
| 4853 | } |
| 4854 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4855 | if (DEBUG_FOCUS) { |
| 4856 | logDispatchStateLocked(); |
| 4857 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4858 | } // release lock |
| 4859 | |
| 4860 | if (changed) { |
| 4861 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4862 | mLooper->wake(); |
| 4863 | } |
| 4864 | } |
| 4865 | |
| 4866 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4867 | if (DEBUG_FOCUS) { |
| 4868 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4869 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4870 | |
| 4871 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4872 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4873 | |
| 4874 | if (mInputFilterEnabled == enabled) { |
| 4875 | return; |
| 4876 | } |
| 4877 | |
| 4878 | mInputFilterEnabled = enabled; |
| 4879 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4880 | } // release lock |
| 4881 | |
| 4882 | // Wake up poll loop since there might be work to do to drop everything. |
| 4883 | mLooper->wake(); |
| 4884 | } |
| 4885 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4886 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4887 | bool needWake = false; |
| 4888 | { |
| 4889 | std::scoped_lock lock(mLock); |
| 4890 | if (mInTouchMode == inTouchMode) { |
| 4891 | return; |
| 4892 | } |
| 4893 | if (DEBUG_TOUCH_MODE) { |
| 4894 | ALOGD("Request to change touch mode from %s to %s", toString(mInTouchMode), |
| 4895 | toString(inTouchMode)); |
| 4896 | // TODO(b/198487159): Also print the current last interacted apps. |
| 4897 | } |
| 4898 | |
| 4899 | // TODO(b/198499018): Store touch mode per display. |
| 4900 | mInTouchMode = inTouchMode; |
| 4901 | |
| 4902 | // TODO(b/198487159): Enforce that only last interacted apps can change touch mode. |
| 4903 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 4904 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4905 | } // release lock |
| 4906 | |
| 4907 | if (needWake) { |
| 4908 | mLooper->wake(); |
| 4909 | } |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4910 | } |
| 4911 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4912 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4913 | if (opacity < 0 || opacity > 1) { |
| 4914 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4915 | return; |
| 4916 | } |
| 4917 | |
| 4918 | std::scoped_lock lock(mLock); |
| 4919 | mMaximumObscuringOpacityForTouch = opacity; |
| 4920 | } |
| 4921 | |
| 4922 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4923 | std::scoped_lock lock(mLock); |
| 4924 | mBlockUntrustedTouchesMode = mode; |
| 4925 | } |
| 4926 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4927 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 4928 | const sp<IBinder>& token) { |
| 4929 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 4930 | for (TouchedWindow& w : state.windows) { |
| 4931 | if (w.windowHandle->getToken() == token) { |
| 4932 | return std::make_pair(&state, &w); |
| 4933 | } |
| 4934 | } |
| 4935 | } |
| 4936 | return std::make_pair(nullptr, nullptr); |
| 4937 | } |
| 4938 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4939 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 4940 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4941 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4942 | if (DEBUG_FOCUS) { |
| 4943 | ALOGD("Trivial transfer to same window."); |
| 4944 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4945 | return true; |
| 4946 | } |
| 4947 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4948 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4949 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4950 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4951 | // Find the target touch state and touched window by fromToken. |
| 4952 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 4953 | if (state == nullptr || touchedWindow == nullptr) { |
| 4954 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4955 | return false; |
| 4956 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4957 | |
| 4958 | const int32_t displayId = state->displayId; |
| 4959 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 4960 | if (toWindowHandle == nullptr) { |
| 4961 | ALOGW("Cannot transfer focus because to window not found."); |
| 4962 | return false; |
| 4963 | } |
| 4964 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4965 | if (DEBUG_FOCUS) { |
| 4966 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4967 | touchedWindow->windowHandle->getName().c_str(), |
| 4968 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4969 | } |
| 4970 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4971 | // Erase old window. |
| 4972 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 4973 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 4974 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4975 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4976 | // Add new window. |
| 4977 | int32_t newTargetFlags = oldTargetFlags & |
| 4978 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4979 | InputTarget::FLAG_DISPATCH_AS_IS); |
| 4980 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4981 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4982 | // Store the dragging window. |
| 4983 | if (isDragDrop) { |
| 4984 | mDragState = std::make_unique<DragState>(toWindowHandle); |
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 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4988 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4989 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4990 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4991 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4992 | CancelationOptions |
| 4993 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4994 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4995 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4996 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4997 | } |
| 4998 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4999 | if (DEBUG_FOCUS) { |
| 5000 | logDispatchStateLocked(); |
| 5001 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5002 | } // release lock |
| 5003 | |
| 5004 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5005 | mLooper->wake(); |
| 5006 | return true; |
| 5007 | } |
| 5008 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5009 | // Binder call |
| 5010 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 5011 | sp<IBinder> fromToken; |
| 5012 | { // acquire lock |
| 5013 | std::scoped_lock _l(mLock); |
| 5014 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5015 | auto it = std::find_if(mTouchStatesByDisplay.begin(), mTouchStatesByDisplay.end(), |
| 5016 | [](const auto& pair) { return pair.second.windows.size() == 1; }); |
| 5017 | if (it == mTouchStatesByDisplay.end()) { |
| 5018 | ALOGW("Cannot transfer touch state because there is no exact window being touched"); |
| 5019 | return false; |
| 5020 | } |
| 5021 | const int32_t displayId = it->first; |
| 5022 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5023 | if (toWindowHandle == nullptr) { |
| 5024 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 5025 | return false; |
| 5026 | } |
| 5027 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5028 | TouchState& state = it->second; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5029 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 5030 | fromToken = touchedWindow.windowHandle->getToken(); |
| 5031 | } // release lock |
| 5032 | |
| 5033 | return transferTouchFocus(fromToken, destChannelToken); |
| 5034 | } |
| 5035 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5036 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5037 | if (DEBUG_FOCUS) { |
| 5038 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5039 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5040 | |
| 5041 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5042 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5043 | |
| 5044 | resetKeyRepeatLocked(); |
| 5045 | releasePendingEventLocked(); |
| 5046 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5047 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5048 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5049 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5050 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5051 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5052 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5053 | } |
| 5054 | |
| 5055 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5056 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5057 | dumpDispatchStateLocked(dump); |
| 5058 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5059 | std::istringstream stream(dump); |
| 5060 | std::string line; |
| 5061 | |
| 5062 | while (std::getline(stream, line, '\n')) { |
| 5063 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5064 | } |
| 5065 | } |
| 5066 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5067 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5068 | std::string dump; |
| 5069 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5070 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5071 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5072 | |
| 5073 | std::string windowName = "None"; |
| 5074 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5075 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5076 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5077 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5078 | : "token has capture without window"; |
| 5079 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5080 | 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] | 5081 | |
| 5082 | return dump; |
| 5083 | } |
| 5084 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5085 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5086 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5087 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5088 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5089 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5090 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5091 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5092 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5093 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5094 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5095 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5096 | const std::chrono::duration timeout = |
| 5097 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5098 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5099 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5100 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5101 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5102 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5103 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5104 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5105 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5106 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5107 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5108 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5109 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5110 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5111 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5112 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5113 | 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] | 5114 | state.displayId, toString(state.down), toString(state.split), |
| 5115 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5116 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5117 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5118 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5119 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5120 | dump += StringPrintf(INDENT4 |
| 5121 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5122 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5123 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5124 | } |
| 5125 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5126 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5127 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5128 | } |
| 5129 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5130 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5131 | } |
| 5132 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5133 | if (mDragState) { |
| 5134 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5135 | mDragState->dump(dump, INDENT2); |
| 5136 | } |
| 5137 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5138 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5139 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5140 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5141 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5142 | const auto& displayInfo = it->second; |
| 5143 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5144 | displayInfo.logicalHeight); |
| 5145 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5146 | } else { |
| 5147 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5148 | } |
| 5149 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5150 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5151 | dump += INDENT2 "Windows:\n"; |
| 5152 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5153 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5154 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5155 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5156 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5157 | "paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5158 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5159 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5160 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5161 | "applicationInfo.name=%s, " |
| 5162 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5163 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5164 | i, windowInfo->name.c_str(), windowInfo->id, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5165 | windowInfo->displayId, toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5166 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5167 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5168 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 5169 | windowInfo->flags.string().c_str(), |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 5170 | ftl::enum_string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5171 | windowInfo->frameLeft, windowInfo->frameTop, |
| 5172 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5173 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5174 | windowInfo->applicationInfo.name.c_str(), |
| 5175 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5176 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5177 | dump += StringPrintf(", inputFeatures=%s", |
| 5178 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5179 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5180 | "ms, trustedOverlay=%s, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5181 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5182 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5183 | millis(windowInfo->dispatchingTimeout), |
| 5184 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5185 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5186 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5187 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5188 | } |
| 5189 | } else { |
| 5190 | dump += INDENT2 "Windows: <none>\n"; |
| 5191 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5192 | } |
| 5193 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5194 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5195 | } |
| 5196 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5197 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5198 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5199 | const std::vector<Monitor>& monitors = it.second; |
| 5200 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5201 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5202 | } |
| 5203 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5204 | const std::vector<Monitor>& monitors = it.second; |
| 5205 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5206 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5207 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5208 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5209 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5210 | } |
| 5211 | |
| 5212 | nsecs_t currentTime = now(); |
| 5213 | |
| 5214 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5215 | if (!mRecentQueue.empty()) { |
| 5216 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5217 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5218 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5219 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5220 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5221 | } |
| 5222 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5223 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | // Dump event currently being dispatched. |
| 5227 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5228 | dump += INDENT "PendingEvent:\n"; |
| 5229 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5230 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5231 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5232 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5233 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5234 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5235 | } |
| 5236 | |
| 5237 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5238 | if (!mInboundQueue.empty()) { |
| 5239 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5240 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5241 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5242 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5243 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5244 | } |
| 5245 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5246 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5247 | } |
| 5248 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5249 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5250 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5251 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5252 | const KeyReplacement& replacement = pair.first; |
| 5253 | int32_t newKeyCode = pair.second; |
| 5254 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5255 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5256 | } |
| 5257 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5258 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5259 | } |
| 5260 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5261 | if (!mCommandQueue.empty()) { |
| 5262 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5263 | } else { |
| 5264 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5265 | } |
| 5266 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5267 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5268 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5269 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5270 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5271 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5272 | connection->inputChannel->getFd().get(), |
| 5273 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5274 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5275 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5276 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5277 | if (!connection->outboundQueue.empty()) { |
| 5278 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5279 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5280 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5281 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5282 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5283 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5284 | } |
| 5285 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5286 | if (!connection->waitQueue.empty()) { |
| 5287 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5288 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5289 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5290 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5291 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5292 | } |
| 5293 | } |
| 5294 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5295 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5296 | } |
| 5297 | |
| 5298 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5299 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5300 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5301 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5302 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5303 | } |
| 5304 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5305 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5306 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5307 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5308 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5309 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5310 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5311 | } |
| 5312 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5313 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5314 | const size_t numMonitors = monitors.size(); |
| 5315 | for (size_t i = 0; i < numMonitors; i++) { |
| 5316 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5317 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5318 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5319 | dump += "\n"; |
| 5320 | } |
| 5321 | } |
| 5322 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5323 | class LooperEventCallback : public LooperCallback { |
| 5324 | public: |
| 5325 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5326 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5327 | |
| 5328 | private: |
| 5329 | std::function<int(int events)> mCallback; |
| 5330 | }; |
| 5331 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5332 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5333 | if (DEBUG_CHANNEL_CREATION) { |
| 5334 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5335 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5336 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5337 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5338 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5339 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5340 | |
| 5341 | if (result) { |
| 5342 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5343 | } |
| 5344 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5345 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5346 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5347 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5348 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5349 | sp<Connection> connection = |
| 5350 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5351 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5352 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5353 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5354 | } |
| 5355 | mConnectionsByToken.emplace(token, connection); |
| 5356 | |
| 5357 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5358 | this, std::placeholders::_1, token); |
| 5359 | |
| 5360 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5361 | } // release lock |
| 5362 | |
| 5363 | // Wake the looper because some connections have changed. |
| 5364 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5365 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5366 | } |
| 5367 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5368 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5369 | bool isGestureMonitor, |
| 5370 | const std::string& name, |
| 5371 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5372 | std::shared_ptr<InputChannel> serverChannel; |
| 5373 | std::unique_ptr<InputChannel> clientChannel; |
| 5374 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5375 | if (result) { |
| 5376 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5377 | } |
| 5378 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5379 | { // acquire lock |
| 5380 | std::scoped_lock _l(mLock); |
| 5381 | |
| 5382 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5383 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5384 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5385 | } |
| 5386 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5387 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5388 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5389 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5390 | |
| 5391 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5392 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5393 | } |
| 5394 | mConnectionsByToken.emplace(token, connection); |
| 5395 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5396 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5397 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5398 | auto& monitorsByDisplay = |
| 5399 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5400 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5401 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5402 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5403 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), |
| 5404 | displayId, toString(isGestureMonitor), pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5405 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5406 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5407 | // Wake the looper because some connections have changed. |
| 5408 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5409 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5412 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5413 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5414 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5415 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5416 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5417 | if (status) { |
| 5418 | return status; |
| 5419 | } |
| 5420 | } // release lock |
| 5421 | |
| 5422 | // Wake the poll loop because removing the connection may have changed the current |
| 5423 | // synchronization state. |
| 5424 | mLooper->wake(); |
| 5425 | return OK; |
| 5426 | } |
| 5427 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5428 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5429 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5430 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5431 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5432 | // 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] | 5433 | return BAD_VALUE; |
| 5434 | } |
| 5435 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5436 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5437 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5438 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5439 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5440 | } |
| 5441 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5442 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5443 | |
| 5444 | nsecs_t currentTime = now(); |
| 5445 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5446 | |
| 5447 | connection->status = Connection::STATUS_ZOMBIE; |
| 5448 | return OK; |
| 5449 | } |
| 5450 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5451 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5452 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5453 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5454 | } |
| 5455 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5456 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5457 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5458 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5459 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5460 | std::vector<Monitor>& monitors = it->second; |
| 5461 | const size_t numMonitors = monitors.size(); |
| 5462 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5463 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5464 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5465 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5466 | monitors.erase(monitors.begin() + i); |
| 5467 | break; |
| 5468 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5469 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5470 | if (monitors.empty()) { |
| 5471 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5472 | } else { |
| 5473 | ++it; |
| 5474 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5475 | } |
| 5476 | } |
| 5477 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5478 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5479 | { // acquire lock |
| 5480 | std::scoped_lock _l(mLock); |
| 5481 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 5482 | |
| 5483 | if (!foundDisplayId) { |
| 5484 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 5485 | return BAD_VALUE; |
| 5486 | } |
| 5487 | int32_t displayId = foundDisplayId.value(); |
| 5488 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5489 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5490 | mTouchStatesByDisplay.find(displayId); |
| 5491 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5492 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5493 | return BAD_VALUE; |
| 5494 | } |
| 5495 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5496 | TouchState& state = stateIt->second; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5497 | std::shared_ptr<InputChannel> requestingChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5498 | std::optional<int32_t> foundDeviceId; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 5499 | for (const auto& monitor : state.gestureMonitors) { |
| 5500 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 5501 | requestingChannel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5502 | foundDeviceId = state.deviceId; |
| 5503 | } |
| 5504 | } |
| 5505 | if (!foundDeviceId || !state.down) { |
| 5506 | 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] | 5507 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5508 | return BAD_VALUE; |
| 5509 | } |
| 5510 | int32_t deviceId = foundDeviceId.value(); |
| 5511 | |
| 5512 | // Send cancel events to all the input channels we're stealing from. |
| 5513 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5514 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5515 | options.deviceId = deviceId; |
| 5516 | options.displayId = displayId; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5517 | std::string canceledWindows = "["; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5518 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5519 | std::shared_ptr<InputChannel> channel = |
| 5520 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5521 | if (channel != nullptr) { |
| 5522 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5523 | canceledWindows += channel->getName() + ", "; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5524 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5525 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5526 | canceledWindows += "]"; |
| 5527 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5528 | canceledWindows.c_str()); |
| 5529 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5530 | // Then clear the current touch state so we stop dispatching to them as well. |
| 5531 | state.filterNonMonitors(); |
| 5532 | } |
| 5533 | return OK; |
| 5534 | } |
| 5535 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5536 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5537 | { // acquire lock |
| 5538 | std::scoped_lock _l(mLock); |
| 5539 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5540 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5541 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5542 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5543 | : "token without window"); |
| 5544 | } |
| 5545 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5546 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5547 | if (focusedToken != windowToken) { |
| 5548 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5549 | enabled ? "enable" : "disable"); |
| 5550 | return; |
| 5551 | } |
| 5552 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5553 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5554 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5555 | "window has %s requested pointer capture.", |
| 5556 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5557 | return; |
| 5558 | } |
| 5559 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5560 | setPointerCaptureLocked(enabled); |
| 5561 | } // release lock |
| 5562 | |
| 5563 | // Wake the thread to process command entries. |
| 5564 | mLooper->wake(); |
| 5565 | } |
| 5566 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5567 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5568 | const sp<IBinder>& token) { |
| 5569 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5570 | const std::vector<Monitor>& monitors = it.second; |
| 5571 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5572 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5573 | return it.first; |
| 5574 | } |
| 5575 | } |
| 5576 | } |
| 5577 | return std::nullopt; |
| 5578 | } |
| 5579 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5580 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5581 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5582 | if (gesturePid.has_value()) { |
| 5583 | return gesturePid; |
| 5584 | } |
| 5585 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5586 | } |
| 5587 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5588 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5589 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5590 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5591 | } |
| 5592 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5593 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5594 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5595 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5596 | } |
| 5597 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5598 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5599 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5600 | } |
| 5601 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5602 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5603 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5604 | if (connection == nullptr) { |
| 5605 | return "<nullptr>"; |
| 5606 | } |
| 5607 | return connection->getInputChannelName(); |
| 5608 | } |
| 5609 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5610 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5611 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5612 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5613 | } |
| 5614 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5615 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5616 | const sp<Connection>& connection, uint32_t seq, |
| 5617 | bool handled, nsecs_t consumeTime) { |
| 5618 | // Handle post-event policy actions. |
| 5619 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5620 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5621 | return; |
| 5622 | } |
| 5623 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5624 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5625 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5626 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5627 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5628 | } |
| 5629 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5630 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5631 | connection->inputChannel->getConnectionToken(), |
| 5632 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5633 | } |
| 5634 | |
| 5635 | bool restartEvent; |
| 5636 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5637 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5638 | restartEvent = |
| 5639 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5640 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5641 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5642 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5643 | handled); |
| 5644 | } else { |
| 5645 | restartEvent = false; |
| 5646 | } |
| 5647 | |
| 5648 | // Dequeue the event and start the next cycle. |
| 5649 | // Because the lock might have been released, it is possible that the |
| 5650 | // contents of the wait queue to have been drained, so we need to double-check |
| 5651 | // a few things. |
| 5652 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5653 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5654 | dispatchEntry = *dispatchEntryIt; |
| 5655 | connection->waitQueue.erase(dispatchEntryIt); |
| 5656 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5657 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5658 | if (!connection->responsive) { |
| 5659 | connection->responsive = isConnectionResponsive(*connection); |
| 5660 | if (connection->responsive) { |
| 5661 | // The connection was unresponsive, and now it's responsive. |
| 5662 | processConnectionResponsiveLocked(*connection); |
| 5663 | } |
| 5664 | } |
| 5665 | traceWaitQueueLength(*connection); |
| 5666 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
| 5667 | connection->outboundQueue.push_front(dispatchEntry); |
| 5668 | traceOutboundQueueLength(*connection); |
| 5669 | } else { |
| 5670 | releaseDispatchEntry(dispatchEntry); |
| 5671 | } |
| 5672 | } |
| 5673 | |
| 5674 | // Start the next dispatch cycle for this connection. |
| 5675 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5676 | } |
| 5677 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5678 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5679 | const sp<IBinder>& newToken) { |
| 5680 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5681 | scoped_unlock unlock(mLock); |
| 5682 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5683 | }; |
| 5684 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5685 | } |
| 5686 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5687 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5688 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5689 | scoped_unlock unlock(mLock); |
| 5690 | mPolicy->notifyDropWindow(token, x, y); |
| 5691 | }; |
| 5692 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5693 | } |
| 5694 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5695 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5696 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5697 | scoped_unlock unlock(mLock); |
| 5698 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5699 | }; |
| 5700 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5701 | } |
| 5702 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5703 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5704 | if (connection == nullptr) { |
| 5705 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5706 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5707 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5708 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5709 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5710 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5711 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5712 | return; |
| 5713 | } |
| 5714 | /** |
| 5715 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5716 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5717 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5718 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5719 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5720 | * most useful. |
| 5721 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5722 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5723 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5724 | std::string reason = |
| 5725 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5726 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5727 | ns2ms(currentWait), |
| 5728 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5729 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5730 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5731 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5732 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5733 | |
| 5734 | // Stop waking up for events on this connection, it is already unresponsive |
| 5735 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5736 | } |
| 5737 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5738 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5739 | std::string reason = |
| 5740 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5741 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5742 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5743 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5744 | scoped_unlock unlock(mLock); |
| 5745 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5746 | }; |
| 5747 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5748 | } |
| 5749 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5750 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5751 | const std::string& reason) { |
| 5752 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5753 | updateLastAnrStateLocked(windowLabel, reason); |
| 5754 | } |
| 5755 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5756 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5757 | const std::string& reason) { |
| 5758 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5759 | updateLastAnrStateLocked(windowLabel, reason); |
| 5760 | } |
| 5761 | |
| 5762 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5763 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5764 | // 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] | 5765 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5766 | struct tm tm; |
| 5767 | localtime_r(&t, &tm); |
| 5768 | char timestr[64]; |
| 5769 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5770 | mLastAnrState.clear(); |
| 5771 | mLastAnrState += INDENT "ANR:\n"; |
| 5772 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5773 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5774 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5775 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5776 | } |
| 5777 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5778 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5779 | KeyEntry& entry) { |
| 5780 | const KeyEvent event = createKeyEvent(entry); |
| 5781 | nsecs_t delay = 0; |
| 5782 | { // release lock |
| 5783 | scoped_unlock unlock(mLock); |
| 5784 | android::base::Timer t; |
| 5785 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5786 | entry.policyFlags); |
| 5787 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5788 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5789 | std::to_string(t.duration().count()).c_str()); |
| 5790 | } |
| 5791 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5792 | |
| 5793 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5794 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5795 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5796 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5797 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5798 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5799 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5800 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5801 | } |
| 5802 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5803 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5804 | auto command = [this, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5805 | scoped_unlock unlock(mLock); |
| 5806 | mPolicy->notifyMonitorUnresponsive(pid, reason); |
| 5807 | }; |
| 5808 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5809 | } |
| 5810 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5811 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5812 | std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5813 | auto command = [this, token, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5814 | scoped_unlock unlock(mLock); |
| 5815 | mPolicy->notifyWindowUnresponsive(token, reason); |
| 5816 | }; |
| 5817 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5818 | } |
| 5819 | |
| 5820 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5821 | auto command = [this, pid]() REQUIRES(mLock) { |
| 5822 | scoped_unlock unlock(mLock); |
| 5823 | mPolicy->notifyMonitorResponsive(pid); |
| 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::sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken) { |
| 5829 | auto command = [this, connectionToken]() REQUIRES(mLock) { |
| 5830 | scoped_unlock unlock(mLock); |
| 5831 | mPolicy->notifyWindowResponsive(connectionToken); |
| 5832 | }; |
| 5833 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5834 | } |
| 5835 | |
| 5836 | /** |
| 5837 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5838 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5839 | * command entry to the command queue. |
| 5840 | */ |
| 5841 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5842 | std::string reason) { |
| 5843 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5844 | if (connection.monitor) { |
| 5845 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5846 | reason.c_str()); |
| 5847 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5848 | if (!pid.has_value()) { |
| 5849 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5850 | connection.inputChannel->getName().c_str()); |
| 5851 | return; |
| 5852 | } |
| 5853 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5854 | return; |
| 5855 | } |
| 5856 | // If not a monitor, must be a window |
| 5857 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5858 | reason.c_str()); |
| 5859 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5860 | } |
| 5861 | |
| 5862 | /** |
| 5863 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5864 | */ |
| 5865 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5866 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5867 | if (connection.monitor) { |
| 5868 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5869 | if (!pid.has_value()) { |
| 5870 | ALOGE("Could not find responsive monitor for connection %s", |
| 5871 | connection.inputChannel->getName().c_str()); |
| 5872 | return; |
| 5873 | } |
| 5874 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5875 | return; |
| 5876 | } |
| 5877 | // If not a monitor, must be a window |
| 5878 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5879 | } |
| 5880 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5881 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5882 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5883 | KeyEntry& keyEntry, bool handled) { |
| 5884 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5885 | if (!handled) { |
| 5886 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5887 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5888 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5889 | return false; |
| 5890 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5891 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5892 | // Get the fallback key state. |
| 5893 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5894 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5895 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5896 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5897 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5898 | } |
| 5899 | |
| 5900 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5901 | // If the application handles the original key for which we previously |
| 5902 | // generated a fallback or if the window is not a foreground window, |
| 5903 | // then cancel the associated fallback key, if any. |
| 5904 | if (fallbackKeyCode != -1) { |
| 5905 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5906 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5907 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 5908 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5909 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 5910 | keyEntry.policyFlags); |
| 5911 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5912 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5913 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5914 | |
| 5915 | mLock.unlock(); |
| 5916 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5917 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5918 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5919 | |
| 5920 | mLock.lock(); |
| 5921 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5922 | // Cancel the fallback key. |
| 5923 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5924 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5925 | "application handled the original non-fallback key " |
| 5926 | "or is no longer a foreground target, " |
| 5927 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5928 | options.keyCode = fallbackKeyCode; |
| 5929 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5930 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5931 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5932 | } |
| 5933 | } else { |
| 5934 | // If the application did not handle a non-fallback key, first check |
| 5935 | // that we are in a good state to perform unhandled key event processing |
| 5936 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5937 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5938 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5939 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5940 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 5941 | "since this is not an initial down. " |
| 5942 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5943 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 5944 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5945 | return false; |
| 5946 | } |
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 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5949 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5950 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 5951 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5952 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 5953 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5954 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5955 | |
| 5956 | mLock.unlock(); |
| 5957 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5958 | bool fallback = |
| 5959 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5960 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5961 | |
| 5962 | mLock.lock(); |
| 5963 | |
| 5964 | if (connection->status != Connection::STATUS_NORMAL) { |
| 5965 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5966 | return false; |
| 5967 | } |
| 5968 | |
| 5969 | // Latch the fallback keycode for this key on an initial down. |
| 5970 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5971 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5972 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5973 | fallbackKeyCode = event.getKeyCode(); |
| 5974 | } else { |
| 5975 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5976 | } |
| 5977 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5978 | } |
| 5979 | |
| 5980 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5981 | |
| 5982 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5983 | // We will continue to dispatch the key to the policy but we will no |
| 5984 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5985 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 5986 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5987 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5988 | if (fallback) { |
| 5989 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 5990 | "as a fallback for %d, but on the DOWN it had requested " |
| 5991 | "to send %d instead. Fallback canceled.", |
| 5992 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 5993 | } else { |
| 5994 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 5995 | "but on the DOWN it had requested to send %d. " |
| 5996 | "Fallback canceled.", |
| 5997 | originalKeyCode, fallbackKeyCode); |
| 5998 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5999 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6000 | |
| 6001 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6002 | "canceling fallback, policy no longer desires it"); |
| 6003 | options.keyCode = fallbackKeyCode; |
| 6004 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6005 | |
| 6006 | fallback = false; |
| 6007 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6008 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6009 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6010 | } |
| 6011 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6012 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6013 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6014 | { |
| 6015 | std::string msg; |
| 6016 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6017 | connection->inputState.getFallbackKeys(); |
| 6018 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6019 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6020 | } |
| 6021 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6022 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6023 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6024 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6025 | |
| 6026 | if (fallback) { |
| 6027 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6028 | keyEntry.eventTime = event.getEventTime(); |
| 6029 | keyEntry.deviceId = event.getDeviceId(); |
| 6030 | keyEntry.source = event.getSource(); |
| 6031 | keyEntry.displayId = event.getDisplayId(); |
| 6032 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6033 | keyEntry.keyCode = fallbackKeyCode; |
| 6034 | keyEntry.scanCode = event.getScanCode(); |
| 6035 | keyEntry.metaState = event.getMetaState(); |
| 6036 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6037 | keyEntry.downTime = event.getDownTime(); |
| 6038 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6039 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6040 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6041 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6042 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6043 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6044 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6045 | return true; // restart the event |
| 6046 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6047 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6048 | ALOGD("Unhandled key event: No fallback key."); |
| 6049 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6050 | |
| 6051 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6052 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6053 | } |
| 6054 | } |
| 6055 | return false; |
| 6056 | } |
| 6057 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6058 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6059 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6060 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6061 | return false; |
| 6062 | } |
| 6063 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6064 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6065 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6066 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6067 | } |
| 6068 | } |
| 6069 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6070 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6071 | if (ATRACE_ENABLED()) { |
| 6072 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6073 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6074 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6075 | } |
| 6076 | } |
| 6077 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6078 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6079 | if (ATRACE_ENABLED()) { |
| 6080 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6081 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6082 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6083 | } |
| 6084 | } |
| 6085 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6086 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6087 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6088 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6089 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6090 | dumpDispatchStateLocked(dump); |
| 6091 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6092 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6093 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6094 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6095 | } |
| 6096 | } |
| 6097 | |
| 6098 | void InputDispatcher::monitor() { |
| 6099 | // 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] | 6100 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6101 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6102 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6103 | } |
| 6104 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6105 | /** |
| 6106 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6107 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6108 | * this method can be safely called from any thread, as long as you've ensured that |
| 6109 | * the work you are interested in completing has already been queued. |
| 6110 | */ |
| 6111 | bool InputDispatcher::waitForIdle() { |
| 6112 | /** |
| 6113 | * Timeout should represent the longest possible time that a device might spend processing |
| 6114 | * events and commands. |
| 6115 | */ |
| 6116 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6117 | std::unique_lock lock(mLock); |
| 6118 | mLooper->wake(); |
| 6119 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6120 | return result == std::cv_status::no_timeout; |
| 6121 | } |
| 6122 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6123 | /** |
| 6124 | * Sets focus to the window identified by the token. This must be called |
| 6125 | * after updating any input window handles. |
| 6126 | * |
| 6127 | * Params: |
| 6128 | * request.token - input channel token used to identify the window that should gain focus. |
| 6129 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6130 | * specified token does not match the currently focused window, this request will be dropped. |
| 6131 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6132 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6133 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6134 | * when requesting the focus change. This determines which request gets |
| 6135 | * precedence if there is a focus change request from another source such as pointer down. |
| 6136 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6137 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6138 | { // acquire lock |
| 6139 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6140 | std::optional<FocusResolver::FocusChanges> changes = |
| 6141 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6142 | if (changes) { |
| 6143 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6144 | } |
| 6145 | } // release lock |
| 6146 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6147 | mLooper->wake(); |
| 6148 | } |
| 6149 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6150 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6151 | if (changes.oldFocus) { |
| 6152 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6153 | if (focusedInputChannel) { |
| 6154 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6155 | "focus left window"); |
| 6156 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6157 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6158 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6159 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6160 | if (changes.newFocus) { |
| 6161 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6162 | } |
| 6163 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6164 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6165 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6166 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6167 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6168 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6169 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6170 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6171 | // front. |
| 6172 | disablePointerCaptureForcedLocked(); |
| 6173 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6174 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6175 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6176 | } |
| 6177 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6178 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6179 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6180 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6181 | return; |
| 6182 | } |
| 6183 | |
| 6184 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6185 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6186 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6187 | setPointerCaptureLocked(false); |
| 6188 | } |
| 6189 | |
| 6190 | if (!mWindowTokenWithPointerCapture) { |
| 6191 | // No need to send capture changes because no window has capture. |
| 6192 | return; |
| 6193 | } |
| 6194 | |
| 6195 | if (mPendingEvent != nullptr) { |
| 6196 | // Move the pending event to the front of the queue. This will give the chance |
| 6197 | // for the pending event to be dropped if it is a captured event. |
| 6198 | mInboundQueue.push_front(mPendingEvent); |
| 6199 | mPendingEvent = nullptr; |
| 6200 | } |
| 6201 | |
| 6202 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6203 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6204 | mInboundQueue.push_front(std::move(entry)); |
| 6205 | } |
| 6206 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6207 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6208 | mCurrentPointerCaptureRequest.enable = enable; |
| 6209 | mCurrentPointerCaptureRequest.seq++; |
| 6210 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6211 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6212 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6213 | }; |
| 6214 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6215 | } |
| 6216 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6217 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6218 | { // acquire lock |
| 6219 | std::scoped_lock _l(mLock); |
| 6220 | // Set an empty list to remove all handles from the specific display. |
| 6221 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6222 | setFocusedApplicationLocked(displayId, nullptr); |
| 6223 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6224 | // have been removed for the removed display. |
| 6225 | mFocusResolver.displayRemoved(displayId); |
| 6226 | } // release lock |
| 6227 | |
| 6228 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6229 | mLooper->wake(); |
| 6230 | } |
| 6231 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6232 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6233 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6234 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6235 | // more convenient parsing. |
| 6236 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6237 | for (const auto& info : windowInfos) { |
| 6238 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6239 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6240 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6241 | |
| 6242 | { // acquire lock |
| 6243 | std::scoped_lock _l(mLock); |
| 6244 | mDisplayInfos.clear(); |
| 6245 | for (const auto& displayInfo : displayInfos) { |
| 6246 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6247 | } |
| 6248 | |
| 6249 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6250 | setInputWindowsLocked(handles, displayId); |
| 6251 | } |
| 6252 | } |
| 6253 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6254 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6255 | } |
| 6256 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6257 | bool InputDispatcher::shouldDropInput( |
| 6258 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
| 6259 | if (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT) || |
| 6260 | (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED) && |
| 6261 | isWindowObscuredLocked(windowHandle))) { |
| 6262 | ALOGW("Dropping %s event targeting %s as requested by input feature %s on display " |
| 6263 | "%" PRId32 ".", |
| 6264 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
| 6265 | windowHandle->getInfo()->inputFeatures.string().c_str(), |
| 6266 | windowHandle->getInfo()->displayId); |
| 6267 | return true; |
| 6268 | } |
| 6269 | return false; |
| 6270 | } |
| 6271 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6272 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6273 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6274 | const std::vector<DisplayInfo>& displayInfos) { |
| 6275 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6276 | } |
| 6277 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6278 | } // namespace android::inputdispatcher |