Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "InputDispatcher" |
| 18 | #define ATRACE_TAG ATRACE_TAG_INPUT |
| 19 | |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 20 | #define LOG_NDEBUG 1 |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 22 | #include <android-base/chrono_utils.h> |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 23 | #include <android-base/properties.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 25 | #include <android/os/IInputConstants.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 26 | #include <binder/Binder.h> |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 27 | #include <binder/IServiceManager.h> |
| 28 | #include <com/android/internal/compat/IPlatformCompatNative.h> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 29 | #include <ftl/enum.h> |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 30 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 31 | #include <input/InputDevice.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 32 | #include <log/log.h> |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 33 | #include <log/log_event_list.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 34 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 35 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 36 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 38 | #include <cerrno> |
| 39 | #include <cinttypes> |
| 40 | #include <climits> |
| 41 | #include <cstddef> |
| 42 | #include <ctime> |
| 43 | #include <queue> |
| 44 | #include <sstream> |
| 45 | |
| 46 | #include "Connection.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 47 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 48 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 49 | #define INDENT " " |
| 50 | #define INDENT2 " " |
| 51 | #define INDENT3 " " |
| 52 | #define INDENT4 " " |
| 53 | |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 54 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 55 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 56 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 57 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 58 | using android::gui::FocusRequest; |
| 59 | using android::gui::TouchOcclusionMode; |
| 60 | using android::gui::WindowInfo; |
| 61 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 62 | using android::os::BlockUntrustedTouchesMode; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 63 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 64 | using android::os::InputEventInjectionResult; |
| 65 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 66 | using com::android::internal::compat::IPlatformCompatNative; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 67 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 68 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 69 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 70 | namespace { |
| 71 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 72 | // Log detailed debug messages about each inbound event notification to the dispatcher. |
| 73 | constexpr bool DEBUG_INBOUND_EVENT_DETAILS = false; |
| 74 | |
| 75 | // Log detailed debug messages about each outbound event processed by the dispatcher. |
| 76 | constexpr bool DEBUG_OUTBOUND_EVENT_DETAILS = false; |
| 77 | |
| 78 | // Log debug messages about the dispatch cycle. |
| 79 | constexpr bool DEBUG_DISPATCH_CYCLE = false; |
| 80 | |
| 81 | // Log debug messages about channel creation |
| 82 | constexpr bool DEBUG_CHANNEL_CREATION = false; |
| 83 | |
| 84 | // Log debug messages about input event injection. |
| 85 | constexpr bool DEBUG_INJECTION = false; |
| 86 | |
| 87 | // Log debug messages about input focus tracking. |
| 88 | constexpr bool DEBUG_FOCUS = false; |
| 89 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 90 | // Log debug messages about touch mode event |
| 91 | constexpr bool DEBUG_TOUCH_MODE = false; |
| 92 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 93 | // Log debug messages about touch occlusion |
| 94 | // STOPSHIP(b/169067926): Set to false |
| 95 | constexpr bool DEBUG_TOUCH_OCCLUSION = true; |
| 96 | |
| 97 | // Log debug messages about the app switch latency optimization. |
| 98 | constexpr bool DEBUG_APP_SWITCH = false; |
| 99 | |
| 100 | // Log debug messages about hover events. |
| 101 | constexpr bool DEBUG_HOVER = false; |
| 102 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 103 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 104 | // Named to match std::scoped_lock |
| 105 | class scoped_unlock { |
| 106 | public: |
| 107 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 108 | ~scoped_unlock() { mMutex.lock(); } |
| 109 | |
| 110 | private: |
| 111 | std::mutex& mMutex; |
| 112 | }; |
| 113 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 114 | // Default input dispatching timeout if there is no focused application or paused window |
| 115 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 116 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 117 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 118 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 119 | |
| 120 | // Amount of time to allow for all pending events to be processed when an app switch |
| 121 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 122 | // when an application takes too long to respond and the user has pressed an app switch key. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 123 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 124 | |
| 125 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 126 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 127 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 128 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 129 | // Log a warning when an event takes longer than this to process, even if an ANR does not occur. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 130 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 131 | |
| 132 | // Log a warning when an interception call takes longer than this to process. |
| 133 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 134 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 135 | // Additional key latency in case a connection is still processing some motion events. |
| 136 | // This will help with the case when a user touched a button that opens a new window, |
| 137 | // and gives us the chance to dispatch the key to this new window. |
| 138 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 139 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 140 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 141 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 142 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 143 | // Event log tags. See EventLogTags.logtags for reference |
| 144 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 145 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 146 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 147 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 148 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 149 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 150 | } |
| 151 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 152 | inline const char* toString(bool value) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 153 | return value ? "true" : "false"; |
| 154 | } |
| 155 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 156 | inline const std::string toString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 157 | if (binder == nullptr) { |
| 158 | return "<null>"; |
| 159 | } |
| 160 | return StringPrintf("%p", binder.get()); |
| 161 | } |
| 162 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 163 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 164 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 165 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 168 | bool isValidKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 169 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 170 | case AKEY_EVENT_ACTION_DOWN: |
| 171 | case AKEY_EVENT_ACTION_UP: |
| 172 | return true; |
| 173 | default: |
| 174 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 178 | bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 179 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 180 | ALOGE("Key event has invalid action code 0x%x", action); |
| 181 | return false; |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 186 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 187 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 188 | case AMOTION_EVENT_ACTION_DOWN: |
| 189 | case AMOTION_EVENT_ACTION_UP: |
| 190 | case AMOTION_EVENT_ACTION_CANCEL: |
| 191 | case AMOTION_EVENT_ACTION_MOVE: |
| 192 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 193 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 194 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 195 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 196 | case AMOTION_EVENT_ACTION_SCROLL: |
| 197 | return true; |
| 198 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 199 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 200 | int32_t index = getMotionEventActionPointerIndex(action); |
| 201 | return index >= 0 && index < pointerCount; |
| 202 | } |
| 203 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 204 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 205 | return actionButton != 0; |
| 206 | default: |
| 207 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 211 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 212 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 213 | } |
| 214 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 215 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 216 | const PointerProperties* pointerProperties) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 217 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 218 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 219 | return false; |
| 220 | } |
| 221 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 222 | ALOGE("Motion event has invalid pointer count %zu; value must be between 1 and %d.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 223 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | BitSet32 pointerIdBits; |
| 227 | for (size_t i = 0; i < pointerCount; i++) { |
| 228 | int32_t id = pointerProperties[i].id; |
| 229 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 230 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 231 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | if (pointerIdBits.hasBit(id)) { |
| 235 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 236 | return false; |
| 237 | } |
| 238 | pointerIdBits.markBit(id); |
| 239 | } |
| 240 | return true; |
| 241 | } |
| 242 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 243 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 244 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 245 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 248 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 249 | bool first = true; |
| 250 | Region::const_iterator cur = region.begin(); |
| 251 | Region::const_iterator const tail = region.end(); |
| 252 | while (cur != tail) { |
| 253 | if (first) { |
| 254 | first = false; |
| 255 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 256 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 257 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 258 | dump += StringPrintf("[%d,%d][%d,%d]", cur->left, cur->top, cur->right, cur->bottom); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 259 | cur++; |
| 260 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 261 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 264 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 265 | constexpr size_t maxEntries = 50; // max events to print |
| 266 | constexpr size_t skipBegin = maxEntries / 2; |
| 267 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 268 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 269 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 270 | |
| 271 | std::string dump; |
| 272 | for (size_t i = 0; i < queue.size(); i++) { |
| 273 | const DispatchEntry& entry = *queue[i]; |
| 274 | if (i >= skipBegin && i < skipEnd) { |
| 275 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 276 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 277 | continue; |
| 278 | } |
| 279 | dump.append(INDENT4); |
| 280 | dump += entry.eventEntry->getDescription(); |
| 281 | dump += StringPrintf(", seq=%" PRIu32 |
| 282 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 283 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 284 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 285 | if (entry.deliveryTime != 0) { |
| 286 | // This entry was delivered, so add information on how long we've been waiting |
| 287 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 288 | } |
| 289 | dump.append("\n"); |
| 290 | } |
| 291 | return dump; |
| 292 | } |
| 293 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 294 | /** |
| 295 | * Find the entry in std::unordered_map by key, and return it. |
| 296 | * If the entry is not found, return a default constructed entry. |
| 297 | * |
| 298 | * Useful when the entries are vectors, since an empty vector will be returned |
| 299 | * if the entry is not found. |
| 300 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 301 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 302 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 303 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 304 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 305 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 306 | } |
| 307 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 308 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 309 | if (first == second) { |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | if (first == nullptr || second == nullptr) { |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | return first->getToken() == second->getToken(); |
| 318 | } |
| 319 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 320 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 321 | if (first == nullptr || second == nullptr) { |
| 322 | return false; |
| 323 | } |
| 324 | return first->applicationInfo.token != nullptr && |
| 325 | first->applicationInfo.token == second->applicationInfo.token; |
| 326 | } |
| 327 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 328 | bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 329 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 330 | } |
| 331 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 332 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 333 | std::shared_ptr<EventEntry> eventEntry, |
| 334 | int32_t inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 335 | if (inputTarget.useDefaultPointerTransform()) { |
| 336 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 337 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 338 | inputTarget.displayTransform, |
| 339 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 343 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 344 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 345 | std::vector<PointerCoords> pointerCoords; |
| 346 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 347 | |
| 348 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 349 | // as long as all other pointers are normalized to the same value and the final DispatchEntry |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 350 | // uses the transform for the normalized pointer. |
| 351 | const ui::Transform& firstPointerTransform = |
| 352 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 353 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 354 | |
| 355 | // Iterate through all pointers in the event to normalize against the first. |
| 356 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 357 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 358 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 359 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 360 | |
| 361 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 362 | // First, apply the current pointer's transform to update the coordinates into |
| 363 | // window space. |
| 364 | pointerCoords[pointerIndex].transform(currTransform); |
| 365 | // Next, apply the inverse transform of the normalized coordinates so the |
| 366 | // current coordinates are transformed into the normalized coordinate space. |
| 367 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 370 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 371 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 372 | motionEntry.deviceId, motionEntry.source, |
| 373 | motionEntry.displayId, motionEntry.policyFlags, |
| 374 | motionEntry.action, motionEntry.actionButton, |
| 375 | motionEntry.flags, motionEntry.metaState, |
| 376 | motionEntry.buttonState, motionEntry.classification, |
| 377 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 378 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 379 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 380 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 381 | pointerCoords.data(), 0 /* xOffset */, 0 /* yOffset */); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 382 | |
| 383 | if (motionEntry.injectionState) { |
| 384 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 385 | combinedMotionEntry->injectionState->refCount += 1; |
| 386 | } |
| 387 | |
| 388 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 389 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 390 | firstPointerTransform, inputTarget.displayTransform, |
| 391 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 392 | return dispatchEntry; |
| 393 | } |
| 394 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 395 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 396 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 397 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 398 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 399 | |
| 400 | serverChannel = std::move(uniqueServerChannel); |
| 401 | return result; |
| 402 | } |
| 403 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 404 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 405 | bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 406 | if (lhs == nullptr && rhs == nullptr) { |
| 407 | return true; |
| 408 | } |
| 409 | if (lhs == nullptr || rhs == nullptr) { |
| 410 | return false; |
| 411 | } |
| 412 | return *lhs == *rhs; |
| 413 | } |
| 414 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 415 | sp<IPlatformCompatNative> getCompatService() { |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 416 | sp<IBinder> service(defaultServiceManager()->getService(String16("platform_compat_native"))); |
| 417 | if (service == nullptr) { |
| 418 | ALOGE("Failed to link to compat service"); |
| 419 | return nullptr; |
| 420 | } |
| 421 | return interface_cast<IPlatformCompatNative>(service); |
| 422 | } |
| 423 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 424 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 425 | KeyEvent event; |
| 426 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 427 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 428 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 429 | return event; |
| 430 | } |
| 431 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 432 | std::optional<int32_t> findMonitorPidByToken( |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 433 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, |
| 434 | const sp<IBinder>& token) { |
| 435 | for (const auto& it : monitorsByDisplay) { |
| 436 | const std::vector<Monitor>& monitors = it.second; |
| 437 | for (const Monitor& monitor : monitors) { |
| 438 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 439 | return monitor.pid; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | return std::nullopt; |
| 444 | } |
| 445 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 446 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 447 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 448 | // affect the statistics. |
| 449 | if (connection.monitor) { |
| 450 | return false; |
| 451 | } |
| 452 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 453 | if (!connection.responsive) { |
| 454 | return false; |
| 455 | } |
| 456 | return true; |
| 457 | } |
| 458 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 459 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 460 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 461 | const int32_t& inputEventId = eventEntry.id; |
| 462 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 463 | // Event was transmuted |
| 464 | return false; |
| 465 | } |
| 466 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 467 | return false; |
| 468 | } |
| 469 | // Only track latency for events that originated from hardware |
| 470 | if (eventEntry.isSynthesized()) { |
| 471 | return false; |
| 472 | } |
| 473 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 474 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 475 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 476 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 477 | return false; |
| 478 | } |
| 479 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 480 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 481 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 482 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 483 | return false; |
| 484 | } |
| 485 | } else { |
| 486 | // Not a key or a motion |
| 487 | return false; |
| 488 | } |
| 489 | if (!shouldReportMetricsForConnection(connection)) { |
| 490 | return false; |
| 491 | } |
| 492 | return true; |
| 493 | } |
| 494 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 495 | /** |
| 496 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 497 | * current time. |
| 498 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 499 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 500 | const nsecs_t currentTime = now(); |
| 501 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 502 | if (entry->timeoutTime < currentTime) { |
| 503 | return false; |
| 504 | } |
| 505 | } |
| 506 | return true; |
| 507 | } |
| 508 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 509 | bool isFromSource(uint32_t source, uint32_t test) { |
| 510 | return (source & test) == test; |
| 511 | } |
| 512 | |
| 513 | vec2 transformWithoutTranslation(const ui::Transform& transform, float x, float y) { |
| 514 | const vec2 transformedXy = transform.transform(x, y); |
| 515 | const vec2 transformedOrigin = transform.transform(0, 0); |
| 516 | return transformedXy - transformedOrigin; |
| 517 | } |
| 518 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 519 | // Returns true if the event type passed as argument represents a user activity. |
| 520 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 521 | switch (eventEntry.type) { |
| 522 | case EventEntry::Type::FOCUS: |
| 523 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 524 | case EventEntry::Type::DRAG: |
| 525 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 526 | case EventEntry::Type::SENSOR: |
| 527 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 528 | return false; |
| 529 | case EventEntry::Type::DEVICE_RESET: |
| 530 | case EventEntry::Type::KEY: |
| 531 | case EventEntry::Type::MOTION: |
| 532 | return true; |
| 533 | } |
| 534 | } |
| 535 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 536 | } // namespace |
| 537 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 538 | // --- InputDispatcher --- |
| 539 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 540 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 541 | : mPolicy(policy), |
| 542 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 543 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 544 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 545 | mAppSwitchSawKeyDown(false), |
| 546 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 547 | mNextUnblockedEvent(nullptr), |
| 548 | mDispatchEnabled(false), |
| 549 | mDispatchFrozen(false), |
| 550 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 551 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 552 | // To avoid leaking stack in case that call never comes, and for tests, |
| 553 | // initialize it here anyways. |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 554 | mInTouchMode(kDefaultInTouchMode), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 555 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 556 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 557 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 558 | mLatencyAggregator(), |
| 559 | mLatencyTracker(&mLatencyAggregator), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 560 | mCompatService(getCompatService()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 561 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 562 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 563 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 564 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 565 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 566 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 567 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 568 | |
| 569 | policy->getDispatcherConfiguration(&mConfig); |
| 570 | } |
| 571 | |
| 572 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 573 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 574 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 575 | resetKeyRepeatLocked(); |
| 576 | releasePendingEventLocked(); |
| 577 | drainInboundQueueLocked(); |
| 578 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 579 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 580 | while (!mConnectionsByToken.empty()) { |
| 581 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 582 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 583 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 587 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 588 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 589 | return ALREADY_EXISTS; |
| 590 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 591 | mThread = std::make_unique<InputThread>( |
| 592 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 593 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 597 | if (mThread && mThread->isCallingThread()) { |
| 598 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 599 | return INVALID_OPERATION; |
| 600 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 601 | mThread.reset(); |
| 602 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 605 | void InputDispatcher::dispatchOnce() { |
| 606 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 607 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 608 | std::scoped_lock _l(mLock); |
| 609 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 610 | |
| 611 | // Run a dispatch loop if there are no pending commands. |
| 612 | // The dispatch loop might enqueue commands to run afterwards. |
| 613 | if (!haveCommandsLocked()) { |
| 614 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 615 | } |
| 616 | |
| 617 | // Run all pending commands if there are any. |
| 618 | // 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] | 619 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 620 | nextWakeupTime = LONG_LONG_MIN; |
| 621 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 622 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 623 | // If we are still waiting for ack on some events, |
| 624 | // we might have to wake up earlier to check if an app is anr'ing. |
| 625 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 626 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 627 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 628 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 629 | // pending or queued events |
| 630 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 631 | mDispatcherEnteredIdle.notify_all(); |
| 632 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 633 | } // release lock |
| 634 | |
| 635 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 636 | nsecs_t currentTime = now(); |
| 637 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 638 | mLooper->pollOnce(timeoutMillis); |
| 639 | } |
| 640 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 641 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 642 | * Raise ANR if there is no focused window. |
| 643 | * Before the ANR is raised, do a final state check: |
| 644 | * 1. The currently focused application must be the same one we are waiting for. |
| 645 | * 2. Ensure we still don't have a focused window. |
| 646 | */ |
| 647 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 648 | // Check if the application that we are waiting for is still focused. |
| 649 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 650 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 651 | if (focusedApplication == nullptr || |
| 652 | focusedApplication->getApplicationToken() != |
| 653 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 654 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 655 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 656 | focusedApplication->getName().c_str()); |
| 657 | return; // The focused application has changed. |
| 658 | } |
| 659 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 660 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 661 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 662 | if (focusedWindowHandle != nullptr) { |
| 663 | return; // We now have a focused window. No need for ANR. |
| 664 | } |
| 665 | onAnrLocked(mAwaitedFocusedApplication); |
| 666 | } |
| 667 | |
| 668 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 669 | * Check if any of the connections' wait queues have events that are too old. |
| 670 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 671 | * Return the time at which we should wake up next. |
| 672 | */ |
| 673 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 674 | const nsecs_t currentTime = now(); |
| 675 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 676 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 677 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 678 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 679 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 680 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 681 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 682 | return LONG_LONG_MIN; |
| 683 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 684 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 685 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | // Check if any connection ANRs are due |
| 690 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 691 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 692 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 693 | } |
| 694 | |
| 695 | // If we reached here, we have an unresponsive connection. |
| 696 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 697 | if (connection == nullptr) { |
| 698 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 699 | return nextAnrCheck; |
| 700 | } |
| 701 | connection->responsive = false; |
| 702 | // Stop waking up for this unresponsive connection |
| 703 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 704 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 705 | return LONG_LONG_MIN; |
| 706 | } |
| 707 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 708 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 709 | sp<WindowInfoHandle> window = getWindowHandleLocked(token); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 710 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 711 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 712 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 713 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 716 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 717 | nsecs_t currentTime = now(); |
| 718 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 719 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 720 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 721 | // repeat if the device is just coming out of sleep. |
| 722 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 723 | resetKeyRepeatLocked(); |
| 724 | } |
| 725 | |
| 726 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 727 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 728 | if (DEBUG_FOCUS) { |
| 729 | ALOGD("Dispatch frozen. Waiting some more."); |
| 730 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 731 | return; |
| 732 | } |
| 733 | |
| 734 | // Optimize latency of app switches. |
| 735 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 736 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 737 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 738 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 739 | *nextWakeupTime = mAppSwitchDueTime; |
| 740 | } |
| 741 | |
| 742 | // Ready to start a new event. |
| 743 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 744 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 745 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 746 | if (isAppSwitchDue) { |
| 747 | // The inbound queue is empty so the app switch key we were waiting |
| 748 | // for will never arrive. Stop waiting for it. |
| 749 | resetPendingAppSwitchLocked(false); |
| 750 | isAppSwitchDue = false; |
| 751 | } |
| 752 | |
| 753 | // Synthesize a key repeat if appropriate. |
| 754 | if (mKeyRepeatState.lastKeyEntry) { |
| 755 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 756 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 757 | } else { |
| 758 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 759 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | // Nothing to do if there is no pending event. |
| 765 | if (!mPendingEvent) { |
| 766 | return; |
| 767 | } |
| 768 | } else { |
| 769 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 770 | mPendingEvent = mInboundQueue.front(); |
| 771 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 772 | traceInboundQueueLengthLocked(); |
| 773 | } |
| 774 | |
| 775 | // Poke user activity for this event. |
| 776 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 777 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 778 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | // Now we have an event to dispatch. |
| 782 | // 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] | 783 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 784 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 785 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 786 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 787 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 788 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 789 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 793 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 797 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 798 | const ConfigurationChangedEntry& typedEntry = |
| 799 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 800 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 801 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 802 | break; |
| 803 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 804 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 805 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 806 | const DeviceResetEntry& typedEntry = |
| 807 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 808 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 809 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 810 | break; |
| 811 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 812 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 813 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 814 | std::shared_ptr<FocusEntry> typedEntry = |
| 815 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 816 | dispatchFocusLocked(currentTime, typedEntry); |
| 817 | done = true; |
| 818 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 819 | break; |
| 820 | } |
| 821 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 822 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 823 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 824 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 825 | done = true; |
| 826 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 827 | break; |
| 828 | } |
| 829 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 830 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 831 | const auto typedEntry = |
| 832 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 833 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 834 | done = true; |
| 835 | break; |
| 836 | } |
| 837 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 838 | case EventEntry::Type::DRAG: { |
| 839 | std::shared_ptr<DragEntry> typedEntry = |
| 840 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 841 | dispatchDragLocked(currentTime, typedEntry); |
| 842 | done = true; |
| 843 | break; |
| 844 | } |
| 845 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 846 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 847 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 848 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 849 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 850 | resetPendingAppSwitchLocked(true); |
| 851 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 852 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 853 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 854 | } |
| 855 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 856 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 857 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 858 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 859 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 860 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 861 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 862 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 863 | break; |
| 864 | } |
| 865 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 866 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 867 | std::shared_ptr<MotionEntry> motionEntry = |
| 868 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 869 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 870 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 871 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 872 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 873 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 874 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 875 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 876 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 877 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 878 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 879 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 880 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 881 | |
| 882 | case EventEntry::Type::SENSOR: { |
| 883 | std::shared_ptr<SensorEntry> sensorEntry = |
| 884 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 885 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 886 | dropReason = DropReason::APP_SWITCH; |
| 887 | } |
| 888 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 889 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 890 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 891 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 892 | dropReason = DropReason::STALE; |
| 893 | } |
| 894 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 895 | done = true; |
| 896 | break; |
| 897 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 901 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 902 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 903 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 904 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 905 | |
| 906 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 907 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 911 | /** |
| 912 | * Return true if the events preceding this incoming motion event should be dropped |
| 913 | * Return false otherwise (the default behaviour) |
| 914 | */ |
| 915 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 916 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 917 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 918 | |
| 919 | // Optimize case where the current application is unresponsive and the user |
| 920 | // decides to touch a window in a different application. |
| 921 | // If the application takes too long to catch up then we drop all events preceding |
| 922 | // the touch into the other window. |
| 923 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 924 | int32_t displayId = motionEntry.displayId; |
| 925 | int32_t x = static_cast<int32_t>( |
| 926 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 927 | int32_t y = static_cast<int32_t>( |
| 928 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 929 | sp<WindowInfoHandle> touchedWindowHandle = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 930 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 931 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 932 | touchedWindowHandle->getApplicationToken() != |
| 933 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 934 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 935 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 936 | "for %s", |
| 937 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 938 | return true; |
| 939 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 940 | |
| 941 | // Alternatively, maybe there's a gesture monitor that could handle this event |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 942 | for (const auto& monitor : getValueByKey(mGestureMonitorsByDisplay, displayId)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 943 | sp<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 944 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 945 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 946 | // This monitor could take more input. Drop all events preceding this |
| 947 | // event, so that gesture monitor could get a chance to receive the stream |
| 948 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 949 | "responsive gesture monitor that may handle the event", |
| 950 | mAwaitedFocusedApplication->getName().c_str()); |
| 951 | return true; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 957 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 958 | // events to be processed before dispatching the key event. This is because these motion events |
| 959 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 960 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 961 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 962 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 963 | "just send the pending key event to the focused window."); |
| 964 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 965 | } |
| 966 | return false; |
| 967 | } |
| 968 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 969 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 970 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 971 | mInboundQueue.push_back(std::move(newEntry)); |
| 972 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 973 | traceInboundQueueLengthLocked(); |
| 974 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 975 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 976 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 977 | // Optimize app switch latency. |
| 978 | // If the application takes too long to catch up then we drop all events preceding |
| 979 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 980 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 981 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 982 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 983 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 984 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 985 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 986 | if (DEBUG_APP_SWITCH) { |
| 987 | ALOGD("App switch is pending!"); |
| 988 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 989 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 990 | mAppSwitchSawKeyDown = false; |
| 991 | needWake = true; |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | break; |
| 996 | } |
| 997 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 998 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 999 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1000 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1001 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1002 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1003 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1004 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1005 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1006 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1007 | break; |
| 1008 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1009 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1010 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1011 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1012 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1013 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1014 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1015 | // nothing to do |
| 1016 | break; |
| 1017 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | return needWake; |
| 1021 | } |
| 1022 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1023 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1024 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1025 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1026 | mRecentQueue.push_back(entry); |
| 1027 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1028 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1029 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1033 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1034 | int32_t y, TouchState* touchState, |
| 1035 | bool addOutsideTargets, |
| 1036 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1037 | if (addOutsideTargets && touchState == nullptr) { |
| 1038 | 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] | 1039 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1040 | // Traverse windows from front to back to find touched window. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1041 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 1042 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1043 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1044 | continue; |
| 1045 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1046 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1047 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1048 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1049 | |
| 1050 | if (windowInfo->visible) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1051 | if (!flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
| 1052 | bool isTouchModal = !flags.test(WindowInfo::Flag::NOT_FOCUSABLE) && |
| 1053 | !flags.test(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1054 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
| 1055 | // Found window. |
| 1056 | return windowHandle; |
| 1057 | } |
| 1058 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1059 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1060 | if (addOutsideTargets && flags.test(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1061 | touchState->addOrUpdateWindow(windowHandle, |
| 1062 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1063 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1064 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1065 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1066 | } |
| 1067 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1068 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1069 | } |
| 1070 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1071 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1072 | const char* reason; |
| 1073 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1074 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1075 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1076 | ALOGD("Dropped event because policy consumed it."); |
| 1077 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1078 | reason = "inbound event was dropped because the policy consumed it"; |
| 1079 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1080 | case DropReason::DISABLED: |
| 1081 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1082 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1083 | } |
| 1084 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1085 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1086 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1087 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1088 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1089 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1090 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1091 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1092 | "has started interacting with a different application."); |
| 1093 | reason = "inbound event was dropped because the current application is not responding " |
| 1094 | "and the user has started interacting with a different application"; |
| 1095 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1096 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1097 | ALOGI("Dropped event because it is stale."); |
| 1098 | reason = "inbound event was dropped because it is stale"; |
| 1099 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1100 | case DropReason::NO_POINTER_CAPTURE: |
| 1101 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1102 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1103 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1104 | case DropReason::NOT_DROPPED: { |
| 1105 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1106 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1107 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1110 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1111 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1112 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1113 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1114 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1115 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1116 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1117 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1118 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1119 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1120 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1121 | } else { |
| 1122 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1123 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1124 | } |
| 1125 | break; |
| 1126 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1127 | case EventEntry::Type::SENSOR: { |
| 1128 | break; |
| 1129 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1130 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1131 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1132 | break; |
| 1133 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1134 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1135 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1136 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1137 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1138 | 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] | 1139 | break; |
| 1140 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1144 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1145 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1146 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1149 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1150 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1151 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1152 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1156 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1157 | } |
| 1158 | |
| 1159 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1160 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1161 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1162 | if (DEBUG_APP_SWITCH) { |
| 1163 | if (handled) { |
| 1164 | ALOGD("App switch has arrived."); |
| 1165 | } else { |
| 1166 | ALOGD("App switch was abandoned."); |
| 1167 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1168 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1171 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1172 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1173 | } |
| 1174 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1175 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1176 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1177 | return false; |
| 1178 | } |
| 1179 | |
| 1180 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1181 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1182 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1183 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1184 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1185 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1186 | return true; |
| 1187 | } |
| 1188 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1189 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1190 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1194 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1195 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1196 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1197 | releaseInboundEventLocked(entry); |
| 1198 | } |
| 1199 | traceInboundQueueLengthLocked(); |
| 1200 | } |
| 1201 | |
| 1202 | void InputDispatcher::releasePendingEventLocked() { |
| 1203 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1204 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1205 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1206 | } |
| 1207 | } |
| 1208 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1209 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1210 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1211 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1212 | if (DEBUG_DISPATCH_CYCLE) { |
| 1213 | ALOGD("Injected inbound event was dropped."); |
| 1214 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1215 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1216 | } |
| 1217 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1218 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1219 | } |
| 1220 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1224 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1225 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1229 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1230 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1231 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1232 | uint32_t policyFlags = entry->policyFlags & |
| 1233 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1234 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1235 | std::shared_ptr<KeyEntry> newEntry = |
| 1236 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1237 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1238 | entry->flags, entry->keyCode, entry->scanCode, |
| 1239 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1240 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1241 | newEntry->syntheticRepeat = true; |
| 1242 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1243 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1244 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1247 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1248 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1249 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1250 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1251 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1252 | |
| 1253 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1254 | resetKeyRepeatLocked(); |
| 1255 | |
| 1256 | // 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] | 1257 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1258 | scoped_unlock unlock(mLock); |
| 1259 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1260 | }; |
| 1261 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1262 | return true; |
| 1263 | } |
| 1264 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1265 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1266 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1267 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1268 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1269 | entry.deviceId); |
| 1270 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1271 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1272 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1273 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1274 | resetKeyRepeatLocked(); |
| 1275 | } |
| 1276 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1277 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1278 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1279 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1280 | return true; |
| 1281 | } |
| 1282 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1283 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1284 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1285 | if (mPendingEvent != nullptr) { |
| 1286 | // Move the pending event to the front of the queue. This will give the chance |
| 1287 | // for the pending event to get dispatched to the newly focused window |
| 1288 | mInboundQueue.push_front(mPendingEvent); |
| 1289 | mPendingEvent = nullptr; |
| 1290 | } |
| 1291 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1292 | std::unique_ptr<FocusEntry> focusEntry = |
| 1293 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1294 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1295 | |
| 1296 | // This event should go to the front of the queue, but behind all other focus events |
| 1297 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1298 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1299 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1300 | [](const std::shared_ptr<EventEntry>& event) { |
| 1301 | return event->type == EventEntry::Type::FOCUS; |
| 1302 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1303 | |
| 1304 | // 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] | 1305 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1306 | } |
| 1307 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1308 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1309 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1310 | if (channel == nullptr) { |
| 1311 | return; // Window has gone away |
| 1312 | } |
| 1313 | InputTarget target; |
| 1314 | target.inputChannel = channel; |
| 1315 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1316 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1317 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1318 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1319 | std::string reason = std::string("reason=").append(entry->reason); |
| 1320 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1321 | dispatchEventLocked(currentTime, entry, {target}); |
| 1322 | } |
| 1323 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1324 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1325 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1326 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1327 | dropReason = DropReason::NOT_DROPPED; |
| 1328 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1329 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1330 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1331 | |
| 1332 | if (entry->pointerCaptureRequest.enable) { |
| 1333 | // Enable Pointer Capture. |
| 1334 | if (haveWindowWithPointerCapture && |
| 1335 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
| 1336 | LOG_ALWAYS_FATAL("This request to enable Pointer Capture has already been dispatched " |
| 1337 | "to the window."); |
| 1338 | } |
| 1339 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1340 | // This can happen if a window requests capture and immediately releases capture. |
| 1341 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1342 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1343 | return; |
| 1344 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1345 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1346 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1347 | return; |
| 1348 | } |
| 1349 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1350 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1351 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1352 | mWindowTokenWithPointerCapture = token; |
| 1353 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1354 | // Disable Pointer Capture. |
| 1355 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1356 | // for two reasons: |
| 1357 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1358 | // to disable capture with the same sequence number: one generated by |
| 1359 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1360 | // Capture being disabled in InputReader. |
| 1361 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1362 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1363 | // in InputReader. |
| 1364 | if (!haveWindowWithPointerCapture) { |
| 1365 | // Pointer capture was already forcefully disabled because of focus change. |
| 1366 | dropReason = DropReason::NOT_DROPPED; |
| 1367 | return; |
| 1368 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1369 | token = mWindowTokenWithPointerCapture; |
| 1370 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1371 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1372 | setPointerCaptureLocked(false); |
| 1373 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | auto channel = getInputChannelLocked(token); |
| 1377 | if (channel == nullptr) { |
| 1378 | // Window has gone away, clean up Pointer Capture state. |
| 1379 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1380 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1381 | setPointerCaptureLocked(false); |
| 1382 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1383 | return; |
| 1384 | } |
| 1385 | InputTarget target; |
| 1386 | target.inputChannel = channel; |
| 1387 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1388 | entry->dispatchInProgress = true; |
| 1389 | dispatchEventLocked(currentTime, entry, {target}); |
| 1390 | |
| 1391 | dropReason = DropReason::NOT_DROPPED; |
| 1392 | } |
| 1393 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1394 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1395 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1396 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1397 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1398 | if (windowHandles.empty()) { |
| 1399 | return; |
| 1400 | } |
| 1401 | const std::vector<InputTarget> inputTargets = |
| 1402 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1403 | if (inputTargets.empty()) { |
| 1404 | return; |
| 1405 | } |
| 1406 | entry->dispatchInProgress = true; |
| 1407 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1408 | } |
| 1409 | |
| 1410 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1411 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1412 | std::vector<InputTarget> inputTargets; |
| 1413 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1414 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1415 | const sp<IBinder>& token = handle->getToken(); |
| 1416 | if (token == nullptr) { |
| 1417 | continue; |
| 1418 | } |
| 1419 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1420 | if (channel == nullptr) { |
| 1421 | continue; // Window has gone away |
| 1422 | } |
| 1423 | InputTarget target; |
| 1424 | target.inputChannel = channel; |
| 1425 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1426 | inputTargets.push_back(target); |
| 1427 | } |
| 1428 | return inputTargets; |
| 1429 | } |
| 1430 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1431 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1432 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1433 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1434 | if (!entry->dispatchInProgress) { |
| 1435 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1436 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1437 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1438 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1439 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1440 | // We have seen two identical key downs in a row which indicates that the device |
| 1441 | // driver is automatically generating key repeats itself. We take note of the |
| 1442 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1443 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1444 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1445 | // Make sure we don't get key down from a different device. If a different |
| 1446 | // device Id has same key pressed down, the new device Id will replace the |
| 1447 | // current one to hold the key repeat with repeat count reset. |
| 1448 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1449 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1450 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1451 | resetKeyRepeatLocked(); |
| 1452 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1453 | } else { |
| 1454 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1455 | resetKeyRepeatLocked(); |
| 1456 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1457 | } |
| 1458 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1459 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1460 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1461 | // 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] | 1462 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1463 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1464 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1465 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1466 | resetKeyRepeatLocked(); |
| 1467 | } |
| 1468 | |
| 1469 | if (entry->repeatCount == 1) { |
| 1470 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1471 | } else { |
| 1472 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1473 | } |
| 1474 | |
| 1475 | entry->dispatchInProgress = true; |
| 1476 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1477 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | // Handle case where the policy asked us to try again later last time. |
| 1481 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1482 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1483 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1484 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1485 | } |
| 1486 | return false; // wait until next wakeup |
| 1487 | } |
| 1488 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1489 | entry->interceptKeyWakeupTime = 0; |
| 1490 | } |
| 1491 | |
| 1492 | // Give the policy a chance to intercept the key. |
| 1493 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1494 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1495 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1496 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1497 | |
| 1498 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1499 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1500 | }; |
| 1501 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1502 | return false; // wait for the command to run |
| 1503 | } else { |
| 1504 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1505 | } |
| 1506 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1507 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1508 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1513 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1514 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1515 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1516 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1517 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1518 | return true; |
| 1519 | } |
| 1520 | |
| 1521 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1522 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1523 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1524 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1525 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1526 | return false; |
| 1527 | } |
| 1528 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1529 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1530 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1531 | return true; |
| 1532 | } |
| 1533 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1534 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1535 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1536 | |
| 1537 | // Dispatch the key. |
| 1538 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1539 | return true; |
| 1540 | } |
| 1541 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1542 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1543 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1544 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1545 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1546 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1547 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1548 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1549 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1550 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1553 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1554 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1555 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1556 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1557 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1558 | "source=0x%x, sensorType=%s", |
| 1559 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1560 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1561 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1562 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1563 | scoped_unlock unlock(mLock); |
| 1564 | |
| 1565 | if (entry->accuracyChanged) { |
| 1566 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1567 | } |
| 1568 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1569 | entry->hwTimestamp, entry->values); |
| 1570 | }; |
| 1571 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1575 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1576 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1577 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1578 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1579 | { // acquire lock |
| 1580 | std::scoped_lock _l(mLock); |
| 1581 | |
| 1582 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1583 | std::shared_ptr<EventEntry> entry = *it; |
| 1584 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1585 | it = mInboundQueue.erase(it); |
| 1586 | releaseInboundEventLocked(entry); |
| 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 | return true; |
| 1591 | } |
| 1592 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1593 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1594 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1595 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1596 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1597 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1598 | entry->dispatchInProgress = true; |
| 1599 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1600 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1604 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1605 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1606 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1607 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1608 | return true; |
| 1609 | } |
| 1610 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1611 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1612 | |
| 1613 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1614 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1615 | |
| 1616 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1617 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1618 | if (isPointerEvent) { |
| 1619 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1620 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1621 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1622 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1623 | } else { |
| 1624 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1625 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1626 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1627 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1628 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1629 | return false; |
| 1630 | } |
| 1631 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1632 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1633 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1634 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1635 | return true; |
| 1636 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1637 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1638 | CancelationOptions::Mode mode(isPointerEvent |
| 1639 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1640 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1641 | CancelationOptions options(mode, "input event injection failed"); |
| 1642 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1643 | return true; |
| 1644 | } |
| 1645 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1646 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1647 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1648 | |
| 1649 | // Dispatch the motion. |
| 1650 | if (conflictingPointerActions) { |
| 1651 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1652 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1653 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1654 | } |
| 1655 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1656 | return true; |
| 1657 | } |
| 1658 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1659 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1660 | bool isExiting, const MotionEntry& motionEntry) { |
| 1661 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1662 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1663 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1664 | PointerCoords pointerCoords; |
| 1665 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1666 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1667 | |
| 1668 | std::unique_ptr<DragEntry> dragEntry = |
| 1669 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1670 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1671 | pointerCoords.getY()); |
| 1672 | |
| 1673 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1674 | } |
| 1675 | |
| 1676 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1677 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1678 | if (channel == nullptr) { |
| 1679 | return; // Window has gone away |
| 1680 | } |
| 1681 | InputTarget target; |
| 1682 | target.inputChannel = channel; |
| 1683 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1684 | entry->dispatchInProgress = true; |
| 1685 | dispatchEventLocked(currentTime, entry, {target}); |
| 1686 | } |
| 1687 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1688 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1689 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1690 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1691 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1692 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1693 | "metaState=0x%x, buttonState=0x%x," |
| 1694 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1695 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1696 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1697 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1698 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1699 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1700 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1701 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1702 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1703 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1704 | "orientation=%f", |
| 1705 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1706 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1707 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1708 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1709 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1710 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1711 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1712 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1713 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1714 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1715 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1716 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1717 | } |
| 1718 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1719 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1720 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1721 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1722 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1723 | if (DEBUG_DISPATCH_CYCLE) { |
| 1724 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1725 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1726 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1727 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1728 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1729 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1730 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1731 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1732 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1733 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1734 | sp<Connection> connection = |
| 1735 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1736 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1737 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1738 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1739 | if (DEBUG_FOCUS) { |
| 1740 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1741 | "is no longer registered with the input dispatcher.", |
| 1742 | inputTarget.inputChannel->getName().c_str()); |
| 1743 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1744 | } |
| 1745 | } |
| 1746 | } |
| 1747 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1748 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1749 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1750 | // If the policy decides to close the app, we will get a channel removal event via |
| 1751 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1752 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1753 | // pile up. |
| 1754 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1755 | connection->inputChannel->getName().c_str()); |
| 1756 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1757 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1758 | "application not responding"); |
| 1759 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1763 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1764 | if (DEBUG_FOCUS) { |
| 1765 | ALOGD("Resetting ANR timeouts."); |
| 1766 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1767 | |
| 1768 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1769 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1770 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1771 | } |
| 1772 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1773 | /** |
| 1774 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1775 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1776 | * Focused display is the display that the user most recently interacted with. |
| 1777 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1778 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1779 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1780 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1781 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1782 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1783 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1784 | break; |
| 1785 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1786 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1787 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1788 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1789 | break; |
| 1790 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1791 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1792 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1793 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1794 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1795 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1796 | case EventEntry::Type::SENSOR: |
| 1797 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1798 | 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] | 1799 | return ADISPLAY_ID_NONE; |
| 1800 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1801 | } |
| 1802 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1803 | } |
| 1804 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1805 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1806 | const char* focusedWindowName) { |
| 1807 | if (mAnrTracker.empty()) { |
| 1808 | // already processed all events that we waited for |
| 1809 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1810 | return false; |
| 1811 | } |
| 1812 | |
| 1813 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1814 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1815 | // 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] | 1816 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1817 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1818 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1819 | return true; |
| 1820 | } |
| 1821 | |
| 1822 | // We still have pending events, and already started the timer |
| 1823 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1824 | return true; // Still waiting |
| 1825 | } |
| 1826 | |
| 1827 | // Waited too long, and some connection still hasn't processed all motions |
| 1828 | // Just send the key to the focused window |
| 1829 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1830 | focusedWindowName); |
| 1831 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1832 | return false; |
| 1833 | } |
| 1834 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1835 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1836 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1837 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1838 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1839 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1840 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1841 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1842 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1843 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1844 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1845 | // If there is no currently focused window and no focused application |
| 1846 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1847 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1848 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1849 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1850 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1851 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1852 | } |
| 1853 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1854 | // Drop key events if requested by input feature |
| 1855 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1856 | return InputEventInjectionResult::FAILED; |
| 1857 | } |
| 1858 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1859 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1860 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1861 | // start interacting with another application via touch (app switch). This code can be removed |
| 1862 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1863 | // an app is expected to have a focused window. |
| 1864 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1865 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1866 | // 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] | 1867 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1868 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1869 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1870 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1871 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1872 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1873 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1874 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1875 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1876 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1877 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1878 | // Already raised ANR. Drop the event |
| 1879 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1880 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1881 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1882 | } else { |
| 1883 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1884 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | // we have a valid, non-null focused window |
| 1889 | resetNoFocusedWindowTimeoutLocked(); |
| 1890 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1891 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1892 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1893 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1894 | } |
| 1895 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1896 | if (focusedWindowHandle->getInfo()->paused) { |
| 1897 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1898 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | // If the event is a key event, then we must wait for all previous events to |
| 1902 | // complete before delivering it because previous events may have the |
| 1903 | // side-effect of transferring focus to a different window and we want to |
| 1904 | // ensure that the following keys are sent to the new window. |
| 1905 | // |
| 1906 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1907 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1908 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1909 | // often anticipate pending UI changes when typing on a keyboard. |
| 1910 | // To obtain this behavior, we must serialize key events with respect to all |
| 1911 | // prior input events. |
| 1912 | if (entry.type == EventEntry::Type::KEY) { |
| 1913 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1914 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1915 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1916 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1920 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1921 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1922 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1923 | |
| 1924 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1925 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1926 | } |
| 1927 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1928 | /** |
| 1929 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1930 | * that are currently unresponsive. |
| 1931 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1932 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1933 | const std::vector<Monitor>& monitors) const { |
| 1934 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1935 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1936 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 1937 | sp<Connection> connection = |
| 1938 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1939 | if (connection == nullptr) { |
| 1940 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1941 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1942 | return false; |
| 1943 | } |
| 1944 | if (!connection->responsive) { |
| 1945 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1946 | connection->inputChannel->getName().c_str()); |
| 1947 | return false; |
| 1948 | } |
| 1949 | return true; |
| 1950 | }); |
| 1951 | return responsiveMonitors; |
| 1952 | } |
| 1953 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1954 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1955 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1956 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1957 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1958 | enum InjectionPermission { |
| 1959 | INJECTION_PERMISSION_UNKNOWN, |
| 1960 | INJECTION_PERMISSION_GRANTED, |
| 1961 | INJECTION_PERMISSION_DENIED |
| 1962 | }; |
| 1963 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1964 | // For security reasons, we defer updating the touch state until we are sure that |
| 1965 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1966 | int32_t displayId = entry.displayId; |
| 1967 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1968 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1969 | |
| 1970 | // 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] | 1971 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1972 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1973 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1974 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1975 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1976 | // Copy current touch state into tempTouchState. |
| 1977 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1978 | // 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] | 1979 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1980 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1981 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1982 | mTouchStatesByDisplay.find(displayId); |
| 1983 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1984 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1985 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1986 | } |
| 1987 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1988 | bool isSplit = tempTouchState.split; |
| 1989 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1990 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1991 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1992 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1993 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1994 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1995 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1996 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1997 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1998 | bool wrongDevice = false; |
| 1999 | if (newGesture) { |
| 2000 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2001 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2002 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2003 | "in display %" PRId32, |
| 2004 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2005 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2006 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2007 | switchedDevice = false; |
| 2008 | wrongDevice = true; |
| 2009 | goto Failed; |
| 2010 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2011 | tempTouchState.reset(); |
| 2012 | tempTouchState.down = down; |
| 2013 | tempTouchState.deviceId = entry.deviceId; |
| 2014 | tempTouchState.source = entry.source; |
| 2015 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2016 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2017 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2018 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2019 | "in display %" PRId32, |
| 2020 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2021 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2022 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2023 | switchedDevice = false; |
| 2024 | wrongDevice = true; |
| 2025 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2029 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2030 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2031 | int32_t x; |
| 2032 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2033 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2034 | // Always dispatch mouse events to cursor position. |
| 2035 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2036 | x = int32_t(entry.xCursorPosition); |
| 2037 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2038 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2039 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2040 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2041 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2042 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2043 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 2044 | isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2045 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2046 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2047 | if (newTouchedWindowHandle != nullptr && |
| 2048 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 2049 | // New window supports splitting, but we should never split mouse events. |
| 2050 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2051 | } else if (isSplit) { |
| 2052 | // New window does not support splitting but we have already split events. |
| 2053 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2054 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2058 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2059 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2060 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2061 | // 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] | 2062 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2065 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 2066 | ALOGI("Not sending touch event to %s because it is paused", |
| 2067 | newTouchedWindowHandle->getName().c_str()); |
| 2068 | newTouchedWindowHandle = nullptr; |
| 2069 | } |
| 2070 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2071 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2072 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2073 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 2074 | if (!isResponsive) { |
| 2075 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2076 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 2077 | newTouchedWindowHandle = nullptr; |
| 2078 | } |
| 2079 | } |
| 2080 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2081 | // Drop events that can't be trusted due to occlusion |
| 2082 | if (newTouchedWindowHandle != nullptr && |
| 2083 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2084 | TouchOcclusionInfo occlusionInfo = |
| 2085 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2086 | if (!isTouchTrustedLocked(occlusionInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2087 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2088 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2089 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2090 | ALOGD("%s", log.c_str()); |
| 2091 | } |
| 2092 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2093 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2094 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2095 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2096 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2097 | newTouchedWindowHandle = nullptr; |
| 2098 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2099 | } |
| 2100 | } |
| 2101 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2102 | // Drop touch events if requested by input feature |
| 2103 | if (newTouchedWindowHandle != nullptr && shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2104 | newTouchedWindowHandle = nullptr; |
| 2105 | } |
| 2106 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2107 | const std::vector<Monitor> newGestureMonitors = isDown |
| 2108 | ? selectResponsiveMonitorsLocked( |
| 2109 | getValueByKey(mGestureMonitorsByDisplay, displayId)) |
| 2110 | : std::vector<Monitor>{}; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2111 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2112 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 2113 | 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] | 2114 | "(%d, %d) in display %" PRId32 ".", |
| 2115 | x, y, displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2116 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2117 | goto Failed; |
| 2118 | } |
| 2119 | |
| 2120 | if (newTouchedWindowHandle != nullptr) { |
| 2121 | // Set target flags. |
| 2122 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 2123 | if (isSplit) { |
| 2124 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2125 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2126 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2127 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2128 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2129 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2130 | } |
| 2131 | |
| 2132 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2133 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2134 | newHoverWindowHandle = nullptr; |
| 2135 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2136 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | // Update the temporary touch state. |
| 2140 | BitSet32 pointerIds; |
| 2141 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2142 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2143 | pointerIds.markBit(pointerId); |
| 2144 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2145 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2146 | } |
| 2147 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2148 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2149 | } else { |
| 2150 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2151 | |
| 2152 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2153 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2154 | if (DEBUG_FOCUS) { |
| 2155 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2156 | "dropped the pointer down event in display %" PRId32, |
| 2157 | displayId); |
| 2158 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2159 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2160 | goto Failed; |
| 2161 | } |
| 2162 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2163 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2164 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2165 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2166 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2167 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2168 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2169 | 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] | 2170 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2171 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2172 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2173 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2174 | |
| 2175 | // Drop touch events if requested by input feature |
| 2176 | if (newTouchedWindowHandle != nullptr && |
| 2177 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2178 | newTouchedWindowHandle = nullptr; |
| 2179 | } |
| 2180 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2181 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2182 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2183 | if (DEBUG_FOCUS) { |
| 2184 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2185 | oldTouchedWindowHandle->getName().c_str(), |
| 2186 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2187 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2188 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2189 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2190 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2191 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2192 | |
| 2193 | // Make a slippery entrance into the new window. |
| 2194 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2195 | isSplit = true; |
| 2196 | } |
| 2197 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2198 | int32_t targetFlags = |
| 2199 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2200 | if (isSplit) { |
| 2201 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2202 | } |
| 2203 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2204 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2205 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2206 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | BitSet32 pointerIds; |
| 2210 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2211 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2212 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2213 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2214 | } |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2219 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 2220 | // when dispatching it as is to newTouchedWindowHandle. |
| 2221 | if (mLastHoverWindowHandle != nullptr && |
| 2222 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2223 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2224 | if (DEBUG_HOVER) { |
| 2225 | ALOGD("Sending hover exit event to window %s.", |
| 2226 | mLastHoverWindowHandle->getName().c_str()); |
| 2227 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2228 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2229 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2230 | } |
| 2231 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2232 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2233 | // when dispatching it as is to newTouchedWindowHandle. |
| 2234 | if (newHoverWindowHandle != nullptr && |
| 2235 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2236 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2237 | if (DEBUG_HOVER) { |
| 2238 | ALOGD("Sending hover enter event to window %s.", |
| 2239 | newHoverWindowHandle->getName().c_str()); |
| 2240 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2241 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2242 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2243 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | // Check permission to inject into all touched foreground windows and ensure there |
| 2248 | // is at least one touched foreground window. |
| 2249 | { |
| 2250 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2251 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2252 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 2253 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2254 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2255 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2256 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2257 | goto Failed; |
| 2258 | } |
| 2259 | } |
| 2260 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2261 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2262 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2263 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 2264 | "%" PRId32 " or gesture monitor to receive it.", |
| 2265 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2266 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2267 | goto Failed; |
| 2268 | } |
| 2269 | |
| 2270 | // Permission granted to injection into all touched foreground windows. |
| 2271 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2272 | } |
| 2273 | |
| 2274 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2275 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2276 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2277 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2278 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2279 | if (foregroundWindowHandle) { |
| 2280 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2281 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2282 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2283 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2284 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2285 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2286 | InputTarget::FLAG_ZERO_COORDS, |
| 2287 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2288 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2289 | } |
| 2290 | } |
| 2291 | } |
| 2292 | } |
| 2293 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2294 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2295 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2296 | // of the touch gesture. |
| 2297 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2298 | // engine only supports touch events. We would need to add a mechanism similar |
| 2299 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2300 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2301 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2302 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2303 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2304 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2305 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2306 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2307 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2308 | if (info->displayId == displayId && |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2309 | windowHandle->getInfo()->type == WindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2310 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2311 | .addOrUpdateWindow(windowHandle, |
| 2312 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2313 | InputTarget:: |
| 2314 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2315 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2316 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2317 | } |
| 2318 | } |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2323 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2324 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2325 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2326 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2327 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2328 | } |
| 2329 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2330 | for (const auto& monitor : tempTouchState.gestureMonitors) { |
| 2331 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2334 | // Drop the outside or hover touch windows since we will not care about them |
| 2335 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2336 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2337 | |
| 2338 | Failed: |
| 2339 | // Check injection permission once and for all. |
| 2340 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2341 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2342 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2343 | } else { |
| 2344 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2345 | } |
| 2346 | } |
| 2347 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2348 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2349 | return injectionResult; |
| 2350 | } |
| 2351 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2352 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2353 | if (!wrongDevice) { |
| 2354 | if (switchedDevice) { |
| 2355 | if (DEBUG_FOCUS) { |
| 2356 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2357 | } |
| 2358 | *outConflictingPointerActions = true; |
| 2359 | } |
| 2360 | |
| 2361 | if (isHoverAction) { |
| 2362 | // Started hovering, therefore no longer down. |
| 2363 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2364 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2365 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2366 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2367 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2368 | *outConflictingPointerActions = true; |
| 2369 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2370 | tempTouchState.reset(); |
| 2371 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2372 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2373 | tempTouchState.deviceId = entry.deviceId; |
| 2374 | tempTouchState.source = entry.source; |
| 2375 | tempTouchState.displayId = displayId; |
| 2376 | } |
| 2377 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2378 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2379 | // All pointers up or canceled. |
| 2380 | tempTouchState.reset(); |
| 2381 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2382 | // First pointer went down. |
| 2383 | if (oldState && oldState->down) { |
| 2384 | if (DEBUG_FOCUS) { |
| 2385 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2386 | } |
| 2387 | *outConflictingPointerActions = true; |
| 2388 | } |
| 2389 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2390 | // One pointer went up. |
| 2391 | if (isSplit) { |
| 2392 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2393 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2394 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2395 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2396 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2397 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2398 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2399 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2400 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2401 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2402 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2403 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2404 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2405 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2406 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2407 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2408 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2409 | // Save changes unless the action was scroll in which case the temporary touch |
| 2410 | // state was only valid for this one action. |
| 2411 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2412 | if (tempTouchState.displayId >= 0) { |
| 2413 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2414 | } else { |
| 2415 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2416 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2417 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2418 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2419 | // Update hover state. |
| 2420 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2421 | } |
| 2422 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2423 | return injectionResult; |
| 2424 | } |
| 2425 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2426 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2427 | const sp<WindowInfoHandle> dropWindow = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2428 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2429 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2430 | if (dropWindow) { |
| 2431 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2432 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2433 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2434 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2435 | } |
| 2436 | mDragState.reset(); |
| 2437 | } |
| 2438 | |
| 2439 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2440 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2441 | return; |
| 2442 | } |
| 2443 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2444 | if (!mDragState->isStartDrag) { |
| 2445 | mDragState->isStartDrag = true; |
| 2446 | mDragState->isStylusButtonDownAtStart = |
| 2447 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2448 | } |
| 2449 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2450 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2451 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2452 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2453 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2454 | // Handle the special case : stylus button no longer pressed. |
| 2455 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2456 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2457 | finishDragAndDrop(entry.displayId, x, y); |
| 2458 | return; |
| 2459 | } |
| 2460 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2461 | const sp<WindowInfoHandle> hoverWindowHandle = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2462 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2463 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2464 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2465 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2466 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2467 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2468 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2469 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2470 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2471 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2472 | } |
| 2473 | // enqueue drag location if needed. |
| 2474 | if (hoverWindowHandle != nullptr) { |
| 2475 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2476 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2477 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2478 | finishDragAndDrop(entry.displayId, x, y); |
| 2479 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2480 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2481 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2482 | } |
| 2483 | } |
| 2484 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2485 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2486 | int32_t targetFlags, BitSet32 pointerIds, |
| 2487 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2488 | std::vector<InputTarget>::iterator it = |
| 2489 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2490 | [&windowHandle](const InputTarget& inputTarget) { |
| 2491 | return inputTarget.inputChannel->getConnectionToken() == |
| 2492 | windowHandle->getToken(); |
| 2493 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2494 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2495 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2496 | |
| 2497 | if (it == inputTargets.end()) { |
| 2498 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2499 | std::shared_ptr<InputChannel> inputChannel = |
| 2500 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2501 | if (inputChannel == nullptr) { |
| 2502 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2503 | return; |
| 2504 | } |
| 2505 | inputTarget.inputChannel = inputChannel; |
| 2506 | inputTarget.flags = targetFlags; |
| 2507 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2508 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2509 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2510 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2511 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame^] | 2512 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2513 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2514 | inputTargets.push_back(inputTarget); |
| 2515 | it = inputTargets.end() - 1; |
| 2516 | } |
| 2517 | |
| 2518 | ALOG_ASSERT(it->flags == targetFlags); |
| 2519 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2520 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2521 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2522 | } |
| 2523 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2524 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2525 | int32_t displayId) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2526 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2527 | mGlobalMonitorsByDisplay.find(displayId); |
| 2528 | |
| 2529 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2530 | const std::vector<Monitor>& monitors = it->second; |
| 2531 | for (const Monitor& monitor : monitors) { |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2532 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2533 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2534 | } |
| 2535 | } |
| 2536 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2537 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, int32_t displayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2538 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2539 | InputTarget target; |
| 2540 | target.inputChannel = monitor.inputChannel; |
| 2541 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2542 | ui::Transform t; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2543 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | b5402b2 | 2021-10-04 05:52:50 -0700 | [diff] [blame] | 2544 | const auto& displayTransform = it->second.transform; |
| 2545 | target.displayTransform = displayTransform; |
| 2546 | t = displayTransform; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2547 | } |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2548 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2549 | inputTargets.push_back(target); |
| 2550 | } |
| 2551 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2552 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2553 | const InjectionState* injectionState) { |
| 2554 | if (injectionState && |
| 2555 | (windowHandle == nullptr || |
| 2556 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2557 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2558 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2559 | 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] | 2560 | "owned by uid %d", |
| 2561 | injectionState->injectorPid, injectionState->injectorUid, |
| 2562 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2563 | } else { |
| 2564 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2565 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2566 | } |
| 2567 | return false; |
| 2568 | } |
| 2569 | return true; |
| 2570 | } |
| 2571 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2572 | /** |
| 2573 | * Indicate whether one window handle should be considered as obscuring |
| 2574 | * another window handle. We only check a few preconditions. Actually |
| 2575 | * checking the bounds is left to the caller. |
| 2576 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2577 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2578 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2579 | // Compare by token so cloned layers aren't counted |
| 2580 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2581 | return false; |
| 2582 | } |
| 2583 | auto info = windowHandle->getInfo(); |
| 2584 | auto otherInfo = otherHandle->getInfo(); |
| 2585 | if (!otherInfo->visible) { |
| 2586 | return false; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2587 | } else if (otherInfo->alpha == 0 && otherInfo->flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2588 | // Those act as if they were invisible, so we don't need to flag them. |
| 2589 | // We do want to potentially flag touchable windows even if they have 0 |
| 2590 | // opacity, since they can consume touches and alter the effects of the |
| 2591 | // user interaction (eg. apps that rely on |
| 2592 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2593 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2594 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2595 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2596 | // If ownerUid is the same we don't generate occlusion events as there |
| 2597 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2598 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2599 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2600 | return false; |
| 2601 | } else if (otherInfo->displayId != info->displayId) { |
| 2602 | return false; |
| 2603 | } |
| 2604 | return true; |
| 2605 | } |
| 2606 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2607 | /** |
| 2608 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2609 | * untrusted, one should check: |
| 2610 | * |
| 2611 | * 1. If result.hasBlockingOcclusion is true. |
| 2612 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2613 | * BLOCK_UNTRUSTED. |
| 2614 | * |
| 2615 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2616 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2617 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2618 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2619 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2620 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2621 | * |
| 2622 | * If neither of those is true, then it means the touch can be allowed. |
| 2623 | */ |
| 2624 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2625 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2626 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2627 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2628 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2629 | TouchOcclusionInfo info; |
| 2630 | info.hasBlockingOcclusion = false; |
| 2631 | info.obscuringOpacity = 0; |
| 2632 | info.obscuringUid = -1; |
| 2633 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2634 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2635 | if (windowHandle == otherHandle) { |
| 2636 | break; // All future windows are below us. Exit early. |
| 2637 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2638 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2639 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2640 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2641 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2642 | info.debugInfo.push_back( |
| 2643 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2644 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2645 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2646 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2647 | // window's touch occlusion mode |
| 2648 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2649 | info.hasBlockingOcclusion = true; |
| 2650 | info.obscuringUid = otherInfo->ownerUid; |
| 2651 | info.obscuringPackage = otherInfo->packageName; |
| 2652 | break; |
| 2653 | } |
| 2654 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2655 | uint32_t uid = otherInfo->ownerUid; |
| 2656 | float opacity = |
| 2657 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2658 | // Given windows A and B: |
| 2659 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2660 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2661 | opacityByUid[uid] = opacity; |
| 2662 | if (opacity > info.obscuringOpacity) { |
| 2663 | info.obscuringOpacity = opacity; |
| 2664 | info.obscuringUid = uid; |
| 2665 | info.obscuringPackage = otherInfo->packageName; |
| 2666 | } |
| 2667 | } |
| 2668 | } |
| 2669 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2670 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2671 | info.debugInfo.push_back( |
| 2672 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2673 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2674 | return info; |
| 2675 | } |
| 2676 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2677 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2678 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2679 | return StringPrintf(INDENT2 |
| 2680 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2681 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2682 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2683 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2684 | isTouchedWindow ? "[TOUCHED] " : "", ftl::enum_string(info->type).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2685 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2686 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2687 | info->frameTop, info->frameRight, info->frameBottom, |
| 2688 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2689 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2690 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2691 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2694 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2695 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2696 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2697 | occlusionInfo.obscuringUid); |
| 2698 | return false; |
| 2699 | } |
| 2700 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2701 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2702 | "%.2f, maximum allowed = %.2f)", |
| 2703 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2704 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2705 | return false; |
| 2706 | } |
| 2707 | return true; |
| 2708 | } |
| 2709 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2710 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2711 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2712 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2713 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2714 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2715 | if (windowHandle == otherHandle) { |
| 2716 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2717 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2718 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2719 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2720 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2721 | return true; |
| 2722 | } |
| 2723 | } |
| 2724 | return false; |
| 2725 | } |
| 2726 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2727 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2728 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2729 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2730 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2731 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2732 | if (windowHandle == otherHandle) { |
| 2733 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2734 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2735 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2736 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2737 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2738 | return true; |
| 2739 | } |
| 2740 | } |
| 2741 | return false; |
| 2742 | } |
| 2743 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2744 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2745 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2746 | if (applicationHandle != nullptr) { |
| 2747 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2748 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2749 | } else { |
| 2750 | return applicationHandle->getName(); |
| 2751 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2752 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2753 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2754 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2755 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2759 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2760 | if (!isUserActivityEvent(eventEntry)) { |
| 2761 | // 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] | 2762 | return; |
| 2763 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2764 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2765 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2766 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2767 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
| 2768 | if (info->inputFeatures.test(WindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2769 | if (DEBUG_DISPATCH_CYCLE) { |
| 2770 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2771 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2772 | return; |
| 2773 | } |
| 2774 | } |
| 2775 | |
| 2776 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2777 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2778 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2779 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2780 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2781 | return; |
| 2782 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2783 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2784 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2785 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2786 | } |
| 2787 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2788 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2789 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2790 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2791 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2792 | return; |
| 2793 | } |
| 2794 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2795 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2796 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2797 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2798 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2799 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2800 | break; |
| 2801 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2802 | } |
| 2803 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2804 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2805 | REQUIRES(mLock) { |
| 2806 | scoped_unlock unlock(mLock); |
| 2807 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2808 | }; |
| 2809 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2813 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2814 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2815 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2816 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2817 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2818 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2819 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2820 | ATRACE_NAME(message.c_str()); |
| 2821 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2822 | if (DEBUG_DISPATCH_CYCLE) { |
| 2823 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2824 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2825 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2826 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2827 | inputTarget.getPointerInfoString().c_str()); |
| 2828 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2829 | |
| 2830 | // Skip this event if the connection status is not normal. |
| 2831 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2832 | if (connection->status != Connection::STATUS_NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2833 | if (DEBUG_DISPATCH_CYCLE) { |
| 2834 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
| 2835 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
| 2836 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2837 | return; |
| 2838 | } |
| 2839 | |
| 2840 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2841 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2842 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2843 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2844 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2845 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2846 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2847 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2848 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2849 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2850 | if (!splitMotionEntry) { |
| 2851 | return; // split event was dropped |
| 2852 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2853 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2854 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2855 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2856 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2857 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2858 | if (DEBUG_FOCUS) { |
| 2859 | ALOGD("channel '%s' ~ Split motion event.", |
| 2860 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2861 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2862 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2863 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2864 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2865 | return; |
| 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2870 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2871 | } |
| 2872 | |
| 2873 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2874 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2875 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2876 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2877 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2878 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2879 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2880 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2881 | ATRACE_NAME(message.c_str()); |
| 2882 | } |
| 2883 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2884 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2885 | |
| 2886 | // Enqueue dispatch entries for the requested modes. |
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_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2889 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2890 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2891 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2892 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2893 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2894 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2895 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2896 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2897 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2898 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2899 | |
| 2900 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2901 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2902 | startDispatchCycleLocked(currentTime, connection); |
| 2903 | } |
| 2904 | } |
| 2905 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2906 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2907 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2908 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2909 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2910 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2911 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2912 | connection->getInputChannelName().c_str(), |
| 2913 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2914 | ATRACE_NAME(message.c_str()); |
| 2915 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2916 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2917 | if (!(inputTargetFlags & dispatchMode)) { |
| 2918 | return; |
| 2919 | } |
| 2920 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2921 | |
| 2922 | // This is a new event. |
| 2923 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2924 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2925 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2926 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2927 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2928 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2929 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2930 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2931 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2932 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2933 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2934 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2935 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2936 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2937 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2938 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2939 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2940 | if (DEBUG_DISPATCH_CYCLE) { |
| 2941 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 2942 | "event", |
| 2943 | connection->getInputChannelName().c_str()); |
| 2944 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2945 | return; // skip the inconsistent event |
| 2946 | } |
| 2947 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2948 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2949 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2950 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2951 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2952 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2953 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2954 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2955 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2956 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2957 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2958 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2959 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2960 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2961 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2962 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2963 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2964 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2965 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2966 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2967 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2968 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2969 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2970 | } |
| 2971 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2972 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2973 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2974 | if (DEBUG_DISPATCH_CYCLE) { |
| 2975 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 2976 | "enter event", |
| 2977 | connection->getInputChannelName().c_str()); |
| 2978 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 2979 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 2980 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2981 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2982 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2983 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2984 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2985 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2986 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2987 | } |
| 2988 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2989 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2990 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2991 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2992 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2993 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2994 | if (DEBUG_DISPATCH_CYCLE) { |
| 2995 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2996 | "event", |
| 2997 | connection->getInputChannelName().c_str()); |
| 2998 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2999 | return; // skip the inconsistent event |
| 3000 | } |
| 3001 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3002 | dispatchEntry->resolvedEventId = |
| 3003 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3004 | ? mIdGenerator.nextId() |
| 3005 | : motionEntry.id; |
| 3006 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3007 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3008 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3009 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3010 | ATRACE_NAME(message.c_str()); |
| 3011 | } |
| 3012 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3013 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3014 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3015 | // Skip reporting pointer down outside focus to the policy. |
| 3016 | break; |
| 3017 | } |
| 3018 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3019 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3020 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3021 | |
| 3022 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3023 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3024 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3025 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3026 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3027 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3028 | break; |
| 3029 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3030 | case EventEntry::Type::SENSOR: { |
| 3031 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3032 | break; |
| 3033 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3034 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3035 | case EventEntry::Type::DEVICE_RESET: { |
| 3036 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3037 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3038 | break; |
| 3039 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | // Remember that we are waiting for this dispatch to complete. |
| 3043 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3044 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3048 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3049 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3050 | } |
| 3051 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3052 | /** |
| 3053 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3054 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3055 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3056 | * We will see both launcher and wallpaper in that list. |
| 3057 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3058 | * until the set of interacted connections changes. |
| 3059 | * |
| 3060 | * The following items are skipped, to reduce the logspam: |
| 3061 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3062 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3063 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3064 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3065 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3066 | */ |
| 3067 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3068 | const std::vector<InputTarget>& targets) { |
| 3069 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3070 | if (entry.type == EventEntry::Type::KEY) { |
| 3071 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3072 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3073 | return; |
| 3074 | } |
| 3075 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3076 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3077 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3078 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3079 | return; |
| 3080 | } |
| 3081 | } else { |
| 3082 | return; // Not a key or a motion |
| 3083 | } |
| 3084 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3085 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3086 | std::vector<sp<Connection>> newConnections; |
| 3087 | for (const InputTarget& target : targets) { |
| 3088 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3089 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3090 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3091 | } |
| 3092 | |
| 3093 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3094 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3095 | if (connection == nullptr) { |
| 3096 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3097 | } |
| 3098 | newConnectionTokens.insert(std::move(token)); |
| 3099 | newConnections.emplace_back(connection); |
| 3100 | } |
| 3101 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3102 | return; // no change |
| 3103 | } |
| 3104 | mInteractionConnectionTokens = newConnectionTokens; |
| 3105 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3106 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3107 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3108 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3109 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3110 | std::string message = "Interaction with: " + targetList; |
| 3111 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3112 | message += "<none>"; |
| 3113 | } |
| 3114 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3115 | } |
| 3116 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3117 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3118 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3119 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3120 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3121 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3122 | return; |
| 3123 | } |
| 3124 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3125 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3126 | if (focusedToken == token) { |
| 3127 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3128 | return; |
| 3129 | } |
| 3130 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3131 | auto command = [this, token]() REQUIRES(mLock) { |
| 3132 | scoped_unlock unlock(mLock); |
| 3133 | mPolicy->onPointerDownOutsideFocus(token); |
| 3134 | }; |
| 3135 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3139 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3140 | if (ATRACE_ENABLED()) { |
| 3141 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3142 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3143 | ATRACE_NAME(message.c_str()); |
| 3144 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3145 | if (DEBUG_DISPATCH_CYCLE) { |
| 3146 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3147 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3148 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3149 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 3150 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3151 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3152 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3153 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3154 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3155 | |
| 3156 | // Publish the event. |
| 3157 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3158 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3159 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3160 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3161 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3162 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3163 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3164 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3165 | status = connection->inputPublisher |
| 3166 | .publishKeyEvent(dispatchEntry->seq, |
| 3167 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3168 | keyEntry.source, keyEntry.displayId, |
| 3169 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3170 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3171 | keyEntry.scanCode, keyEntry.metaState, |
| 3172 | keyEntry.repeatCount, keyEntry.downTime, |
| 3173 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3174 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3175 | } |
| 3176 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3177 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3178 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3179 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3180 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3181 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3182 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3183 | // 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] | 3184 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3185 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3186 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3187 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3188 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3189 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3190 | // Don't apply window scale here since we don't want scale to affect raw |
| 3191 | // coordinates. The scale will be sent back to the client and applied |
| 3192 | // later when requesting relative coordinates. |
| 3193 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3194 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3195 | } |
| 3196 | usingCoords = scaledCoords; |
| 3197 | } |
| 3198 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3199 | // We don't want the dispatch target to know. |
| 3200 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3201 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3202 | scaledCoords[i].clear(); |
| 3203 | } |
| 3204 | usingCoords = scaledCoords; |
| 3205 | } |
| 3206 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3207 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3208 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3209 | |
| 3210 | // Publish the motion event. |
| 3211 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3212 | .publishMotionEvent(dispatchEntry->seq, |
| 3213 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3214 | motionEntry.deviceId, motionEntry.source, |
| 3215 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3216 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3217 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3218 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3219 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3220 | motionEntry.buttonState, |
| 3221 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3222 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3223 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3224 | motionEntry.xCursorPosition, |
| 3225 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3226 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3227 | motionEntry.downTime, motionEntry.eventTime, |
| 3228 | motionEntry.pointerCount, |
| 3229 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3230 | break; |
| 3231 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3232 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3233 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3234 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3235 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3236 | focusEntry.id, |
| 3237 | focusEntry.hasFocus, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3238 | mInTouchMode); |
| 3239 | break; |
| 3240 | } |
| 3241 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3242 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3243 | const TouchModeEntry& touchModeEntry = |
| 3244 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3245 | status = connection->inputPublisher |
| 3246 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3247 | touchModeEntry.inTouchMode); |
| 3248 | |
| 3249 | break; |
| 3250 | } |
| 3251 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3252 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3253 | const auto& captureEntry = |
| 3254 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3255 | status = connection->inputPublisher |
| 3256 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3257 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3258 | break; |
| 3259 | } |
| 3260 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3261 | case EventEntry::Type::DRAG: { |
| 3262 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3263 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3264 | dragEntry.id, dragEntry.x, |
| 3265 | dragEntry.y, |
| 3266 | dragEntry.isExiting); |
| 3267 | break; |
| 3268 | } |
| 3269 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3270 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3271 | case EventEntry::Type::DEVICE_RESET: |
| 3272 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3273 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3274 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3275 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3276 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | // Check the result. |
| 3280 | if (status) { |
| 3281 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3282 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3283 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3284 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3285 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3286 | "event to it, status=%s(%d)", |
| 3287 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3288 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3289 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3290 | } else { |
| 3291 | // Pipe is full and we are waiting for the app to finish process some events |
| 3292 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3293 | if (DEBUG_DISPATCH_CYCLE) { |
| 3294 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3295 | "waiting for the application to catch up", |
| 3296 | connection->getInputChannelName().c_str()); |
| 3297 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3298 | } |
| 3299 | } else { |
| 3300 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3301 | "status=%s(%d)", |
| 3302 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3303 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3304 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3305 | } |
| 3306 | return; |
| 3307 | } |
| 3308 | |
| 3309 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3310 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3311 | connection->outboundQueue.end(), |
| 3312 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3313 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3314 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3315 | if (connection->responsive) { |
| 3316 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3317 | connection->inputChannel->getConnectionToken()); |
| 3318 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3319 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3320 | } |
| 3321 | } |
| 3322 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3323 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3324 | size_t size; |
| 3325 | switch (event.type) { |
| 3326 | case VerifiedInputEvent::Type::KEY: { |
| 3327 | size = sizeof(VerifiedKeyEvent); |
| 3328 | break; |
| 3329 | } |
| 3330 | case VerifiedInputEvent::Type::MOTION: { |
| 3331 | size = sizeof(VerifiedMotionEvent); |
| 3332 | break; |
| 3333 | } |
| 3334 | } |
| 3335 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3336 | return mHmacKeyManager.sign(start, size); |
| 3337 | } |
| 3338 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3339 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3340 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3341 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3342 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3343 | // Only sign events up and down events as the purely move events |
| 3344 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3345 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3346 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3347 | |
| 3348 | VerifiedMotionEvent verifiedEvent = |
| 3349 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3350 | verifiedEvent.actionMasked = actionMasked; |
| 3351 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3352 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3356 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3357 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3358 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3359 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3360 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3361 | } |
| 3362 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3363 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3364 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3365 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3366 | if (DEBUG_DISPATCH_CYCLE) { |
| 3367 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3368 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3369 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3370 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3371 | if (connection->status == Connection::STATUS_BROKEN || |
| 3372 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3373 | return; |
| 3374 | } |
| 3375 | |
| 3376 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3377 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3378 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3379 | }; |
| 3380 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3381 | } |
| 3382 | |
| 3383 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3384 | const sp<Connection>& connection, |
| 3385 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3386 | if (DEBUG_DISPATCH_CYCLE) { |
| 3387 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3388 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3389 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3390 | |
| 3391 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3392 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3393 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3394 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3395 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3396 | |
| 3397 | // The connection appears to be unrecoverably broken. |
| 3398 | // Ignore already broken or zombie connections. |
| 3399 | if (connection->status == Connection::STATUS_NORMAL) { |
| 3400 | connection->status = Connection::STATUS_BROKEN; |
| 3401 | |
| 3402 | if (notify) { |
| 3403 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3404 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3405 | connection->getInputChannelName().c_str()); |
| 3406 | |
| 3407 | auto command = [this, connection]() REQUIRES(mLock) { |
| 3408 | if (connection->status == Connection::STATUS_ZOMBIE) return; |
| 3409 | scoped_unlock unlock(mLock); |
| 3410 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3411 | }; |
| 3412 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3413 | } |
| 3414 | } |
| 3415 | } |
| 3416 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3417 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3418 | while (!queue.empty()) { |
| 3419 | DispatchEntry* dispatchEntry = queue.front(); |
| 3420 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3421 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3422 | } |
| 3423 | } |
| 3424 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3425 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3426 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3427 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3428 | } |
| 3429 | delete dispatchEntry; |
| 3430 | } |
| 3431 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3432 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3433 | std::scoped_lock _l(mLock); |
| 3434 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3435 | if (connection == nullptr) { |
| 3436 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3437 | connectionToken.get(), events); |
| 3438 | return 0; // remove the callback |
| 3439 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3440 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3441 | bool notify; |
| 3442 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3443 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3444 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3445 | "events=0x%x", |
| 3446 | connection->getInputChannelName().c_str(), events); |
| 3447 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3448 | } |
| 3449 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3450 | nsecs_t currentTime = now(); |
| 3451 | bool gotOne = false; |
| 3452 | status_t status = OK; |
| 3453 | for (;;) { |
| 3454 | Result<InputPublisher::ConsumerResponse> result = |
| 3455 | connection->inputPublisher.receiveConsumerResponse(); |
| 3456 | if (!result.ok()) { |
| 3457 | status = result.error().code(); |
| 3458 | break; |
| 3459 | } |
| 3460 | |
| 3461 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3462 | const InputPublisher::Finished& finish = |
| 3463 | std::get<InputPublisher::Finished>(*result); |
| 3464 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3465 | finish.consumeTime); |
| 3466 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3467 | if (shouldReportMetricsForConnection(*connection)) { |
| 3468 | const InputPublisher::Timeline& timeline = |
| 3469 | std::get<InputPublisher::Timeline>(*result); |
| 3470 | mLatencyTracker |
| 3471 | .trackGraphicsLatency(timeline.inputEventId, |
| 3472 | connection->inputChannel->getConnectionToken(), |
| 3473 | std::move(timeline.graphicsTimeline)); |
| 3474 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3475 | } |
| 3476 | gotOne = true; |
| 3477 | } |
| 3478 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3479 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3480 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3481 | return 1; |
| 3482 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3483 | } |
| 3484 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3485 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3486 | if (notify) { |
| 3487 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3488 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3489 | status); |
| 3490 | } |
| 3491 | } else { |
| 3492 | // Monitor channels are never explicitly unregistered. |
| 3493 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3494 | const bool stillHaveWindowHandle = |
| 3495 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3496 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3497 | if (notify) { |
| 3498 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3499 | connection->getInputChannelName().c_str(), events); |
| 3500 | } |
| 3501 | } |
| 3502 | |
| 3503 | // Remove the channel. |
| 3504 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3505 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3506 | } |
| 3507 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3508 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3509 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3510 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3511 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3512 | } |
| 3513 | } |
| 3514 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3515 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3516 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3517 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3518 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3519 | } |
| 3520 | |
| 3521 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3522 | const CancelationOptions& options, |
| 3523 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3524 | for (const auto& it : monitorsByDisplay) { |
| 3525 | const std::vector<Monitor>& monitors = it.second; |
| 3526 | for (const Monitor& monitor : monitors) { |
| 3527 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3528 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3529 | } |
| 3530 | } |
| 3531 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3532 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3533 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3534 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3535 | if (connection == nullptr) { |
| 3536 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3537 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3538 | |
| 3539 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3543 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 3544 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3545 | return; |
| 3546 | } |
| 3547 | |
| 3548 | nsecs_t currentTime = now(); |
| 3549 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3550 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3551 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3552 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3553 | if (cancelationEvents.empty()) { |
| 3554 | return; |
| 3555 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3556 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3557 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3558 | "with reality: %s, mode=%d.", |
| 3559 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3560 | options.mode); |
| 3561 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3562 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3563 | std::string reason = std::string("reason=").append(options.reason); |
| 3564 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3565 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3566 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3567 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3568 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3569 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3570 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3571 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3572 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3573 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3574 | } |
| 3575 | target.inputChannel = connection->inputChannel; |
| 3576 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3577 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3578 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3579 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3580 | switch (cancelationEventEntry->type) { |
| 3581 | case EventEntry::Type::KEY: { |
| 3582 | logOutboundKeyDetails("cancel - ", |
| 3583 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3584 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3585 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3586 | case EventEntry::Type::MOTION: { |
| 3587 | logOutboundMotionDetails("cancel - ", |
| 3588 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3589 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3590 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3591 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3592 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3593 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3594 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3595 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3596 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3597 | break; |
| 3598 | } |
| 3599 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3600 | case EventEntry::Type::DEVICE_RESET: |
| 3601 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3602 | 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] | 3603 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3604 | break; |
| 3605 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3606 | } |
| 3607 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3608 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3609 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3610 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3611 | |
| 3612 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3613 | } |
| 3614 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3615 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3616 | const sp<Connection>& connection) { |
| 3617 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3618 | return; |
| 3619 | } |
| 3620 | |
| 3621 | nsecs_t currentTime = now(); |
| 3622 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3623 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3624 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3625 | |
| 3626 | if (downEvents.empty()) { |
| 3627 | return; |
| 3628 | } |
| 3629 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3630 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3631 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3632 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3633 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3634 | |
| 3635 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3636 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3637 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3638 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3639 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3640 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3641 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3642 | } |
| 3643 | target.inputChannel = connection->inputChannel; |
| 3644 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3645 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3646 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3647 | switch (downEventEntry->type) { |
| 3648 | case EventEntry::Type::MOTION: { |
| 3649 | logOutboundMotionDetails("down - ", |
| 3650 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3651 | break; |
| 3652 | } |
| 3653 | |
| 3654 | case EventEntry::Type::KEY: |
| 3655 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3656 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3657 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3658 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3659 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3660 | case EventEntry::Type::SENSOR: |
| 3661 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3662 | 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] | 3663 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3664 | break; |
| 3665 | } |
| 3666 | } |
| 3667 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3668 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3669 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | startDispatchCycleLocked(currentTime, connection); |
| 3673 | } |
| 3674 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3675 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3676 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3677 | ALOG_ASSERT(pointerIds.value != 0); |
| 3678 | |
| 3679 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3680 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3681 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3682 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3683 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3684 | uint32_t splitPointerCount = 0; |
| 3685 | |
| 3686 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3687 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3688 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3689 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3690 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3691 | if (pointerIds.hasBit(pointerId)) { |
| 3692 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3693 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3694 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3695 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3696 | splitPointerCount += 1; |
| 3697 | } |
| 3698 | } |
| 3699 | |
| 3700 | if (splitPointerCount != pointerIds.count()) { |
| 3701 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3702 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3703 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3704 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3705 | // in this way. |
| 3706 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3707 | "we expected there to be %d pointers. This probably means we received " |
| 3708 | "a broken sequence of pointer ids from the input device.", |
| 3709 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3710 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3711 | } |
| 3712 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3713 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3714 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3715 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3716 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3717 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3718 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3719 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3720 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3721 | if (pointerIds.hasBit(pointerId)) { |
| 3722 | if (pointerIds.count() == 1) { |
| 3723 | // The first/last pointer went down/up. |
| 3724 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3725 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3726 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3727 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3728 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3729 | } else { |
| 3730 | // A secondary pointer went down/up. |
| 3731 | uint32_t splitPointerIndex = 0; |
| 3732 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3733 | splitPointerIndex += 1; |
| 3734 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3735 | action = maskedAction | |
| 3736 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3737 | } |
| 3738 | } else { |
| 3739 | // An unrelated pointer changed. |
| 3740 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3741 | } |
| 3742 | } |
| 3743 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3744 | int32_t newId = mIdGenerator.nextId(); |
| 3745 | if (ATRACE_ENABLED()) { |
| 3746 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3747 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3748 | originalMotionEntry.id, newId); |
| 3749 | ATRACE_NAME(message.c_str()); |
| 3750 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3751 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3752 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3753 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3754 | originalMotionEntry.displayId, |
| 3755 | originalMotionEntry.policyFlags, action, |
| 3756 | originalMotionEntry.actionButton, |
| 3757 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3758 | originalMotionEntry.buttonState, |
| 3759 | originalMotionEntry.classification, |
| 3760 | originalMotionEntry.edgeFlags, |
| 3761 | originalMotionEntry.xPrecision, |
| 3762 | originalMotionEntry.yPrecision, |
| 3763 | originalMotionEntry.xCursorPosition, |
| 3764 | originalMotionEntry.yCursorPosition, |
| 3765 | originalMotionEntry.downTime, splitPointerCount, |
| 3766 | splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3767 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3768 | if (originalMotionEntry.injectionState) { |
| 3769 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3770 | splitMotionEntry->injectionState->refCount += 1; |
| 3771 | } |
| 3772 | |
| 3773 | return splitMotionEntry; |
| 3774 | } |
| 3775 | |
| 3776 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3777 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3778 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3779 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3780 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3781 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3782 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3783 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3784 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3785 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3786 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3787 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3788 | } // release lock |
| 3789 | |
| 3790 | if (needWake) { |
| 3791 | mLooper->wake(); |
| 3792 | } |
| 3793 | } |
| 3794 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3795 | /** |
| 3796 | * If one of the meta shortcuts is detected, process them here: |
| 3797 | * Meta + Backspace -> generate BACK |
| 3798 | * Meta + Enter -> generate HOME |
| 3799 | * This will potentially overwrite keyCode and metaState. |
| 3800 | */ |
| 3801 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3802 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3803 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3804 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3805 | if (keyCode == AKEYCODE_DEL) { |
| 3806 | newKeyCode = AKEYCODE_BACK; |
| 3807 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3808 | newKeyCode = AKEYCODE_HOME; |
| 3809 | } |
| 3810 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3811 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3812 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3813 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3814 | keyCode = newKeyCode; |
| 3815 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3816 | } |
| 3817 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3818 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3819 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3820 | // 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] | 3821 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3822 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3823 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3824 | if (replacementIt != mReplacedKeys.end()) { |
| 3825 | keyCode = replacementIt->second; |
| 3826 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3827 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3828 | } |
| 3829 | } |
| 3830 | } |
| 3831 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3832 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3833 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3834 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3835 | "policyFlags=0x%x, action=0x%x, " |
| 3836 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3837 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3838 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3839 | args->downTime); |
| 3840 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3841 | if (!validateKeyEvent(args->action)) { |
| 3842 | return; |
| 3843 | } |
| 3844 | |
| 3845 | uint32_t policyFlags = args->policyFlags; |
| 3846 | int32_t flags = args->flags; |
| 3847 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3848 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3849 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3850 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3851 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3852 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3853 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3854 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3855 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3856 | metaState |= AMETA_FUNCTION_ON; |
| 3857 | } |
| 3858 | |
| 3859 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3860 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3861 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3862 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3863 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3864 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3865 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3866 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3867 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3868 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3869 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3870 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3871 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3872 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3873 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3874 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3875 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3876 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3877 | { // acquire lock |
| 3878 | mLock.lock(); |
| 3879 | |
| 3880 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3881 | mLock.unlock(); |
| 3882 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3883 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3884 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3885 | return; // event was consumed by the filter |
| 3886 | } |
| 3887 | |
| 3888 | mLock.lock(); |
| 3889 | } |
| 3890 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3891 | std::unique_ptr<KeyEntry> newEntry = |
| 3892 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3893 | args->displayId, policyFlags, args->action, flags, |
| 3894 | keyCode, args->scanCode, metaState, repeatCount, |
| 3895 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3896 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3897 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3898 | mLock.unlock(); |
| 3899 | } // release lock |
| 3900 | |
| 3901 | if (needWake) { |
| 3902 | mLooper->wake(); |
| 3903 | } |
| 3904 | } |
| 3905 | |
| 3906 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3907 | return mInputFilterEnabled; |
| 3908 | } |
| 3909 | |
| 3910 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3911 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3912 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3913 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 3914 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3915 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 3916 | "yCursorPosition=%f, downTime=%" PRId64, |
| 3917 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3918 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3919 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3920 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 3921 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3922 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 3923 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3924 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3925 | "orientation=%f", |
| 3926 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3927 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3928 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3929 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3930 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3931 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3932 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3933 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3934 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3935 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 3936 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3937 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3938 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3939 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3940 | return; |
| 3941 | } |
| 3942 | |
| 3943 | uint32_t policyFlags = args->policyFlags; |
| 3944 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3945 | |
| 3946 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3947 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3948 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3949 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3950 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3951 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3952 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3953 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3954 | { // acquire lock |
| 3955 | mLock.lock(); |
| 3956 | |
| 3957 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3958 | ui::Transform displayTransform; |
| 3959 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 3960 | displayTransform = it->second.transform; |
| 3961 | } |
| 3962 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3963 | mLock.unlock(); |
| 3964 | |
| 3965 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3966 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3967 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3968 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3969 | displayTransform, args->xPrecision, args->yPrecision, |
| 3970 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3971 | args->downTime, args->eventTime, args->pointerCount, |
| 3972 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3973 | |
| 3974 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3975 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3976 | return; // event was consumed by the filter |
| 3977 | } |
| 3978 | |
| 3979 | mLock.lock(); |
| 3980 | } |
| 3981 | |
| 3982 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3983 | std::unique_ptr<MotionEntry> newEntry = |
| 3984 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 3985 | args->source, args->displayId, policyFlags, |
| 3986 | args->action, args->actionButton, args->flags, |
| 3987 | args->metaState, args->buttonState, |
| 3988 | args->classification, args->edgeFlags, |
| 3989 | args->xPrecision, args->yPrecision, |
| 3990 | args->xCursorPosition, args->yCursorPosition, |
| 3991 | args->downTime, args->pointerCount, |
| 3992 | args->pointerProperties, args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3993 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 3994 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 3995 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 3996 | !mInputFilterEnabled) { |
| 3997 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 3998 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 3999 | } |
| 4000 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4001 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4002 | mLock.unlock(); |
| 4003 | } // release lock |
| 4004 | |
| 4005 | if (needWake) { |
| 4006 | mLooper->wake(); |
| 4007 | } |
| 4008 | } |
| 4009 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4010 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4011 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4012 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4013 | " sensorType=%s", |
| 4014 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4015 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4016 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4017 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4018 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4019 | { // acquire lock |
| 4020 | mLock.lock(); |
| 4021 | |
| 4022 | // Just enqueue a new sensor event. |
| 4023 | std::unique_ptr<SensorEntry> newEntry = |
| 4024 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4025 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4026 | args->sensorType, args->accuracy, |
| 4027 | args->accuracyChanged, args->values); |
| 4028 | |
| 4029 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4030 | mLock.unlock(); |
| 4031 | } // release lock |
| 4032 | |
| 4033 | if (needWake) { |
| 4034 | mLooper->wake(); |
| 4035 | } |
| 4036 | } |
| 4037 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4038 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4039 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4040 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4041 | args->deviceId, args->isOn); |
| 4042 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4043 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4044 | } |
| 4045 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4046 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4047 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4051 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4052 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4053 | "switchMask=0x%08x", |
| 4054 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4055 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4056 | |
| 4057 | uint32_t policyFlags = args->policyFlags; |
| 4058 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4059 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4063 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4064 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4065 | args->deviceId); |
| 4066 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4067 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4068 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4069 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4070 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4071 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4072 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4073 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4074 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4075 | } // release lock |
| 4076 | |
| 4077 | if (needWake) { |
| 4078 | mLooper->wake(); |
| 4079 | } |
| 4080 | } |
| 4081 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4082 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4083 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4084 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4085 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4086 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4087 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4088 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4089 | { // acquire lock |
| 4090 | std::scoped_lock _l(mLock); |
| 4091 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4092 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4093 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4094 | } // release lock |
| 4095 | |
| 4096 | if (needWake) { |
| 4097 | mLooper->wake(); |
| 4098 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4099 | } |
| 4100 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4101 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4102 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4103 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4104 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4105 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 4106 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4107 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
| 4108 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4109 | 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] | 4110 | |
| 4111 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4112 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4113 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4114 | } |
| 4115 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4116 | // 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] | 4117 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4118 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4119 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4120 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4121 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4122 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4123 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4124 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4127 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4128 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4129 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4130 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4131 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4132 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4133 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4134 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4135 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4136 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4137 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4138 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4139 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4140 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4141 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4142 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4143 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4144 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4145 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4146 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4147 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4148 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4149 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4150 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4151 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4152 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4153 | |
| 4154 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4155 | android::base::Timer t; |
| 4156 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4157 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4158 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4159 | std::to_string(t.duration().count()).c_str()); |
| 4160 | } |
| 4161 | } |
| 4162 | |
| 4163 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4164 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4165 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4166 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4167 | incomingKey.getDisplayId(), policyFlags, action, |
| 4168 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4169 | incomingKey.getRepeatCount(), |
| 4170 | incomingKey.getDownTime()); |
| 4171 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4172 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4173 | } |
| 4174 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4175 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4176 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4177 | const int32_t action = motionEvent.getAction(); |
| 4178 | const bool isPointerEvent = |
| 4179 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4180 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4181 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4182 | ? ADISPLAY_ID_DEFAULT |
| 4183 | : event->getDisplayId(); |
| 4184 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4185 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4186 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4187 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4188 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4189 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4193 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4194 | android::base::Timer t; |
| 4195 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4196 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4197 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4198 | std::to_string(t.duration().count()).c_str()); |
| 4199 | } |
| 4200 | } |
| 4201 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4202 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4203 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4204 | } |
| 4205 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4206 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4207 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4208 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4209 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4210 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4211 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4212 | displayId, policyFlags, action, actionButton, |
| 4213 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4214 | motionEvent.getButtonState(), |
| 4215 | motionEvent.getClassification(), |
| 4216 | motionEvent.getEdgeFlags(), |
| 4217 | motionEvent.getXPrecision(), |
| 4218 | motionEvent.getYPrecision(), |
| 4219 | motionEvent.getRawXCursorPosition(), |
| 4220 | motionEvent.getRawYCursorPosition(), |
| 4221 | motionEvent.getDownTime(), uint32_t(pointerCount), |
| 4222 | pointerProperties, samplePointerCoords, |
| 4223 | motionEvent.getXOffset(), |
| 4224 | motionEvent.getYOffset()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4225 | transformMotionEntryForInjectionLocked(*injectedEntry); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4226 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4227 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4228 | sampleEventTimes += 1; |
| 4229 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4230 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4231 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4232 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4233 | displayId, policyFlags, action, actionButton, |
| 4234 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4235 | motionEvent.getButtonState(), |
| 4236 | motionEvent.getClassification(), |
| 4237 | motionEvent.getEdgeFlags(), |
| 4238 | motionEvent.getXPrecision(), |
| 4239 | motionEvent.getYPrecision(), |
| 4240 | motionEvent.getRawXCursorPosition(), |
| 4241 | motionEvent.getRawYCursorPosition(), |
| 4242 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4243 | uint32_t(pointerCount), pointerProperties, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4244 | samplePointerCoords, motionEvent.getXOffset(), |
| 4245 | motionEvent.getYOffset()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4246 | transformMotionEntryForInjectionLocked(*nextInjectedEntry); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4247 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4248 | } |
| 4249 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4250 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4251 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4252 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4253 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4254 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4258 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4259 | injectionState->injectionIsAsync = true; |
| 4260 | } |
| 4261 | |
| 4262 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4263 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4264 | |
| 4265 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4266 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4267 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4268 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4269 | } |
| 4270 | |
| 4271 | mLock.unlock(); |
| 4272 | |
| 4273 | if (needWake) { |
| 4274 | mLooper->wake(); |
| 4275 | } |
| 4276 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4277 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4278 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4279 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4280 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4281 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4282 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4283 | } else { |
| 4284 | for (;;) { |
| 4285 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4286 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4287 | break; |
| 4288 | } |
| 4289 | |
| 4290 | nsecs_t remainingTimeout = endTime - now(); |
| 4291 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4292 | if (DEBUG_INJECTION) { |
| 4293 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4294 | "to become available."); |
| 4295 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4296 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4297 | break; |
| 4298 | } |
| 4299 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4300 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4301 | } |
| 4302 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4303 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4304 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4305 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4306 | if (DEBUG_INJECTION) { |
| 4307 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4308 | injectionState->pendingForegroundDispatches); |
| 4309 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4310 | nsecs_t remainingTimeout = endTime - now(); |
| 4311 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4312 | if (DEBUG_INJECTION) { |
| 4313 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4314 | "dispatches to finish."); |
| 4315 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4316 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4317 | break; |
| 4318 | } |
| 4319 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4320 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4321 | } |
| 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | injectionState->release(); |
| 4326 | } // release lock |
| 4327 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4328 | if (DEBUG_INJECTION) { |
| 4329 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
| 4330 | injectionResult, injectorPid, injectorUid); |
| 4331 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4332 | |
| 4333 | return injectionResult; |
| 4334 | } |
| 4335 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4336 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4337 | std::array<uint8_t, 32> calculatedHmac; |
| 4338 | std::unique_ptr<VerifiedInputEvent> result; |
| 4339 | switch (event.getType()) { |
| 4340 | case AINPUT_EVENT_TYPE_KEY: { |
| 4341 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4342 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4343 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4344 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4345 | break; |
| 4346 | } |
| 4347 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4348 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4349 | VerifiedMotionEvent verifiedMotionEvent = |
| 4350 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4351 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4352 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4353 | break; |
| 4354 | } |
| 4355 | default: { |
| 4356 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4357 | return nullptr; |
| 4358 | } |
| 4359 | } |
| 4360 | if (calculatedHmac == INVALID_HMAC) { |
| 4361 | return nullptr; |
| 4362 | } |
| 4363 | if (calculatedHmac != event.getHmac()) { |
| 4364 | return nullptr; |
| 4365 | } |
| 4366 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4367 | } |
| 4368 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4369 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4370 | return injectorUid == 0 || |
| 4371 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4372 | } |
| 4373 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4374 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4375 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4376 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4377 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4378 | if (DEBUG_INJECTION) { |
| 4379 | ALOGD("Setting input event injection result to %d. " |
| 4380 | "injectorPid=%d, injectorUid=%d", |
| 4381 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
| 4382 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4383 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4384 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4385 | // Log the outcome since the injector did not wait for the injection result. |
| 4386 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4387 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4388 | ALOGV("Asynchronous input event injection succeeded."); |
| 4389 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4390 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4391 | ALOGW("Asynchronous input event injection failed."); |
| 4392 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4393 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4394 | ALOGW("Asynchronous input event injection permission denied."); |
| 4395 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4396 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4397 | ALOGW("Asynchronous input event injection timed out."); |
| 4398 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4399 | case InputEventInjectionResult::PENDING: |
| 4400 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4401 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4402 | } |
| 4403 | } |
| 4404 | |
| 4405 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4406 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4407 | } |
| 4408 | } |
| 4409 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4410 | void InputDispatcher::transformMotionEntryForInjectionLocked(MotionEntry& entry) const { |
| 4411 | const bool isRelativeMouseEvent = isFromSource(entry.source, AINPUT_SOURCE_MOUSE_RELATIVE); |
| 4412 | if (!isRelativeMouseEvent && !isFromSource(entry.source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 4413 | return; |
| 4414 | } |
| 4415 | |
| 4416 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4417 | // display space, so we need to transform the injected events accordingly. |
| 4418 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4419 | if (it == mDisplayInfos.end()) return; |
| 4420 | const auto& transformToDisplay = it->second.transform.inverse(); |
| 4421 | |
| 4422 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 4423 | PointerCoords& pc = entry.pointerCoords[i]; |
| 4424 | const auto xy = isRelativeMouseEvent |
| 4425 | ? transformWithoutTranslation(transformToDisplay, pc.getX(), pc.getY()) |
| 4426 | : transformToDisplay.transform(pc.getXYValue()); |
| 4427 | pc.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 4428 | pc.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
| 4429 | |
| 4430 | // Axes with relative values never represent points on a screen, so they should never have |
| 4431 | // translation applied. If a device does not report relative values, these values are always |
| 4432 | // 0, and will remain unaffected by the following operation. |
| 4433 | const auto rel = |
| 4434 | transformWithoutTranslation(transformToDisplay, |
| 4435 | pc.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 4436 | pc.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)); |
| 4437 | pc.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, rel.x); |
| 4438 | pc.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, rel.y); |
| 4439 | } |
| 4440 | } |
| 4441 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4442 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4443 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4444 | if (injectionState) { |
| 4445 | injectionState->pendingForegroundDispatches += 1; |
| 4446 | } |
| 4447 | } |
| 4448 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4449 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4450 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4451 | if (injectionState) { |
| 4452 | injectionState->pendingForegroundDispatches -= 1; |
| 4453 | |
| 4454 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4455 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4456 | } |
| 4457 | } |
| 4458 | } |
| 4459 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4460 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4461 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4462 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4463 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4464 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4465 | } |
| 4466 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4467 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4468 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4469 | if (windowHandleToken == nullptr) { |
| 4470 | return nullptr; |
| 4471 | } |
| 4472 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4473 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4474 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4475 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4476 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4477 | return windowHandle; |
| 4478 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4479 | } |
| 4480 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4481 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4482 | } |
| 4483 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4484 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4485 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4486 | if (windowHandleToken == nullptr) { |
| 4487 | return nullptr; |
| 4488 | } |
| 4489 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4490 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4491 | if (windowHandle->getToken() == windowHandleToken) { |
| 4492 | return windowHandle; |
| 4493 | } |
| 4494 | } |
| 4495 | return nullptr; |
| 4496 | } |
| 4497 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4498 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4499 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4500 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4501 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4502 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4503 | if (handle->getId() == windowHandle->getId() && |
| 4504 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4505 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4506 | ALOGE("Found window %s in display %" PRId32 |
| 4507 | ", but it should belong to display %" PRId32, |
| 4508 | windowHandle->getName().c_str(), it.first, |
| 4509 | windowHandle->getInfo()->displayId); |
| 4510 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4511 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4512 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4513 | } |
| 4514 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4515 | return nullptr; |
| 4516 | } |
| 4517 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4518 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4519 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4520 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4521 | } |
| 4522 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4523 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4524 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4525 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4526 | windowHandle.getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4527 | if (connection != nullptr && noInputChannel) { |
| 4528 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4529 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4530 | return false; |
| 4531 | } |
| 4532 | |
| 4533 | if (connection == nullptr) { |
| 4534 | if (!noInputChannel) { |
| 4535 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4536 | } |
| 4537 | return false; |
| 4538 | } |
| 4539 | if (!connection->responsive) { |
| 4540 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4541 | return false; |
| 4542 | } |
| 4543 | return true; |
| 4544 | } |
| 4545 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4546 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4547 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4548 | auto connectionIt = mConnectionsByToken.find(token); |
| 4549 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4550 | return nullptr; |
| 4551 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4552 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4553 | } |
| 4554 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4555 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4556 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4557 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4558 | // Remove all handles on a display if there are no windows left. |
| 4559 | mWindowHandlesByDisplay.erase(displayId); |
| 4560 | return; |
| 4561 | } |
| 4562 | |
| 4563 | // Since we compare the pointer of input window handles across window updates, we need |
| 4564 | // 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] | 4565 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4566 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4567 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4568 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4569 | } |
| 4570 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4571 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4572 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4573 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4574 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4575 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4576 | info->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4577 | const bool canReceiveInput = !info->flags.test(WindowInfo::Flag::NOT_TOUCHABLE) || |
| 4578 | !info->flags.test(WindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4579 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4580 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4581 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4582 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4583 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | if (info->displayId != displayId) { |
| 4587 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4588 | handle->getName().c_str(), displayId, info->displayId); |
| 4589 | continue; |
| 4590 | } |
| 4591 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4592 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4593 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4594 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4595 | oldHandle->updateFrom(handle); |
| 4596 | newHandles.push_back(oldHandle); |
| 4597 | } else { |
| 4598 | newHandles.push_back(handle); |
| 4599 | } |
| 4600 | } |
| 4601 | |
| 4602 | // Insert or replace |
| 4603 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4604 | } |
| 4605 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4606 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4607 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4608 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4609 | { // acquire lock |
| 4610 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4611 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4612 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4613 | } |
| 4614 | } |
| 4615 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4616 | mLooper->wake(); |
| 4617 | } |
| 4618 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4619 | /** |
| 4620 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4621 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4622 | * If set an empty list, remove all handles from the specific display. |
| 4623 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4624 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4625 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4626 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4627 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4628 | if (DEBUG_FOCUS) { |
| 4629 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4630 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4631 | windowList += iwh->getName() + " "; |
| 4632 | } |
| 4633 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4634 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4635 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4636 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4637 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4638 | const bool noInputWindow = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4639 | window->getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4640 | if (noInputWindow && window->getToken() != nullptr) { |
| 4641 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4642 | window->getName().c_str()); |
| 4643 | window->releaseChannel(); |
| 4644 | } |
| 4645 | } |
| 4646 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4647 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4648 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4649 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4650 | // Save the old windows' orientation by ID before it gets updated. |
| 4651 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4652 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4653 | oldWindowOrientations.emplace(handle->getId(), |
| 4654 | handle->getInfo()->transform.getOrientation()); |
| 4655 | } |
| 4656 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4657 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4658 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4659 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4660 | if (mLastHoverWindowHandle && |
| 4661 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4662 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4663 | mLastHoverWindowHandle = nullptr; |
| 4664 | } |
| 4665 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4666 | std::optional<FocusResolver::FocusChanges> changes = |
| 4667 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4668 | if (changes) { |
| 4669 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4670 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4671 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4672 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4673 | mTouchStatesByDisplay.find(displayId); |
| 4674 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4675 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4676 | for (size_t i = 0; i < state.windows.size();) { |
| 4677 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4678 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4679 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4680 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4681 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4682 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4683 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4684 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4685 | if (touchedInputChannel != nullptr) { |
| 4686 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4687 | "touched window was removed"); |
| 4688 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4689 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4690 | // well. |
| 4691 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
| 4692 | touchedWindow.windowHandle->getInfo()->hasWallpaper) { |
| 4693 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4694 | if (wallpaper != nullptr) { |
| 4695 | sp<Connection> wallpaperConnection = |
| 4696 | getConnectionLocked(wallpaper->getToken()); |
| 4697 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4698 | options); |
| 4699 | } |
| 4700 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4701 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4702 | state.windows.erase(state.windows.begin() + i); |
| 4703 | } else { |
| 4704 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4705 | } |
| 4706 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4707 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4708 | // 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] | 4709 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4710 | if (mDragState && |
| 4711 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4712 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4713 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4714 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4715 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4716 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame^] | 4717 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4718 | // pointer events if necessary. |
| 4719 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4720 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4721 | if (newWindowHandle != nullptr && |
| 4722 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4723 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4724 | std::shared_ptr<InputChannel> inputChannel = |
| 4725 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4726 | if (inputChannel != nullptr) { |
| 4727 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4728 | "touched window's orientation changed"); |
| 4729 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4730 | } |
| 4731 | } |
| 4732 | } |
| 4733 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4734 | // Release information for windows that are no longer present. |
| 4735 | // This ensures that unused input channels are released promptly. |
| 4736 | // Otherwise, they might stick around until the window handle is destroyed |
| 4737 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4738 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4739 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4740 | if (DEBUG_FOCUS) { |
| 4741 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4742 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4743 | oldWindowHandle->releaseChannel(); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4744 | // To avoid making too many calls into the compat framework, only |
| 4745 | // check for window flags when windows are going away. |
| 4746 | // TODO(b/157929241) : delete this. This is only needed temporarily |
| 4747 | // in order to gather some data about the flag usage |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4748 | if (oldWindowHandle->getInfo()->flags.test(WindowInfo::Flag::SLIPPERY)) { |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4749 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", |
| 4750 | oldWindowHandle->getName().c_str()); |
| 4751 | if (mCompatService != nullptr) { |
| 4752 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, |
| 4753 | oldWindowHandle->getInfo()->ownerUid); |
| 4754 | } |
| 4755 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4756 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4757 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4761 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4762 | if (DEBUG_FOCUS) { |
| 4763 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4764 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4765 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4766 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4767 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4768 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4769 | } // release lock |
| 4770 | |
| 4771 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4772 | mLooper->wake(); |
| 4773 | } |
| 4774 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4775 | void InputDispatcher::setFocusedApplicationLocked( |
| 4776 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4777 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4778 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4779 | |
| 4780 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4781 | return; // This application is already focused. No need to wake up or change anything. |
| 4782 | } |
| 4783 | |
| 4784 | // Set the new application handle. |
| 4785 | if (inputApplicationHandle != nullptr) { |
| 4786 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4787 | } else { |
| 4788 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4789 | } |
| 4790 | |
| 4791 | // No matter what the old focused application was, stop waiting on it because it is |
| 4792 | // no longer focused. |
| 4793 | resetNoFocusedWindowTimeoutLocked(); |
| 4794 | } |
| 4795 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4796 | /** |
| 4797 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4798 | * the display not specified. |
| 4799 | * |
| 4800 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4801 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4802 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4803 | * display. The display-specified events won't be affected. |
| 4804 | */ |
| 4805 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4806 | if (DEBUG_FOCUS) { |
| 4807 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4808 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4809 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4810 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4811 | |
| 4812 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4813 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4814 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4815 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4816 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4817 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4818 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4819 | CancelationOptions |
| 4820 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4821 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4822 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4823 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4824 | } |
| 4825 | } |
| 4826 | mFocusedDisplayId = displayId; |
| 4827 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4828 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4829 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4830 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4831 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4832 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4833 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4834 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4835 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4836 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4837 | } |
| 4838 | } |
| 4839 | } |
| 4840 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4841 | if (DEBUG_FOCUS) { |
| 4842 | logDispatchStateLocked(); |
| 4843 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4844 | } // release lock |
| 4845 | |
| 4846 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4847 | mLooper->wake(); |
| 4848 | } |
| 4849 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4850 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4851 | if (DEBUG_FOCUS) { |
| 4852 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4853 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4854 | |
| 4855 | bool changed; |
| 4856 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4857 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4858 | |
| 4859 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4860 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4861 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4862 | } |
| 4863 | |
| 4864 | if (mDispatchEnabled && !enabled) { |
| 4865 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4866 | } |
| 4867 | |
| 4868 | mDispatchEnabled = enabled; |
| 4869 | mDispatchFrozen = frozen; |
| 4870 | changed = true; |
| 4871 | } else { |
| 4872 | changed = false; |
| 4873 | } |
| 4874 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4875 | if (DEBUG_FOCUS) { |
| 4876 | logDispatchStateLocked(); |
| 4877 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4878 | } // release lock |
| 4879 | |
| 4880 | if (changed) { |
| 4881 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4882 | mLooper->wake(); |
| 4883 | } |
| 4884 | } |
| 4885 | |
| 4886 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4887 | if (DEBUG_FOCUS) { |
| 4888 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4889 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4890 | |
| 4891 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4892 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4893 | |
| 4894 | if (mInputFilterEnabled == enabled) { |
| 4895 | return; |
| 4896 | } |
| 4897 | |
| 4898 | mInputFilterEnabled = enabled; |
| 4899 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4900 | } // release lock |
| 4901 | |
| 4902 | // Wake up poll loop since there might be work to do to drop everything. |
| 4903 | mLooper->wake(); |
| 4904 | } |
| 4905 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4906 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4907 | bool needWake = false; |
| 4908 | { |
| 4909 | std::scoped_lock lock(mLock); |
| 4910 | if (mInTouchMode == inTouchMode) { |
| 4911 | return; |
| 4912 | } |
| 4913 | if (DEBUG_TOUCH_MODE) { |
| 4914 | ALOGD("Request to change touch mode from %s to %s", toString(mInTouchMode), |
| 4915 | toString(inTouchMode)); |
| 4916 | // TODO(b/198487159): Also print the current last interacted apps. |
| 4917 | } |
| 4918 | |
| 4919 | // TODO(b/198499018): Store touch mode per display. |
| 4920 | mInTouchMode = inTouchMode; |
| 4921 | |
| 4922 | // TODO(b/198487159): Enforce that only last interacted apps can change touch mode. |
| 4923 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 4924 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4925 | } // release lock |
| 4926 | |
| 4927 | if (needWake) { |
| 4928 | mLooper->wake(); |
| 4929 | } |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4930 | } |
| 4931 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4932 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4933 | if (opacity < 0 || opacity > 1) { |
| 4934 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4935 | return; |
| 4936 | } |
| 4937 | |
| 4938 | std::scoped_lock lock(mLock); |
| 4939 | mMaximumObscuringOpacityForTouch = opacity; |
| 4940 | } |
| 4941 | |
| 4942 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4943 | std::scoped_lock lock(mLock); |
| 4944 | mBlockUntrustedTouchesMode = mode; |
| 4945 | } |
| 4946 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4947 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 4948 | const sp<IBinder>& token) { |
| 4949 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 4950 | for (TouchedWindow& w : state.windows) { |
| 4951 | if (w.windowHandle->getToken() == token) { |
| 4952 | return std::make_pair(&state, &w); |
| 4953 | } |
| 4954 | } |
| 4955 | } |
| 4956 | return std::make_pair(nullptr, nullptr); |
| 4957 | } |
| 4958 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4959 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 4960 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4961 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4962 | if (DEBUG_FOCUS) { |
| 4963 | ALOGD("Trivial transfer to same window."); |
| 4964 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4965 | return true; |
| 4966 | } |
| 4967 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4968 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4969 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4970 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4971 | // Find the target touch state and touched window by fromToken. |
| 4972 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 4973 | if (state == nullptr || touchedWindow == nullptr) { |
| 4974 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4975 | return false; |
| 4976 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4977 | |
| 4978 | const int32_t displayId = state->displayId; |
| 4979 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 4980 | if (toWindowHandle == nullptr) { |
| 4981 | ALOGW("Cannot transfer focus because to window not found."); |
| 4982 | return false; |
| 4983 | } |
| 4984 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4985 | if (DEBUG_FOCUS) { |
| 4986 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4987 | touchedWindow->windowHandle->getName().c_str(), |
| 4988 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4989 | } |
| 4990 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4991 | // Erase old window. |
| 4992 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 4993 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 4994 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4995 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4996 | // Add new window. |
| 4997 | int32_t newTargetFlags = oldTargetFlags & |
| 4998 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4999 | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5000 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5001 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5002 | // Store the dragging window. |
| 5003 | if (isDragDrop) { |
| 5004 | mDragState = std::make_unique<DragState>(toWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5005 | } |
| 5006 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5007 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5008 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5009 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5010 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5011 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5012 | CancelationOptions |
| 5013 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5014 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5015 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5016 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5017 | } |
| 5018 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5019 | if (DEBUG_FOCUS) { |
| 5020 | logDispatchStateLocked(); |
| 5021 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5022 | } // release lock |
| 5023 | |
| 5024 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5025 | mLooper->wake(); |
| 5026 | return true; |
| 5027 | } |
| 5028 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5029 | // Binder call |
| 5030 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 5031 | sp<IBinder> fromToken; |
| 5032 | { // acquire lock |
| 5033 | std::scoped_lock _l(mLock); |
| 5034 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5035 | auto it = std::find_if(mTouchStatesByDisplay.begin(), mTouchStatesByDisplay.end(), |
| 5036 | [](const auto& pair) { return pair.second.windows.size() == 1; }); |
| 5037 | if (it == mTouchStatesByDisplay.end()) { |
| 5038 | ALOGW("Cannot transfer touch state because there is no exact window being touched"); |
| 5039 | return false; |
| 5040 | } |
| 5041 | const int32_t displayId = it->first; |
| 5042 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5043 | if (toWindowHandle == nullptr) { |
| 5044 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 5045 | return false; |
| 5046 | } |
| 5047 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5048 | TouchState& state = it->second; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5049 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 5050 | fromToken = touchedWindow.windowHandle->getToken(); |
| 5051 | } // release lock |
| 5052 | |
| 5053 | return transferTouchFocus(fromToken, destChannelToken); |
| 5054 | } |
| 5055 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5056 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5057 | if (DEBUG_FOCUS) { |
| 5058 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5059 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5060 | |
| 5061 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5062 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5063 | |
| 5064 | resetKeyRepeatLocked(); |
| 5065 | releasePendingEventLocked(); |
| 5066 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5067 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5068 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5069 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5070 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5071 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5072 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5073 | } |
| 5074 | |
| 5075 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5076 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5077 | dumpDispatchStateLocked(dump); |
| 5078 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5079 | std::istringstream stream(dump); |
| 5080 | std::string line; |
| 5081 | |
| 5082 | while (std::getline(stream, line, '\n')) { |
| 5083 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5084 | } |
| 5085 | } |
| 5086 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5087 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5088 | std::string dump; |
| 5089 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5090 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5091 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5092 | |
| 5093 | std::string windowName = "None"; |
| 5094 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5095 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5096 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5097 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5098 | : "token has capture without window"; |
| 5099 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5100 | 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] | 5101 | |
| 5102 | return dump; |
| 5103 | } |
| 5104 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5105 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5106 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5107 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5108 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5109 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5110 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5111 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5112 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5113 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5114 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5115 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5116 | const std::chrono::duration timeout = |
| 5117 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5118 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5119 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5120 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5121 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5122 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5123 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5124 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5125 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5126 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5127 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5128 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5129 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5130 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5131 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5132 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5133 | 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] | 5134 | state.displayId, toString(state.down), toString(state.split), |
| 5135 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5136 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5137 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5138 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5139 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5140 | dump += StringPrintf(INDENT4 |
| 5141 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5142 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5143 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5144 | } |
| 5145 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5146 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5147 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5148 | } |
| 5149 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5150 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5151 | } |
| 5152 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5153 | if (mDragState) { |
| 5154 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5155 | mDragState->dump(dump, INDENT2); |
| 5156 | } |
| 5157 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5158 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5159 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5160 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5161 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5162 | const auto& displayInfo = it->second; |
| 5163 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5164 | displayInfo.logicalHeight); |
| 5165 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5166 | } else { |
| 5167 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5168 | } |
| 5169 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5170 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5171 | dump += INDENT2 "Windows:\n"; |
| 5172 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5173 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5174 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5175 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5176 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5177 | "paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5178 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5179 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5180 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5181 | "applicationInfo.name=%s, " |
| 5182 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5183 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5184 | i, windowInfo->name.c_str(), windowInfo->id, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5185 | windowInfo->displayId, toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5186 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5187 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5188 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 5189 | windowInfo->flags.string().c_str(), |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 5190 | ftl::enum_string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5191 | windowInfo->frameLeft, windowInfo->frameTop, |
| 5192 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5193 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5194 | windowInfo->applicationInfo.name.c_str(), |
| 5195 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5196 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5197 | dump += StringPrintf(", inputFeatures=%s", |
| 5198 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5199 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5200 | "ms, trustedOverlay=%s, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5201 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5202 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5203 | millis(windowInfo->dispatchingTimeout), |
| 5204 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5205 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5206 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5207 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5208 | } |
| 5209 | } else { |
| 5210 | dump += INDENT2 "Windows: <none>\n"; |
| 5211 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5212 | } |
| 5213 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5214 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5215 | } |
| 5216 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5217 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5218 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5219 | const std::vector<Monitor>& monitors = it.second; |
| 5220 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5221 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5222 | } |
| 5223 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5224 | const std::vector<Monitor>& monitors = it.second; |
| 5225 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5226 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5227 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5228 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5229 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5230 | } |
| 5231 | |
| 5232 | nsecs_t currentTime = now(); |
| 5233 | |
| 5234 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5235 | if (!mRecentQueue.empty()) { |
| 5236 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5237 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5238 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5239 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5240 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5241 | } |
| 5242 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5243 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5244 | } |
| 5245 | |
| 5246 | // Dump event currently being dispatched. |
| 5247 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5248 | dump += INDENT "PendingEvent:\n"; |
| 5249 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5250 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5251 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5252 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5253 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5254 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5255 | } |
| 5256 | |
| 5257 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5258 | if (!mInboundQueue.empty()) { |
| 5259 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5260 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5261 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5262 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5263 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5264 | } |
| 5265 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5266 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5267 | } |
| 5268 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5269 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5270 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5271 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5272 | const KeyReplacement& replacement = pair.first; |
| 5273 | int32_t newKeyCode = pair.second; |
| 5274 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5275 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5276 | } |
| 5277 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5278 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5279 | } |
| 5280 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5281 | if (!mCommandQueue.empty()) { |
| 5282 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5283 | } else { |
| 5284 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5285 | } |
| 5286 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5287 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5288 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5289 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5290 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5291 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5292 | connection->inputChannel->getFd().get(), |
| 5293 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5294 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5295 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5296 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5297 | if (!connection->outboundQueue.empty()) { |
| 5298 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5299 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5300 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5301 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5302 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5303 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5304 | } |
| 5305 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5306 | if (!connection->waitQueue.empty()) { |
| 5307 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5308 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5309 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5310 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5311 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5312 | } |
| 5313 | } |
| 5314 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5315 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5316 | } |
| 5317 | |
| 5318 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5319 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5320 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5321 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5322 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5323 | } |
| 5324 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5325 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5326 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5327 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5328 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5329 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5330 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5331 | } |
| 5332 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5333 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5334 | const size_t numMonitors = monitors.size(); |
| 5335 | for (size_t i = 0; i < numMonitors; i++) { |
| 5336 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5337 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5338 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5339 | dump += "\n"; |
| 5340 | } |
| 5341 | } |
| 5342 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5343 | class LooperEventCallback : public LooperCallback { |
| 5344 | public: |
| 5345 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5346 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5347 | |
| 5348 | private: |
| 5349 | std::function<int(int events)> mCallback; |
| 5350 | }; |
| 5351 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5352 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5353 | if (DEBUG_CHANNEL_CREATION) { |
| 5354 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5355 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5356 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5357 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5358 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5359 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5360 | |
| 5361 | if (result) { |
| 5362 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5363 | } |
| 5364 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5365 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5366 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5367 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5368 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5369 | sp<Connection> connection = |
| 5370 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5371 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5372 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5373 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5374 | } |
| 5375 | mConnectionsByToken.emplace(token, connection); |
| 5376 | |
| 5377 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5378 | this, std::placeholders::_1, token); |
| 5379 | |
| 5380 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5381 | } // release lock |
| 5382 | |
| 5383 | // Wake the looper because some connections have changed. |
| 5384 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5385 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5386 | } |
| 5387 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5388 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5389 | bool isGestureMonitor, |
| 5390 | const std::string& name, |
| 5391 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5392 | std::shared_ptr<InputChannel> serverChannel; |
| 5393 | std::unique_ptr<InputChannel> clientChannel; |
| 5394 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5395 | if (result) { |
| 5396 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5397 | } |
| 5398 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5399 | { // acquire lock |
| 5400 | std::scoped_lock _l(mLock); |
| 5401 | |
| 5402 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5403 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5404 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5405 | } |
| 5406 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5407 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5408 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5409 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5410 | |
| 5411 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5412 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5413 | } |
| 5414 | mConnectionsByToken.emplace(token, connection); |
| 5415 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5416 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5417 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5418 | auto& monitorsByDisplay = |
| 5419 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5420 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5421 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5422 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5423 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), |
| 5424 | displayId, toString(isGestureMonitor), pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5425 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5426 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5427 | // Wake the looper because some connections have changed. |
| 5428 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5429 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5430 | } |
| 5431 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5432 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5433 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5434 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5435 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5436 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5437 | if (status) { |
| 5438 | return status; |
| 5439 | } |
| 5440 | } // release lock |
| 5441 | |
| 5442 | // Wake the poll loop because removing the connection may have changed the current |
| 5443 | // synchronization state. |
| 5444 | mLooper->wake(); |
| 5445 | return OK; |
| 5446 | } |
| 5447 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5448 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5449 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5450 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5451 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5452 | // 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] | 5453 | return BAD_VALUE; |
| 5454 | } |
| 5455 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5456 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5457 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5458 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5459 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5460 | } |
| 5461 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5462 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5463 | |
| 5464 | nsecs_t currentTime = now(); |
| 5465 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5466 | |
| 5467 | connection->status = Connection::STATUS_ZOMBIE; |
| 5468 | return OK; |
| 5469 | } |
| 5470 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5471 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5472 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5473 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5474 | } |
| 5475 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5476 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5477 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5478 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5479 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5480 | std::vector<Monitor>& monitors = it->second; |
| 5481 | const size_t numMonitors = monitors.size(); |
| 5482 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5483 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5484 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5485 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5486 | monitors.erase(monitors.begin() + i); |
| 5487 | break; |
| 5488 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5489 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5490 | if (monitors.empty()) { |
| 5491 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5492 | } else { |
| 5493 | ++it; |
| 5494 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5495 | } |
| 5496 | } |
| 5497 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5498 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5499 | { // acquire lock |
| 5500 | std::scoped_lock _l(mLock); |
| 5501 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 5502 | |
| 5503 | if (!foundDisplayId) { |
| 5504 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 5505 | return BAD_VALUE; |
| 5506 | } |
| 5507 | int32_t displayId = foundDisplayId.value(); |
| 5508 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5509 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5510 | mTouchStatesByDisplay.find(displayId); |
| 5511 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5512 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5513 | return BAD_VALUE; |
| 5514 | } |
| 5515 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5516 | TouchState& state = stateIt->second; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5517 | std::shared_ptr<InputChannel> requestingChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5518 | std::optional<int32_t> foundDeviceId; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 5519 | for (const auto& monitor : state.gestureMonitors) { |
| 5520 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 5521 | requestingChannel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5522 | foundDeviceId = state.deviceId; |
| 5523 | } |
| 5524 | } |
| 5525 | if (!foundDeviceId || !state.down) { |
| 5526 | 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] | 5527 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5528 | return BAD_VALUE; |
| 5529 | } |
| 5530 | int32_t deviceId = foundDeviceId.value(); |
| 5531 | |
| 5532 | // Send cancel events to all the input channels we're stealing from. |
| 5533 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5534 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5535 | options.deviceId = deviceId; |
| 5536 | options.displayId = displayId; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5537 | std::string canceledWindows = "["; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5538 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5539 | std::shared_ptr<InputChannel> channel = |
| 5540 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5541 | if (channel != nullptr) { |
| 5542 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5543 | canceledWindows += channel->getName() + ", "; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5544 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5545 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5546 | canceledWindows += "]"; |
| 5547 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5548 | canceledWindows.c_str()); |
| 5549 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5550 | // Then clear the current touch state so we stop dispatching to them as well. |
| 5551 | state.filterNonMonitors(); |
| 5552 | } |
| 5553 | return OK; |
| 5554 | } |
| 5555 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5556 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5557 | { // acquire lock |
| 5558 | std::scoped_lock _l(mLock); |
| 5559 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5560 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5561 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5562 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5563 | : "token without window"); |
| 5564 | } |
| 5565 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5566 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5567 | if (focusedToken != windowToken) { |
| 5568 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5569 | enabled ? "enable" : "disable"); |
| 5570 | return; |
| 5571 | } |
| 5572 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5573 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5574 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5575 | "window has %s requested pointer capture.", |
| 5576 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5577 | return; |
| 5578 | } |
| 5579 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5580 | setPointerCaptureLocked(enabled); |
| 5581 | } // release lock |
| 5582 | |
| 5583 | // Wake the thread to process command entries. |
| 5584 | mLooper->wake(); |
| 5585 | } |
| 5586 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5587 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5588 | const sp<IBinder>& token) { |
| 5589 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5590 | const std::vector<Monitor>& monitors = it.second; |
| 5591 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5592 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5593 | return it.first; |
| 5594 | } |
| 5595 | } |
| 5596 | } |
| 5597 | return std::nullopt; |
| 5598 | } |
| 5599 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5600 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5601 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5602 | if (gesturePid.has_value()) { |
| 5603 | return gesturePid; |
| 5604 | } |
| 5605 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5606 | } |
| 5607 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5608 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5609 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5610 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5611 | } |
| 5612 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5613 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5614 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5615 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5616 | } |
| 5617 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5618 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5619 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5620 | } |
| 5621 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5622 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5623 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5624 | if (connection == nullptr) { |
| 5625 | return "<nullptr>"; |
| 5626 | } |
| 5627 | return connection->getInputChannelName(); |
| 5628 | } |
| 5629 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5630 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5631 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5632 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5633 | } |
| 5634 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5635 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5636 | const sp<Connection>& connection, uint32_t seq, |
| 5637 | bool handled, nsecs_t consumeTime) { |
| 5638 | // Handle post-event policy actions. |
| 5639 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5640 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5641 | return; |
| 5642 | } |
| 5643 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5644 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5645 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5646 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5647 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5648 | } |
| 5649 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5650 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5651 | connection->inputChannel->getConnectionToken(), |
| 5652 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5653 | } |
| 5654 | |
| 5655 | bool restartEvent; |
| 5656 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5657 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5658 | restartEvent = |
| 5659 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5660 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5661 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5662 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5663 | handled); |
| 5664 | } else { |
| 5665 | restartEvent = false; |
| 5666 | } |
| 5667 | |
| 5668 | // Dequeue the event and start the next cycle. |
| 5669 | // Because the lock might have been released, it is possible that the |
| 5670 | // contents of the wait queue to have been drained, so we need to double-check |
| 5671 | // a few things. |
| 5672 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5673 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5674 | dispatchEntry = *dispatchEntryIt; |
| 5675 | connection->waitQueue.erase(dispatchEntryIt); |
| 5676 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5677 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5678 | if (!connection->responsive) { |
| 5679 | connection->responsive = isConnectionResponsive(*connection); |
| 5680 | if (connection->responsive) { |
| 5681 | // The connection was unresponsive, and now it's responsive. |
| 5682 | processConnectionResponsiveLocked(*connection); |
| 5683 | } |
| 5684 | } |
| 5685 | traceWaitQueueLength(*connection); |
| 5686 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
| 5687 | connection->outboundQueue.push_front(dispatchEntry); |
| 5688 | traceOutboundQueueLength(*connection); |
| 5689 | } else { |
| 5690 | releaseDispatchEntry(dispatchEntry); |
| 5691 | } |
| 5692 | } |
| 5693 | |
| 5694 | // Start the next dispatch cycle for this connection. |
| 5695 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5696 | } |
| 5697 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5698 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5699 | const sp<IBinder>& newToken) { |
| 5700 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5701 | scoped_unlock unlock(mLock); |
| 5702 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5703 | }; |
| 5704 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5705 | } |
| 5706 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5707 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5708 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5709 | scoped_unlock unlock(mLock); |
| 5710 | mPolicy->notifyDropWindow(token, x, y); |
| 5711 | }; |
| 5712 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5713 | } |
| 5714 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5715 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5716 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5717 | scoped_unlock unlock(mLock); |
| 5718 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5719 | }; |
| 5720 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5721 | } |
| 5722 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5723 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5724 | if (connection == nullptr) { |
| 5725 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5726 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5727 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5728 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5729 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5730 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5731 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5732 | return; |
| 5733 | } |
| 5734 | /** |
| 5735 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5736 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5737 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5738 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5739 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5740 | * most useful. |
| 5741 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5742 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5743 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5744 | std::string reason = |
| 5745 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5746 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5747 | ns2ms(currentWait), |
| 5748 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5749 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5750 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5751 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5752 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5753 | |
| 5754 | // Stop waking up for events on this connection, it is already unresponsive |
| 5755 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5756 | } |
| 5757 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5758 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5759 | std::string reason = |
| 5760 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5761 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5762 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5763 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5764 | scoped_unlock unlock(mLock); |
| 5765 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5766 | }; |
| 5767 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5768 | } |
| 5769 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5770 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5771 | const std::string& reason) { |
| 5772 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5773 | updateLastAnrStateLocked(windowLabel, reason); |
| 5774 | } |
| 5775 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5776 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5777 | const std::string& reason) { |
| 5778 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5779 | updateLastAnrStateLocked(windowLabel, reason); |
| 5780 | } |
| 5781 | |
| 5782 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5783 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5784 | // 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] | 5785 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5786 | struct tm tm; |
| 5787 | localtime_r(&t, &tm); |
| 5788 | char timestr[64]; |
| 5789 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5790 | mLastAnrState.clear(); |
| 5791 | mLastAnrState += INDENT "ANR:\n"; |
| 5792 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5793 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5794 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5795 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5796 | } |
| 5797 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5798 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5799 | KeyEntry& entry) { |
| 5800 | const KeyEvent event = createKeyEvent(entry); |
| 5801 | nsecs_t delay = 0; |
| 5802 | { // release lock |
| 5803 | scoped_unlock unlock(mLock); |
| 5804 | android::base::Timer t; |
| 5805 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5806 | entry.policyFlags); |
| 5807 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5808 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5809 | std::to_string(t.duration().count()).c_str()); |
| 5810 | } |
| 5811 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5812 | |
| 5813 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5814 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5815 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5816 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5817 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5818 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5819 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5820 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5821 | } |
| 5822 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5823 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5824 | auto command = [this, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5825 | scoped_unlock unlock(mLock); |
| 5826 | mPolicy->notifyMonitorUnresponsive(pid, reason); |
| 5827 | }; |
| 5828 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5831 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5832 | std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5833 | auto command = [this, token, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5834 | scoped_unlock unlock(mLock); |
| 5835 | mPolicy->notifyWindowUnresponsive(token, reason); |
| 5836 | }; |
| 5837 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5838 | } |
| 5839 | |
| 5840 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5841 | auto command = [this, pid]() REQUIRES(mLock) { |
| 5842 | scoped_unlock unlock(mLock); |
| 5843 | mPolicy->notifyMonitorResponsive(pid); |
| 5844 | }; |
| 5845 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5846 | } |
| 5847 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5848 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken) { |
| 5849 | auto command = [this, connectionToken]() REQUIRES(mLock) { |
| 5850 | scoped_unlock unlock(mLock); |
| 5851 | mPolicy->notifyWindowResponsive(connectionToken); |
| 5852 | }; |
| 5853 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5854 | } |
| 5855 | |
| 5856 | /** |
| 5857 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5858 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5859 | * command entry to the command queue. |
| 5860 | */ |
| 5861 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5862 | std::string reason) { |
| 5863 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5864 | if (connection.monitor) { |
| 5865 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5866 | reason.c_str()); |
| 5867 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5868 | if (!pid.has_value()) { |
| 5869 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5870 | connection.inputChannel->getName().c_str()); |
| 5871 | return; |
| 5872 | } |
| 5873 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5874 | return; |
| 5875 | } |
| 5876 | // If not a monitor, must be a window |
| 5877 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5878 | reason.c_str()); |
| 5879 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5880 | } |
| 5881 | |
| 5882 | /** |
| 5883 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5884 | */ |
| 5885 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5886 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5887 | if (connection.monitor) { |
| 5888 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5889 | if (!pid.has_value()) { |
| 5890 | ALOGE("Could not find responsive monitor for connection %s", |
| 5891 | connection.inputChannel->getName().c_str()); |
| 5892 | return; |
| 5893 | } |
| 5894 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5895 | return; |
| 5896 | } |
| 5897 | // If not a monitor, must be a window |
| 5898 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5899 | } |
| 5900 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5901 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5902 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5903 | KeyEntry& keyEntry, bool handled) { |
| 5904 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5905 | if (!handled) { |
| 5906 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5907 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5908 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5909 | return false; |
| 5910 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5911 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5912 | // Get the fallback key state. |
| 5913 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5914 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5915 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5916 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5917 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5918 | } |
| 5919 | |
| 5920 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5921 | // If the application handles the original key for which we previously |
| 5922 | // generated a fallback or if the window is not a foreground window, |
| 5923 | // then cancel the associated fallback key, if any. |
| 5924 | if (fallbackKeyCode != -1) { |
| 5925 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5926 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5927 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 5928 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5929 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 5930 | keyEntry.policyFlags); |
| 5931 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5932 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5933 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5934 | |
| 5935 | mLock.unlock(); |
| 5936 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5937 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5938 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5939 | |
| 5940 | mLock.lock(); |
| 5941 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5942 | // Cancel the fallback key. |
| 5943 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5944 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5945 | "application handled the original non-fallback key " |
| 5946 | "or is no longer a foreground target, " |
| 5947 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5948 | options.keyCode = fallbackKeyCode; |
| 5949 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5950 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5951 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5952 | } |
| 5953 | } else { |
| 5954 | // If the application did not handle a non-fallback key, first check |
| 5955 | // that we are in a good state to perform unhandled key event processing |
| 5956 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5957 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5958 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5959 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5960 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 5961 | "since this is not an initial down. " |
| 5962 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5963 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 5964 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5965 | return false; |
| 5966 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5967 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5968 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5969 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5970 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 5971 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5972 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 5973 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5974 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5975 | |
| 5976 | mLock.unlock(); |
| 5977 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5978 | bool fallback = |
| 5979 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5980 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5981 | |
| 5982 | mLock.lock(); |
| 5983 | |
| 5984 | if (connection->status != Connection::STATUS_NORMAL) { |
| 5985 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5986 | return false; |
| 5987 | } |
| 5988 | |
| 5989 | // Latch the fallback keycode for this key on an initial down. |
| 5990 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5991 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5992 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5993 | fallbackKeyCode = event.getKeyCode(); |
| 5994 | } else { |
| 5995 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5996 | } |
| 5997 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5998 | } |
| 5999 | |
| 6000 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6001 | |
| 6002 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6003 | // We will continue to dispatch the key to the policy but we will no |
| 6004 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6005 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6006 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6007 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6008 | if (fallback) { |
| 6009 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6010 | "as a fallback for %d, but on the DOWN it had requested " |
| 6011 | "to send %d instead. Fallback canceled.", |
| 6012 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6013 | } else { |
| 6014 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6015 | "but on the DOWN it had requested to send %d. " |
| 6016 | "Fallback canceled.", |
| 6017 | originalKeyCode, fallbackKeyCode); |
| 6018 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6019 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6020 | |
| 6021 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6022 | "canceling fallback, policy no longer desires it"); |
| 6023 | options.keyCode = fallbackKeyCode; |
| 6024 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6025 | |
| 6026 | fallback = false; |
| 6027 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6028 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6029 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6030 | } |
| 6031 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6032 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6033 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6034 | { |
| 6035 | std::string msg; |
| 6036 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6037 | connection->inputState.getFallbackKeys(); |
| 6038 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6039 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6040 | } |
| 6041 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6042 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6043 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6044 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6045 | |
| 6046 | if (fallback) { |
| 6047 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6048 | keyEntry.eventTime = event.getEventTime(); |
| 6049 | keyEntry.deviceId = event.getDeviceId(); |
| 6050 | keyEntry.source = event.getSource(); |
| 6051 | keyEntry.displayId = event.getDisplayId(); |
| 6052 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6053 | keyEntry.keyCode = fallbackKeyCode; |
| 6054 | keyEntry.scanCode = event.getScanCode(); |
| 6055 | keyEntry.metaState = event.getMetaState(); |
| 6056 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6057 | keyEntry.downTime = event.getDownTime(); |
| 6058 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6059 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6060 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6061 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6062 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6063 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6064 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6065 | return true; // restart the event |
| 6066 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6067 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6068 | ALOGD("Unhandled key event: No fallback key."); |
| 6069 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6070 | |
| 6071 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6072 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6073 | } |
| 6074 | } |
| 6075 | return false; |
| 6076 | } |
| 6077 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6078 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6079 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6080 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6081 | return false; |
| 6082 | } |
| 6083 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6084 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6085 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6086 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6087 | } |
| 6088 | } |
| 6089 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6090 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6091 | if (ATRACE_ENABLED()) { |
| 6092 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6093 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6094 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6095 | } |
| 6096 | } |
| 6097 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6098 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6099 | if (ATRACE_ENABLED()) { |
| 6100 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6101 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6102 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6103 | } |
| 6104 | } |
| 6105 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6106 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6107 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6108 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6109 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6110 | dumpDispatchStateLocked(dump); |
| 6111 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6112 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6113 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6114 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6115 | } |
| 6116 | } |
| 6117 | |
| 6118 | void InputDispatcher::monitor() { |
| 6119 | // 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] | 6120 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6121 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6122 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6123 | } |
| 6124 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6125 | /** |
| 6126 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6127 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6128 | * this method can be safely called from any thread, as long as you've ensured that |
| 6129 | * the work you are interested in completing has already been queued. |
| 6130 | */ |
| 6131 | bool InputDispatcher::waitForIdle() { |
| 6132 | /** |
| 6133 | * Timeout should represent the longest possible time that a device might spend processing |
| 6134 | * events and commands. |
| 6135 | */ |
| 6136 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6137 | std::unique_lock lock(mLock); |
| 6138 | mLooper->wake(); |
| 6139 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6140 | return result == std::cv_status::no_timeout; |
| 6141 | } |
| 6142 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6143 | /** |
| 6144 | * Sets focus to the window identified by the token. This must be called |
| 6145 | * after updating any input window handles. |
| 6146 | * |
| 6147 | * Params: |
| 6148 | * request.token - input channel token used to identify the window that should gain focus. |
| 6149 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6150 | * specified token does not match the currently focused window, this request will be dropped. |
| 6151 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6152 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6153 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6154 | * when requesting the focus change. This determines which request gets |
| 6155 | * precedence if there is a focus change request from another source such as pointer down. |
| 6156 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6157 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6158 | { // acquire lock |
| 6159 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6160 | std::optional<FocusResolver::FocusChanges> changes = |
| 6161 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6162 | if (changes) { |
| 6163 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6164 | } |
| 6165 | } // release lock |
| 6166 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6167 | mLooper->wake(); |
| 6168 | } |
| 6169 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6170 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6171 | if (changes.oldFocus) { |
| 6172 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6173 | if (focusedInputChannel) { |
| 6174 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6175 | "focus left window"); |
| 6176 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6177 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6178 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6179 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6180 | if (changes.newFocus) { |
| 6181 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6182 | } |
| 6183 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6184 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6185 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6186 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6187 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6188 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6189 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6190 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6191 | // front. |
| 6192 | disablePointerCaptureForcedLocked(); |
| 6193 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6194 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6195 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6196 | } |
| 6197 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6198 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6199 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6200 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6201 | return; |
| 6202 | } |
| 6203 | |
| 6204 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6205 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6206 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6207 | setPointerCaptureLocked(false); |
| 6208 | } |
| 6209 | |
| 6210 | if (!mWindowTokenWithPointerCapture) { |
| 6211 | // No need to send capture changes because no window has capture. |
| 6212 | return; |
| 6213 | } |
| 6214 | |
| 6215 | if (mPendingEvent != nullptr) { |
| 6216 | // Move the pending event to the front of the queue. This will give the chance |
| 6217 | // for the pending event to be dropped if it is a captured event. |
| 6218 | mInboundQueue.push_front(mPendingEvent); |
| 6219 | mPendingEvent = nullptr; |
| 6220 | } |
| 6221 | |
| 6222 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6223 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6224 | mInboundQueue.push_front(std::move(entry)); |
| 6225 | } |
| 6226 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6227 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6228 | mCurrentPointerCaptureRequest.enable = enable; |
| 6229 | mCurrentPointerCaptureRequest.seq++; |
| 6230 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6231 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6232 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6233 | }; |
| 6234 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6235 | } |
| 6236 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6237 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6238 | { // acquire lock |
| 6239 | std::scoped_lock _l(mLock); |
| 6240 | // Set an empty list to remove all handles from the specific display. |
| 6241 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6242 | setFocusedApplicationLocked(displayId, nullptr); |
| 6243 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6244 | // have been removed for the removed display. |
| 6245 | mFocusResolver.displayRemoved(displayId); |
| 6246 | } // release lock |
| 6247 | |
| 6248 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6249 | mLooper->wake(); |
| 6250 | } |
| 6251 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6252 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6253 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6254 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6255 | // more convenient parsing. |
| 6256 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6257 | for (const auto& info : windowInfos) { |
| 6258 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6259 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6260 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6261 | |
| 6262 | { // acquire lock |
| 6263 | std::scoped_lock _l(mLock); |
| 6264 | mDisplayInfos.clear(); |
| 6265 | for (const auto& displayInfo : displayInfos) { |
| 6266 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6267 | } |
| 6268 | |
| 6269 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6270 | setInputWindowsLocked(handles, displayId); |
| 6271 | } |
| 6272 | } |
| 6273 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6274 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6275 | } |
| 6276 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6277 | bool InputDispatcher::shouldDropInput( |
| 6278 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
| 6279 | if (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT) || |
| 6280 | (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED) && |
| 6281 | isWindowObscuredLocked(windowHandle))) { |
| 6282 | ALOGW("Dropping %s event targeting %s as requested by input feature %s on display " |
| 6283 | "%" PRId32 ".", |
| 6284 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
| 6285 | windowHandle->getInfo()->inputFeatures.string().c_str(), |
| 6286 | windowHandle->getInfo()->displayId); |
| 6287 | return true; |
| 6288 | } |
| 6289 | return false; |
| 6290 | } |
| 6291 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6292 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6293 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6294 | const std::vector<DisplayInfo>& displayInfos) { |
| 6295 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6296 | } |
| 6297 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6298 | } // namespace android::inputdispatcher |