Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "InputDispatcher" |
| 18 | #define ATRACE_TAG ATRACE_TAG_INPUT |
| 19 | |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 20 | #define LOG_NDEBUG 1 |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 22 | #include <android-base/chrono_utils.h> |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 23 | #include <android-base/properties.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 25 | #include <android/os/IInputConstants.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 26 | #include <binder/Binder.h> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 27 | #include <ftl/enum.h> |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 28 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 29 | #include <input/InputDevice.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 30 | #include <log/log.h> |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 31 | #include <log/log_event_list.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 32 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 33 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 34 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 35 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 36 | #include <cerrno> |
| 37 | #include <cinttypes> |
| 38 | #include <climits> |
| 39 | #include <cstddef> |
| 40 | #include <ctime> |
| 41 | #include <queue> |
| 42 | #include <sstream> |
| 43 | |
| 44 | #include "Connection.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 45 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 46 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 47 | #define INDENT " " |
| 48 | #define INDENT2 " " |
| 49 | #define INDENT3 " " |
| 50 | #define INDENT4 " " |
| 51 | |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 52 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 53 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 54 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 55 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 56 | using android::gui::FocusRequest; |
| 57 | using android::gui::TouchOcclusionMode; |
| 58 | using android::gui::WindowInfo; |
| 59 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 60 | using android::os::BlockUntrustedTouchesMode; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 61 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 62 | using android::os::InputEventInjectionResult; |
| 63 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 64 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 65 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 66 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 67 | namespace { |
| 68 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 69 | // Log detailed debug messages about each inbound event notification to the dispatcher. |
| 70 | constexpr bool DEBUG_INBOUND_EVENT_DETAILS = false; |
| 71 | |
| 72 | // Log detailed debug messages about each outbound event processed by the dispatcher. |
| 73 | constexpr bool DEBUG_OUTBOUND_EVENT_DETAILS = false; |
| 74 | |
| 75 | // Log debug messages about the dispatch cycle. |
| 76 | constexpr bool DEBUG_DISPATCH_CYCLE = false; |
| 77 | |
| 78 | // Log debug messages about channel creation |
| 79 | constexpr bool DEBUG_CHANNEL_CREATION = false; |
| 80 | |
| 81 | // Log debug messages about input event injection. |
| 82 | constexpr bool DEBUG_INJECTION = false; |
| 83 | |
| 84 | // Log debug messages about input focus tracking. |
| 85 | constexpr bool DEBUG_FOCUS = false; |
| 86 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 87 | // Log debug messages about touch mode event |
| 88 | constexpr bool DEBUG_TOUCH_MODE = false; |
| 89 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 90 | // Log debug messages about touch occlusion |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 91 | constexpr bool DEBUG_TOUCH_OCCLUSION = true; |
| 92 | |
| 93 | // Log debug messages about the app switch latency optimization. |
| 94 | constexpr bool DEBUG_APP_SWITCH = false; |
| 95 | |
| 96 | // Log debug messages about hover events. |
| 97 | constexpr bool DEBUG_HOVER = false; |
| 98 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 99 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 100 | // Named to match std::scoped_lock |
| 101 | class scoped_unlock { |
| 102 | public: |
| 103 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 104 | ~scoped_unlock() { mMutex.lock(); } |
| 105 | |
| 106 | private: |
| 107 | std::mutex& mMutex; |
| 108 | }; |
| 109 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 110 | // Default input dispatching timeout if there is no focused application or paused window |
| 111 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 112 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 113 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 114 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 115 | |
| 116 | // Amount of time to allow for all pending events to be processed when an app switch |
| 117 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 118 | // when an application takes too long to respond and the user has pressed an app switch key. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 119 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 120 | |
| 121 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 122 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 123 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 124 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 125 | // 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] | 126 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 127 | |
| 128 | // Log a warning when an interception call takes longer than this to process. |
| 129 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 130 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 131 | // Additional key latency in case a connection is still processing some motion events. |
| 132 | // This will help with the case when a user touched a button that opens a new window, |
| 133 | // and gives us the chance to dispatch the key to this new window. |
| 134 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 135 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 136 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 137 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 138 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 139 | // Event log tags. See EventLogTags.logtags for reference |
| 140 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 141 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 142 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 143 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 144 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 145 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 146 | } |
| 147 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 148 | inline const char* toString(bool value) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 149 | return value ? "true" : "false"; |
| 150 | } |
| 151 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 152 | inline const std::string toString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 153 | if (binder == nullptr) { |
| 154 | return "<null>"; |
| 155 | } |
| 156 | return StringPrintf("%p", binder.get()); |
| 157 | } |
| 158 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 159 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 160 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 161 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 164 | bool isValidKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 166 | case AKEY_EVENT_ACTION_DOWN: |
| 167 | case AKEY_EVENT_ACTION_UP: |
| 168 | return true; |
| 169 | default: |
| 170 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 174 | bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 175 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 176 | ALOGE("Key event has invalid action code 0x%x", action); |
| 177 | return false; |
| 178 | } |
| 179 | return true; |
| 180 | } |
| 181 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 182 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 183 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 184 | case AMOTION_EVENT_ACTION_DOWN: |
| 185 | case AMOTION_EVENT_ACTION_UP: |
| 186 | case AMOTION_EVENT_ACTION_CANCEL: |
| 187 | case AMOTION_EVENT_ACTION_MOVE: |
| 188 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 189 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 190 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 191 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 192 | case AMOTION_EVENT_ACTION_SCROLL: |
| 193 | return true; |
| 194 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 195 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 196 | int32_t index = getMotionEventActionPointerIndex(action); |
| 197 | return index >= 0 && index < pointerCount; |
| 198 | } |
| 199 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 200 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 201 | return actionButton != 0; |
| 202 | default: |
| 203 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 207 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 208 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 209 | } |
| 210 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 211 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 212 | const PointerProperties* pointerProperties) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 213 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 214 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 215 | return false; |
| 216 | } |
| 217 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame^] | 218 | ALOGE("Motion event has invalid pointer count %zu; value must be between 1 and %zu.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 219 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | BitSet32 pointerIdBits; |
| 223 | for (size_t i = 0; i < pointerCount; i++) { |
| 224 | int32_t id = pointerProperties[i].id; |
| 225 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 226 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 227 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | if (pointerIdBits.hasBit(id)) { |
| 231 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 232 | return false; |
| 233 | } |
| 234 | pointerIdBits.markBit(id); |
| 235 | } |
| 236 | return true; |
| 237 | } |
| 238 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 239 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 240 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 241 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 244 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 245 | bool first = true; |
| 246 | Region::const_iterator cur = region.begin(); |
| 247 | Region::const_iterator const tail = region.end(); |
| 248 | while (cur != tail) { |
| 249 | if (first) { |
| 250 | first = false; |
| 251 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 252 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 253 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 254 | 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] | 255 | cur++; |
| 256 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 257 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 260 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 261 | constexpr size_t maxEntries = 50; // max events to print |
| 262 | constexpr size_t skipBegin = maxEntries / 2; |
| 263 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 264 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 265 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 266 | |
| 267 | std::string dump; |
| 268 | for (size_t i = 0; i < queue.size(); i++) { |
| 269 | const DispatchEntry& entry = *queue[i]; |
| 270 | if (i >= skipBegin && i < skipEnd) { |
| 271 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 272 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 273 | continue; |
| 274 | } |
| 275 | dump.append(INDENT4); |
| 276 | dump += entry.eventEntry->getDescription(); |
| 277 | dump += StringPrintf(", seq=%" PRIu32 |
| 278 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 279 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 280 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 281 | if (entry.deliveryTime != 0) { |
| 282 | // This entry was delivered, so add information on how long we've been waiting |
| 283 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 284 | } |
| 285 | dump.append("\n"); |
| 286 | } |
| 287 | return dump; |
| 288 | } |
| 289 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 290 | /** |
| 291 | * Find the entry in std::unordered_map by key, and return it. |
| 292 | * If the entry is not found, return a default constructed entry. |
| 293 | * |
| 294 | * Useful when the entries are vectors, since an empty vector will be returned |
| 295 | * if the entry is not found. |
| 296 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 297 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 298 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 299 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 300 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 301 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 302 | } |
| 303 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 304 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 305 | if (first == second) { |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | if (first == nullptr || second == nullptr) { |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | return first->getToken() == second->getToken(); |
| 314 | } |
| 315 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 316 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 317 | if (first == nullptr || second == nullptr) { |
| 318 | return false; |
| 319 | } |
| 320 | return first->applicationInfo.token != nullptr && |
| 321 | first->applicationInfo.token == second->applicationInfo.token; |
| 322 | } |
| 323 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 324 | bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 325 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 326 | } |
| 327 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 328 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 329 | std::shared_ptr<EventEntry> eventEntry, |
| 330 | int32_t inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 331 | if (inputTarget.useDefaultPointerTransform()) { |
| 332 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 333 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 334 | inputTarget.displayTransform, |
| 335 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 339 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 340 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 341 | std::vector<PointerCoords> pointerCoords; |
| 342 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 343 | |
| 344 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 345 | // 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] | 346 | // uses the transform for the normalized pointer. |
| 347 | const ui::Transform& firstPointerTransform = |
| 348 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 349 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 350 | |
| 351 | // Iterate through all pointers in the event to normalize against the first. |
| 352 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 353 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 354 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 355 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 356 | |
| 357 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 358 | // First, apply the current pointer's transform to update the coordinates into |
| 359 | // window space. |
| 360 | pointerCoords[pointerIndex].transform(currTransform); |
| 361 | // Next, apply the inverse transform of the normalized coordinates so the |
| 362 | // current coordinates are transformed into the normalized coordinate space. |
| 363 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 366 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 367 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 368 | motionEntry.deviceId, motionEntry.source, |
| 369 | motionEntry.displayId, motionEntry.policyFlags, |
| 370 | motionEntry.action, motionEntry.actionButton, |
| 371 | motionEntry.flags, motionEntry.metaState, |
| 372 | motionEntry.buttonState, motionEntry.classification, |
| 373 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 374 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 375 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 376 | motionEntry.pointerCount, motionEntry.pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 377 | pointerCoords.data()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 378 | |
| 379 | if (motionEntry.injectionState) { |
| 380 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 381 | combinedMotionEntry->injectionState->refCount += 1; |
| 382 | } |
| 383 | |
| 384 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 385 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 386 | firstPointerTransform, inputTarget.displayTransform, |
| 387 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 388 | return dispatchEntry; |
| 389 | } |
| 390 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 391 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 392 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 393 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 394 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 395 | |
| 396 | serverChannel = std::move(uniqueServerChannel); |
| 397 | return result; |
| 398 | } |
| 399 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 400 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 401 | 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] | 402 | if (lhs == nullptr && rhs == nullptr) { |
| 403 | return true; |
| 404 | } |
| 405 | if (lhs == nullptr || rhs == nullptr) { |
| 406 | return false; |
| 407 | } |
| 408 | return *lhs == *rhs; |
| 409 | } |
| 410 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 411 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 412 | KeyEvent event; |
| 413 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 414 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 415 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 416 | return event; |
| 417 | } |
| 418 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 419 | std::optional<int32_t> findMonitorPidByToken( |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 420 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, |
| 421 | const sp<IBinder>& token) { |
| 422 | for (const auto& it : monitorsByDisplay) { |
| 423 | const std::vector<Monitor>& monitors = it.second; |
| 424 | for (const Monitor& monitor : monitors) { |
| 425 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 426 | return monitor.pid; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | return std::nullopt; |
| 431 | } |
| 432 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 433 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 434 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 435 | // affect the statistics. |
| 436 | if (connection.monitor) { |
| 437 | return false; |
| 438 | } |
| 439 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 440 | if (!connection.responsive) { |
| 441 | return false; |
| 442 | } |
| 443 | return true; |
| 444 | } |
| 445 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 446 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 447 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 448 | const int32_t& inputEventId = eventEntry.id; |
| 449 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 450 | // Event was transmuted |
| 451 | return false; |
| 452 | } |
| 453 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 454 | return false; |
| 455 | } |
| 456 | // Only track latency for events that originated from hardware |
| 457 | if (eventEntry.isSynthesized()) { |
| 458 | return false; |
| 459 | } |
| 460 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 461 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 462 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 463 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 464 | return false; |
| 465 | } |
| 466 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 467 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 468 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 469 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 470 | return false; |
| 471 | } |
| 472 | } else { |
| 473 | // Not a key or a motion |
| 474 | return false; |
| 475 | } |
| 476 | if (!shouldReportMetricsForConnection(connection)) { |
| 477 | return false; |
| 478 | } |
| 479 | return true; |
| 480 | } |
| 481 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 482 | /** |
| 483 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 484 | * current time. |
| 485 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 486 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 487 | const nsecs_t currentTime = now(); |
| 488 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 489 | if (entry->timeoutTime < currentTime) { |
| 490 | return false; |
| 491 | } |
| 492 | } |
| 493 | return true; |
| 494 | } |
| 495 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 496 | // Returns true if the event type passed as argument represents a user activity. |
| 497 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 498 | switch (eventEntry.type) { |
| 499 | case EventEntry::Type::FOCUS: |
| 500 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 501 | case EventEntry::Type::DRAG: |
| 502 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 503 | case EventEntry::Type::SENSOR: |
| 504 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 505 | return false; |
| 506 | case EventEntry::Type::DEVICE_RESET: |
| 507 | case EventEntry::Type::KEY: |
| 508 | case EventEntry::Type::MOTION: |
| 509 | return true; |
| 510 | } |
| 511 | } |
| 512 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 513 | // Returns true if the given window can accept pointer events at the given display location. |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 514 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, int32_t x, int32_t y, |
| 515 | bool isStylus) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 516 | if (windowInfo.displayId != displayId || !windowInfo.visible) { |
| 517 | return false; |
| 518 | } |
| 519 | const auto flags = windowInfo.flags; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 520 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
| 521 | if (flags.test(WindowInfo::Flag::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 522 | return false; |
| 523 | } |
| 524 | const bool isModalWindow = !flags.test(WindowInfo::Flag::NOT_FOCUSABLE) && |
| 525 | !flags.test(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 526 | if (!isModalWindow && !windowInfo.touchableRegionContainsPoint(x, y)) { |
| 527 | return false; |
| 528 | } |
| 529 | return true; |
| 530 | } |
| 531 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 532 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 533 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
| 534 | (entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || |
| 535 | entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_ERASER); |
| 536 | } |
| 537 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 538 | } // namespace |
| 539 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 540 | // --- InputDispatcher --- |
| 541 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 542 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 543 | : mPolicy(policy), |
| 544 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 545 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 546 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 547 | mAppSwitchSawKeyDown(false), |
| 548 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 549 | mNextUnblockedEvent(nullptr), |
| 550 | mDispatchEnabled(false), |
| 551 | mDispatchFrozen(false), |
| 552 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 553 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 554 | // To avoid leaking stack in case that call never comes, and for tests, |
| 555 | // initialize it here anyways. |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 556 | mInTouchMode(kDefaultInTouchMode), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 557 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 558 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 559 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 560 | mLatencyAggregator(), |
Siarhei Vishniakou | bd25272 | 2022-01-06 03:49:35 -0800 | [diff] [blame] | 561 | mLatencyTracker(&mLatencyAggregator) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 562 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 563 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 564 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 565 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 566 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 567 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 568 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 569 | |
| 570 | policy->getDispatcherConfiguration(&mConfig); |
| 571 | } |
| 572 | |
| 573 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 574 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 575 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 576 | resetKeyRepeatLocked(); |
| 577 | releasePendingEventLocked(); |
| 578 | drainInboundQueueLocked(); |
| 579 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 581 | while (!mConnectionsByToken.empty()) { |
| 582 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 583 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 584 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 588 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 589 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 590 | return ALREADY_EXISTS; |
| 591 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 592 | mThread = std::make_unique<InputThread>( |
| 593 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 594 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 598 | if (mThread && mThread->isCallingThread()) { |
| 599 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 600 | return INVALID_OPERATION; |
| 601 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 602 | mThread.reset(); |
| 603 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 606 | void InputDispatcher::dispatchOnce() { |
| 607 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 608 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 609 | std::scoped_lock _l(mLock); |
| 610 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 611 | |
| 612 | // Run a dispatch loop if there are no pending commands. |
| 613 | // The dispatch loop might enqueue commands to run afterwards. |
| 614 | if (!haveCommandsLocked()) { |
| 615 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 616 | } |
| 617 | |
| 618 | // Run all pending commands if there are any. |
| 619 | // 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] | 620 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 621 | nextWakeupTime = LONG_LONG_MIN; |
| 622 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 623 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 624 | // If we are still waiting for ack on some events, |
| 625 | // we might have to wake up earlier to check if an app is anr'ing. |
| 626 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 627 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 628 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 629 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 630 | // pending or queued events |
| 631 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 632 | mDispatcherEnteredIdle.notify_all(); |
| 633 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 634 | } // release lock |
| 635 | |
| 636 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 637 | nsecs_t currentTime = now(); |
| 638 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 639 | mLooper->pollOnce(timeoutMillis); |
| 640 | } |
| 641 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 642 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 643 | * Raise ANR if there is no focused window. |
| 644 | * Before the ANR is raised, do a final state check: |
| 645 | * 1. The currently focused application must be the same one we are waiting for. |
| 646 | * 2. Ensure we still don't have a focused window. |
| 647 | */ |
| 648 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 649 | // Check if the application that we are waiting for is still focused. |
| 650 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 651 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 652 | if (focusedApplication == nullptr || |
| 653 | focusedApplication->getApplicationToken() != |
| 654 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 655 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 656 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 657 | focusedApplication->getName().c_str()); |
| 658 | return; // The focused application has changed. |
| 659 | } |
| 660 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 661 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 662 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 663 | if (focusedWindowHandle != nullptr) { |
| 664 | return; // We now have a focused window. No need for ANR. |
| 665 | } |
| 666 | onAnrLocked(mAwaitedFocusedApplication); |
| 667 | } |
| 668 | |
| 669 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 670 | * Check if any of the connections' wait queues have events that are too old. |
| 671 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 672 | * Return the time at which we should wake up next. |
| 673 | */ |
| 674 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 675 | const nsecs_t currentTime = now(); |
| 676 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 677 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 678 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 679 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 680 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 681 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 682 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 683 | return LONG_LONG_MIN; |
| 684 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 685 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 686 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // Check if any connection ANRs are due |
| 691 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 692 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 693 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 694 | } |
| 695 | |
| 696 | // If we reached here, we have an unresponsive connection. |
| 697 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 698 | if (connection == nullptr) { |
| 699 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 700 | return nextAnrCheck; |
| 701 | } |
| 702 | connection->responsive = false; |
| 703 | // Stop waking up for this unresponsive connection |
| 704 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 705 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 706 | return LONG_LONG_MIN; |
| 707 | } |
| 708 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 709 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 710 | sp<WindowInfoHandle> window = getWindowHandleLocked(token); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 711 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 712 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 713 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 714 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 717 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 718 | nsecs_t currentTime = now(); |
| 719 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 720 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 721 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 722 | // repeat if the device is just coming out of sleep. |
| 723 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 724 | resetKeyRepeatLocked(); |
| 725 | } |
| 726 | |
| 727 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 728 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 729 | if (DEBUG_FOCUS) { |
| 730 | ALOGD("Dispatch frozen. Waiting some more."); |
| 731 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 732 | return; |
| 733 | } |
| 734 | |
| 735 | // Optimize latency of app switches. |
| 736 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 737 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 738 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 739 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 740 | *nextWakeupTime = mAppSwitchDueTime; |
| 741 | } |
| 742 | |
| 743 | // Ready to start a new event. |
| 744 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 745 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 746 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 747 | if (isAppSwitchDue) { |
| 748 | // The inbound queue is empty so the app switch key we were waiting |
| 749 | // for will never arrive. Stop waiting for it. |
| 750 | resetPendingAppSwitchLocked(false); |
| 751 | isAppSwitchDue = false; |
| 752 | } |
| 753 | |
| 754 | // Synthesize a key repeat if appropriate. |
| 755 | if (mKeyRepeatState.lastKeyEntry) { |
| 756 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 757 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 758 | } else { |
| 759 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 760 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | // Nothing to do if there is no pending event. |
| 766 | if (!mPendingEvent) { |
| 767 | return; |
| 768 | } |
| 769 | } else { |
| 770 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 771 | mPendingEvent = mInboundQueue.front(); |
| 772 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 773 | traceInboundQueueLengthLocked(); |
| 774 | } |
| 775 | |
| 776 | // Poke user activity for this event. |
| 777 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 778 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 779 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | // Now we have an event to dispatch. |
| 783 | // 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] | 784 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 785 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 786 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 787 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 788 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 789 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 790 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 794 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 798 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 799 | const ConfigurationChangedEntry& typedEntry = |
| 800 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 801 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 802 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 803 | break; |
| 804 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 805 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 806 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 807 | const DeviceResetEntry& typedEntry = |
| 808 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 809 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 810 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 811 | break; |
| 812 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 813 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 814 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 815 | std::shared_ptr<FocusEntry> typedEntry = |
| 816 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 817 | dispatchFocusLocked(currentTime, typedEntry); |
| 818 | done = true; |
| 819 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 820 | break; |
| 821 | } |
| 822 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 823 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 824 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 825 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 826 | done = true; |
| 827 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 828 | break; |
| 829 | } |
| 830 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 831 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 832 | const auto typedEntry = |
| 833 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 834 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 835 | done = true; |
| 836 | break; |
| 837 | } |
| 838 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 839 | case EventEntry::Type::DRAG: { |
| 840 | std::shared_ptr<DragEntry> typedEntry = |
| 841 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 842 | dispatchDragLocked(currentTime, typedEntry); |
| 843 | done = true; |
| 844 | break; |
| 845 | } |
| 846 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 847 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 848 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 849 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 850 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 851 | resetPendingAppSwitchLocked(true); |
| 852 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 853 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 854 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 857 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 858 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 859 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 860 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 861 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 862 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 863 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 864 | break; |
| 865 | } |
| 866 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 867 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 868 | std::shared_ptr<MotionEntry> motionEntry = |
| 869 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 870 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 871 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 872 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 873 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 874 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 875 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 876 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 877 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 878 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 879 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 880 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 881 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 882 | |
| 883 | case EventEntry::Type::SENSOR: { |
| 884 | std::shared_ptr<SensorEntry> sensorEntry = |
| 885 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 886 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 887 | dropReason = DropReason::APP_SWITCH; |
| 888 | } |
| 889 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 890 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 891 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 892 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 893 | dropReason = DropReason::STALE; |
| 894 | } |
| 895 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 896 | done = true; |
| 897 | break; |
| 898 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 902 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 903 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 904 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 905 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 906 | |
| 907 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 908 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 912 | /** |
| 913 | * Return true if the events preceding this incoming motion event should be dropped |
| 914 | * Return false otherwise (the default behaviour) |
| 915 | */ |
| 916 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 917 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 918 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 919 | |
| 920 | // Optimize case where the current application is unresponsive and the user |
| 921 | // decides to touch a window in a different application. |
| 922 | // If the application takes too long to catch up then we drop all events preceding |
| 923 | // the touch into the other window. |
| 924 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 925 | int32_t displayId = motionEntry.displayId; |
| 926 | int32_t x = static_cast<int32_t>( |
| 927 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 928 | int32_t y = static_cast<int32_t>( |
| 929 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 930 | |
| 931 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 932 | sp<WindowInfoHandle> touchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 933 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 934 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 935 | touchedWindowHandle->getApplicationToken() != |
| 936 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 937 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 938 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 939 | "for %s", |
| 940 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 941 | return true; |
| 942 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 943 | |
| 944 | // Alternatively, maybe there's a gesture monitor that could handle this event |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 945 | for (const auto& monitor : getValueByKey(mGestureMonitorsByDisplay, displayId)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 946 | sp<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 947 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 948 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 949 | // This monitor could take more input. Drop all events preceding this |
| 950 | // event, so that gesture monitor could get a chance to receive the stream |
| 951 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 952 | "responsive gesture monitor that may handle the event", |
| 953 | mAwaitedFocusedApplication->getName().c_str()); |
| 954 | return true; |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 960 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 961 | // events to be processed before dispatching the key event. This is because these motion events |
| 962 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 963 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 964 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 965 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 966 | "just send the pending key event to the focused window."); |
| 967 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 968 | } |
| 969 | return false; |
| 970 | } |
| 971 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 972 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 973 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 974 | mInboundQueue.push_back(std::move(newEntry)); |
| 975 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 976 | traceInboundQueueLengthLocked(); |
| 977 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 978 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 979 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 980 | // Optimize app switch latency. |
| 981 | // If the application takes too long to catch up then we drop all events preceding |
| 982 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 983 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 984 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 985 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 986 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 987 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 988 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 989 | if (DEBUG_APP_SWITCH) { |
| 990 | ALOGD("App switch is pending!"); |
| 991 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 992 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 993 | mAppSwitchSawKeyDown = false; |
| 994 | needWake = true; |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | break; |
| 999 | } |
| 1000 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1001 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1002 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1003 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1004 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1005 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1006 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1007 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1008 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1009 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1010 | break; |
| 1011 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1012 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1013 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1014 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1015 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1016 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1017 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1018 | // nothing to do |
| 1019 | break; |
| 1020 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | return needWake; |
| 1024 | } |
| 1025 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1026 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1027 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1028 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1029 | mRecentQueue.push_back(entry); |
| 1030 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1031 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1032 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1036 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1037 | int32_t y, TouchState* touchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1038 | bool isStylus, |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1039 | bool addOutsideTargets, |
| 1040 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1041 | if (addOutsideTargets && touchState == nullptr) { |
| 1042 | 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] | 1043 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1044 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1045 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1046 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1047 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1048 | continue; |
| 1049 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1051 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1052 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1053 | return windowHandle; |
| 1054 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1055 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1056 | if (addOutsideTargets && info.flags.test(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
| 1057 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1058 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1059 | } |
| 1060 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1061 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1064 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1065 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1066 | // Traverse windows from front to back and gather the touched spy windows. |
| 1067 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1068 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1069 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1070 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1071 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1072 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1073 | continue; |
| 1074 | } |
| 1075 | if (!info.isSpy()) { |
| 1076 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1077 | return spyWindows; |
| 1078 | } |
| 1079 | spyWindows.push_back(windowHandle); |
| 1080 | } |
| 1081 | return spyWindows; |
| 1082 | } |
| 1083 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1084 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1085 | const char* reason; |
| 1086 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1087 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1088 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1089 | ALOGD("Dropped event because policy consumed it."); |
| 1090 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1091 | reason = "inbound event was dropped because the policy consumed it"; |
| 1092 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1093 | case DropReason::DISABLED: |
| 1094 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1095 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1096 | } |
| 1097 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1098 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1099 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1100 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1101 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1102 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1103 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1104 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1105 | "has started interacting with a different application."); |
| 1106 | reason = "inbound event was dropped because the current application is not responding " |
| 1107 | "and the user has started interacting with a different application"; |
| 1108 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1109 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1110 | ALOGI("Dropped event because it is stale."); |
| 1111 | reason = "inbound event was dropped because it is stale"; |
| 1112 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1113 | case DropReason::NO_POINTER_CAPTURE: |
| 1114 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1115 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1116 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1117 | case DropReason::NOT_DROPPED: { |
| 1118 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1119 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1120 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1121 | } |
| 1122 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1123 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1124 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1125 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1126 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1127 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1128 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1129 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1130 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1131 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1132 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1133 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1134 | } else { |
| 1135 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1136 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1137 | } |
| 1138 | break; |
| 1139 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1140 | case EventEntry::Type::SENSOR: { |
| 1141 | break; |
| 1142 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1143 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1144 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1145 | break; |
| 1146 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1147 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1148 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1149 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1150 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1151 | 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] | 1152 | break; |
| 1153 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1157 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1158 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1159 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1162 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1163 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1164 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1165 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1169 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1170 | } |
| 1171 | |
| 1172 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1173 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1174 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1175 | if (DEBUG_APP_SWITCH) { |
| 1176 | if (handled) { |
| 1177 | ALOGD("App switch has arrived."); |
| 1178 | } else { |
| 1179 | ALOGD("App switch was abandoned."); |
| 1180 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1181 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1184 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1185 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1188 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1189 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1190 | return false; |
| 1191 | } |
| 1192 | |
| 1193 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1194 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1195 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1196 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1197 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1198 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1199 | return true; |
| 1200 | } |
| 1201 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1202 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1203 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1207 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1208 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1209 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1210 | releaseInboundEventLocked(entry); |
| 1211 | } |
| 1212 | traceInboundQueueLengthLocked(); |
| 1213 | } |
| 1214 | |
| 1215 | void InputDispatcher::releasePendingEventLocked() { |
| 1216 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1217 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1218 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1222 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1223 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1224 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1225 | if (DEBUG_DISPATCH_CYCLE) { |
| 1226 | ALOGD("Injected inbound event was dropped."); |
| 1227 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1228 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1229 | } |
| 1230 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1231 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1232 | } |
| 1233 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1237 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1238 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1242 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1243 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1244 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1245 | uint32_t policyFlags = entry->policyFlags & |
| 1246 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1247 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1248 | std::shared_ptr<KeyEntry> newEntry = |
| 1249 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1250 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1251 | entry->flags, entry->keyCode, entry->scanCode, |
| 1252 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1253 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1254 | newEntry->syntheticRepeat = true; |
| 1255 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1256 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1257 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1258 | } |
| 1259 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1260 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1261 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1262 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1263 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1264 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1265 | |
| 1266 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1267 | resetKeyRepeatLocked(); |
| 1268 | |
| 1269 | // 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] | 1270 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1271 | scoped_unlock unlock(mLock); |
| 1272 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1273 | }; |
| 1274 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1275 | return true; |
| 1276 | } |
| 1277 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1278 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1279 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1280 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1281 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1282 | entry.deviceId); |
| 1283 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1284 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1285 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1286 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1287 | resetKeyRepeatLocked(); |
| 1288 | } |
| 1289 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1290 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1291 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1292 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1293 | return true; |
| 1294 | } |
| 1295 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1296 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1297 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1298 | if (mPendingEvent != nullptr) { |
| 1299 | // Move the pending event to the front of the queue. This will give the chance |
| 1300 | // for the pending event to get dispatched to the newly focused window |
| 1301 | mInboundQueue.push_front(mPendingEvent); |
| 1302 | mPendingEvent = nullptr; |
| 1303 | } |
| 1304 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1305 | std::unique_ptr<FocusEntry> focusEntry = |
| 1306 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1307 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1308 | |
| 1309 | // This event should go to the front of the queue, but behind all other focus events |
| 1310 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1311 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1312 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1313 | [](const std::shared_ptr<EventEntry>& event) { |
| 1314 | return event->type == EventEntry::Type::FOCUS; |
| 1315 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1316 | |
| 1317 | // 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] | 1318 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1319 | } |
| 1320 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1321 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1322 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1323 | if (channel == nullptr) { |
| 1324 | return; // Window has gone away |
| 1325 | } |
| 1326 | InputTarget target; |
| 1327 | target.inputChannel = channel; |
| 1328 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1329 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1330 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1331 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1332 | std::string reason = std::string("reason=").append(entry->reason); |
| 1333 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1334 | dispatchEventLocked(currentTime, entry, {target}); |
| 1335 | } |
| 1336 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1337 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1338 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1339 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1340 | dropReason = DropReason::NOT_DROPPED; |
| 1341 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1342 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1343 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1344 | |
| 1345 | if (entry->pointerCaptureRequest.enable) { |
| 1346 | // Enable Pointer Capture. |
| 1347 | if (haveWindowWithPointerCapture && |
| 1348 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
| 1349 | LOG_ALWAYS_FATAL("This request to enable Pointer Capture has already been dispatched " |
| 1350 | "to the window."); |
| 1351 | } |
| 1352 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1353 | // This can happen if a window requests capture and immediately releases capture. |
| 1354 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1355 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1356 | return; |
| 1357 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1358 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1359 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1360 | return; |
| 1361 | } |
| 1362 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1363 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1364 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1365 | mWindowTokenWithPointerCapture = token; |
| 1366 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1367 | // Disable Pointer Capture. |
| 1368 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1369 | // for two reasons: |
| 1370 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1371 | // to disable capture with the same sequence number: one generated by |
| 1372 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1373 | // Capture being disabled in InputReader. |
| 1374 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1375 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1376 | // in InputReader. |
| 1377 | if (!haveWindowWithPointerCapture) { |
| 1378 | // Pointer capture was already forcefully disabled because of focus change. |
| 1379 | dropReason = DropReason::NOT_DROPPED; |
| 1380 | return; |
| 1381 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1382 | token = mWindowTokenWithPointerCapture; |
| 1383 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1384 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1385 | setPointerCaptureLocked(false); |
| 1386 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | auto channel = getInputChannelLocked(token); |
| 1390 | if (channel == nullptr) { |
| 1391 | // Window has gone away, clean up Pointer Capture state. |
| 1392 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1393 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1394 | setPointerCaptureLocked(false); |
| 1395 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1396 | return; |
| 1397 | } |
| 1398 | InputTarget target; |
| 1399 | target.inputChannel = channel; |
| 1400 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1401 | entry->dispatchInProgress = true; |
| 1402 | dispatchEventLocked(currentTime, entry, {target}); |
| 1403 | |
| 1404 | dropReason = DropReason::NOT_DROPPED; |
| 1405 | } |
| 1406 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1407 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1408 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1409 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1410 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1411 | if (windowHandles.empty()) { |
| 1412 | return; |
| 1413 | } |
| 1414 | const std::vector<InputTarget> inputTargets = |
| 1415 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1416 | if (inputTargets.empty()) { |
| 1417 | return; |
| 1418 | } |
| 1419 | entry->dispatchInProgress = true; |
| 1420 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1421 | } |
| 1422 | |
| 1423 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1424 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1425 | std::vector<InputTarget> inputTargets; |
| 1426 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1427 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1428 | const sp<IBinder>& token = handle->getToken(); |
| 1429 | if (token == nullptr) { |
| 1430 | continue; |
| 1431 | } |
| 1432 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1433 | if (channel == nullptr) { |
| 1434 | continue; // Window has gone away |
| 1435 | } |
| 1436 | InputTarget target; |
| 1437 | target.inputChannel = channel; |
| 1438 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1439 | inputTargets.push_back(target); |
| 1440 | } |
| 1441 | return inputTargets; |
| 1442 | } |
| 1443 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1444 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1445 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1446 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1447 | if (!entry->dispatchInProgress) { |
| 1448 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1449 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1450 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1451 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1452 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1453 | // We have seen two identical key downs in a row which indicates that the device |
| 1454 | // driver is automatically generating key repeats itself. We take note of the |
| 1455 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1456 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1457 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1458 | // Make sure we don't get key down from a different device. If a different |
| 1459 | // device Id has same key pressed down, the new device Id will replace the |
| 1460 | // current one to hold the key repeat with repeat count reset. |
| 1461 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1462 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1463 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1464 | resetKeyRepeatLocked(); |
| 1465 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1466 | } else { |
| 1467 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1468 | resetKeyRepeatLocked(); |
| 1469 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1470 | } |
| 1471 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1472 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1473 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1474 | // 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] | 1475 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1476 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1477 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1478 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1479 | resetKeyRepeatLocked(); |
| 1480 | } |
| 1481 | |
| 1482 | if (entry->repeatCount == 1) { |
| 1483 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1484 | } else { |
| 1485 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1486 | } |
| 1487 | |
| 1488 | entry->dispatchInProgress = true; |
| 1489 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1490 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | // Handle case where the policy asked us to try again later last time. |
| 1494 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1495 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1496 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1497 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1498 | } |
| 1499 | return false; // wait until next wakeup |
| 1500 | } |
| 1501 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1502 | entry->interceptKeyWakeupTime = 0; |
| 1503 | } |
| 1504 | |
| 1505 | // Give the policy a chance to intercept the key. |
| 1506 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1507 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1508 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1509 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1510 | |
| 1511 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1512 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1513 | }; |
| 1514 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1515 | return false; // wait for the command to run |
| 1516 | } else { |
| 1517 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1518 | } |
| 1519 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1520 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1521 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1526 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1527 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1528 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1529 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1530 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1531 | return true; |
| 1532 | } |
| 1533 | |
| 1534 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1535 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1536 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1537 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1538 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1539 | return false; |
| 1540 | } |
| 1541 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1542 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1543 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1544 | return true; |
| 1545 | } |
| 1546 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1547 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1548 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1549 | |
| 1550 | // Dispatch the key. |
| 1551 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1552 | return true; |
| 1553 | } |
| 1554 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1555 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1556 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1557 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1558 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1559 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1560 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1561 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1562 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1563 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1564 | } |
| 1565 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1566 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1567 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1568 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1569 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1570 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1571 | "source=0x%x, sensorType=%s", |
| 1572 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1573 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1574 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1575 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1576 | scoped_unlock unlock(mLock); |
| 1577 | |
| 1578 | if (entry->accuracyChanged) { |
| 1579 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1580 | } |
| 1581 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1582 | entry->hwTimestamp, entry->values); |
| 1583 | }; |
| 1584 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1588 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1589 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1590 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1591 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1592 | { // acquire lock |
| 1593 | std::scoped_lock _l(mLock); |
| 1594 | |
| 1595 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1596 | std::shared_ptr<EventEntry> entry = *it; |
| 1597 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1598 | it = mInboundQueue.erase(it); |
| 1599 | releaseInboundEventLocked(entry); |
| 1600 | } |
| 1601 | } |
| 1602 | } |
| 1603 | return true; |
| 1604 | } |
| 1605 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1606 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1607 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1608 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1609 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1610 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1611 | entry->dispatchInProgress = true; |
| 1612 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1613 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1617 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1618 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1619 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1620 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1621 | return true; |
| 1622 | } |
| 1623 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1624 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1625 | |
| 1626 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1627 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1628 | |
| 1629 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1630 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1631 | if (isPointerEvent) { |
| 1632 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1633 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1634 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1635 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1636 | } else { |
| 1637 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1638 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1639 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1640 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1641 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1642 | return false; |
| 1643 | } |
| 1644 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1645 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1646 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1647 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1648 | return true; |
| 1649 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1650 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1651 | CancelationOptions::Mode mode(isPointerEvent |
| 1652 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1653 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1654 | CancelationOptions options(mode, "input event injection failed"); |
| 1655 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1656 | return true; |
| 1657 | } |
| 1658 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1659 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1660 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1661 | |
| 1662 | // Dispatch the motion. |
| 1663 | if (conflictingPointerActions) { |
| 1664 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1665 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1666 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1667 | } |
| 1668 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1669 | return true; |
| 1670 | } |
| 1671 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1672 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1673 | bool isExiting, const MotionEntry& motionEntry) { |
| 1674 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1675 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1676 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1677 | PointerCoords pointerCoords; |
| 1678 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1679 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1680 | |
| 1681 | std::unique_ptr<DragEntry> dragEntry = |
| 1682 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1683 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1684 | pointerCoords.getY()); |
| 1685 | |
| 1686 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1687 | } |
| 1688 | |
| 1689 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1690 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1691 | if (channel == nullptr) { |
| 1692 | return; // Window has gone away |
| 1693 | } |
| 1694 | InputTarget target; |
| 1695 | target.inputChannel = channel; |
| 1696 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1697 | entry->dispatchInProgress = true; |
| 1698 | dispatchEventLocked(currentTime, entry, {target}); |
| 1699 | } |
| 1700 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1701 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1702 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1703 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1704 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1705 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1706 | "metaState=0x%x, buttonState=0x%x," |
| 1707 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1708 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1709 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1710 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1711 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1712 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1713 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1714 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1715 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1716 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1717 | "orientation=%f", |
| 1718 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1719 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1720 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1721 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1722 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1723 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1724 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1725 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1726 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1727 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1728 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1729 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1730 | } |
| 1731 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1732 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1733 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1734 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1735 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1736 | if (DEBUG_DISPATCH_CYCLE) { |
| 1737 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1738 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1739 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1740 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1741 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1742 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1743 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1744 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1745 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1746 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1747 | sp<Connection> connection = |
| 1748 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1749 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1750 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1751 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1752 | if (DEBUG_FOCUS) { |
| 1753 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1754 | "is no longer registered with the input dispatcher.", |
| 1755 | inputTarget.inputChannel->getName().c_str()); |
| 1756 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1757 | } |
| 1758 | } |
| 1759 | } |
| 1760 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1761 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1762 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1763 | // If the policy decides to close the app, we will get a channel removal event via |
| 1764 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1765 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1766 | // pile up. |
| 1767 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1768 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1769 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1770 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1771 | "application not responding"); |
| 1772 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1773 | } |
| 1774 | } |
| 1775 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1776 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1777 | if (DEBUG_FOCUS) { |
| 1778 | ALOGD("Resetting ANR timeouts."); |
| 1779 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1780 | |
| 1781 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1782 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1783 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1784 | } |
| 1785 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1786 | /** |
| 1787 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1788 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1789 | * Focused display is the display that the user most recently interacted with. |
| 1790 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1791 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1792 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1793 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1794 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1795 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1796 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1797 | break; |
| 1798 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1799 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1800 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1801 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1802 | break; |
| 1803 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1804 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1805 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1806 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1807 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1808 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1809 | case EventEntry::Type::SENSOR: |
| 1810 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1811 | 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] | 1812 | return ADISPLAY_ID_NONE; |
| 1813 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1814 | } |
| 1815 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1816 | } |
| 1817 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1818 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1819 | const char* focusedWindowName) { |
| 1820 | if (mAnrTracker.empty()) { |
| 1821 | // already processed all events that we waited for |
| 1822 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1823 | return false; |
| 1824 | } |
| 1825 | |
| 1826 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1827 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1828 | // 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] | 1829 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1830 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1831 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1832 | return true; |
| 1833 | } |
| 1834 | |
| 1835 | // We still have pending events, and already started the timer |
| 1836 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1837 | return true; // Still waiting |
| 1838 | } |
| 1839 | |
| 1840 | // Waited too long, and some connection still hasn't processed all motions |
| 1841 | // Just send the key to the focused window |
| 1842 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1843 | focusedWindowName); |
| 1844 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1845 | return false; |
| 1846 | } |
| 1847 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1848 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1849 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1850 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1851 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1852 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1853 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1854 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1855 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1856 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1857 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1858 | // If there is no currently focused window and no focused application |
| 1859 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1860 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1861 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1862 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1863 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1864 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1865 | } |
| 1866 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1867 | // Drop key events if requested by input feature |
| 1868 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1869 | return InputEventInjectionResult::FAILED; |
| 1870 | } |
| 1871 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1872 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1873 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1874 | // start interacting with another application via touch (app switch). This code can be removed |
| 1875 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1876 | // an app is expected to have a focused window. |
| 1877 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1878 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1879 | // 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] | 1880 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1881 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1882 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1883 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1884 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1885 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1886 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1887 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1888 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1889 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1890 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1891 | // Already raised ANR. Drop the event |
| 1892 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1893 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1894 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1895 | } else { |
| 1896 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1897 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | // we have a valid, non-null focused window |
| 1902 | resetNoFocusedWindowTimeoutLocked(); |
| 1903 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1904 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1905 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1906 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1907 | } |
| 1908 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1909 | if (focusedWindowHandle->getInfo()->paused) { |
| 1910 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1911 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | // If the event is a key event, then we must wait for all previous events to |
| 1915 | // complete before delivering it because previous events may have the |
| 1916 | // side-effect of transferring focus to a different window and we want to |
| 1917 | // ensure that the following keys are sent to the new window. |
| 1918 | // |
| 1919 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1920 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1921 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1922 | // often anticipate pending UI changes when typing on a keyboard. |
| 1923 | // To obtain this behavior, we must serialize key events with respect to all |
| 1924 | // prior input events. |
| 1925 | if (entry.type == EventEntry::Type::KEY) { |
| 1926 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1927 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1928 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1929 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1933 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1934 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1935 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1936 | |
| 1937 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1938 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1939 | } |
| 1940 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1941 | /** |
| 1942 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1943 | * that are currently unresponsive. |
| 1944 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1945 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1946 | const std::vector<Monitor>& monitors) const { |
| 1947 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1948 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1949 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 1950 | sp<Connection> connection = |
| 1951 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1952 | if (connection == nullptr) { |
| 1953 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1954 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1955 | return false; |
| 1956 | } |
| 1957 | if (!connection->responsive) { |
| 1958 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1959 | connection->inputChannel->getName().c_str()); |
| 1960 | return false; |
| 1961 | } |
| 1962 | return true; |
| 1963 | }); |
| 1964 | return responsiveMonitors; |
| 1965 | } |
| 1966 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1967 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1968 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1969 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1970 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1971 | enum InjectionPermission { |
| 1972 | INJECTION_PERMISSION_UNKNOWN, |
| 1973 | INJECTION_PERMISSION_GRANTED, |
| 1974 | INJECTION_PERMISSION_DENIED |
| 1975 | }; |
| 1976 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1977 | // For security reasons, we defer updating the touch state until we are sure that |
| 1978 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1979 | const int32_t displayId = entry.displayId; |
| 1980 | const int32_t action = entry.action; |
| 1981 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1982 | |
| 1983 | // 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] | 1984 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1985 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1986 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1987 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1988 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1989 | // Copy current touch state into tempTouchState. |
| 1990 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1991 | // 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] | 1992 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1993 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1994 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 1995 | oldState = &(it->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1996 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1999 | bool isSplit = tempTouchState.split; |
| 2000 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 2001 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 2002 | tempTouchState.displayId != displayId); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2003 | |
| 2004 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2005 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2006 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2007 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2008 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2009 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2010 | bool wrongDevice = false; |
| 2011 | if (newGesture) { |
| 2012 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2013 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2014 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2015 | "in display %" PRId32, |
| 2016 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2017 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2018 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2019 | switchedDevice = false; |
| 2020 | wrongDevice = true; |
| 2021 | goto Failed; |
| 2022 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2023 | tempTouchState.reset(); |
| 2024 | tempTouchState.down = down; |
| 2025 | tempTouchState.deviceId = entry.deviceId; |
| 2026 | tempTouchState.source = entry.source; |
| 2027 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2028 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2029 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2030 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2031 | "in display %" PRId32, |
| 2032 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2033 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2034 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2035 | switchedDevice = false; |
| 2036 | wrongDevice = true; |
| 2037 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2041 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2042 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2043 | int32_t x; |
| 2044 | int32_t y; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2045 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2046 | // Always dispatch mouse events to cursor position. |
| 2047 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2048 | x = int32_t(entry.xCursorPosition); |
| 2049 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2050 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2051 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2052 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2053 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2054 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2055 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2056 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2057 | isStylus, isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2058 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2059 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2060 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2061 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2062 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2063 | // 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] | 2064 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2067 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2068 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2069 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2070 | // New window supports splitting, but we should never split mouse events. |
| 2071 | isSplit = !isFromMouse; |
| 2072 | } else if (isSplit) { |
| 2073 | // New window does not support splitting but we have already split events. |
| 2074 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2075 | newTouchedWindowHandle = nullptr; |
| 2076 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2077 | } else { |
| 2078 | // No window is touched, so set split to true. This will allow the next pointer down to |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2079 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2080 | // should never be split. |
| 2081 | tempTouchState.split = isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2084 | // Update hover state. |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2085 | if (newTouchedWindowHandle != nullptr) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2086 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2087 | newHoverWindowHandle = nullptr; |
| 2088 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2089 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2090 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2091 | } |
| 2092 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2093 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2094 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2095 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2096 | // Process the foreground window first so that it is the first to receive the event. |
| 2097 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
| 2101 | const WindowInfo& info = *windowHandle->getInfo(); |
| 2102 | |
| 2103 | if (info.paused) { |
| 2104 | ALOGI("Not sending touch event to %s because it is paused", |
| 2105 | windowHandle->getName().c_str()); |
| 2106 | continue; |
| 2107 | } |
| 2108 | |
| 2109 | // Ensure the window has a connection and the connection is responsive |
| 2110 | const bool isResponsive = hasResponsiveConnectionLocked(*windowHandle); |
| 2111 | if (!isResponsive) { |
| 2112 | ALOGW("Not sending touch gesture to %s because it is not responsive", |
| 2113 | windowHandle->getName().c_str()); |
| 2114 | continue; |
| 2115 | } |
| 2116 | |
| 2117 | // Drop events that can't be trusted due to occlusion |
| 2118 | if (mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2119 | TouchOcclusionInfo occlusionInfo = |
| 2120 | computeTouchOcclusionInfoLocked(windowHandle, x, y); |
| 2121 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 2122 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2123 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2124 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2125 | ALOGD("%s", log.c_str()); |
| 2126 | } |
| 2127 | } |
| 2128 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
| 2129 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2130 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2131 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2132 | continue; |
| 2133 | } |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | // Drop touch events if requested by input feature |
| 2138 | if (shouldDropInput(entry, windowHandle)) { |
| 2139 | continue; |
| 2140 | } |
| 2141 | |
| 2142 | // Set target flags. |
| 2143 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2144 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2145 | if (!info.isSpy()) { |
| 2146 | // There should only be one new foreground (non-spy) window at this location. |
| 2147 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2148 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2149 | |
| 2150 | if (isSplit) { |
| 2151 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2152 | } |
| 2153 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
| 2154 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2155 | } else if (isWindowObscuredLocked(windowHandle)) { |
| 2156 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2157 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2158 | |
| 2159 | // Update the temporary touch state. |
| 2160 | BitSet32 pointerIds; |
| 2161 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2162 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2163 | pointerIds.markBit(pointerId); |
| 2164 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2165 | |
| 2166 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2167 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2168 | |
| 2169 | const std::vector<Monitor> newGestureMonitors = isDown |
| 2170 | ? selectResponsiveMonitorsLocked( |
| 2171 | getValueByKey(mGestureMonitorsByDisplay, displayId)) |
| 2172 | : std::vector<Monitor>{}; |
| 2173 | |
| 2174 | if (newTouchedWindows.empty() && newGestureMonitors.empty() && |
| 2175 | tempTouchState.gestureMonitors.empty()) { |
| 2176 | ALOGI("Dropping event because there is no touchable window or gesture monitor at " |
| 2177 | "(%d, %d) in display %" PRId32 ".", |
| 2178 | x, y, displayId); |
| 2179 | injectionResult = InputEventInjectionResult::FAILED; |
| 2180 | goto Failed; |
Arthur Hung | 2ea44b9 | 2021-11-23 07:42:21 +0000 | [diff] [blame] | 2181 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2182 | |
| 2183 | tempTouchState.addGestureMonitors(newGestureMonitors); |
| 2184 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2185 | } else { |
| 2186 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2187 | |
| 2188 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2189 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2190 | if (DEBUG_FOCUS) { |
| 2191 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2192 | "dropped the pointer down event in display %" PRId32, |
| 2193 | displayId); |
| 2194 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2195 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2196 | goto Failed; |
| 2197 | } |
| 2198 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2199 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2200 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2201 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2202 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2203 | tempTouchState.isSlippery()) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2204 | const int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2205 | const int32_t y = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2206 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2207 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2208 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2209 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2210 | newTouchedWindowHandle = |
| 2211 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2212 | |
| 2213 | // Drop touch events if requested by input feature |
| 2214 | if (newTouchedWindowHandle != nullptr && |
| 2215 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2216 | newTouchedWindowHandle = nullptr; |
| 2217 | } |
| 2218 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2219 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2220 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2221 | if (DEBUG_FOCUS) { |
| 2222 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2223 | oldTouchedWindowHandle->getName().c_str(), |
| 2224 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2225 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2226 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2227 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2228 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2229 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2230 | |
| 2231 | // Make a slippery entrance into the new window. |
| 2232 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2233 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2234 | } |
| 2235 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2236 | int32_t targetFlags = |
| 2237 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2238 | if (isSplit) { |
| 2239 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2240 | } |
| 2241 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2242 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2243 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2244 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | BitSet32 pointerIds; |
| 2248 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2249 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2250 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2251 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2256 | // Update dispatching for hover enter and exit. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2257 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2258 | // Let the previous window know that the hover sequence is over, unless we already did |
| 2259 | // it when dispatching it as is to newTouchedWindowHandle. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2260 | if (mLastHoverWindowHandle != nullptr && |
| 2261 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2262 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2263 | if (DEBUG_HOVER) { |
| 2264 | ALOGD("Sending hover exit event to window %s.", |
| 2265 | mLastHoverWindowHandle->getName().c_str()); |
| 2266 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2267 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2268 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2269 | } |
| 2270 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2271 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2272 | // when dispatching it as is to newTouchedWindowHandle. |
| 2273 | if (newHoverWindowHandle != nullptr && |
| 2274 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2275 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2276 | if (DEBUG_HOVER) { |
| 2277 | ALOGD("Sending hover enter event to window %s.", |
| 2278 | newHoverWindowHandle->getName().c_str()); |
| 2279 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2280 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2281 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2282 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | // Check permission to inject into all touched foreground windows and ensure there |
| 2287 | // is at least one touched foreground window. |
| 2288 | { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2289 | bool haveForegroundOrSpyWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2290 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2291 | const bool isForeground = |
| 2292 | (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0; |
| 2293 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 2294 | haveForegroundOrSpyWindow = true; |
| 2295 | LOG_ALWAYS_FATAL_IF(isForeground, |
| 2296 | "Spy window cannot be dispatched as a foreground window."); |
| 2297 | } |
| 2298 | if (isForeground) { |
| 2299 | haveForegroundOrSpyWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2300 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2301 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2302 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2303 | goto Failed; |
| 2304 | } |
| 2305 | } |
| 2306 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2307 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2308 | if (!haveForegroundOrSpyWindow && !hasGestureMonitor) { |
| 2309 | ALOGI("Dropping event because there is no touched window in display " |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2310 | "%" PRId32 " or gesture monitor to receive it.", |
| 2311 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2312 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2313 | goto Failed; |
| 2314 | } |
| 2315 | |
| 2316 | // Permission granted to injection into all touched foreground windows. |
| 2317 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2318 | } |
| 2319 | |
| 2320 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2321 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2322 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2323 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2324 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2325 | if (foregroundWindowHandle) { |
| 2326 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2327 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2328 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2329 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2330 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2331 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2332 | InputTarget::FLAG_ZERO_COORDS, |
| 2333 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2334 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2335 | } |
| 2336 | } |
| 2337 | } |
| 2338 | } |
| 2339 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2340 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2341 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2342 | // of the touch gesture. |
| 2343 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2344 | // engine only supports touch events. We would need to add a mechanism similar |
| 2345 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2346 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2347 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2348 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2349 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2350 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2351 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2352 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2353 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2354 | if (info->displayId == displayId && |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2355 | windowHandle->getInfo()->type == WindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2356 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2357 | .addOrUpdateWindow(windowHandle, |
| 2358 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2359 | InputTarget:: |
| 2360 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2361 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2362 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2363 | } |
| 2364 | } |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2369 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2370 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2371 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2372 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2373 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2374 | } |
| 2375 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2376 | for (const auto& monitor : tempTouchState.gestureMonitors) { |
| 2377 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2380 | // Drop the outside or hover touch windows since we will not care about them |
| 2381 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2382 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2383 | |
| 2384 | Failed: |
| 2385 | // Check injection permission once and for all. |
| 2386 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2387 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2388 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2389 | } else { |
| 2390 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2391 | } |
| 2392 | } |
| 2393 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2394 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2395 | return injectionResult; |
| 2396 | } |
| 2397 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2398 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2399 | if (!wrongDevice) { |
| 2400 | if (switchedDevice) { |
| 2401 | if (DEBUG_FOCUS) { |
| 2402 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2403 | } |
| 2404 | *outConflictingPointerActions = true; |
| 2405 | } |
| 2406 | |
| 2407 | if (isHoverAction) { |
| 2408 | // Started hovering, therefore no longer down. |
| 2409 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2410 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2411 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2412 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2413 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2414 | *outConflictingPointerActions = true; |
| 2415 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2416 | tempTouchState.reset(); |
| 2417 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2418 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2419 | tempTouchState.deviceId = entry.deviceId; |
| 2420 | tempTouchState.source = entry.source; |
| 2421 | tempTouchState.displayId = displayId; |
| 2422 | } |
| 2423 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2424 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2425 | // All pointers up or canceled. |
| 2426 | tempTouchState.reset(); |
| 2427 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2428 | // First pointer went down. |
| 2429 | if (oldState && oldState->down) { |
| 2430 | if (DEBUG_FOCUS) { |
| 2431 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2432 | } |
| 2433 | *outConflictingPointerActions = true; |
| 2434 | } |
| 2435 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2436 | // One pointer went up. |
| 2437 | if (isSplit) { |
| 2438 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2439 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2440 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2441 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2442 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2443 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2444 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2445 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2446 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2447 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2448 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2449 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2450 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2451 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2452 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2453 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2454 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2455 | // Save changes unless the action was scroll in which case the temporary touch |
| 2456 | // state was only valid for this one action. |
| 2457 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2458 | if (tempTouchState.displayId >= 0) { |
| 2459 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2460 | } else { |
| 2461 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2462 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2463 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2464 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2465 | // Update hover state. |
| 2466 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2467 | } |
| 2468 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2469 | return injectionResult; |
| 2470 | } |
| 2471 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2472 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2473 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2474 | // have an explicit reason to support it. |
| 2475 | constexpr bool isStylus = false; |
| 2476 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2477 | const sp<WindowInfoHandle> dropWindow = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2478 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2479 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2480 | if (dropWindow) { |
| 2481 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2482 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2483 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2484 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2485 | } |
| 2486 | mDragState.reset(); |
| 2487 | } |
| 2488 | |
| 2489 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2490 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2491 | return; |
| 2492 | } |
| 2493 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2494 | if (!mDragState->isStartDrag) { |
| 2495 | mDragState->isStartDrag = true; |
| 2496 | mDragState->isStylusButtonDownAtStart = |
| 2497 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2498 | } |
| 2499 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2500 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2501 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2502 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2503 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2504 | // Handle the special case : stylus button no longer pressed. |
| 2505 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2506 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2507 | finishDragAndDrop(entry.displayId, x, y); |
| 2508 | return; |
| 2509 | } |
| 2510 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2511 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until |
| 2512 | // we have an explicit reason to support it. |
| 2513 | constexpr bool isStylus = false; |
| 2514 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2515 | const sp<WindowInfoHandle> hoverWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2516 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2517 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2518 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2519 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2520 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2521 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2522 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2523 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2524 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2525 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2526 | } |
| 2527 | // enqueue drag location if needed. |
| 2528 | if (hoverWindowHandle != nullptr) { |
| 2529 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2530 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2531 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2532 | finishDragAndDrop(entry.displayId, x, y); |
| 2533 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2534 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2535 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2536 | } |
| 2537 | } |
| 2538 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2539 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2540 | int32_t targetFlags, BitSet32 pointerIds, |
| 2541 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2542 | std::vector<InputTarget>::iterator it = |
| 2543 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2544 | [&windowHandle](const InputTarget& inputTarget) { |
| 2545 | return inputTarget.inputChannel->getConnectionToken() == |
| 2546 | windowHandle->getToken(); |
| 2547 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2548 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2549 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2550 | |
| 2551 | if (it == inputTargets.end()) { |
| 2552 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2553 | std::shared_ptr<InputChannel> inputChannel = |
| 2554 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2555 | if (inputChannel == nullptr) { |
| 2556 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2557 | return; |
| 2558 | } |
| 2559 | inputTarget.inputChannel = inputChannel; |
| 2560 | inputTarget.flags = targetFlags; |
| 2561 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2562 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2563 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2564 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2565 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2566 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2567 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2568 | inputTargets.push_back(inputTarget); |
| 2569 | it = inputTargets.end() - 1; |
| 2570 | } |
| 2571 | |
| 2572 | ALOG_ASSERT(it->flags == targetFlags); |
| 2573 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2574 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2575 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2576 | } |
| 2577 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2578 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2579 | int32_t displayId) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2580 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2581 | mGlobalMonitorsByDisplay.find(displayId); |
| 2582 | |
| 2583 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2584 | const std::vector<Monitor>& monitors = it->second; |
| 2585 | for (const Monitor& monitor : monitors) { |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2586 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2587 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2588 | } |
| 2589 | } |
| 2590 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2591 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, int32_t displayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2592 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2593 | InputTarget target; |
| 2594 | target.inputChannel = monitor.inputChannel; |
| 2595 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2596 | ui::Transform t; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2597 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | b5402b2 | 2021-10-04 05:52:50 -0700 | [diff] [blame] | 2598 | const auto& displayTransform = it->second.transform; |
| 2599 | target.displayTransform = displayTransform; |
| 2600 | t = displayTransform; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2601 | } |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2602 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2603 | inputTargets.push_back(target); |
| 2604 | } |
| 2605 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2606 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2607 | const InjectionState* injectionState) { |
| 2608 | if (injectionState && |
| 2609 | (windowHandle == nullptr || |
| 2610 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2611 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2612 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2613 | 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] | 2614 | "owned by uid %d", |
| 2615 | injectionState->injectorPid, injectionState->injectorUid, |
| 2616 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2617 | } else { |
| 2618 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2619 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2620 | } |
| 2621 | return false; |
| 2622 | } |
| 2623 | return true; |
| 2624 | } |
| 2625 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2626 | /** |
| 2627 | * Indicate whether one window handle should be considered as obscuring |
| 2628 | * another window handle. We only check a few preconditions. Actually |
| 2629 | * checking the bounds is left to the caller. |
| 2630 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2631 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2632 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2633 | // Compare by token so cloned layers aren't counted |
| 2634 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2635 | return false; |
| 2636 | } |
| 2637 | auto info = windowHandle->getInfo(); |
| 2638 | auto otherInfo = otherHandle->getInfo(); |
| 2639 | if (!otherInfo->visible) { |
| 2640 | return false; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2641 | } else if (otherInfo->alpha == 0 && otherInfo->flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2642 | // Those act as if they were invisible, so we don't need to flag them. |
| 2643 | // We do want to potentially flag touchable windows even if they have 0 |
| 2644 | // opacity, since they can consume touches and alter the effects of the |
| 2645 | // user interaction (eg. apps that rely on |
| 2646 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2647 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2648 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2649 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2650 | // If ownerUid is the same we don't generate occlusion events as there |
| 2651 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2652 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2653 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2654 | return false; |
| 2655 | } else if (otherInfo->displayId != info->displayId) { |
| 2656 | return false; |
| 2657 | } |
| 2658 | return true; |
| 2659 | } |
| 2660 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2661 | /** |
| 2662 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2663 | * untrusted, one should check: |
| 2664 | * |
| 2665 | * 1. If result.hasBlockingOcclusion is true. |
| 2666 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2667 | * BLOCK_UNTRUSTED. |
| 2668 | * |
| 2669 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2670 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2671 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2672 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2673 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2674 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2675 | * |
| 2676 | * If neither of those is true, then it means the touch can be allowed. |
| 2677 | */ |
| 2678 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2679 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2680 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2681 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2682 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2683 | TouchOcclusionInfo info; |
| 2684 | info.hasBlockingOcclusion = false; |
| 2685 | info.obscuringOpacity = 0; |
| 2686 | info.obscuringUid = -1; |
| 2687 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2688 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2689 | if (windowHandle == otherHandle) { |
| 2690 | break; // All future windows are below us. Exit early. |
| 2691 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2692 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2693 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2694 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2695 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2696 | info.debugInfo.push_back( |
| 2697 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2698 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2699 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2700 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2701 | // window's touch occlusion mode |
| 2702 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2703 | info.hasBlockingOcclusion = true; |
| 2704 | info.obscuringUid = otherInfo->ownerUid; |
| 2705 | info.obscuringPackage = otherInfo->packageName; |
| 2706 | break; |
| 2707 | } |
| 2708 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2709 | uint32_t uid = otherInfo->ownerUid; |
| 2710 | float opacity = |
| 2711 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2712 | // Given windows A and B: |
| 2713 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2714 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2715 | opacityByUid[uid] = opacity; |
| 2716 | if (opacity > info.obscuringOpacity) { |
| 2717 | info.obscuringOpacity = opacity; |
| 2718 | info.obscuringUid = uid; |
| 2719 | info.obscuringPackage = otherInfo->packageName; |
| 2720 | } |
| 2721 | } |
| 2722 | } |
| 2723 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2724 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2725 | info.debugInfo.push_back( |
| 2726 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2727 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2728 | return info; |
| 2729 | } |
| 2730 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2731 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2732 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2733 | return StringPrintf(INDENT2 |
| 2734 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2735 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2736 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2737 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2738 | isTouchedWindow ? "[TOUCHED] " : "", ftl::enum_string(info->type).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2739 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2740 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2741 | info->frameTop, info->frameRight, info->frameBottom, |
| 2742 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2743 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2744 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2745 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2748 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2749 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2750 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2751 | occlusionInfo.obscuringUid); |
| 2752 | return false; |
| 2753 | } |
| 2754 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2755 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2756 | "%.2f, maximum allowed = %.2f)", |
| 2757 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2758 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2759 | return false; |
| 2760 | } |
| 2761 | return true; |
| 2762 | } |
| 2763 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2764 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2765 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2766 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2767 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2768 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2769 | if (windowHandle == otherHandle) { |
| 2770 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2771 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2772 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2773 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2774 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2775 | return true; |
| 2776 | } |
| 2777 | } |
| 2778 | return false; |
| 2779 | } |
| 2780 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2781 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2782 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2783 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2784 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2785 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2786 | if (windowHandle == otherHandle) { |
| 2787 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2788 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2789 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2790 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2791 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2792 | return true; |
| 2793 | } |
| 2794 | } |
| 2795 | return false; |
| 2796 | } |
| 2797 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2798 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2799 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2800 | if (applicationHandle != nullptr) { |
| 2801 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2802 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2803 | } else { |
| 2804 | return applicationHandle->getName(); |
| 2805 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2806 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2807 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2808 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2809 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2810 | } |
| 2811 | } |
| 2812 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2813 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2814 | if (!isUserActivityEvent(eventEntry)) { |
| 2815 | // 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] | 2816 | return; |
| 2817 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2818 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2819 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2820 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2821 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
| 2822 | if (info->inputFeatures.test(WindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2823 | if (DEBUG_DISPATCH_CYCLE) { |
| 2824 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2825 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2826 | return; |
| 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2831 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2832 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2833 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2834 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2835 | return; |
| 2836 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2837 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2838 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2839 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2840 | } |
| 2841 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2842 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2843 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2844 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2845 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2846 | return; |
| 2847 | } |
| 2848 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2849 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2850 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2851 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2852 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2853 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2854 | break; |
| 2855 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2856 | } |
| 2857 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2858 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2859 | REQUIRES(mLock) { |
| 2860 | scoped_unlock unlock(mLock); |
| 2861 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2862 | }; |
| 2863 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2864 | } |
| 2865 | |
| 2866 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2867 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2868 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2869 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2870 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2871 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2872 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2873 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2874 | ATRACE_NAME(message.c_str()); |
| 2875 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2876 | if (DEBUG_DISPATCH_CYCLE) { |
| 2877 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2878 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2879 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2880 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2881 | inputTarget.getPointerInfoString().c_str()); |
| 2882 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2883 | |
| 2884 | // Skip this event if the connection status is not normal. |
| 2885 | // We don't want to enqueue additional outbound events if the connection is broken. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2886 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2887 | if (DEBUG_DISPATCH_CYCLE) { |
| 2888 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2889 | connection->getInputChannelName().c_str(), |
| 2890 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2891 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2892 | return; |
| 2893 | } |
| 2894 | |
| 2895 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2896 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2897 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2898 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2899 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2900 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2901 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2902 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2903 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2904 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2905 | if (!splitMotionEntry) { |
| 2906 | return; // split event was dropped |
| 2907 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2908 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2909 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2910 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2911 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2912 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2913 | if (DEBUG_FOCUS) { |
| 2914 | ALOGD("channel '%s' ~ Split motion event.", |
| 2915 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2916 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2917 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2918 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2919 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2920 | return; |
| 2921 | } |
| 2922 | } |
| 2923 | |
| 2924 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2925 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2926 | } |
| 2927 | |
| 2928 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2929 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2930 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2931 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2932 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2933 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2934 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2935 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2936 | ATRACE_NAME(message.c_str()); |
| 2937 | } |
| 2938 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2939 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2940 | |
| 2941 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2942 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2943 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2944 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2945 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2946 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2947 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2948 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2949 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2950 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2951 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2952 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2953 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2954 | |
| 2955 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2956 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2957 | startDispatchCycleLocked(currentTime, connection); |
| 2958 | } |
| 2959 | } |
| 2960 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2961 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2962 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2963 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2964 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2965 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2966 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2967 | connection->getInputChannelName().c_str(), |
| 2968 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2969 | ATRACE_NAME(message.c_str()); |
| 2970 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2971 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2972 | if (!(inputTargetFlags & dispatchMode)) { |
| 2973 | return; |
| 2974 | } |
| 2975 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2976 | |
| 2977 | // This is a new event. |
| 2978 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2979 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2980 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2981 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2982 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2983 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2984 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2985 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2986 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2987 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2988 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2989 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2990 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2991 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2992 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2993 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2994 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2995 | if (DEBUG_DISPATCH_CYCLE) { |
| 2996 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 2997 | "event", |
| 2998 | connection->getInputChannelName().c_str()); |
| 2999 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3000 | return; // skip the inconsistent event |
| 3001 | } |
| 3002 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3003 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3004 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3005 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3006 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3007 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3008 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3009 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3010 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3011 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3012 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3013 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 3014 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 3015 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3016 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 3017 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3018 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 3019 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 3020 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 3021 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3022 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3023 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3024 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3025 | } |
| 3026 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3027 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3028 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3029 | if (DEBUG_DISPATCH_CYCLE) { |
| 3030 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3031 | "enter event", |
| 3032 | connection->getInputChannelName().c_str()); |
| 3033 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3034 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3035 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3036 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3037 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3038 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3039 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3040 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3041 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3042 | } |
| 3043 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3044 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3045 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3046 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3047 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3048 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3049 | if (DEBUG_DISPATCH_CYCLE) { |
| 3050 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3051 | "event", |
| 3052 | connection->getInputChannelName().c_str()); |
| 3053 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3054 | return; // skip the inconsistent event |
| 3055 | } |
| 3056 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3057 | dispatchEntry->resolvedEventId = |
| 3058 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3059 | ? mIdGenerator.nextId() |
| 3060 | : motionEntry.id; |
| 3061 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3062 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3063 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3064 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3065 | ATRACE_NAME(message.c_str()); |
| 3066 | } |
| 3067 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3068 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3069 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3070 | // Skip reporting pointer down outside focus to the policy. |
| 3071 | break; |
| 3072 | } |
| 3073 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3074 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3075 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3076 | |
| 3077 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3078 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3079 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3080 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3081 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3082 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3083 | break; |
| 3084 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3085 | case EventEntry::Type::SENSOR: { |
| 3086 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3087 | break; |
| 3088 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3089 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3090 | case EventEntry::Type::DEVICE_RESET: { |
| 3091 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3092 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3093 | break; |
| 3094 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | // Remember that we are waiting for this dispatch to complete. |
| 3098 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3099 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3103 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3104 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3105 | } |
| 3106 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3107 | /** |
| 3108 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3109 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3110 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3111 | * We will see both launcher and wallpaper in that list. |
| 3112 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3113 | * until the set of interacted connections changes. |
| 3114 | * |
| 3115 | * The following items are skipped, to reduce the logspam: |
| 3116 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3117 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3118 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3119 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3120 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3121 | */ |
| 3122 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3123 | const std::vector<InputTarget>& targets) { |
| 3124 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3125 | if (entry.type == EventEntry::Type::KEY) { |
| 3126 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3127 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3128 | return; |
| 3129 | } |
| 3130 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3131 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3132 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3133 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3134 | return; |
| 3135 | } |
| 3136 | } else { |
| 3137 | return; // Not a key or a motion |
| 3138 | } |
| 3139 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3140 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3141 | std::vector<sp<Connection>> newConnections; |
| 3142 | for (const InputTarget& target : targets) { |
| 3143 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3144 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3145 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3146 | } |
| 3147 | |
| 3148 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3149 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3150 | if (connection == nullptr) { |
| 3151 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3152 | } |
| 3153 | newConnectionTokens.insert(std::move(token)); |
| 3154 | newConnections.emplace_back(connection); |
| 3155 | } |
| 3156 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3157 | return; // no change |
| 3158 | } |
| 3159 | mInteractionConnectionTokens = newConnectionTokens; |
| 3160 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3161 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3162 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3163 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3164 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3165 | std::string message = "Interaction with: " + targetList; |
| 3166 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3167 | message += "<none>"; |
| 3168 | } |
| 3169 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3170 | } |
| 3171 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3172 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3173 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3174 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3175 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3176 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3177 | return; |
| 3178 | } |
| 3179 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3180 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3181 | if (focusedToken == token) { |
| 3182 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3183 | return; |
| 3184 | } |
| 3185 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3186 | auto command = [this, token]() REQUIRES(mLock) { |
| 3187 | scoped_unlock unlock(mLock); |
| 3188 | mPolicy->onPointerDownOutsideFocus(token); |
| 3189 | }; |
| 3190 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3194 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3195 | if (ATRACE_ENABLED()) { |
| 3196 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3197 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3198 | ATRACE_NAME(message.c_str()); |
| 3199 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3200 | if (DEBUG_DISPATCH_CYCLE) { |
| 3201 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3202 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3203 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3204 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3205 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3206 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3207 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3208 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3209 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3210 | |
| 3211 | // Publish the event. |
| 3212 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3213 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3214 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3215 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3216 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3217 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3218 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3219 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3220 | status = connection->inputPublisher |
| 3221 | .publishKeyEvent(dispatchEntry->seq, |
| 3222 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3223 | keyEntry.source, keyEntry.displayId, |
| 3224 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3225 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3226 | keyEntry.scanCode, keyEntry.metaState, |
| 3227 | keyEntry.repeatCount, keyEntry.downTime, |
| 3228 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3229 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3230 | } |
| 3231 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3232 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3233 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3234 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3235 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3236 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3237 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3238 | // 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] | 3239 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3240 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3241 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3242 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3243 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3244 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3245 | // Don't apply window scale here since we don't want scale to affect raw |
| 3246 | // coordinates. The scale will be sent back to the client and applied |
| 3247 | // later when requesting relative coordinates. |
| 3248 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3249 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3250 | } |
| 3251 | usingCoords = scaledCoords; |
| 3252 | } |
| 3253 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3254 | // We don't want the dispatch target to know. |
| 3255 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3256 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3257 | scaledCoords[i].clear(); |
| 3258 | } |
| 3259 | usingCoords = scaledCoords; |
| 3260 | } |
| 3261 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3262 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3263 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3264 | |
| 3265 | // Publish the motion event. |
| 3266 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3267 | .publishMotionEvent(dispatchEntry->seq, |
| 3268 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3269 | motionEntry.deviceId, motionEntry.source, |
| 3270 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3271 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3272 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3273 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3274 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3275 | motionEntry.buttonState, |
| 3276 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3277 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3278 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3279 | motionEntry.xCursorPosition, |
| 3280 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3281 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3282 | motionEntry.downTime, motionEntry.eventTime, |
| 3283 | motionEntry.pointerCount, |
| 3284 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3285 | break; |
| 3286 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3287 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3288 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3289 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3290 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3291 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3292 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3293 | break; |
| 3294 | } |
| 3295 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3296 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3297 | const TouchModeEntry& touchModeEntry = |
| 3298 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3299 | status = connection->inputPublisher |
| 3300 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3301 | touchModeEntry.inTouchMode); |
| 3302 | |
| 3303 | break; |
| 3304 | } |
| 3305 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3306 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3307 | const auto& captureEntry = |
| 3308 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3309 | status = connection->inputPublisher |
| 3310 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3311 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3312 | break; |
| 3313 | } |
| 3314 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3315 | case EventEntry::Type::DRAG: { |
| 3316 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3317 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3318 | dragEntry.id, dragEntry.x, |
| 3319 | dragEntry.y, |
| 3320 | dragEntry.isExiting); |
| 3321 | break; |
| 3322 | } |
| 3323 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3324 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3325 | case EventEntry::Type::DEVICE_RESET: |
| 3326 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3327 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3328 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3329 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3330 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | // Check the result. |
| 3334 | if (status) { |
| 3335 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3336 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3337 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3338 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3339 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3340 | "event to it, status=%s(%d)", |
| 3341 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3342 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3343 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3344 | } else { |
| 3345 | // Pipe is full and we are waiting for the app to finish process some events |
| 3346 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3347 | if (DEBUG_DISPATCH_CYCLE) { |
| 3348 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3349 | "waiting for the application to catch up", |
| 3350 | connection->getInputChannelName().c_str()); |
| 3351 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3352 | } |
| 3353 | } else { |
| 3354 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3355 | "status=%s(%d)", |
| 3356 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3357 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3358 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3359 | } |
| 3360 | return; |
| 3361 | } |
| 3362 | |
| 3363 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3364 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3365 | connection->outboundQueue.end(), |
| 3366 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3367 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3368 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3369 | if (connection->responsive) { |
| 3370 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3371 | connection->inputChannel->getConnectionToken()); |
| 3372 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3373 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3374 | } |
| 3375 | } |
| 3376 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3377 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3378 | size_t size; |
| 3379 | switch (event.type) { |
| 3380 | case VerifiedInputEvent::Type::KEY: { |
| 3381 | size = sizeof(VerifiedKeyEvent); |
| 3382 | break; |
| 3383 | } |
| 3384 | case VerifiedInputEvent::Type::MOTION: { |
| 3385 | size = sizeof(VerifiedMotionEvent); |
| 3386 | break; |
| 3387 | } |
| 3388 | } |
| 3389 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3390 | return mHmacKeyManager.sign(start, size); |
| 3391 | } |
| 3392 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3393 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3394 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3395 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3396 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3397 | // Only sign events up and down events as the purely move events |
| 3398 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3399 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3400 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3401 | |
| 3402 | VerifiedMotionEvent verifiedEvent = |
| 3403 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3404 | verifiedEvent.actionMasked = actionMasked; |
| 3405 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3406 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3410 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3411 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3412 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3413 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3414 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3415 | } |
| 3416 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3417 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3418 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3419 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3420 | if (DEBUG_DISPATCH_CYCLE) { |
| 3421 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3422 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3423 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3424 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3425 | if (connection->status == Connection::Status::BROKEN || |
| 3426 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3427 | return; |
| 3428 | } |
| 3429 | |
| 3430 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3431 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3432 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3433 | }; |
| 3434 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3438 | const sp<Connection>& connection, |
| 3439 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3440 | if (DEBUG_DISPATCH_CYCLE) { |
| 3441 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3442 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3443 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3444 | |
| 3445 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3446 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3447 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3448 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3449 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3450 | |
| 3451 | // The connection appears to be unrecoverably broken. |
| 3452 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3453 | if (connection->status == Connection::Status::NORMAL) { |
| 3454 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3455 | |
| 3456 | if (notify) { |
| 3457 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3458 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3459 | connection->getInputChannelName().c_str()); |
| 3460 | |
| 3461 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3462 | scoped_unlock unlock(mLock); |
| 3463 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3464 | }; |
| 3465 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3466 | } |
| 3467 | } |
| 3468 | } |
| 3469 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3470 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3471 | while (!queue.empty()) { |
| 3472 | DispatchEntry* dispatchEntry = queue.front(); |
| 3473 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3474 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3475 | } |
| 3476 | } |
| 3477 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3478 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3479 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3480 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3481 | } |
| 3482 | delete dispatchEntry; |
| 3483 | } |
| 3484 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3485 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3486 | std::scoped_lock _l(mLock); |
| 3487 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3488 | if (connection == nullptr) { |
| 3489 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3490 | connectionToken.get(), events); |
| 3491 | return 0; // remove the callback |
| 3492 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3493 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3494 | bool notify; |
| 3495 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3496 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3497 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3498 | "events=0x%x", |
| 3499 | connection->getInputChannelName().c_str(), events); |
| 3500 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3501 | } |
| 3502 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3503 | nsecs_t currentTime = now(); |
| 3504 | bool gotOne = false; |
| 3505 | status_t status = OK; |
| 3506 | for (;;) { |
| 3507 | Result<InputPublisher::ConsumerResponse> result = |
| 3508 | connection->inputPublisher.receiveConsumerResponse(); |
| 3509 | if (!result.ok()) { |
| 3510 | status = result.error().code(); |
| 3511 | break; |
| 3512 | } |
| 3513 | |
| 3514 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3515 | const InputPublisher::Finished& finish = |
| 3516 | std::get<InputPublisher::Finished>(*result); |
| 3517 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3518 | finish.consumeTime); |
| 3519 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3520 | if (shouldReportMetricsForConnection(*connection)) { |
| 3521 | const InputPublisher::Timeline& timeline = |
| 3522 | std::get<InputPublisher::Timeline>(*result); |
| 3523 | mLatencyTracker |
| 3524 | .trackGraphicsLatency(timeline.inputEventId, |
| 3525 | connection->inputChannel->getConnectionToken(), |
| 3526 | std::move(timeline.graphicsTimeline)); |
| 3527 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3528 | } |
| 3529 | gotOne = true; |
| 3530 | } |
| 3531 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3532 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3533 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3534 | return 1; |
| 3535 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3536 | } |
| 3537 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3538 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3539 | if (notify) { |
| 3540 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3541 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3542 | status); |
| 3543 | } |
| 3544 | } else { |
| 3545 | // Monitor channels are never explicitly unregistered. |
| 3546 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3547 | const bool stillHaveWindowHandle = |
| 3548 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3549 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3550 | if (notify) { |
| 3551 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3552 | connection->getInputChannelName().c_str(), events); |
| 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | // Remove the channel. |
| 3557 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3558 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3559 | } |
| 3560 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3561 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3562 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3563 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3564 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3565 | } |
| 3566 | } |
| 3567 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3568 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3569 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3570 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3571 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3572 | } |
| 3573 | |
| 3574 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3575 | const CancelationOptions& options, |
| 3576 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3577 | for (const auto& it : monitorsByDisplay) { |
| 3578 | const std::vector<Monitor>& monitors = it.second; |
| 3579 | for (const Monitor& monitor : monitors) { |
| 3580 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3581 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3582 | } |
| 3583 | } |
| 3584 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3585 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3586 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3587 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3588 | if (connection == nullptr) { |
| 3589 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3590 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3591 | |
| 3592 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3593 | } |
| 3594 | |
| 3595 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3596 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3597 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3598 | return; |
| 3599 | } |
| 3600 | |
| 3601 | nsecs_t currentTime = now(); |
| 3602 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3603 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3604 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3605 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3606 | if (cancelationEvents.empty()) { |
| 3607 | return; |
| 3608 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3609 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3610 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3611 | "with reality: %s, mode=%d.", |
| 3612 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3613 | options.mode); |
| 3614 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3615 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3616 | std::string reason = std::string("reason=").append(options.reason); |
| 3617 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3618 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3619 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3620 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3621 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3622 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3623 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3624 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3625 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3626 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3627 | } |
| 3628 | target.inputChannel = connection->inputChannel; |
| 3629 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3630 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3631 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3632 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3633 | switch (cancelationEventEntry->type) { |
| 3634 | case EventEntry::Type::KEY: { |
| 3635 | logOutboundKeyDetails("cancel - ", |
| 3636 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3637 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3638 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3639 | case EventEntry::Type::MOTION: { |
| 3640 | logOutboundMotionDetails("cancel - ", |
| 3641 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3642 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3643 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3644 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3645 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3646 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3647 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3648 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3649 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3650 | break; |
| 3651 | } |
| 3652 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3653 | case EventEntry::Type::DEVICE_RESET: |
| 3654 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3655 | 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] | 3656 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3657 | break; |
| 3658 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3659 | } |
| 3660 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3661 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3662 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3663 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3664 | |
| 3665 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3666 | } |
| 3667 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3668 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3669 | const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3670 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3671 | return; |
| 3672 | } |
| 3673 | |
| 3674 | nsecs_t currentTime = now(); |
| 3675 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3676 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3677 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3678 | |
| 3679 | if (downEvents.empty()) { |
| 3680 | return; |
| 3681 | } |
| 3682 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3683 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3684 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3685 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3686 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3687 | |
| 3688 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3689 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3690 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3691 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3692 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3693 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3694 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3695 | } |
| 3696 | target.inputChannel = connection->inputChannel; |
| 3697 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3698 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3699 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3700 | switch (downEventEntry->type) { |
| 3701 | case EventEntry::Type::MOTION: { |
| 3702 | logOutboundMotionDetails("down - ", |
| 3703 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3704 | break; |
| 3705 | } |
| 3706 | |
| 3707 | case EventEntry::Type::KEY: |
| 3708 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3709 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3710 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3711 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3712 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3713 | case EventEntry::Type::SENSOR: |
| 3714 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3715 | 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] | 3716 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3717 | break; |
| 3718 | } |
| 3719 | } |
| 3720 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3721 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3722 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | startDispatchCycleLocked(currentTime, connection); |
| 3726 | } |
| 3727 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3728 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3729 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3730 | ALOG_ASSERT(pointerIds.value != 0); |
| 3731 | |
| 3732 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3733 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3734 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3735 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3736 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3737 | uint32_t splitPointerCount = 0; |
| 3738 | |
| 3739 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3740 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3741 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3742 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3743 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3744 | if (pointerIds.hasBit(pointerId)) { |
| 3745 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3746 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3747 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3748 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3749 | splitPointerCount += 1; |
| 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | if (splitPointerCount != pointerIds.count()) { |
| 3754 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3755 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3756 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3757 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3758 | // in this way. |
| 3759 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3760 | "we expected there to be %d pointers. This probably means we received " |
| 3761 | "a broken sequence of pointer ids from the input device.", |
| 3762 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3763 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3764 | } |
| 3765 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3766 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3767 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3768 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3769 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3770 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3771 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3772 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3773 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3774 | if (pointerIds.hasBit(pointerId)) { |
| 3775 | if (pointerIds.count() == 1) { |
| 3776 | // The first/last pointer went down/up. |
| 3777 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3778 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3779 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3780 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3781 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3782 | } else { |
| 3783 | // A secondary pointer went down/up. |
| 3784 | uint32_t splitPointerIndex = 0; |
| 3785 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3786 | splitPointerIndex += 1; |
| 3787 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3788 | action = maskedAction | |
| 3789 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3790 | } |
| 3791 | } else { |
| 3792 | // An unrelated pointer changed. |
| 3793 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3794 | } |
| 3795 | } |
| 3796 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3797 | int32_t newId = mIdGenerator.nextId(); |
| 3798 | if (ATRACE_ENABLED()) { |
| 3799 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3800 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3801 | originalMotionEntry.id, newId); |
| 3802 | ATRACE_NAME(message.c_str()); |
| 3803 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3804 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3805 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3806 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3807 | originalMotionEntry.displayId, |
| 3808 | originalMotionEntry.policyFlags, action, |
| 3809 | originalMotionEntry.actionButton, |
| 3810 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3811 | originalMotionEntry.buttonState, |
| 3812 | originalMotionEntry.classification, |
| 3813 | originalMotionEntry.edgeFlags, |
| 3814 | originalMotionEntry.xPrecision, |
| 3815 | originalMotionEntry.yPrecision, |
| 3816 | originalMotionEntry.xCursorPosition, |
| 3817 | originalMotionEntry.yCursorPosition, |
| 3818 | originalMotionEntry.downTime, splitPointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3819 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3820 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3821 | if (originalMotionEntry.injectionState) { |
| 3822 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3823 | splitMotionEntry->injectionState->refCount += 1; |
| 3824 | } |
| 3825 | |
| 3826 | return splitMotionEntry; |
| 3827 | } |
| 3828 | |
| 3829 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3830 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3831 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3832 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3833 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3834 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3835 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3836 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3837 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3838 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3839 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3840 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3841 | } // release lock |
| 3842 | |
| 3843 | if (needWake) { |
| 3844 | mLooper->wake(); |
| 3845 | } |
| 3846 | } |
| 3847 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3848 | /** |
| 3849 | * If one of the meta shortcuts is detected, process them here: |
| 3850 | * Meta + Backspace -> generate BACK |
| 3851 | * Meta + Enter -> generate HOME |
| 3852 | * This will potentially overwrite keyCode and metaState. |
| 3853 | */ |
| 3854 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3855 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3856 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3857 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3858 | if (keyCode == AKEYCODE_DEL) { |
| 3859 | newKeyCode = AKEYCODE_BACK; |
| 3860 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3861 | newKeyCode = AKEYCODE_HOME; |
| 3862 | } |
| 3863 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3864 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3865 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3866 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3867 | keyCode = newKeyCode; |
| 3868 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3869 | } |
| 3870 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3871 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3872 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3873 | // 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] | 3874 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3875 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3876 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3877 | if (replacementIt != mReplacedKeys.end()) { |
| 3878 | keyCode = replacementIt->second; |
| 3879 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3880 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3881 | } |
| 3882 | } |
| 3883 | } |
| 3884 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3885 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3886 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3887 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3888 | "policyFlags=0x%x, action=0x%x, " |
| 3889 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3890 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3891 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3892 | args->downTime); |
| 3893 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3894 | if (!validateKeyEvent(args->action)) { |
| 3895 | return; |
| 3896 | } |
| 3897 | |
| 3898 | uint32_t policyFlags = args->policyFlags; |
| 3899 | int32_t flags = args->flags; |
| 3900 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3901 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3902 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3903 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3904 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3905 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3906 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3907 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3908 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3909 | metaState |= AMETA_FUNCTION_ON; |
| 3910 | } |
| 3911 | |
| 3912 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3913 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3914 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3915 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3916 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3917 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3918 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3919 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3920 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3921 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3922 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3923 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3924 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3925 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3926 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3927 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3928 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3929 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3930 | { // acquire lock |
| 3931 | mLock.lock(); |
| 3932 | |
| 3933 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3934 | mLock.unlock(); |
| 3935 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3936 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3937 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3938 | return; // event was consumed by the filter |
| 3939 | } |
| 3940 | |
| 3941 | mLock.lock(); |
| 3942 | } |
| 3943 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3944 | std::unique_ptr<KeyEntry> newEntry = |
| 3945 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3946 | args->displayId, policyFlags, args->action, flags, |
| 3947 | keyCode, args->scanCode, metaState, repeatCount, |
| 3948 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3949 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3950 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3951 | mLock.unlock(); |
| 3952 | } // release lock |
| 3953 | |
| 3954 | if (needWake) { |
| 3955 | mLooper->wake(); |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3960 | return mInputFilterEnabled; |
| 3961 | } |
| 3962 | |
| 3963 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3964 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3965 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3966 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 3967 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3968 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 3969 | "yCursorPosition=%f, downTime=%" PRId64, |
| 3970 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3971 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3972 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3973 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 3974 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3975 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 3976 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3977 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3978 | "orientation=%f", |
| 3979 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3980 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3981 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3982 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3983 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3984 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3985 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3986 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3987 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3988 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 3989 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3990 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3991 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3992 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3993 | return; |
| 3994 | } |
| 3995 | |
| 3996 | uint32_t policyFlags = args->policyFlags; |
| 3997 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3998 | |
| 3999 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4000 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4001 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4002 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4003 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4004 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4005 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4006 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4007 | { // acquire lock |
| 4008 | mLock.lock(); |
| 4009 | |
| 4010 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4011 | ui::Transform displayTransform; |
| 4012 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4013 | displayTransform = it->second.transform; |
| 4014 | } |
| 4015 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4016 | mLock.unlock(); |
| 4017 | |
| 4018 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4019 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4020 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4021 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4022 | displayTransform, args->xPrecision, args->yPrecision, |
| 4023 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4024 | args->downTime, args->eventTime, args->pointerCount, |
| 4025 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4026 | |
| 4027 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4028 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4029 | return; // event was consumed by the filter |
| 4030 | } |
| 4031 | |
| 4032 | mLock.lock(); |
| 4033 | } |
| 4034 | |
| 4035 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4036 | std::unique_ptr<MotionEntry> newEntry = |
| 4037 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4038 | args->source, args->displayId, policyFlags, |
| 4039 | args->action, args->actionButton, args->flags, |
| 4040 | args->metaState, args->buttonState, |
| 4041 | args->classification, args->edgeFlags, |
| 4042 | args->xPrecision, args->yPrecision, |
| 4043 | args->xCursorPosition, args->yCursorPosition, |
| 4044 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4045 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4046 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4047 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4048 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4049 | !mInputFilterEnabled) { |
| 4050 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4051 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4052 | } |
| 4053 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4054 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4055 | mLock.unlock(); |
| 4056 | } // release lock |
| 4057 | |
| 4058 | if (needWake) { |
| 4059 | mLooper->wake(); |
| 4060 | } |
| 4061 | } |
| 4062 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4063 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4064 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4065 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4066 | " sensorType=%s", |
| 4067 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4068 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4069 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4070 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4071 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4072 | { // acquire lock |
| 4073 | mLock.lock(); |
| 4074 | |
| 4075 | // Just enqueue a new sensor event. |
| 4076 | std::unique_ptr<SensorEntry> newEntry = |
| 4077 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4078 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4079 | args->sensorType, args->accuracy, |
| 4080 | args->accuracyChanged, args->values); |
| 4081 | |
| 4082 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4083 | mLock.unlock(); |
| 4084 | } // release lock |
| 4085 | |
| 4086 | if (needWake) { |
| 4087 | mLooper->wake(); |
| 4088 | } |
| 4089 | } |
| 4090 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4091 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4092 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4093 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4094 | args->deviceId, args->isOn); |
| 4095 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4096 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4097 | } |
| 4098 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4099 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4100 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4104 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4105 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4106 | "switchMask=0x%08x", |
| 4107 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4108 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4109 | |
| 4110 | uint32_t policyFlags = args->policyFlags; |
| 4111 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4112 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4113 | } |
| 4114 | |
| 4115 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4116 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4117 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4118 | args->deviceId); |
| 4119 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4120 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4121 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4122 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4123 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4124 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4125 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4126 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4127 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4128 | } // release lock |
| 4129 | |
| 4130 | if (needWake) { |
| 4131 | mLooper->wake(); |
| 4132 | } |
| 4133 | } |
| 4134 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4135 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4136 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4137 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4138 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4139 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4140 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4141 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4142 | { // acquire lock |
| 4143 | std::scoped_lock _l(mLock); |
| 4144 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4145 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4146 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4147 | } // release lock |
| 4148 | |
| 4149 | if (needWake) { |
| 4150 | mLooper->wake(); |
| 4151 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4152 | } |
| 4153 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4154 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4155 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4156 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4157 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4158 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 4159 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4160 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
| 4161 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4162 | 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] | 4163 | |
| 4164 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4165 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4166 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4167 | } |
| 4168 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4169 | // 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] | 4170 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4171 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4172 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4173 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4174 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4175 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4176 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4177 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4180 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4181 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4182 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4183 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4184 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4185 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4186 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4187 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4188 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4189 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4190 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4191 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4192 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4193 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4194 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4195 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4196 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4197 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4198 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4199 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4200 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4201 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4202 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4203 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4204 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4205 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4206 | |
| 4207 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4208 | android::base::Timer t; |
| 4209 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4210 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4211 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4212 | std::to_string(t.duration().count()).c_str()); |
| 4213 | } |
| 4214 | } |
| 4215 | |
| 4216 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4217 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4218 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4219 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4220 | incomingKey.getDisplayId(), policyFlags, action, |
| 4221 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4222 | incomingKey.getRepeatCount(), |
| 4223 | incomingKey.getDownTime()); |
| 4224 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4225 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4226 | } |
| 4227 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4228 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4229 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4230 | const int32_t action = motionEvent.getAction(); |
| 4231 | const bool isPointerEvent = |
| 4232 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4233 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4234 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4235 | ? ADISPLAY_ID_DEFAULT |
| 4236 | : event->getDisplayId(); |
| 4237 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4238 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4239 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4240 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4241 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4242 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4246 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4247 | android::base::Timer t; |
| 4248 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4249 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4250 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4251 | std::to_string(t.duration().count()).c_str()); |
| 4252 | } |
| 4253 | } |
| 4254 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4255 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4256 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4257 | } |
| 4258 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4259 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4260 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4261 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4262 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4263 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4264 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4265 | displayId, policyFlags, action, actionButton, |
| 4266 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4267 | motionEvent.getButtonState(), |
| 4268 | motionEvent.getClassification(), |
| 4269 | motionEvent.getEdgeFlags(), |
| 4270 | motionEvent.getXPrecision(), |
| 4271 | motionEvent.getYPrecision(), |
| 4272 | motionEvent.getRawXCursorPosition(), |
| 4273 | motionEvent.getRawYCursorPosition(), |
| 4274 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4275 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4276 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4277 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4278 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4279 | sampleEventTimes += 1; |
| 4280 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4281 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4282 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4283 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4284 | displayId, policyFlags, action, actionButton, |
| 4285 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4286 | motionEvent.getButtonState(), |
| 4287 | motionEvent.getClassification(), |
| 4288 | motionEvent.getEdgeFlags(), |
| 4289 | motionEvent.getXPrecision(), |
| 4290 | motionEvent.getYPrecision(), |
| 4291 | motionEvent.getRawXCursorPosition(), |
| 4292 | motionEvent.getRawYCursorPosition(), |
| 4293 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4294 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4295 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4296 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4297 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4298 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4299 | } |
| 4300 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4301 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4302 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4303 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4304 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4305 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4309 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4310 | injectionState->injectionIsAsync = true; |
| 4311 | } |
| 4312 | |
| 4313 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4314 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4315 | |
| 4316 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4317 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4318 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4319 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4320 | } |
| 4321 | |
| 4322 | mLock.unlock(); |
| 4323 | |
| 4324 | if (needWake) { |
| 4325 | mLooper->wake(); |
| 4326 | } |
| 4327 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4328 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4329 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4330 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4331 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4332 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4333 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4334 | } else { |
| 4335 | for (;;) { |
| 4336 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4337 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4338 | break; |
| 4339 | } |
| 4340 | |
| 4341 | nsecs_t remainingTimeout = endTime - now(); |
| 4342 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4343 | if (DEBUG_INJECTION) { |
| 4344 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4345 | "to become available."); |
| 4346 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4347 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4348 | break; |
| 4349 | } |
| 4350 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4351 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4352 | } |
| 4353 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4354 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4355 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4356 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4357 | if (DEBUG_INJECTION) { |
| 4358 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4359 | injectionState->pendingForegroundDispatches); |
| 4360 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4361 | nsecs_t remainingTimeout = endTime - now(); |
| 4362 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4363 | if (DEBUG_INJECTION) { |
| 4364 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4365 | "dispatches to finish."); |
| 4366 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4367 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4368 | break; |
| 4369 | } |
| 4370 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4371 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4372 | } |
| 4373 | } |
| 4374 | } |
| 4375 | |
| 4376 | injectionState->release(); |
| 4377 | } // release lock |
| 4378 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4379 | if (DEBUG_INJECTION) { |
| 4380 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
| 4381 | injectionResult, injectorPid, injectorUid); |
| 4382 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4383 | |
| 4384 | return injectionResult; |
| 4385 | } |
| 4386 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4387 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4388 | std::array<uint8_t, 32> calculatedHmac; |
| 4389 | std::unique_ptr<VerifiedInputEvent> result; |
| 4390 | switch (event.getType()) { |
| 4391 | case AINPUT_EVENT_TYPE_KEY: { |
| 4392 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4393 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4394 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4395 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4396 | break; |
| 4397 | } |
| 4398 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4399 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4400 | VerifiedMotionEvent verifiedMotionEvent = |
| 4401 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4402 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4403 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4404 | break; |
| 4405 | } |
| 4406 | default: { |
| 4407 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4408 | return nullptr; |
| 4409 | } |
| 4410 | } |
| 4411 | if (calculatedHmac == INVALID_HMAC) { |
| 4412 | return nullptr; |
| 4413 | } |
| 4414 | if (calculatedHmac != event.getHmac()) { |
| 4415 | return nullptr; |
| 4416 | } |
| 4417 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4418 | } |
| 4419 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4420 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4421 | return injectorUid == 0 || |
| 4422 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4423 | } |
| 4424 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4425 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4426 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4427 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4428 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4429 | if (DEBUG_INJECTION) { |
| 4430 | ALOGD("Setting input event injection result to %d. " |
| 4431 | "injectorPid=%d, injectorUid=%d", |
| 4432 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
| 4433 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4434 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4435 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4436 | // Log the outcome since the injector did not wait for the injection result. |
| 4437 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4438 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4439 | ALOGV("Asynchronous input event injection succeeded."); |
| 4440 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4441 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4442 | ALOGW("Asynchronous input event injection failed."); |
| 4443 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4444 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4445 | ALOGW("Asynchronous input event injection permission denied."); |
| 4446 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4447 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4448 | ALOGW("Asynchronous input event injection timed out."); |
| 4449 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4450 | case InputEventInjectionResult::PENDING: |
| 4451 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4452 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4453 | } |
| 4454 | } |
| 4455 | |
| 4456 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4457 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4458 | } |
| 4459 | } |
| 4460 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4461 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4462 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4463 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4464 | // display space, so we need to transform the injected events accordingly. |
| 4465 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4466 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4467 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4468 | |
| 4469 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 4470 | PointerCoords& pc = entry.pointerCoords[i]; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4471 | // Make a copy of the injected coords. We cannot change them in place because some of them |
| 4472 | // are interdependent (for example, X coordinate might depend on the Y coordinate). |
| 4473 | PointerCoords injectedCoords = entry.pointerCoords[i]; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4474 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4475 | BitSet64 bits(injectedCoords.bits); |
| 4476 | while (!bits.isEmpty()) { |
| 4477 | const auto axis = static_cast<int32_t>(bits.clearFirstMarkedBit()); |
| 4478 | const float value = |
| 4479 | MotionEvent::calculateTransformedAxisValue(axis, entry.source, |
| 4480 | transformToDisplay, injectedCoords); |
| 4481 | pc.setAxisValue(axis, value); |
| 4482 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4483 | } |
| 4484 | } |
| 4485 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4486 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4487 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4488 | if (injectionState) { |
| 4489 | injectionState->pendingForegroundDispatches += 1; |
| 4490 | } |
| 4491 | } |
| 4492 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4493 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4494 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4495 | if (injectionState) { |
| 4496 | injectionState->pendingForegroundDispatches -= 1; |
| 4497 | |
| 4498 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4499 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4500 | } |
| 4501 | } |
| 4502 | } |
| 4503 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4504 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4505 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4506 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4507 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4508 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4509 | } |
| 4510 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4511 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4512 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4513 | if (windowHandleToken == nullptr) { |
| 4514 | return nullptr; |
| 4515 | } |
| 4516 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4517 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4518 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4519 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4520 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4521 | return windowHandle; |
| 4522 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4523 | } |
| 4524 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4525 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4526 | } |
| 4527 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4528 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4529 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4530 | if (windowHandleToken == nullptr) { |
| 4531 | return nullptr; |
| 4532 | } |
| 4533 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4534 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4535 | if (windowHandle->getToken() == windowHandleToken) { |
| 4536 | return windowHandle; |
| 4537 | } |
| 4538 | } |
| 4539 | return nullptr; |
| 4540 | } |
| 4541 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4542 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4543 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4544 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4545 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4546 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4547 | if (handle->getId() == windowHandle->getId() && |
| 4548 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4549 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4550 | ALOGE("Found window %s in display %" PRId32 |
| 4551 | ", but it should belong to display %" PRId32, |
| 4552 | windowHandle->getName().c_str(), it.first, |
| 4553 | windowHandle->getInfo()->displayId); |
| 4554 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4555 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4556 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4557 | } |
| 4558 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4559 | return nullptr; |
| 4560 | } |
| 4561 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4562 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4563 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4564 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4565 | } |
| 4566 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4567 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4568 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4569 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4570 | windowHandle.getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4571 | if (connection != nullptr && noInputChannel) { |
| 4572 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4573 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4574 | return false; |
| 4575 | } |
| 4576 | |
| 4577 | if (connection == nullptr) { |
| 4578 | if (!noInputChannel) { |
| 4579 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4580 | } |
| 4581 | return false; |
| 4582 | } |
| 4583 | if (!connection->responsive) { |
| 4584 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4585 | return false; |
| 4586 | } |
| 4587 | return true; |
| 4588 | } |
| 4589 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4590 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4591 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4592 | auto connectionIt = mConnectionsByToken.find(token); |
| 4593 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4594 | return nullptr; |
| 4595 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4596 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4597 | } |
| 4598 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4599 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4600 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4601 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4602 | // Remove all handles on a display if there are no windows left. |
| 4603 | mWindowHandlesByDisplay.erase(displayId); |
| 4604 | return; |
| 4605 | } |
| 4606 | |
| 4607 | // Since we compare the pointer of input window handles across window updates, we need |
| 4608 | // 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] | 4609 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4610 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4611 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4612 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4613 | } |
| 4614 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4615 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4616 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4617 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4618 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4619 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4620 | info->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4621 | const bool canReceiveInput = !info->flags.test(WindowInfo::Flag::NOT_TOUCHABLE) || |
| 4622 | !info->flags.test(WindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4623 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4624 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4625 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4626 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4627 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | if (info->displayId != displayId) { |
| 4631 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4632 | handle->getName().c_str(), displayId, info->displayId); |
| 4633 | continue; |
| 4634 | } |
| 4635 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4636 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4637 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4638 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4639 | oldHandle->updateFrom(handle); |
| 4640 | newHandles.push_back(oldHandle); |
| 4641 | } else { |
| 4642 | newHandles.push_back(handle); |
| 4643 | } |
| 4644 | } |
| 4645 | |
| 4646 | // Insert or replace |
| 4647 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4648 | } |
| 4649 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4650 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4651 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4652 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4653 | { // acquire lock |
| 4654 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4655 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4656 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4657 | } |
| 4658 | } |
| 4659 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4660 | mLooper->wake(); |
| 4661 | } |
| 4662 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4663 | /** |
| 4664 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4665 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4666 | * If set an empty list, remove all handles from the specific display. |
| 4667 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4668 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4669 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4670 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4671 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4672 | if (DEBUG_FOCUS) { |
| 4673 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4674 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4675 | windowList += iwh->getName() + " "; |
| 4676 | } |
| 4677 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4678 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4679 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4680 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4681 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4682 | const WindowInfo& info = *window->getInfo(); |
| 4683 | |
| 4684 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 4685 | const bool noInputWindow = info.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4686 | if (noInputWindow && window->getToken() != nullptr) { |
| 4687 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4688 | window->getName().c_str()); |
| 4689 | window->releaseChannel(); |
| 4690 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4691 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4692 | // Ensure all spy windows are trusted overlays |
| 4693 | LOG_ALWAYS_FATAL_IF(info.isSpy() && !info.trustedOverlay, |
| 4694 | "%s has feature SPY, but is not a trusted overlay.", |
| 4695 | window->getName().c_str()); |
| 4696 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4697 | // Ensure all stylus interceptors are trusted overlays |
| 4698 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && !info.trustedOverlay, |
| 4699 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4700 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4701 | } |
| 4702 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4703 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4704 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4705 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4706 | // Save the old windows' orientation by ID before it gets updated. |
| 4707 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4708 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4709 | oldWindowOrientations.emplace(handle->getId(), |
| 4710 | handle->getInfo()->transform.getOrientation()); |
| 4711 | } |
| 4712 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4713 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4714 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4715 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4716 | if (mLastHoverWindowHandle && |
| 4717 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4718 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4719 | mLastHoverWindowHandle = nullptr; |
| 4720 | } |
| 4721 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4722 | std::optional<FocusResolver::FocusChanges> changes = |
| 4723 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4724 | if (changes) { |
| 4725 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4726 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4727 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4728 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4729 | mTouchStatesByDisplay.find(displayId); |
| 4730 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4731 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4732 | for (size_t i = 0; i < state.windows.size();) { |
| 4733 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4734 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4735 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4736 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4737 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4738 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4739 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4740 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4741 | if (touchedInputChannel != nullptr) { |
| 4742 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4743 | "touched window was removed"); |
| 4744 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4745 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4746 | // well. |
| 4747 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
| 4748 | touchedWindow.windowHandle->getInfo()->hasWallpaper) { |
| 4749 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4750 | if (wallpaper != nullptr) { |
| 4751 | sp<Connection> wallpaperConnection = |
| 4752 | getConnectionLocked(wallpaper->getToken()); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4753 | if (wallpaperConnection != nullptr) { |
| 4754 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4755 | options); |
| 4756 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4757 | } |
| 4758 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4759 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4760 | state.windows.erase(state.windows.begin() + i); |
| 4761 | } else { |
| 4762 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4763 | } |
| 4764 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4765 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4766 | // 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] | 4767 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4768 | if (mDragState && |
| 4769 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4770 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4771 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4772 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4773 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4774 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4775 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4776 | // pointer events if necessary. |
| 4777 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4778 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4779 | if (newWindowHandle != nullptr && |
| 4780 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4781 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4782 | std::shared_ptr<InputChannel> inputChannel = |
| 4783 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4784 | if (inputChannel != nullptr) { |
| 4785 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4786 | "touched window's orientation changed"); |
| 4787 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4788 | } |
| 4789 | } |
| 4790 | } |
| 4791 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4792 | // Release information for windows that are no longer present. |
| 4793 | // This ensures that unused input channels are released promptly. |
| 4794 | // Otherwise, they might stick around until the window handle is destroyed |
| 4795 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4796 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4797 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4798 | if (DEBUG_FOCUS) { |
| 4799 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4800 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4801 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4802 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4803 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4807 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4808 | if (DEBUG_FOCUS) { |
| 4809 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4810 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4811 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4812 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4813 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4814 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4815 | } // release lock |
| 4816 | |
| 4817 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4818 | mLooper->wake(); |
| 4819 | } |
| 4820 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4821 | void InputDispatcher::setFocusedApplicationLocked( |
| 4822 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4823 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4824 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4825 | |
| 4826 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4827 | return; // This application is already focused. No need to wake up or change anything. |
| 4828 | } |
| 4829 | |
| 4830 | // Set the new application handle. |
| 4831 | if (inputApplicationHandle != nullptr) { |
| 4832 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4833 | } else { |
| 4834 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4835 | } |
| 4836 | |
| 4837 | // No matter what the old focused application was, stop waiting on it because it is |
| 4838 | // no longer focused. |
| 4839 | resetNoFocusedWindowTimeoutLocked(); |
| 4840 | } |
| 4841 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4842 | /** |
| 4843 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4844 | * the display not specified. |
| 4845 | * |
| 4846 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4847 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4848 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4849 | * display. The display-specified events won't be affected. |
| 4850 | */ |
| 4851 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4852 | if (DEBUG_FOCUS) { |
| 4853 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4854 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4855 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4856 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4857 | |
| 4858 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4859 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4860 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4861 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4862 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4863 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4864 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4865 | CancelationOptions |
| 4866 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4867 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4868 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4869 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4870 | } |
| 4871 | } |
| 4872 | mFocusedDisplayId = displayId; |
| 4873 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4874 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4875 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4876 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4877 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4878 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4879 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4880 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4881 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4882 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4883 | } |
| 4884 | } |
| 4885 | } |
| 4886 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4887 | if (DEBUG_FOCUS) { |
| 4888 | logDispatchStateLocked(); |
| 4889 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4890 | } // release lock |
| 4891 | |
| 4892 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4893 | mLooper->wake(); |
| 4894 | } |
| 4895 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4896 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4897 | if (DEBUG_FOCUS) { |
| 4898 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4899 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4900 | |
| 4901 | bool changed; |
| 4902 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4903 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4904 | |
| 4905 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4906 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4907 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | if (mDispatchEnabled && !enabled) { |
| 4911 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4912 | } |
| 4913 | |
| 4914 | mDispatchEnabled = enabled; |
| 4915 | mDispatchFrozen = frozen; |
| 4916 | changed = true; |
| 4917 | } else { |
| 4918 | changed = false; |
| 4919 | } |
| 4920 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4921 | if (DEBUG_FOCUS) { |
| 4922 | logDispatchStateLocked(); |
| 4923 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4924 | } // release lock |
| 4925 | |
| 4926 | if (changed) { |
| 4927 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4928 | mLooper->wake(); |
| 4929 | } |
| 4930 | } |
| 4931 | |
| 4932 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4933 | if (DEBUG_FOCUS) { |
| 4934 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4935 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4936 | |
| 4937 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4938 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4939 | |
| 4940 | if (mInputFilterEnabled == enabled) { |
| 4941 | return; |
| 4942 | } |
| 4943 | |
| 4944 | mInputFilterEnabled = enabled; |
| 4945 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4946 | } // release lock |
| 4947 | |
| 4948 | // Wake up poll loop since there might be work to do to drop everything. |
| 4949 | mLooper->wake(); |
| 4950 | } |
| 4951 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4952 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4953 | bool needWake = false; |
| 4954 | { |
| 4955 | std::scoped_lock lock(mLock); |
| 4956 | if (mInTouchMode == inTouchMode) { |
| 4957 | return; |
| 4958 | } |
| 4959 | if (DEBUG_TOUCH_MODE) { |
| 4960 | ALOGD("Request to change touch mode from %s to %s", toString(mInTouchMode), |
| 4961 | toString(inTouchMode)); |
| 4962 | // TODO(b/198487159): Also print the current last interacted apps. |
| 4963 | } |
| 4964 | |
| 4965 | // TODO(b/198499018): Store touch mode per display. |
| 4966 | mInTouchMode = inTouchMode; |
| 4967 | |
| 4968 | // TODO(b/198487159): Enforce that only last interacted apps can change touch mode. |
| 4969 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 4970 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4971 | } // release lock |
| 4972 | |
| 4973 | if (needWake) { |
| 4974 | mLooper->wake(); |
| 4975 | } |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4976 | } |
| 4977 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4978 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4979 | if (opacity < 0 || opacity > 1) { |
| 4980 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4981 | return; |
| 4982 | } |
| 4983 | |
| 4984 | std::scoped_lock lock(mLock); |
| 4985 | mMaximumObscuringOpacityForTouch = opacity; |
| 4986 | } |
| 4987 | |
| 4988 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4989 | std::scoped_lock lock(mLock); |
| 4990 | mBlockUntrustedTouchesMode = mode; |
| 4991 | } |
| 4992 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4993 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 4994 | const sp<IBinder>& token) { |
| 4995 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 4996 | for (TouchedWindow& w : state.windows) { |
| 4997 | if (w.windowHandle->getToken() == token) { |
| 4998 | return std::make_pair(&state, &w); |
| 4999 | } |
| 5000 | } |
| 5001 | } |
| 5002 | return std::make_pair(nullptr, nullptr); |
| 5003 | } |
| 5004 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5005 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5006 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5007 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5008 | if (DEBUG_FOCUS) { |
| 5009 | ALOGD("Trivial transfer to same window."); |
| 5010 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5011 | return true; |
| 5012 | } |
| 5013 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5014 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5015 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5016 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5017 | // Find the target touch state and touched window by fromToken. |
| 5018 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 5019 | if (state == nullptr || touchedWindow == nullptr) { |
| 5020 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5021 | return false; |
| 5022 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5023 | |
| 5024 | const int32_t displayId = state->displayId; |
| 5025 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5026 | if (toWindowHandle == nullptr) { |
| 5027 | ALOGW("Cannot transfer focus because to window not found."); |
| 5028 | return false; |
| 5029 | } |
| 5030 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5031 | if (DEBUG_FOCUS) { |
| 5032 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5033 | touchedWindow->windowHandle->getName().c_str(), |
| 5034 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5035 | } |
| 5036 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5037 | // Erase old window. |
| 5038 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 5039 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 5040 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5041 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5042 | // Add new window. |
| 5043 | int32_t newTargetFlags = oldTargetFlags & |
| 5044 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 5045 | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5046 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5047 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5048 | // Store the dragging window. |
| 5049 | if (isDragDrop) { |
| 5050 | mDragState = std::make_unique<DragState>(toWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5051 | } |
| 5052 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5053 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5054 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5055 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5056 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5057 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5058 | CancelationOptions |
| 5059 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5060 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5061 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5062 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5063 | } |
| 5064 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5065 | if (DEBUG_FOCUS) { |
| 5066 | logDispatchStateLocked(); |
| 5067 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5068 | } // release lock |
| 5069 | |
| 5070 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5071 | mLooper->wake(); |
| 5072 | return true; |
| 5073 | } |
| 5074 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5075 | // Binder call |
| 5076 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 5077 | sp<IBinder> fromToken; |
| 5078 | { // acquire lock |
| 5079 | std::scoped_lock _l(mLock); |
| 5080 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5081 | auto it = std::find_if(mTouchStatesByDisplay.begin(), mTouchStatesByDisplay.end(), |
| 5082 | [](const auto& pair) { return pair.second.windows.size() == 1; }); |
| 5083 | if (it == mTouchStatesByDisplay.end()) { |
| 5084 | ALOGW("Cannot transfer touch state because there is no exact window being touched"); |
| 5085 | return false; |
| 5086 | } |
| 5087 | const int32_t displayId = it->first; |
| 5088 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5089 | if (toWindowHandle == nullptr) { |
| 5090 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 5091 | return false; |
| 5092 | } |
| 5093 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5094 | TouchState& state = it->second; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5095 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 5096 | fromToken = touchedWindow.windowHandle->getToken(); |
| 5097 | } // release lock |
| 5098 | |
| 5099 | return transferTouchFocus(fromToken, destChannelToken); |
| 5100 | } |
| 5101 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5102 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5103 | if (DEBUG_FOCUS) { |
| 5104 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5105 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5106 | |
| 5107 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5108 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5109 | |
| 5110 | resetKeyRepeatLocked(); |
| 5111 | releasePendingEventLocked(); |
| 5112 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5113 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5114 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5115 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5116 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5117 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5118 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5119 | } |
| 5120 | |
| 5121 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5122 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5123 | dumpDispatchStateLocked(dump); |
| 5124 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5125 | std::istringstream stream(dump); |
| 5126 | std::string line; |
| 5127 | |
| 5128 | while (std::getline(stream, line, '\n')) { |
| 5129 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5130 | } |
| 5131 | } |
| 5132 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5133 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5134 | std::string dump; |
| 5135 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5136 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5137 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5138 | |
| 5139 | std::string windowName = "None"; |
| 5140 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5141 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5142 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5143 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5144 | : "token has capture without window"; |
| 5145 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5146 | 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] | 5147 | |
| 5148 | return dump; |
| 5149 | } |
| 5150 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5151 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5152 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5153 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5154 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5155 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5156 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5157 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5158 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5159 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5160 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5161 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5162 | const std::chrono::duration timeout = |
| 5163 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5164 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5165 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5166 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5167 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5168 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5169 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5170 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5171 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5172 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5173 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5174 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5175 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5176 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5177 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5178 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5179 | 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] | 5180 | state.displayId, toString(state.down), toString(state.split), |
| 5181 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5182 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5183 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5184 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5185 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5186 | dump += StringPrintf(INDENT4 |
| 5187 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5188 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5189 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5190 | } |
| 5191 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5192 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5193 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5194 | } |
| 5195 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5196 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5197 | } |
| 5198 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5199 | if (mDragState) { |
| 5200 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5201 | mDragState->dump(dump, INDENT2); |
| 5202 | } |
| 5203 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5204 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5205 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5206 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5207 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5208 | const auto& displayInfo = it->second; |
| 5209 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5210 | displayInfo.logicalHeight); |
| 5211 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5212 | } else { |
| 5213 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5214 | } |
| 5215 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5216 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5217 | dump += INDENT2 "Windows:\n"; |
| 5218 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5219 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5220 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5221 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5222 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5223 | "paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5224 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5225 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5226 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5227 | "applicationInfo.name=%s, " |
| 5228 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5229 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5230 | i, windowInfo->name.c_str(), windowInfo->id, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5231 | windowInfo->displayId, toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5232 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5233 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5234 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 5235 | windowInfo->flags.string().c_str(), |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 5236 | ftl::enum_string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5237 | windowInfo->frameLeft, windowInfo->frameTop, |
| 5238 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5239 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5240 | windowInfo->applicationInfo.name.c_str(), |
| 5241 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5242 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5243 | dump += StringPrintf(", inputFeatures=%s", |
| 5244 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5245 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5246 | "ms, trustedOverlay=%s, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5247 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5248 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5249 | millis(windowInfo->dispatchingTimeout), |
| 5250 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5251 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5252 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5253 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5254 | } |
| 5255 | } else { |
| 5256 | dump += INDENT2 "Windows: <none>\n"; |
| 5257 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5258 | } |
| 5259 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5260 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5261 | } |
| 5262 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5263 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5264 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5265 | const std::vector<Monitor>& monitors = it.second; |
| 5266 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5267 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5268 | } |
| 5269 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5270 | const std::vector<Monitor>& monitors = it.second; |
| 5271 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5272 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5273 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5274 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5275 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | nsecs_t currentTime = now(); |
| 5279 | |
| 5280 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5281 | if (!mRecentQueue.empty()) { |
| 5282 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5283 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5284 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5285 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5286 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5287 | } |
| 5288 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5289 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5290 | } |
| 5291 | |
| 5292 | // Dump event currently being dispatched. |
| 5293 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5294 | dump += INDENT "PendingEvent:\n"; |
| 5295 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5296 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5297 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5298 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5299 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5300 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5301 | } |
| 5302 | |
| 5303 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5304 | if (!mInboundQueue.empty()) { |
| 5305 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5306 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5307 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5308 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5309 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5310 | } |
| 5311 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5312 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5313 | } |
| 5314 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5315 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5316 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5317 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5318 | const KeyReplacement& replacement = pair.first; |
| 5319 | int32_t newKeyCode = pair.second; |
| 5320 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5321 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5322 | } |
| 5323 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5324 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5325 | } |
| 5326 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5327 | if (!mCommandQueue.empty()) { |
| 5328 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5329 | } else { |
| 5330 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5331 | } |
| 5332 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5333 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5334 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5335 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5336 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5337 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5338 | connection->inputChannel->getFd().get(), |
| 5339 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5340 | connection->getWindowName().c_str(), |
| 5341 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5342 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5343 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5344 | if (!connection->outboundQueue.empty()) { |
| 5345 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5346 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5347 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5348 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5349 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5350 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5351 | } |
| 5352 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5353 | if (!connection->waitQueue.empty()) { |
| 5354 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5355 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5356 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5357 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5358 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5359 | } |
| 5360 | } |
| 5361 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5362 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5363 | } |
| 5364 | |
| 5365 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5366 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5367 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5368 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5369 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5370 | } |
| 5371 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5372 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5373 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5374 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5375 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5376 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5377 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5378 | } |
| 5379 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5380 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5381 | const size_t numMonitors = monitors.size(); |
| 5382 | for (size_t i = 0; i < numMonitors; i++) { |
| 5383 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5384 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5385 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5386 | dump += "\n"; |
| 5387 | } |
| 5388 | } |
| 5389 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5390 | class LooperEventCallback : public LooperCallback { |
| 5391 | public: |
| 5392 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5393 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5394 | |
| 5395 | private: |
| 5396 | std::function<int(int events)> mCallback; |
| 5397 | }; |
| 5398 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5399 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5400 | if (DEBUG_CHANNEL_CREATION) { |
| 5401 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5402 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5403 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5404 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5405 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5406 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5407 | |
| 5408 | if (result) { |
| 5409 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5410 | } |
| 5411 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5412 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5413 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5414 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5415 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5416 | sp<Connection> connection = |
| 5417 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5418 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5419 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5420 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5421 | } |
| 5422 | mConnectionsByToken.emplace(token, connection); |
| 5423 | |
| 5424 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5425 | this, std::placeholders::_1, token); |
| 5426 | |
| 5427 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5428 | } // release lock |
| 5429 | |
| 5430 | // Wake the looper because some connections have changed. |
| 5431 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5432 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5433 | } |
| 5434 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5435 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5436 | bool isGestureMonitor, |
| 5437 | const std::string& name, |
| 5438 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5439 | std::shared_ptr<InputChannel> serverChannel; |
| 5440 | std::unique_ptr<InputChannel> clientChannel; |
| 5441 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5442 | if (result) { |
| 5443 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5444 | } |
| 5445 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5446 | { // acquire lock |
| 5447 | std::scoped_lock _l(mLock); |
| 5448 | |
| 5449 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5450 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5451 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5454 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5455 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5456 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5457 | |
| 5458 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5459 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5460 | } |
| 5461 | mConnectionsByToken.emplace(token, connection); |
| 5462 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5463 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5464 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5465 | auto& monitorsByDisplay = |
| 5466 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5467 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5468 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5469 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5470 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), |
| 5471 | displayId, toString(isGestureMonitor), pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5472 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5473 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5474 | // Wake the looper because some connections have changed. |
| 5475 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5476 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5477 | } |
| 5478 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5479 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5480 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5481 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5482 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5483 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5484 | if (status) { |
| 5485 | return status; |
| 5486 | } |
| 5487 | } // release lock |
| 5488 | |
| 5489 | // Wake the poll loop because removing the connection may have changed the current |
| 5490 | // synchronization state. |
| 5491 | mLooper->wake(); |
| 5492 | return OK; |
| 5493 | } |
| 5494 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5495 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5496 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5497 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5498 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5499 | // 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] | 5500 | return BAD_VALUE; |
| 5501 | } |
| 5502 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5503 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5504 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5505 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5506 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5507 | } |
| 5508 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5509 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5510 | |
| 5511 | nsecs_t currentTime = now(); |
| 5512 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5513 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5514 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5515 | return OK; |
| 5516 | } |
| 5517 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5518 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5519 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5520 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5523 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5524 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5525 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5526 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5527 | std::vector<Monitor>& monitors = it->second; |
| 5528 | const size_t numMonitors = monitors.size(); |
| 5529 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5530 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5531 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5532 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5533 | monitors.erase(monitors.begin() + i); |
| 5534 | break; |
| 5535 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5536 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5537 | if (monitors.empty()) { |
| 5538 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5539 | } else { |
| 5540 | ++it; |
| 5541 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5542 | } |
| 5543 | } |
| 5544 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5545 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5546 | { // acquire lock |
| 5547 | std::scoped_lock _l(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5548 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5549 | TouchState* statePtr = nullptr; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5550 | std::shared_ptr<InputChannel> requestingChannel; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5551 | int32_t displayId; |
| 5552 | int32_t deviceId; |
| 5553 | const std::optional<int32_t> foundGestureMonitorDisplayId = |
| 5554 | findGestureMonitorDisplayByTokenLocked(token); |
| 5555 | |
| 5556 | // TODO: Optimize this function for pilfering from windows when removing gesture monitors. |
| 5557 | if (foundGestureMonitorDisplayId) { |
| 5558 | // A gesture monitor has requested to pilfer pointers. |
| 5559 | displayId = *foundGestureMonitorDisplayId; |
| 5560 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5561 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5562 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5563 | return BAD_VALUE; |
| 5564 | } |
| 5565 | statePtr = &stateIt->second; |
| 5566 | |
| 5567 | for (const auto& monitor : statePtr->gestureMonitors) { |
| 5568 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 5569 | requestingChannel = monitor.inputChannel; |
| 5570 | deviceId = statePtr->deviceId; |
| 5571 | } |
| 5572 | } |
| 5573 | } else { |
| 5574 | // Check if a window has requested to pilfer pointers. |
| 5575 | for (auto& [curDisplayId, state] : mTouchStatesByDisplay) { |
| 5576 | const sp<WindowInfoHandle>& windowHandle = state.getWindow(token); |
| 5577 | if (windowHandle != nullptr) { |
| 5578 | displayId = curDisplayId; |
| 5579 | requestingChannel = getInputChannelLocked(token); |
| 5580 | deviceId = state.deviceId; |
| 5581 | statePtr = &state; |
| 5582 | break; |
| 5583 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5584 | } |
| 5585 | } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5586 | |
| 5587 | if (requestingChannel == nullptr) { |
| 5588 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5589 | return BAD_VALUE; |
| 5590 | } |
| 5591 | TouchState& state = *statePtr; |
| 5592 | if (!state.down) { |
| 5593 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5594 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5595 | return BAD_VALUE; |
| 5596 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5597 | |
| 5598 | // Send cancel events to all the input channels we're stealing from. |
| 5599 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5600 | "input channel stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5601 | options.deviceId = deviceId; |
| 5602 | options.displayId = displayId; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5603 | std::string canceledWindows; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5604 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5605 | std::shared_ptr<InputChannel> channel = |
| 5606 | getInputChannelLocked(window.windowHandle->getToken()); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5607 | if (channel != nullptr && channel->getConnectionToken() != token) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5608 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5609 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5610 | canceledWindows += channel->getName(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5611 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5612 | } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5613 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5614 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5615 | canceledWindows.c_str()); |
| 5616 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5617 | // Then clear the current touch state so we stop dispatching to them as well. |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 5618 | state.split = false; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5619 | state.filterWindowsExcept(token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5620 | } |
| 5621 | return OK; |
| 5622 | } |
| 5623 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5624 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5625 | { // acquire lock |
| 5626 | std::scoped_lock _l(mLock); |
| 5627 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5628 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5629 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5630 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5631 | : "token without window"); |
| 5632 | } |
| 5633 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5634 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5635 | if (focusedToken != windowToken) { |
| 5636 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5637 | enabled ? "enable" : "disable"); |
| 5638 | return; |
| 5639 | } |
| 5640 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5641 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5642 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5643 | "window has %s requested pointer capture.", |
| 5644 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5645 | return; |
| 5646 | } |
| 5647 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5648 | setPointerCaptureLocked(enabled); |
| 5649 | } // release lock |
| 5650 | |
| 5651 | // Wake the thread to process command entries. |
| 5652 | mLooper->wake(); |
| 5653 | } |
| 5654 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5655 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5656 | const sp<IBinder>& token) { |
| 5657 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5658 | const std::vector<Monitor>& monitors = it.second; |
| 5659 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5660 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5661 | return it.first; |
| 5662 | } |
| 5663 | } |
| 5664 | } |
| 5665 | return std::nullopt; |
| 5666 | } |
| 5667 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5668 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5669 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5670 | if (gesturePid.has_value()) { |
| 5671 | return gesturePid; |
| 5672 | } |
| 5673 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5674 | } |
| 5675 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5676 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5677 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5678 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5679 | } |
| 5680 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5681 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5682 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5683 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5684 | } |
| 5685 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5686 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5687 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5688 | } |
| 5689 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5690 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5691 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5692 | if (connection == nullptr) { |
| 5693 | return "<nullptr>"; |
| 5694 | } |
| 5695 | return connection->getInputChannelName(); |
| 5696 | } |
| 5697 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5698 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5699 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5700 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5701 | } |
| 5702 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5703 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5704 | const sp<Connection>& connection, uint32_t seq, |
| 5705 | bool handled, nsecs_t consumeTime) { |
| 5706 | // Handle post-event policy actions. |
| 5707 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5708 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5709 | return; |
| 5710 | } |
| 5711 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5712 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5713 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5714 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5715 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5716 | } |
| 5717 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5718 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5719 | connection->inputChannel->getConnectionToken(), |
| 5720 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5721 | } |
| 5722 | |
| 5723 | bool restartEvent; |
| 5724 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5725 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5726 | restartEvent = |
| 5727 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5728 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5729 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5730 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5731 | handled); |
| 5732 | } else { |
| 5733 | restartEvent = false; |
| 5734 | } |
| 5735 | |
| 5736 | // Dequeue the event and start the next cycle. |
| 5737 | // Because the lock might have been released, it is possible that the |
| 5738 | // contents of the wait queue to have been drained, so we need to double-check |
| 5739 | // a few things. |
| 5740 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5741 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5742 | dispatchEntry = *dispatchEntryIt; |
| 5743 | connection->waitQueue.erase(dispatchEntryIt); |
| 5744 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5745 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5746 | if (!connection->responsive) { |
| 5747 | connection->responsive = isConnectionResponsive(*connection); |
| 5748 | if (connection->responsive) { |
| 5749 | // The connection was unresponsive, and now it's responsive. |
| 5750 | processConnectionResponsiveLocked(*connection); |
| 5751 | } |
| 5752 | } |
| 5753 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5754 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5755 | connection->outboundQueue.push_front(dispatchEntry); |
| 5756 | traceOutboundQueueLength(*connection); |
| 5757 | } else { |
| 5758 | releaseDispatchEntry(dispatchEntry); |
| 5759 | } |
| 5760 | } |
| 5761 | |
| 5762 | // Start the next dispatch cycle for this connection. |
| 5763 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5764 | } |
| 5765 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5766 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5767 | const sp<IBinder>& newToken) { |
| 5768 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5769 | scoped_unlock unlock(mLock); |
| 5770 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5771 | }; |
| 5772 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5773 | } |
| 5774 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5775 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5776 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5777 | scoped_unlock unlock(mLock); |
| 5778 | mPolicy->notifyDropWindow(token, x, y); |
| 5779 | }; |
| 5780 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5781 | } |
| 5782 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5783 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5784 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5785 | scoped_unlock unlock(mLock); |
| 5786 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5787 | }; |
| 5788 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5789 | } |
| 5790 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5791 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5792 | if (connection == nullptr) { |
| 5793 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5794 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5795 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5796 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5797 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5798 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5799 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5800 | return; |
| 5801 | } |
| 5802 | /** |
| 5803 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5804 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5805 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5806 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5807 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5808 | * most useful. |
| 5809 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5810 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5811 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5812 | std::string reason = |
| 5813 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5814 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5815 | ns2ms(currentWait), |
| 5816 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5817 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5818 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5819 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5820 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5821 | |
| 5822 | // Stop waking up for events on this connection, it is already unresponsive |
| 5823 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5824 | } |
| 5825 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5826 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5827 | std::string reason = |
| 5828 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5829 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5830 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5831 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5832 | scoped_unlock unlock(mLock); |
| 5833 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5834 | }; |
| 5835 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5836 | } |
| 5837 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5838 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5839 | const std::string& reason) { |
| 5840 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5841 | updateLastAnrStateLocked(windowLabel, reason); |
| 5842 | } |
| 5843 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5844 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5845 | const std::string& reason) { |
| 5846 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5847 | updateLastAnrStateLocked(windowLabel, reason); |
| 5848 | } |
| 5849 | |
| 5850 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5851 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5852 | // 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] | 5853 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5854 | struct tm tm; |
| 5855 | localtime_r(&t, &tm); |
| 5856 | char timestr[64]; |
| 5857 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5858 | mLastAnrState.clear(); |
| 5859 | mLastAnrState += INDENT "ANR:\n"; |
| 5860 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5861 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5862 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5863 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5864 | } |
| 5865 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5866 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5867 | KeyEntry& entry) { |
| 5868 | const KeyEvent event = createKeyEvent(entry); |
| 5869 | nsecs_t delay = 0; |
| 5870 | { // release lock |
| 5871 | scoped_unlock unlock(mLock); |
| 5872 | android::base::Timer t; |
| 5873 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5874 | entry.policyFlags); |
| 5875 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5876 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5877 | std::to_string(t.duration().count()).c_str()); |
| 5878 | } |
| 5879 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5880 | |
| 5881 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5882 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5883 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5884 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5885 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5886 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5887 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5888 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5889 | } |
| 5890 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5891 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5892 | auto command = [this, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5893 | scoped_unlock unlock(mLock); |
| 5894 | mPolicy->notifyMonitorUnresponsive(pid, reason); |
| 5895 | }; |
| 5896 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5897 | } |
| 5898 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5899 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5900 | std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5901 | auto command = [this, token, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5902 | scoped_unlock unlock(mLock); |
| 5903 | mPolicy->notifyWindowUnresponsive(token, reason); |
| 5904 | }; |
| 5905 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5906 | } |
| 5907 | |
| 5908 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5909 | auto command = [this, pid]() REQUIRES(mLock) { |
| 5910 | scoped_unlock unlock(mLock); |
| 5911 | mPolicy->notifyMonitorResponsive(pid); |
| 5912 | }; |
| 5913 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5914 | } |
| 5915 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5916 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken) { |
| 5917 | auto command = [this, connectionToken]() REQUIRES(mLock) { |
| 5918 | scoped_unlock unlock(mLock); |
| 5919 | mPolicy->notifyWindowResponsive(connectionToken); |
| 5920 | }; |
| 5921 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5922 | } |
| 5923 | |
| 5924 | /** |
| 5925 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5926 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5927 | * command entry to the command queue. |
| 5928 | */ |
| 5929 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5930 | std::string reason) { |
| 5931 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5932 | if (connection.monitor) { |
| 5933 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5934 | reason.c_str()); |
| 5935 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5936 | if (!pid.has_value()) { |
| 5937 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5938 | connection.inputChannel->getName().c_str()); |
| 5939 | return; |
| 5940 | } |
| 5941 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5942 | return; |
| 5943 | } |
| 5944 | // If not a monitor, must be a window |
| 5945 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5946 | reason.c_str()); |
| 5947 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5948 | } |
| 5949 | |
| 5950 | /** |
| 5951 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5952 | */ |
| 5953 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5954 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5955 | if (connection.monitor) { |
| 5956 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5957 | if (!pid.has_value()) { |
| 5958 | ALOGE("Could not find responsive monitor for connection %s", |
| 5959 | connection.inputChannel->getName().c_str()); |
| 5960 | return; |
| 5961 | } |
| 5962 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5963 | return; |
| 5964 | } |
| 5965 | // If not a monitor, must be a window |
| 5966 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5967 | } |
| 5968 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5969 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5970 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5971 | KeyEntry& keyEntry, bool handled) { |
| 5972 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5973 | if (!handled) { |
| 5974 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5975 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5976 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5977 | return false; |
| 5978 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5979 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5980 | // Get the fallback key state. |
| 5981 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5982 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5983 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5984 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5985 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5986 | } |
| 5987 | |
| 5988 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5989 | // If the application handles the original key for which we previously |
| 5990 | // generated a fallback or if the window is not a foreground window, |
| 5991 | // then cancel the associated fallback key, if any. |
| 5992 | if (fallbackKeyCode != -1) { |
| 5993 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5994 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5995 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 5996 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5997 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 5998 | keyEntry.policyFlags); |
| 5999 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6000 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6001 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6002 | |
| 6003 | mLock.unlock(); |
| 6004 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6005 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6006 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6007 | |
| 6008 | mLock.lock(); |
| 6009 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6010 | // Cancel the fallback key. |
| 6011 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6012 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6013 | "application handled the original non-fallback key " |
| 6014 | "or is no longer a foreground target, " |
| 6015 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6016 | options.keyCode = fallbackKeyCode; |
| 6017 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6018 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6019 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6020 | } |
| 6021 | } else { |
| 6022 | // If the application did not handle a non-fallback key, first check |
| 6023 | // that we are in a good state to perform unhandled key event processing |
| 6024 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6025 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6026 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6027 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6028 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6029 | "since this is not an initial down. " |
| 6030 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6031 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6032 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6033 | return false; |
| 6034 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6035 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6036 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6037 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6038 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6039 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6040 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6041 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6042 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6043 | |
| 6044 | mLock.unlock(); |
| 6045 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6046 | bool fallback = |
| 6047 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6048 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6049 | |
| 6050 | mLock.lock(); |
| 6051 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6052 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6053 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6054 | return false; |
| 6055 | } |
| 6056 | |
| 6057 | // Latch the fallback keycode for this key on an initial down. |
| 6058 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6059 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6060 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6061 | fallbackKeyCode = event.getKeyCode(); |
| 6062 | } else { |
| 6063 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6064 | } |
| 6065 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6066 | } |
| 6067 | |
| 6068 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6069 | |
| 6070 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6071 | // We will continue to dispatch the key to the policy but we will no |
| 6072 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6073 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6074 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6075 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6076 | if (fallback) { |
| 6077 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6078 | "as a fallback for %d, but on the DOWN it had requested " |
| 6079 | "to send %d instead. Fallback canceled.", |
| 6080 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6081 | } else { |
| 6082 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6083 | "but on the DOWN it had requested to send %d. " |
| 6084 | "Fallback canceled.", |
| 6085 | originalKeyCode, fallbackKeyCode); |
| 6086 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6087 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6088 | |
| 6089 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6090 | "canceling fallback, policy no longer desires it"); |
| 6091 | options.keyCode = fallbackKeyCode; |
| 6092 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6093 | |
| 6094 | fallback = false; |
| 6095 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6096 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6097 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6098 | } |
| 6099 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6100 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6101 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6102 | { |
| 6103 | std::string msg; |
| 6104 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6105 | connection->inputState.getFallbackKeys(); |
| 6106 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6107 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6108 | } |
| 6109 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6110 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6111 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6112 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6113 | |
| 6114 | if (fallback) { |
| 6115 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6116 | keyEntry.eventTime = event.getEventTime(); |
| 6117 | keyEntry.deviceId = event.getDeviceId(); |
| 6118 | keyEntry.source = event.getSource(); |
| 6119 | keyEntry.displayId = event.getDisplayId(); |
| 6120 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6121 | keyEntry.keyCode = fallbackKeyCode; |
| 6122 | keyEntry.scanCode = event.getScanCode(); |
| 6123 | keyEntry.metaState = event.getMetaState(); |
| 6124 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6125 | keyEntry.downTime = event.getDownTime(); |
| 6126 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6127 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6128 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6129 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6130 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6131 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6132 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6133 | return true; // restart the event |
| 6134 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6135 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6136 | ALOGD("Unhandled key event: No fallback key."); |
| 6137 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6138 | |
| 6139 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6140 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6141 | } |
| 6142 | } |
| 6143 | return false; |
| 6144 | } |
| 6145 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6146 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6147 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6148 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6149 | return false; |
| 6150 | } |
| 6151 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6152 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6153 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6154 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6155 | } |
| 6156 | } |
| 6157 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6158 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6159 | if (ATRACE_ENABLED()) { |
| 6160 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6161 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6162 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6163 | } |
| 6164 | } |
| 6165 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6166 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6167 | if (ATRACE_ENABLED()) { |
| 6168 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6169 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6170 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6171 | } |
| 6172 | } |
| 6173 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6174 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6175 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6176 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6177 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6178 | dumpDispatchStateLocked(dump); |
| 6179 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6180 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6181 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6182 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6183 | } |
| 6184 | } |
| 6185 | |
| 6186 | void InputDispatcher::monitor() { |
| 6187 | // 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] | 6188 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6189 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6190 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6191 | } |
| 6192 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6193 | /** |
| 6194 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6195 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6196 | * this method can be safely called from any thread, as long as you've ensured that |
| 6197 | * the work you are interested in completing has already been queued. |
| 6198 | */ |
| 6199 | bool InputDispatcher::waitForIdle() { |
| 6200 | /** |
| 6201 | * Timeout should represent the longest possible time that a device might spend processing |
| 6202 | * events and commands. |
| 6203 | */ |
| 6204 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6205 | std::unique_lock lock(mLock); |
| 6206 | mLooper->wake(); |
| 6207 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6208 | return result == std::cv_status::no_timeout; |
| 6209 | } |
| 6210 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6211 | /** |
| 6212 | * Sets focus to the window identified by the token. This must be called |
| 6213 | * after updating any input window handles. |
| 6214 | * |
| 6215 | * Params: |
| 6216 | * request.token - input channel token used to identify the window that should gain focus. |
| 6217 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6218 | * specified token does not match the currently focused window, this request will be dropped. |
| 6219 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6220 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6221 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6222 | * when requesting the focus change. This determines which request gets |
| 6223 | * precedence if there is a focus change request from another source such as pointer down. |
| 6224 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6225 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6226 | { // acquire lock |
| 6227 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6228 | std::optional<FocusResolver::FocusChanges> changes = |
| 6229 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6230 | if (changes) { |
| 6231 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6232 | } |
| 6233 | } // release lock |
| 6234 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6235 | mLooper->wake(); |
| 6236 | } |
| 6237 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6238 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6239 | if (changes.oldFocus) { |
| 6240 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6241 | if (focusedInputChannel) { |
| 6242 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6243 | "focus left window"); |
| 6244 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6245 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6246 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6247 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6248 | if (changes.newFocus) { |
| 6249 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6250 | } |
| 6251 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6252 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6253 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6254 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6255 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6256 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6257 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6258 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6259 | // front. |
| 6260 | disablePointerCaptureForcedLocked(); |
| 6261 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6262 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6263 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6264 | } |
| 6265 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6266 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6267 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6268 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6269 | return; |
| 6270 | } |
| 6271 | |
| 6272 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6273 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6274 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6275 | setPointerCaptureLocked(false); |
| 6276 | } |
| 6277 | |
| 6278 | if (!mWindowTokenWithPointerCapture) { |
| 6279 | // No need to send capture changes because no window has capture. |
| 6280 | return; |
| 6281 | } |
| 6282 | |
| 6283 | if (mPendingEvent != nullptr) { |
| 6284 | // Move the pending event to the front of the queue. This will give the chance |
| 6285 | // for the pending event to be dropped if it is a captured event. |
| 6286 | mInboundQueue.push_front(mPendingEvent); |
| 6287 | mPendingEvent = nullptr; |
| 6288 | } |
| 6289 | |
| 6290 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6291 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6292 | mInboundQueue.push_front(std::move(entry)); |
| 6293 | } |
| 6294 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6295 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6296 | mCurrentPointerCaptureRequest.enable = enable; |
| 6297 | mCurrentPointerCaptureRequest.seq++; |
| 6298 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6299 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6300 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6301 | }; |
| 6302 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6303 | } |
| 6304 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6305 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6306 | { // acquire lock |
| 6307 | std::scoped_lock _l(mLock); |
| 6308 | // Set an empty list to remove all handles from the specific display. |
| 6309 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6310 | setFocusedApplicationLocked(displayId, nullptr); |
| 6311 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6312 | // have been removed for the removed display. |
| 6313 | mFocusResolver.displayRemoved(displayId); |
| 6314 | } // release lock |
| 6315 | |
| 6316 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6317 | mLooper->wake(); |
| 6318 | } |
| 6319 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6320 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6321 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6322 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6323 | // more convenient parsing. |
| 6324 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6325 | for (const auto& info : windowInfos) { |
| 6326 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6327 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6328 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6329 | |
| 6330 | { // acquire lock |
| 6331 | std::scoped_lock _l(mLock); |
| 6332 | mDisplayInfos.clear(); |
| 6333 | for (const auto& displayInfo : displayInfos) { |
| 6334 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6335 | } |
| 6336 | |
| 6337 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6338 | setInputWindowsLocked(handles, displayId); |
| 6339 | } |
| 6340 | } |
| 6341 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6342 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6343 | } |
| 6344 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6345 | bool InputDispatcher::shouldDropInput( |
| 6346 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
| 6347 | if (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT) || |
| 6348 | (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED) && |
| 6349 | isWindowObscuredLocked(windowHandle))) { |
| 6350 | ALOGW("Dropping %s event targeting %s as requested by input feature %s on display " |
| 6351 | "%" PRId32 ".", |
| 6352 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
| 6353 | windowHandle->getInfo()->inputFeatures.string().c_str(), |
| 6354 | windowHandle->getInfo()->displayId); |
| 6355 | return true; |
| 6356 | } |
| 6357 | return false; |
| 6358 | } |
| 6359 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6360 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6361 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6362 | const std::vector<DisplayInfo>& displayInfos) { |
| 6363 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6364 | } |
| 6365 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6366 | void InputDispatcher::cancelCurrentTouch() { |
| 6367 | { |
| 6368 | std::scoped_lock _l(mLock); |
| 6369 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
| 6370 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 6371 | "cancel current touch"); |
| 6372 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6373 | |
| 6374 | mTouchStatesByDisplay.clear(); |
| 6375 | mLastHoverWindowHandle.clear(); |
| 6376 | } |
| 6377 | // Wake up poll loop since there might be work to do. |
| 6378 | mLooper->wake(); |
| 6379 | } |
| 6380 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6381 | } // namespace android::inputdispatcher |