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. |
Vadim Tryshev | 7854825 | 2022-01-27 19:32:46 +0000 | [diff] [blame] | 123 | const nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL // 10sec |
| 124 | * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 125 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 126 | // 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] | 127 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 128 | |
| 129 | // Log a warning when an interception call takes longer than this to process. |
| 130 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 132 | // Additional key latency in case a connection is still processing some motion events. |
| 133 | // This will help with the case when a user touched a button that opens a new window, |
| 134 | // and gives us the chance to dispatch the key to this new window. |
| 135 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 136 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 137 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 138 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 139 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame^] | 140 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 141 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 142 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 143 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 144 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 145 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 146 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 147 | } |
| 148 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 149 | inline const char* toString(bool value) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 150 | return value ? "true" : "false"; |
| 151 | } |
| 152 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 153 | inline const std::string toString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 154 | if (binder == nullptr) { |
| 155 | return "<null>"; |
| 156 | } |
| 157 | return StringPrintf("%p", binder.get()); |
| 158 | } |
| 159 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 160 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 161 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 162 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 165 | bool isValidKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 166 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 167 | case AKEY_EVENT_ACTION_DOWN: |
| 168 | case AKEY_EVENT_ACTION_UP: |
| 169 | return true; |
| 170 | default: |
| 171 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 175 | bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 176 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 177 | ALOGE("Key event has invalid action code 0x%x", action); |
| 178 | return false; |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 183 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 184 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 185 | case AMOTION_EVENT_ACTION_DOWN: |
| 186 | case AMOTION_EVENT_ACTION_UP: |
| 187 | case AMOTION_EVENT_ACTION_CANCEL: |
| 188 | case AMOTION_EVENT_ACTION_MOVE: |
| 189 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 190 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 191 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 192 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 193 | case AMOTION_EVENT_ACTION_SCROLL: |
| 194 | return true; |
| 195 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 196 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 197 | int32_t index = getMotionEventActionPointerIndex(action); |
| 198 | return index >= 0 && index < pointerCount; |
| 199 | } |
| 200 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 201 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 202 | return actionButton != 0; |
| 203 | default: |
| 204 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 208 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 209 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 210 | } |
| 211 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 212 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 213 | const PointerProperties* pointerProperties) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 214 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 215 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 216 | return false; |
| 217 | } |
| 218 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 219 | 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] | 220 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | BitSet32 pointerIdBits; |
| 224 | for (size_t i = 0; i < pointerCount; i++) { |
| 225 | int32_t id = pointerProperties[i].id; |
| 226 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 227 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 228 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 229 | return false; |
| 230 | } |
| 231 | if (pointerIdBits.hasBit(id)) { |
| 232 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 233 | return false; |
| 234 | } |
| 235 | pointerIdBits.markBit(id); |
| 236 | } |
| 237 | return true; |
| 238 | } |
| 239 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 240 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 241 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 242 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 245 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 246 | bool first = true; |
| 247 | Region::const_iterator cur = region.begin(); |
| 248 | Region::const_iterator const tail = region.end(); |
| 249 | while (cur != tail) { |
| 250 | if (first) { |
| 251 | first = false; |
| 252 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 253 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 254 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 255 | 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] | 256 | cur++; |
| 257 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 258 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 261 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 262 | constexpr size_t maxEntries = 50; // max events to print |
| 263 | constexpr size_t skipBegin = maxEntries / 2; |
| 264 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 265 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 266 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 267 | |
| 268 | std::string dump; |
| 269 | for (size_t i = 0; i < queue.size(); i++) { |
| 270 | const DispatchEntry& entry = *queue[i]; |
| 271 | if (i >= skipBegin && i < skipEnd) { |
| 272 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 273 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 274 | continue; |
| 275 | } |
| 276 | dump.append(INDENT4); |
| 277 | dump += entry.eventEntry->getDescription(); |
| 278 | dump += StringPrintf(", seq=%" PRIu32 |
| 279 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 280 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 281 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 282 | if (entry.deliveryTime != 0) { |
| 283 | // This entry was delivered, so add information on how long we've been waiting |
| 284 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 285 | } |
| 286 | dump.append("\n"); |
| 287 | } |
| 288 | return dump; |
| 289 | } |
| 290 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 291 | /** |
| 292 | * Find the entry in std::unordered_map by key, and return it. |
| 293 | * If the entry is not found, return a default constructed entry. |
| 294 | * |
| 295 | * Useful when the entries are vectors, since an empty vector will be returned |
| 296 | * if the entry is not found. |
| 297 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 298 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 299 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 300 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 301 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 302 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 303 | } |
| 304 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 305 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 306 | if (first == second) { |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | if (first == nullptr || second == nullptr) { |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | return first->getToken() == second->getToken(); |
| 315 | } |
| 316 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 317 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 318 | if (first == nullptr || second == nullptr) { |
| 319 | return false; |
| 320 | } |
| 321 | return first->applicationInfo.token != nullptr && |
| 322 | first->applicationInfo.token == second->applicationInfo.token; |
| 323 | } |
| 324 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 325 | bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 326 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 327 | } |
| 328 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 329 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 330 | std::shared_ptr<EventEntry> eventEntry, |
| 331 | int32_t inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 332 | if (inputTarget.useDefaultPointerTransform()) { |
| 333 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 334 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 335 | inputTarget.displayTransform, |
| 336 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 340 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 341 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 342 | std::vector<PointerCoords> pointerCoords; |
| 343 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 344 | |
| 345 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 346 | // 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] | 347 | // uses the transform for the normalized pointer. |
| 348 | const ui::Transform& firstPointerTransform = |
| 349 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 350 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 351 | |
| 352 | // Iterate through all pointers in the event to normalize against the first. |
| 353 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 354 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 355 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 356 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 357 | |
| 358 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 359 | // First, apply the current pointer's transform to update the coordinates into |
| 360 | // window space. |
| 361 | pointerCoords[pointerIndex].transform(currTransform); |
| 362 | // Next, apply the inverse transform of the normalized coordinates so the |
| 363 | // current coordinates are transformed into the normalized coordinate space. |
| 364 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 367 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 368 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 369 | motionEntry.deviceId, motionEntry.source, |
| 370 | motionEntry.displayId, motionEntry.policyFlags, |
| 371 | motionEntry.action, motionEntry.actionButton, |
| 372 | motionEntry.flags, motionEntry.metaState, |
| 373 | motionEntry.buttonState, motionEntry.classification, |
| 374 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 375 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 376 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 377 | motionEntry.pointerCount, motionEntry.pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 378 | pointerCoords.data()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 379 | |
| 380 | if (motionEntry.injectionState) { |
| 381 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 382 | combinedMotionEntry->injectionState->refCount += 1; |
| 383 | } |
| 384 | |
| 385 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 386 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 387 | firstPointerTransform, inputTarget.displayTransform, |
| 388 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 389 | return dispatchEntry; |
| 390 | } |
| 391 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 392 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 393 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 394 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 395 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 396 | |
| 397 | serverChannel = std::move(uniqueServerChannel); |
| 398 | return result; |
| 399 | } |
| 400 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 401 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 402 | 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] | 403 | if (lhs == nullptr && rhs == nullptr) { |
| 404 | return true; |
| 405 | } |
| 406 | if (lhs == nullptr || rhs == nullptr) { |
| 407 | return false; |
| 408 | } |
| 409 | return *lhs == *rhs; |
| 410 | } |
| 411 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 412 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 413 | KeyEvent event; |
| 414 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 415 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 416 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 417 | return event; |
| 418 | } |
| 419 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 420 | std::optional<int32_t> findMonitorPidByToken( |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 421 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, |
| 422 | const sp<IBinder>& token) { |
| 423 | for (const auto& it : monitorsByDisplay) { |
| 424 | const std::vector<Monitor>& monitors = it.second; |
| 425 | for (const Monitor& monitor : monitors) { |
| 426 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 427 | return monitor.pid; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | return std::nullopt; |
| 432 | } |
| 433 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 434 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 435 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 436 | // affect the statistics. |
| 437 | if (connection.monitor) { |
| 438 | return false; |
| 439 | } |
| 440 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 441 | if (!connection.responsive) { |
| 442 | return false; |
| 443 | } |
| 444 | return true; |
| 445 | } |
| 446 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 447 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 448 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 449 | const int32_t& inputEventId = eventEntry.id; |
| 450 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 451 | // Event was transmuted |
| 452 | return false; |
| 453 | } |
| 454 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 455 | return false; |
| 456 | } |
| 457 | // Only track latency for events that originated from hardware |
| 458 | if (eventEntry.isSynthesized()) { |
| 459 | return false; |
| 460 | } |
| 461 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 462 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 463 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 464 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 465 | return false; |
| 466 | } |
| 467 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 468 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 469 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 470 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 471 | return false; |
| 472 | } |
| 473 | } else { |
| 474 | // Not a key or a motion |
| 475 | return false; |
| 476 | } |
| 477 | if (!shouldReportMetricsForConnection(connection)) { |
| 478 | return false; |
| 479 | } |
| 480 | return true; |
| 481 | } |
| 482 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 483 | /** |
| 484 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 485 | * current time. |
| 486 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 487 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 488 | const nsecs_t currentTime = now(); |
| 489 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 490 | if (entry->timeoutTime < currentTime) { |
| 491 | return false; |
| 492 | } |
| 493 | } |
| 494 | return true; |
| 495 | } |
| 496 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 497 | // Returns true if the event type passed as argument represents a user activity. |
| 498 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 499 | switch (eventEntry.type) { |
| 500 | case EventEntry::Type::FOCUS: |
| 501 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 502 | case EventEntry::Type::DRAG: |
| 503 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 504 | case EventEntry::Type::SENSOR: |
| 505 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 506 | return false; |
| 507 | case EventEntry::Type::DEVICE_RESET: |
| 508 | case EventEntry::Type::KEY: |
| 509 | case EventEntry::Type::MOTION: |
| 510 | return true; |
| 511 | } |
| 512 | } |
| 513 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 514 | // 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] | 515 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, int32_t x, int32_t y, |
| 516 | bool isStylus) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 517 | if (windowInfo.displayId != displayId || !windowInfo.visible) { |
| 518 | return false; |
| 519 | } |
| 520 | const auto flags = windowInfo.flags; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 521 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
| 522 | if (flags.test(WindowInfo::Flag::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 523 | return false; |
| 524 | } |
| 525 | const bool isModalWindow = !flags.test(WindowInfo::Flag::NOT_FOCUSABLE) && |
| 526 | !flags.test(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 527 | if (!isModalWindow && !windowInfo.touchableRegionContainsPoint(x, y)) { |
| 528 | return false; |
| 529 | } |
| 530 | return true; |
| 531 | } |
| 532 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 533 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 534 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
| 535 | (entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || |
| 536 | entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_ERASER); |
| 537 | } |
| 538 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 539 | } // namespace |
| 540 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 541 | // --- InputDispatcher --- |
| 542 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 543 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 544 | : mPolicy(policy), |
| 545 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 546 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 547 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 548 | mAppSwitchSawKeyDown(false), |
| 549 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 550 | mNextUnblockedEvent(nullptr), |
| 551 | mDispatchEnabled(false), |
| 552 | mDispatchFrozen(false), |
| 553 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 554 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 555 | // To avoid leaking stack in case that call never comes, and for tests, |
| 556 | // initialize it here anyways. |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 557 | mInTouchMode(kDefaultInTouchMode), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 558 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 559 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 560 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 561 | mLatencyAggregator(), |
Siarhei Vishniakou | bd25272 | 2022-01-06 03:49:35 -0800 | [diff] [blame] | 562 | mLatencyTracker(&mLatencyAggregator) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 563 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 564 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 565 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 566 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 567 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 568 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 569 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 570 | |
| 571 | policy->getDispatcherConfiguration(&mConfig); |
| 572 | } |
| 573 | |
| 574 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 575 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 576 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 577 | resetKeyRepeatLocked(); |
| 578 | releasePendingEventLocked(); |
| 579 | drainInboundQueueLocked(); |
| 580 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 581 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 582 | while (!mConnectionsByToken.empty()) { |
| 583 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 584 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 585 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 589 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 590 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 591 | return ALREADY_EXISTS; |
| 592 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 593 | mThread = std::make_unique<InputThread>( |
| 594 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 595 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 599 | if (mThread && mThread->isCallingThread()) { |
| 600 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 601 | return INVALID_OPERATION; |
| 602 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 603 | mThread.reset(); |
| 604 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 607 | void InputDispatcher::dispatchOnce() { |
| 608 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 609 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 610 | std::scoped_lock _l(mLock); |
| 611 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 612 | |
| 613 | // Run a dispatch loop if there are no pending commands. |
| 614 | // The dispatch loop might enqueue commands to run afterwards. |
| 615 | if (!haveCommandsLocked()) { |
| 616 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 617 | } |
| 618 | |
| 619 | // Run all pending commands if there are any. |
| 620 | // 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] | 621 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 622 | nextWakeupTime = LONG_LONG_MIN; |
| 623 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 624 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 625 | // If we are still waiting for ack on some events, |
| 626 | // we might have to wake up earlier to check if an app is anr'ing. |
| 627 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 628 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 629 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 630 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 631 | // pending or queued events |
| 632 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 633 | mDispatcherEnteredIdle.notify_all(); |
| 634 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 635 | } // release lock |
| 636 | |
| 637 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 638 | nsecs_t currentTime = now(); |
| 639 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 640 | mLooper->pollOnce(timeoutMillis); |
| 641 | } |
| 642 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 643 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 644 | * Raise ANR if there is no focused window. |
| 645 | * Before the ANR is raised, do a final state check: |
| 646 | * 1. The currently focused application must be the same one we are waiting for. |
| 647 | * 2. Ensure we still don't have a focused window. |
| 648 | */ |
| 649 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 650 | // Check if the application that we are waiting for is still focused. |
| 651 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 652 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 653 | if (focusedApplication == nullptr || |
| 654 | focusedApplication->getApplicationToken() != |
| 655 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 656 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 657 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 658 | focusedApplication->getName().c_str()); |
| 659 | return; // The focused application has changed. |
| 660 | } |
| 661 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 662 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 663 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 664 | if (focusedWindowHandle != nullptr) { |
| 665 | return; // We now have a focused window. No need for ANR. |
| 666 | } |
| 667 | onAnrLocked(mAwaitedFocusedApplication); |
| 668 | } |
| 669 | |
| 670 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 671 | * Check if any of the connections' wait queues have events that are too old. |
| 672 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 673 | * Return the time at which we should wake up next. |
| 674 | */ |
| 675 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 676 | const nsecs_t currentTime = now(); |
| 677 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 678 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 679 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 680 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 681 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 682 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 683 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 684 | return LONG_LONG_MIN; |
| 685 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 686 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 687 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | // Check if any connection ANRs are due |
| 692 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 693 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 694 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 695 | } |
| 696 | |
| 697 | // If we reached here, we have an unresponsive connection. |
| 698 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 699 | if (connection == nullptr) { |
| 700 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 701 | return nextAnrCheck; |
| 702 | } |
| 703 | connection->responsive = false; |
| 704 | // Stop waking up for this unresponsive connection |
| 705 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 706 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 707 | return LONG_LONG_MIN; |
| 708 | } |
| 709 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 710 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 711 | sp<WindowInfoHandle> window = getWindowHandleLocked(token); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 712 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 713 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 714 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 715 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 718 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 719 | nsecs_t currentTime = now(); |
| 720 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 721 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 722 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 723 | // repeat if the device is just coming out of sleep. |
| 724 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 725 | resetKeyRepeatLocked(); |
| 726 | } |
| 727 | |
| 728 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 729 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 730 | if (DEBUG_FOCUS) { |
| 731 | ALOGD("Dispatch frozen. Waiting some more."); |
| 732 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 733 | return; |
| 734 | } |
| 735 | |
| 736 | // Optimize latency of app switches. |
| 737 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 738 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 739 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 740 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 741 | *nextWakeupTime = mAppSwitchDueTime; |
| 742 | } |
| 743 | |
| 744 | // Ready to start a new event. |
| 745 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 746 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 747 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 748 | if (isAppSwitchDue) { |
| 749 | // The inbound queue is empty so the app switch key we were waiting |
| 750 | // for will never arrive. Stop waiting for it. |
| 751 | resetPendingAppSwitchLocked(false); |
| 752 | isAppSwitchDue = false; |
| 753 | } |
| 754 | |
| 755 | // Synthesize a key repeat if appropriate. |
| 756 | if (mKeyRepeatState.lastKeyEntry) { |
| 757 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 758 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 759 | } else { |
| 760 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 761 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | // Nothing to do if there is no pending event. |
| 767 | if (!mPendingEvent) { |
| 768 | return; |
| 769 | } |
| 770 | } else { |
| 771 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 772 | mPendingEvent = mInboundQueue.front(); |
| 773 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 774 | traceInboundQueueLengthLocked(); |
| 775 | } |
| 776 | |
| 777 | // Poke user activity for this event. |
| 778 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 779 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | // Now we have an event to dispatch. |
| 784 | // 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] | 785 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 786 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 787 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 788 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 789 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 790 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 791 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 795 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 799 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 800 | const ConfigurationChangedEntry& typedEntry = |
| 801 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 802 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 803 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 804 | break; |
| 805 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 806 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 807 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 808 | const DeviceResetEntry& typedEntry = |
| 809 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 810 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 811 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 812 | break; |
| 813 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 814 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 815 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 816 | std::shared_ptr<FocusEntry> typedEntry = |
| 817 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 818 | dispatchFocusLocked(currentTime, typedEntry); |
| 819 | done = true; |
| 820 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 821 | break; |
| 822 | } |
| 823 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 824 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 825 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 826 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 827 | done = true; |
| 828 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 829 | break; |
| 830 | } |
| 831 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 832 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 833 | const auto typedEntry = |
| 834 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 835 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 836 | done = true; |
| 837 | break; |
| 838 | } |
| 839 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 840 | case EventEntry::Type::DRAG: { |
| 841 | std::shared_ptr<DragEntry> typedEntry = |
| 842 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 843 | dispatchDragLocked(currentTime, typedEntry); |
| 844 | done = true; |
| 845 | break; |
| 846 | } |
| 847 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 848 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 849 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 850 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 851 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 852 | resetPendingAppSwitchLocked(true); |
| 853 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 854 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 855 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 856 | } |
| 857 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 858 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 859 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 860 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 861 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 862 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 863 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 864 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 865 | break; |
| 866 | } |
| 867 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 868 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 869 | std::shared_ptr<MotionEntry> motionEntry = |
| 870 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 871 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 872 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 873 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 874 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 875 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 876 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 877 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 878 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 879 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 880 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 881 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 882 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 883 | |
| 884 | case EventEntry::Type::SENSOR: { |
| 885 | std::shared_ptr<SensorEntry> sensorEntry = |
| 886 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 887 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 888 | dropReason = DropReason::APP_SWITCH; |
| 889 | } |
| 890 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 891 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 892 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 893 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 894 | dropReason = DropReason::STALE; |
| 895 | } |
| 896 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 897 | done = true; |
| 898 | break; |
| 899 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 903 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 904 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 905 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 906 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 907 | |
| 908 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 909 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 913 | /** |
| 914 | * Return true if the events preceding this incoming motion event should be dropped |
| 915 | * Return false otherwise (the default behaviour) |
| 916 | */ |
| 917 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 918 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 919 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 920 | |
| 921 | // Optimize case where the current application is unresponsive and the user |
| 922 | // decides to touch a window in a different application. |
| 923 | // If the application takes too long to catch up then we drop all events preceding |
| 924 | // the touch into the other window. |
| 925 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 926 | int32_t displayId = motionEntry.displayId; |
| 927 | int32_t x = static_cast<int32_t>( |
| 928 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 929 | int32_t y = static_cast<int32_t>( |
| 930 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 931 | |
| 932 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 933 | sp<WindowInfoHandle> touchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 934 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 935 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 936 | touchedWindowHandle->getApplicationToken() != |
| 937 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 938 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 939 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 940 | "for %s", |
| 941 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 942 | return true; |
| 943 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 944 | |
| 945 | // Alternatively, maybe there's a gesture monitor that could handle this event |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 946 | for (const auto& monitor : getValueByKey(mGestureMonitorsByDisplay, displayId)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 947 | sp<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 948 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 949 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 950 | // This monitor could take more input. Drop all events preceding this |
| 951 | // event, so that gesture monitor could get a chance to receive the stream |
| 952 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 953 | "responsive gesture monitor that may handle the event", |
| 954 | mAwaitedFocusedApplication->getName().c_str()); |
| 955 | return true; |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 961 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 962 | // events to be processed before dispatching the key event. This is because these motion events |
| 963 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 964 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 965 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 966 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 967 | "just send the pending key event to the focused window."); |
| 968 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 969 | } |
| 970 | return false; |
| 971 | } |
| 972 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 973 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 974 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 975 | mInboundQueue.push_back(std::move(newEntry)); |
| 976 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 977 | traceInboundQueueLengthLocked(); |
| 978 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 979 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 980 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 981 | // Optimize app switch latency. |
| 982 | // If the application takes too long to catch up then we drop all events preceding |
| 983 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 984 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 985 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 986 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 987 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 988 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 989 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 990 | if (DEBUG_APP_SWITCH) { |
| 991 | ALOGD("App switch is pending!"); |
| 992 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 993 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 994 | mAppSwitchSawKeyDown = false; |
| 995 | needWake = true; |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | break; |
| 1000 | } |
| 1001 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1002 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1003 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1004 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1005 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1006 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1007 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1008 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1009 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1010 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1011 | break; |
| 1012 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1013 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1014 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1015 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1016 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1017 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1018 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1019 | // nothing to do |
| 1020 | break; |
| 1021 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | return needWake; |
| 1025 | } |
| 1026 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1027 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1028 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1029 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1030 | mRecentQueue.push_back(entry); |
| 1031 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1032 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1033 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1037 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1038 | int32_t y, TouchState* touchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1039 | bool isStylus, |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1040 | bool addOutsideTargets, |
| 1041 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1042 | if (addOutsideTargets && touchState == nullptr) { |
| 1043 | 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] | 1044 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1045 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1046 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1047 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1048 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1049 | continue; |
| 1050 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1051 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1052 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1053 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1054 | return windowHandle; |
| 1055 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1056 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1057 | if (addOutsideTargets && info.flags.test(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
| 1058 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1059 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1060 | } |
| 1061 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1062 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1065 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1066 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1067 | // Traverse windows from front to back and gather the touched spy windows. |
| 1068 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1069 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1070 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1071 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1072 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1073 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1074 | continue; |
| 1075 | } |
| 1076 | if (!info.isSpy()) { |
| 1077 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1078 | return spyWindows; |
| 1079 | } |
| 1080 | spyWindows.push_back(windowHandle); |
| 1081 | } |
| 1082 | return spyWindows; |
| 1083 | } |
| 1084 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1085 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1086 | const char* reason; |
| 1087 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1088 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1089 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1090 | ALOGD("Dropped event because policy consumed it."); |
| 1091 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1092 | reason = "inbound event was dropped because the policy consumed it"; |
| 1093 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1094 | case DropReason::DISABLED: |
| 1095 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1096 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1097 | } |
| 1098 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1099 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1100 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1101 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1102 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1103 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1104 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1105 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1106 | "has started interacting with a different application."); |
| 1107 | reason = "inbound event was dropped because the current application is not responding " |
| 1108 | "and the user has started interacting with a different application"; |
| 1109 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1110 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1111 | ALOGI("Dropped event because it is stale."); |
| 1112 | reason = "inbound event was dropped because it is stale"; |
| 1113 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1114 | case DropReason::NO_POINTER_CAPTURE: |
| 1115 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1116 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1117 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1118 | case DropReason::NOT_DROPPED: { |
| 1119 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1120 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1121 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1124 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1125 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1126 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1127 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1128 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1129 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1130 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1131 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1132 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1133 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1134 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1135 | } else { |
| 1136 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1137 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1138 | } |
| 1139 | break; |
| 1140 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1141 | case EventEntry::Type::SENSOR: { |
| 1142 | break; |
| 1143 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1144 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1145 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1146 | break; |
| 1147 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1148 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1149 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1150 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1151 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1152 | 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] | 1153 | break; |
| 1154 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1158 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1159 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1160 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1163 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1164 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1165 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1166 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1170 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1171 | } |
| 1172 | |
| 1173 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1174 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1175 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1176 | if (DEBUG_APP_SWITCH) { |
| 1177 | if (handled) { |
| 1178 | ALOGD("App switch has arrived."); |
| 1179 | } else { |
| 1180 | ALOGD("App switch was abandoned."); |
| 1181 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1182 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1183 | } |
| 1184 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1185 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1186 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1189 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1190 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1191 | return false; |
| 1192 | } |
| 1193 | |
| 1194 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1195 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1196 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1197 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1198 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1199 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | return true; |
| 1201 | } |
| 1202 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1203 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1204 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1208 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1209 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1210 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | releaseInboundEventLocked(entry); |
| 1212 | } |
| 1213 | traceInboundQueueLengthLocked(); |
| 1214 | } |
| 1215 | |
| 1216 | void InputDispatcher::releasePendingEventLocked() { |
| 1217 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1218 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1219 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1223 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1224 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1225 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1226 | if (DEBUG_DISPATCH_CYCLE) { |
| 1227 | ALOGD("Injected inbound event was dropped."); |
| 1228 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1229 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | } |
| 1231 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1232 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1233 | } |
| 1234 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1238 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1239 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1243 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1244 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1245 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1246 | uint32_t policyFlags = entry->policyFlags & |
| 1247 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1248 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1249 | std::shared_ptr<KeyEntry> newEntry = |
| 1250 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1251 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1252 | entry->flags, entry->keyCode, entry->scanCode, |
| 1253 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1255 | newEntry->syntheticRepeat = true; |
| 1256 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1257 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1258 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1259 | } |
| 1260 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1261 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1262 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1263 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1264 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1265 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1266 | |
| 1267 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1268 | resetKeyRepeatLocked(); |
| 1269 | |
| 1270 | // 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] | 1271 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1272 | scoped_unlock unlock(mLock); |
| 1273 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1274 | }; |
| 1275 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1276 | return true; |
| 1277 | } |
| 1278 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1279 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1280 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1281 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1282 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1283 | entry.deviceId); |
| 1284 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1285 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1286 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1287 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1288 | resetKeyRepeatLocked(); |
| 1289 | } |
| 1290 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1291 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1292 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1293 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1294 | return true; |
| 1295 | } |
| 1296 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1297 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1298 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1299 | if (mPendingEvent != nullptr) { |
| 1300 | // Move the pending event to the front of the queue. This will give the chance |
| 1301 | // for the pending event to get dispatched to the newly focused window |
| 1302 | mInboundQueue.push_front(mPendingEvent); |
| 1303 | mPendingEvent = nullptr; |
| 1304 | } |
| 1305 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1306 | std::unique_ptr<FocusEntry> focusEntry = |
| 1307 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1308 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1309 | |
| 1310 | // This event should go to the front of the queue, but behind all other focus events |
| 1311 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1312 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1313 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1314 | [](const std::shared_ptr<EventEntry>& event) { |
| 1315 | return event->type == EventEntry::Type::FOCUS; |
| 1316 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1317 | |
| 1318 | // 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] | 1319 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1320 | } |
| 1321 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1322 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1323 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1324 | if (channel == nullptr) { |
| 1325 | return; // Window has gone away |
| 1326 | } |
| 1327 | InputTarget target; |
| 1328 | target.inputChannel = channel; |
| 1329 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1330 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1331 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1332 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1333 | std::string reason = std::string("reason=").append(entry->reason); |
| 1334 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1335 | dispatchEventLocked(currentTime, entry, {target}); |
| 1336 | } |
| 1337 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1338 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1339 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1340 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1341 | dropReason = DropReason::NOT_DROPPED; |
| 1342 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1343 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1344 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1345 | |
| 1346 | if (entry->pointerCaptureRequest.enable) { |
| 1347 | // Enable Pointer Capture. |
| 1348 | if (haveWindowWithPointerCapture && |
| 1349 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
| 1350 | LOG_ALWAYS_FATAL("This request to enable Pointer Capture has already been dispatched " |
| 1351 | "to the window."); |
| 1352 | } |
| 1353 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1354 | // This can happen if a window requests capture and immediately releases capture. |
| 1355 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1356 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1357 | return; |
| 1358 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1359 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1360 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1361 | return; |
| 1362 | } |
| 1363 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1364 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1365 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1366 | mWindowTokenWithPointerCapture = token; |
| 1367 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1368 | // Disable Pointer Capture. |
| 1369 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1370 | // for two reasons: |
| 1371 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1372 | // to disable capture with the same sequence number: one generated by |
| 1373 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1374 | // Capture being disabled in InputReader. |
| 1375 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1376 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1377 | // in InputReader. |
| 1378 | if (!haveWindowWithPointerCapture) { |
| 1379 | // Pointer capture was already forcefully disabled because of focus change. |
| 1380 | dropReason = DropReason::NOT_DROPPED; |
| 1381 | return; |
| 1382 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1383 | token = mWindowTokenWithPointerCapture; |
| 1384 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1385 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1386 | setPointerCaptureLocked(false); |
| 1387 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | auto channel = getInputChannelLocked(token); |
| 1391 | if (channel == nullptr) { |
| 1392 | // Window has gone away, clean up Pointer Capture state. |
| 1393 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1394 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1395 | setPointerCaptureLocked(false); |
| 1396 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1397 | return; |
| 1398 | } |
| 1399 | InputTarget target; |
| 1400 | target.inputChannel = channel; |
| 1401 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1402 | entry->dispatchInProgress = true; |
| 1403 | dispatchEventLocked(currentTime, entry, {target}); |
| 1404 | |
| 1405 | dropReason = DropReason::NOT_DROPPED; |
| 1406 | } |
| 1407 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1408 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1409 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1410 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1411 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1412 | if (windowHandles.empty()) { |
| 1413 | return; |
| 1414 | } |
| 1415 | const std::vector<InputTarget> inputTargets = |
| 1416 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1417 | if (inputTargets.empty()) { |
| 1418 | return; |
| 1419 | } |
| 1420 | entry->dispatchInProgress = true; |
| 1421 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1422 | } |
| 1423 | |
| 1424 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1425 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1426 | std::vector<InputTarget> inputTargets; |
| 1427 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1428 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1429 | const sp<IBinder>& token = handle->getToken(); |
| 1430 | if (token == nullptr) { |
| 1431 | continue; |
| 1432 | } |
| 1433 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1434 | if (channel == nullptr) { |
| 1435 | continue; // Window has gone away |
| 1436 | } |
| 1437 | InputTarget target; |
| 1438 | target.inputChannel = channel; |
| 1439 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1440 | inputTargets.push_back(target); |
| 1441 | } |
| 1442 | return inputTargets; |
| 1443 | } |
| 1444 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1445 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1446 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1447 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1448 | if (!entry->dispatchInProgress) { |
| 1449 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1450 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1451 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1452 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1453 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1454 | // We have seen two identical key downs in a row which indicates that the device |
| 1455 | // driver is automatically generating key repeats itself. We take note of the |
| 1456 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1457 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1458 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1459 | // Make sure we don't get key down from a different device. If a different |
| 1460 | // device Id has same key pressed down, the new device Id will replace the |
| 1461 | // current one to hold the key repeat with repeat count reset. |
| 1462 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1463 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1464 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1465 | resetKeyRepeatLocked(); |
| 1466 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1467 | } else { |
| 1468 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1469 | resetKeyRepeatLocked(); |
| 1470 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1471 | } |
| 1472 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1473 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1474 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1475 | // 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] | 1476 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1477 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1478 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1479 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1480 | resetKeyRepeatLocked(); |
| 1481 | } |
| 1482 | |
| 1483 | if (entry->repeatCount == 1) { |
| 1484 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1485 | } else { |
| 1486 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1487 | } |
| 1488 | |
| 1489 | entry->dispatchInProgress = true; |
| 1490 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1491 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | // Handle case where the policy asked us to try again later last time. |
| 1495 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1496 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1497 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1498 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1499 | } |
| 1500 | return false; // wait until next wakeup |
| 1501 | } |
| 1502 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1503 | entry->interceptKeyWakeupTime = 0; |
| 1504 | } |
| 1505 | |
| 1506 | // Give the policy a chance to intercept the key. |
| 1507 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1508 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1509 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1510 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1511 | |
| 1512 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1513 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1514 | }; |
| 1515 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1516 | return false; // wait for the command to run |
| 1517 | } else { |
| 1518 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1519 | } |
| 1520 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1521 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1522 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1527 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1528 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1529 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1530 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1531 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1532 | return true; |
| 1533 | } |
| 1534 | |
| 1535 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1536 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1537 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1538 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1539 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1540 | return false; |
| 1541 | } |
| 1542 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1543 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1544 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1545 | return true; |
| 1546 | } |
| 1547 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1548 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1549 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1550 | |
| 1551 | // Dispatch the key. |
| 1552 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1553 | return true; |
| 1554 | } |
| 1555 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1556 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1557 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1558 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1559 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1560 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1561 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1562 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1563 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1564 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1565 | } |
| 1566 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1567 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1568 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1569 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1570 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1571 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1572 | "source=0x%x, sensorType=%s", |
| 1573 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1574 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1575 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1576 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1577 | scoped_unlock unlock(mLock); |
| 1578 | |
| 1579 | if (entry->accuracyChanged) { |
| 1580 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1581 | } |
| 1582 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1583 | entry->hwTimestamp, entry->values); |
| 1584 | }; |
| 1585 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1589 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1590 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1591 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1592 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1593 | { // acquire lock |
| 1594 | std::scoped_lock _l(mLock); |
| 1595 | |
| 1596 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1597 | std::shared_ptr<EventEntry> entry = *it; |
| 1598 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1599 | it = mInboundQueue.erase(it); |
| 1600 | releaseInboundEventLocked(entry); |
| 1601 | } |
| 1602 | } |
| 1603 | } |
| 1604 | return true; |
| 1605 | } |
| 1606 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1607 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1608 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1609 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1610 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1611 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1612 | entry->dispatchInProgress = true; |
| 1613 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1614 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1618 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1619 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1620 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1621 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1622 | return true; |
| 1623 | } |
| 1624 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1625 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1626 | |
| 1627 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1628 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1629 | |
| 1630 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1631 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1632 | if (isPointerEvent) { |
| 1633 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1634 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1635 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1636 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1637 | } else { |
| 1638 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1639 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1640 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1641 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1642 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1643 | return false; |
| 1644 | } |
| 1645 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1646 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1647 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1648 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1649 | return true; |
| 1650 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1651 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1652 | CancelationOptions::Mode mode(isPointerEvent |
| 1653 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1654 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1655 | CancelationOptions options(mode, "input event injection failed"); |
| 1656 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1657 | return true; |
| 1658 | } |
| 1659 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1660 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1661 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1662 | |
| 1663 | // Dispatch the motion. |
| 1664 | if (conflictingPointerActions) { |
| 1665 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1666 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1667 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1668 | } |
| 1669 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1670 | return true; |
| 1671 | } |
| 1672 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1673 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1674 | bool isExiting, const MotionEntry& motionEntry) { |
| 1675 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1676 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1677 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1678 | PointerCoords pointerCoords; |
| 1679 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1680 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1681 | |
| 1682 | std::unique_ptr<DragEntry> dragEntry = |
| 1683 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1684 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1685 | pointerCoords.getY()); |
| 1686 | |
| 1687 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1688 | } |
| 1689 | |
| 1690 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1691 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1692 | if (channel == nullptr) { |
| 1693 | return; // Window has gone away |
| 1694 | } |
| 1695 | InputTarget target; |
| 1696 | target.inputChannel = channel; |
| 1697 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1698 | entry->dispatchInProgress = true; |
| 1699 | dispatchEventLocked(currentTime, entry, {target}); |
| 1700 | } |
| 1701 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1702 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1703 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1704 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1705 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1706 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1707 | "metaState=0x%x, buttonState=0x%x," |
| 1708 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1709 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1710 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1711 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1712 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1713 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1714 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1715 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1716 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1717 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1718 | "orientation=%f", |
| 1719 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1720 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1721 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1722 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1723 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1724 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1725 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1726 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1727 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1728 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1729 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1730 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1731 | } |
| 1732 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1733 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1734 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1735 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1736 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1737 | if (DEBUG_DISPATCH_CYCLE) { |
| 1738 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1739 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1740 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1741 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1742 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1743 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1744 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1745 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1746 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1747 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1748 | sp<Connection> connection = |
| 1749 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1750 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1751 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1752 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1753 | if (DEBUG_FOCUS) { |
| 1754 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1755 | "is no longer registered with the input dispatcher.", |
| 1756 | inputTarget.inputChannel->getName().c_str()); |
| 1757 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1758 | } |
| 1759 | } |
| 1760 | } |
| 1761 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1762 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1763 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1764 | // If the policy decides to close the app, we will get a channel removal event via |
| 1765 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1766 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1767 | // pile up. |
| 1768 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1769 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1770 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1771 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1772 | "application not responding"); |
| 1773 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1774 | } |
| 1775 | } |
| 1776 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1777 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1778 | if (DEBUG_FOCUS) { |
| 1779 | ALOGD("Resetting ANR timeouts."); |
| 1780 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1781 | |
| 1782 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1783 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1784 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1785 | } |
| 1786 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1787 | /** |
| 1788 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1789 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1790 | * Focused display is the display that the user most recently interacted with. |
| 1791 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1792 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1793 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1794 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1795 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1796 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1797 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1798 | break; |
| 1799 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1800 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1801 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1802 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1803 | break; |
| 1804 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1805 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1806 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1807 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1808 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1809 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1810 | case EventEntry::Type::SENSOR: |
| 1811 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1812 | 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] | 1813 | return ADISPLAY_ID_NONE; |
| 1814 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1815 | } |
| 1816 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1817 | } |
| 1818 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1819 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1820 | const char* focusedWindowName) { |
| 1821 | if (mAnrTracker.empty()) { |
| 1822 | // already processed all events that we waited for |
| 1823 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1824 | return false; |
| 1825 | } |
| 1826 | |
| 1827 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1828 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1829 | // 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] | 1830 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1831 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1832 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1833 | return true; |
| 1834 | } |
| 1835 | |
| 1836 | // We still have pending events, and already started the timer |
| 1837 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1838 | return true; // Still waiting |
| 1839 | } |
| 1840 | |
| 1841 | // Waited too long, and some connection still hasn't processed all motions |
| 1842 | // Just send the key to the focused window |
| 1843 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1844 | focusedWindowName); |
| 1845 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1846 | return false; |
| 1847 | } |
| 1848 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1849 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1850 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1851 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1852 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1853 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1854 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1855 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1856 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1857 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1858 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1859 | // If there is no currently focused window and no focused application |
| 1860 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1861 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1862 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1863 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1864 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1865 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1866 | } |
| 1867 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1868 | // Drop key events if requested by input feature |
| 1869 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1870 | return InputEventInjectionResult::FAILED; |
| 1871 | } |
| 1872 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1873 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1874 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1875 | // start interacting with another application via touch (app switch). This code can be removed |
| 1876 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1877 | // an app is expected to have a focused window. |
| 1878 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1879 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1880 | // 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] | 1881 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1882 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1883 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1884 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1885 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1886 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1887 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1888 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1889 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1890 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1891 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1892 | // Already raised ANR. Drop the event |
| 1893 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1894 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1895 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1896 | } else { |
| 1897 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1898 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1899 | } |
| 1900 | } |
| 1901 | |
| 1902 | // we have a valid, non-null focused window |
| 1903 | resetNoFocusedWindowTimeoutLocked(); |
| 1904 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1905 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1906 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1907 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1908 | } |
| 1909 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1910 | if (focusedWindowHandle->getInfo()->paused) { |
| 1911 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1912 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | // If the event is a key event, then we must wait for all previous events to |
| 1916 | // complete before delivering it because previous events may have the |
| 1917 | // side-effect of transferring focus to a different window and we want to |
| 1918 | // ensure that the following keys are sent to the new window. |
| 1919 | // |
| 1920 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1921 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1922 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1923 | // often anticipate pending UI changes when typing on a keyboard. |
| 1924 | // To obtain this behavior, we must serialize key events with respect to all |
| 1925 | // prior input events. |
| 1926 | if (entry.type == EventEntry::Type::KEY) { |
| 1927 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1928 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1929 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1930 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1934 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1935 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1936 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1937 | |
| 1938 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1939 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1940 | } |
| 1941 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1942 | /** |
| 1943 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1944 | * that are currently unresponsive. |
| 1945 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1946 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1947 | const std::vector<Monitor>& monitors) const { |
| 1948 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1949 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1950 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 1951 | sp<Connection> connection = |
| 1952 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1953 | if (connection == nullptr) { |
| 1954 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1955 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1956 | return false; |
| 1957 | } |
| 1958 | if (!connection->responsive) { |
| 1959 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1960 | connection->inputChannel->getName().c_str()); |
| 1961 | return false; |
| 1962 | } |
| 1963 | return true; |
| 1964 | }); |
| 1965 | return responsiveMonitors; |
| 1966 | } |
| 1967 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1968 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1969 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1970 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1971 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1972 | enum InjectionPermission { |
| 1973 | INJECTION_PERMISSION_UNKNOWN, |
| 1974 | INJECTION_PERMISSION_GRANTED, |
| 1975 | INJECTION_PERMISSION_DENIED |
| 1976 | }; |
| 1977 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1978 | // For security reasons, we defer updating the touch state until we are sure that |
| 1979 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1980 | const int32_t displayId = entry.displayId; |
| 1981 | const int32_t action = entry.action; |
| 1982 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1983 | |
| 1984 | // 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] | 1985 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1986 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1987 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1988 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1989 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1990 | // Copy current touch state into tempTouchState. |
| 1991 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1992 | // 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] | 1993 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1994 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1995 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 1996 | oldState = &(it->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1997 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1998 | } |
| 1999 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2000 | bool isSplit = tempTouchState.split; |
| 2001 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 2002 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 2003 | tempTouchState.displayId != displayId); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2004 | |
| 2005 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2006 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2007 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2008 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2009 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2010 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2011 | bool wrongDevice = false; |
| 2012 | if (newGesture) { |
| 2013 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2014 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2015 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2016 | "in display %" PRId32, |
| 2017 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2018 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2019 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2020 | switchedDevice = false; |
| 2021 | wrongDevice = true; |
| 2022 | goto Failed; |
| 2023 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2024 | tempTouchState.reset(); |
| 2025 | tempTouchState.down = down; |
| 2026 | tempTouchState.deviceId = entry.deviceId; |
| 2027 | tempTouchState.source = entry.source; |
| 2028 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2029 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2030 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2031 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2032 | "in display %" PRId32, |
| 2033 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2034 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2035 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2036 | switchedDevice = false; |
| 2037 | wrongDevice = true; |
| 2038 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2042 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2043 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2044 | int32_t x; |
| 2045 | int32_t y; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2046 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2047 | // Always dispatch mouse events to cursor position. |
| 2048 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2049 | x = int32_t(entry.xCursorPosition); |
| 2050 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2051 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2052 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2053 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2054 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2055 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2056 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2057 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2058 | isStylus, isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2059 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2060 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2061 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2062 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2063 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2064 | // 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] | 2065 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2068 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2069 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2070 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2071 | // New window supports splitting, but we should never split mouse events. |
| 2072 | isSplit = !isFromMouse; |
| 2073 | } else if (isSplit) { |
| 2074 | // New window does not support splitting but we have already split events. |
| 2075 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2076 | newTouchedWindowHandle = nullptr; |
| 2077 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2078 | } else { |
| 2079 | // 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] | 2080 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2081 | // should never be split. |
| 2082 | tempTouchState.split = isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2085 | // Update hover state. |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2086 | if (newTouchedWindowHandle != nullptr) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2087 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2088 | newHoverWindowHandle = nullptr; |
| 2089 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2090 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2091 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2092 | } |
| 2093 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2094 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2095 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2096 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2097 | // Process the foreground window first so that it is the first to receive the event. |
| 2098 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
| 2102 | const WindowInfo& info = *windowHandle->getInfo(); |
| 2103 | |
| 2104 | if (info.paused) { |
| 2105 | ALOGI("Not sending touch event to %s because it is paused", |
| 2106 | windowHandle->getName().c_str()); |
| 2107 | continue; |
| 2108 | } |
| 2109 | |
| 2110 | // Ensure the window has a connection and the connection is responsive |
| 2111 | const bool isResponsive = hasResponsiveConnectionLocked(*windowHandle); |
| 2112 | if (!isResponsive) { |
| 2113 | ALOGW("Not sending touch gesture to %s because it is not responsive", |
| 2114 | windowHandle->getName().c_str()); |
| 2115 | continue; |
| 2116 | } |
| 2117 | |
| 2118 | // Drop events that can't be trusted due to occlusion |
| 2119 | if (mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2120 | TouchOcclusionInfo occlusionInfo = |
| 2121 | computeTouchOcclusionInfoLocked(windowHandle, x, y); |
| 2122 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 2123 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2124 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2125 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2126 | ALOGD("%s", log.c_str()); |
| 2127 | } |
| 2128 | } |
| 2129 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
| 2130 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2131 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2132 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2133 | continue; |
| 2134 | } |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | // Drop touch events if requested by input feature |
| 2139 | if (shouldDropInput(entry, windowHandle)) { |
| 2140 | continue; |
| 2141 | } |
| 2142 | |
| 2143 | // Set target flags. |
| 2144 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2145 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2146 | if (!info.isSpy()) { |
| 2147 | // There should only be one new foreground (non-spy) window at this location. |
| 2148 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2149 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2150 | |
| 2151 | if (isSplit) { |
| 2152 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2153 | } |
| 2154 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
| 2155 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2156 | } else if (isWindowObscuredLocked(windowHandle)) { |
| 2157 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2158 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2159 | |
| 2160 | // Update the temporary touch state. |
| 2161 | BitSet32 pointerIds; |
| 2162 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2163 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2164 | pointerIds.markBit(pointerId); |
| 2165 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2166 | |
| 2167 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2168 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2169 | |
| 2170 | const std::vector<Monitor> newGestureMonitors = isDown |
| 2171 | ? selectResponsiveMonitorsLocked( |
| 2172 | getValueByKey(mGestureMonitorsByDisplay, displayId)) |
| 2173 | : std::vector<Monitor>{}; |
| 2174 | |
| 2175 | if (newTouchedWindows.empty() && newGestureMonitors.empty() && |
| 2176 | tempTouchState.gestureMonitors.empty()) { |
| 2177 | ALOGI("Dropping event because there is no touchable window or gesture monitor at " |
| 2178 | "(%d, %d) in display %" PRId32 ".", |
| 2179 | x, y, displayId); |
| 2180 | injectionResult = InputEventInjectionResult::FAILED; |
| 2181 | goto Failed; |
Arthur Hung | 2ea44b9 | 2021-11-23 07:42:21 +0000 | [diff] [blame] | 2182 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2183 | |
| 2184 | tempTouchState.addGestureMonitors(newGestureMonitors); |
| 2185 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2186 | } else { |
| 2187 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2188 | |
| 2189 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2190 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2191 | if (DEBUG_FOCUS) { |
| 2192 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2193 | "dropped the pointer down event in display %" PRId32, |
| 2194 | displayId); |
| 2195 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2196 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2197 | goto Failed; |
| 2198 | } |
| 2199 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2200 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2201 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2202 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2203 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2204 | tempTouchState.isSlippery()) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2205 | const int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2206 | 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] | 2207 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2208 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2209 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2210 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2211 | newTouchedWindowHandle = |
| 2212 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2213 | |
| 2214 | // Drop touch events if requested by input feature |
| 2215 | if (newTouchedWindowHandle != nullptr && |
| 2216 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2217 | newTouchedWindowHandle = nullptr; |
| 2218 | } |
| 2219 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2220 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2221 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2222 | if (DEBUG_FOCUS) { |
| 2223 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2224 | oldTouchedWindowHandle->getName().c_str(), |
| 2225 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2226 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2227 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2228 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2229 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2230 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2231 | |
| 2232 | // Make a slippery entrance into the new window. |
| 2233 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2234 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2235 | } |
| 2236 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2237 | int32_t targetFlags = |
| 2238 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2239 | if (isSplit) { |
| 2240 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2241 | } |
| 2242 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2243 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2244 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2245 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | BitSet32 pointerIds; |
| 2249 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2250 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2251 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2252 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2253 | } |
| 2254 | } |
| 2255 | } |
| 2256 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2257 | // Update dispatching for hover enter and exit. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2258 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2259 | // Let the previous window know that the hover sequence is over, unless we already did |
| 2260 | // it when dispatching it as is to newTouchedWindowHandle. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2261 | if (mLastHoverWindowHandle != nullptr && |
| 2262 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2263 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2264 | if (DEBUG_HOVER) { |
| 2265 | ALOGD("Sending hover exit event to window %s.", |
| 2266 | mLastHoverWindowHandle->getName().c_str()); |
| 2267 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2268 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2269 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2270 | } |
| 2271 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2272 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2273 | // when dispatching it as is to newTouchedWindowHandle. |
| 2274 | if (newHoverWindowHandle != nullptr && |
| 2275 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2276 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2277 | if (DEBUG_HOVER) { |
| 2278 | ALOGD("Sending hover enter event to window %s.", |
| 2279 | newHoverWindowHandle->getName().c_str()); |
| 2280 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2281 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2282 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2283 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | // Check permission to inject into all touched foreground windows and ensure there |
| 2288 | // is at least one touched foreground window. |
| 2289 | { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2290 | bool haveForegroundOrSpyWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2291 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2292 | const bool isForeground = |
| 2293 | (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0; |
| 2294 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 2295 | haveForegroundOrSpyWindow = true; |
| 2296 | LOG_ALWAYS_FATAL_IF(isForeground, |
| 2297 | "Spy window cannot be dispatched as a foreground window."); |
| 2298 | } |
| 2299 | if (isForeground) { |
| 2300 | haveForegroundOrSpyWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2301 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2302 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2303 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2304 | goto Failed; |
| 2305 | } |
| 2306 | } |
| 2307 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2308 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2309 | if (!haveForegroundOrSpyWindow && !hasGestureMonitor) { |
| 2310 | ALOGI("Dropping event because there is no touched window in display " |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2311 | "%" PRId32 " or gesture monitor to receive it.", |
| 2312 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2313 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2314 | goto Failed; |
| 2315 | } |
| 2316 | |
| 2317 | // Permission granted to injection into all touched foreground windows. |
| 2318 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2319 | } |
| 2320 | |
| 2321 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2322 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2323 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2324 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2325 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2326 | if (foregroundWindowHandle) { |
| 2327 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2328 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2329 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2330 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2331 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2332 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2333 | InputTarget::FLAG_ZERO_COORDS, |
| 2334 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2335 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2336 | } |
| 2337 | } |
| 2338 | } |
| 2339 | } |
| 2340 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2341 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2342 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2343 | // of the touch gesture. |
| 2344 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2345 | // engine only supports touch events. We would need to add a mechanism similar |
| 2346 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2347 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2348 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2349 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2350 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2351 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2352 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2353 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2354 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2355 | if (info->displayId == displayId && |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2356 | windowHandle->getInfo()->type == WindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2357 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2358 | .addOrUpdateWindow(windowHandle, |
| 2359 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2360 | InputTarget:: |
| 2361 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2362 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2363 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2364 | } |
| 2365 | } |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2370 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2371 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2372 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2373 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2374 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2375 | } |
| 2376 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2377 | for (const auto& monitor : tempTouchState.gestureMonitors) { |
| 2378 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2381 | // Drop the outside or hover touch windows since we will not care about them |
| 2382 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2383 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2384 | |
| 2385 | Failed: |
| 2386 | // Check injection permission once and for all. |
| 2387 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2388 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2389 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2390 | } else { |
| 2391 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2392 | } |
| 2393 | } |
| 2394 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2395 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2396 | return injectionResult; |
| 2397 | } |
| 2398 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2399 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2400 | if (!wrongDevice) { |
| 2401 | if (switchedDevice) { |
| 2402 | if (DEBUG_FOCUS) { |
| 2403 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2404 | } |
| 2405 | *outConflictingPointerActions = true; |
| 2406 | } |
| 2407 | |
| 2408 | if (isHoverAction) { |
| 2409 | // Started hovering, therefore no longer down. |
| 2410 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2411 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2412 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2413 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2414 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2415 | *outConflictingPointerActions = true; |
| 2416 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2417 | tempTouchState.reset(); |
| 2418 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2419 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2420 | tempTouchState.deviceId = entry.deviceId; |
| 2421 | tempTouchState.source = entry.source; |
| 2422 | tempTouchState.displayId = displayId; |
| 2423 | } |
| 2424 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2425 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2426 | // All pointers up or canceled. |
| 2427 | tempTouchState.reset(); |
| 2428 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2429 | // First pointer went down. |
| 2430 | if (oldState && oldState->down) { |
| 2431 | if (DEBUG_FOCUS) { |
| 2432 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2433 | } |
| 2434 | *outConflictingPointerActions = true; |
| 2435 | } |
| 2436 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2437 | // One pointer went up. |
| 2438 | if (isSplit) { |
| 2439 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2440 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2441 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2442 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2443 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2444 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2445 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2446 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2447 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2448 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2449 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2450 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2451 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2452 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2453 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2454 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2455 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2456 | // Save changes unless the action was scroll in which case the temporary touch |
| 2457 | // state was only valid for this one action. |
| 2458 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2459 | if (tempTouchState.displayId >= 0) { |
| 2460 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2461 | } else { |
| 2462 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2463 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2464 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2465 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2466 | // Update hover state. |
| 2467 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2468 | } |
| 2469 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2470 | return injectionResult; |
| 2471 | } |
| 2472 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2473 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2474 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2475 | // have an explicit reason to support it. |
| 2476 | constexpr bool isStylus = false; |
| 2477 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2478 | const sp<WindowInfoHandle> dropWindow = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2479 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2480 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2481 | if (dropWindow) { |
| 2482 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2483 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2484 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2485 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2486 | } |
| 2487 | mDragState.reset(); |
| 2488 | } |
| 2489 | |
| 2490 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2491 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2492 | return; |
| 2493 | } |
| 2494 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2495 | if (!mDragState->isStartDrag) { |
| 2496 | mDragState->isStartDrag = true; |
| 2497 | mDragState->isStylusButtonDownAtStart = |
| 2498 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2499 | } |
| 2500 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2501 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2502 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2503 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2504 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2505 | // Handle the special case : stylus button no longer pressed. |
| 2506 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2507 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2508 | finishDragAndDrop(entry.displayId, x, y); |
| 2509 | return; |
| 2510 | } |
| 2511 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2512 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until |
| 2513 | // we have an explicit reason to support it. |
| 2514 | constexpr bool isStylus = false; |
| 2515 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2516 | const sp<WindowInfoHandle> hoverWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2517 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2518 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2519 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2520 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2521 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2522 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2523 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2524 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2525 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2526 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2527 | } |
| 2528 | // enqueue drag location if needed. |
| 2529 | if (hoverWindowHandle != nullptr) { |
| 2530 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2531 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2532 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2533 | finishDragAndDrop(entry.displayId, x, y); |
| 2534 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2535 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2536 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2537 | } |
| 2538 | } |
| 2539 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2540 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2541 | int32_t targetFlags, BitSet32 pointerIds, |
| 2542 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2543 | std::vector<InputTarget>::iterator it = |
| 2544 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2545 | [&windowHandle](const InputTarget& inputTarget) { |
| 2546 | return inputTarget.inputChannel->getConnectionToken() == |
| 2547 | windowHandle->getToken(); |
| 2548 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2549 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2550 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2551 | |
| 2552 | if (it == inputTargets.end()) { |
| 2553 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2554 | std::shared_ptr<InputChannel> inputChannel = |
| 2555 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2556 | if (inputChannel == nullptr) { |
| 2557 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2558 | return; |
| 2559 | } |
| 2560 | inputTarget.inputChannel = inputChannel; |
| 2561 | inputTarget.flags = targetFlags; |
| 2562 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2563 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2564 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2565 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2566 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2567 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2568 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2569 | inputTargets.push_back(inputTarget); |
| 2570 | it = inputTargets.end() - 1; |
| 2571 | } |
| 2572 | |
| 2573 | ALOG_ASSERT(it->flags == targetFlags); |
| 2574 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2575 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2576 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2577 | } |
| 2578 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2579 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2580 | int32_t displayId) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2581 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2582 | mGlobalMonitorsByDisplay.find(displayId); |
| 2583 | |
| 2584 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2585 | const std::vector<Monitor>& monitors = it->second; |
| 2586 | for (const Monitor& monitor : monitors) { |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2587 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2588 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2592 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, int32_t displayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2593 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2594 | InputTarget target; |
| 2595 | target.inputChannel = monitor.inputChannel; |
| 2596 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2597 | ui::Transform t; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2598 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | b5402b2 | 2021-10-04 05:52:50 -0700 | [diff] [blame] | 2599 | const auto& displayTransform = it->second.transform; |
| 2600 | target.displayTransform = displayTransform; |
| 2601 | t = displayTransform; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2602 | } |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2603 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2604 | inputTargets.push_back(target); |
| 2605 | } |
| 2606 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2607 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2608 | const InjectionState* injectionState) { |
| 2609 | if (injectionState && |
| 2610 | (windowHandle == nullptr || |
| 2611 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2612 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2613 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2614 | 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] | 2615 | "owned by uid %d", |
| 2616 | injectionState->injectorPid, injectionState->injectorUid, |
| 2617 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2618 | } else { |
| 2619 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2620 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2621 | } |
| 2622 | return false; |
| 2623 | } |
| 2624 | return true; |
| 2625 | } |
| 2626 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2627 | /** |
| 2628 | * Indicate whether one window handle should be considered as obscuring |
| 2629 | * another window handle. We only check a few preconditions. Actually |
| 2630 | * checking the bounds is left to the caller. |
| 2631 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2632 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2633 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2634 | // Compare by token so cloned layers aren't counted |
| 2635 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2636 | return false; |
| 2637 | } |
| 2638 | auto info = windowHandle->getInfo(); |
| 2639 | auto otherInfo = otherHandle->getInfo(); |
| 2640 | if (!otherInfo->visible) { |
| 2641 | return false; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2642 | } else if (otherInfo->alpha == 0 && otherInfo->flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2643 | // Those act as if they were invisible, so we don't need to flag them. |
| 2644 | // We do want to potentially flag touchable windows even if they have 0 |
| 2645 | // opacity, since they can consume touches and alter the effects of the |
| 2646 | // user interaction (eg. apps that rely on |
| 2647 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2648 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2649 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2650 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2651 | // If ownerUid is the same we don't generate occlusion events as there |
| 2652 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2653 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2654 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2655 | return false; |
| 2656 | } else if (otherInfo->displayId != info->displayId) { |
| 2657 | return false; |
| 2658 | } |
| 2659 | return true; |
| 2660 | } |
| 2661 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2662 | /** |
| 2663 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2664 | * untrusted, one should check: |
| 2665 | * |
| 2666 | * 1. If result.hasBlockingOcclusion is true. |
| 2667 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2668 | * BLOCK_UNTRUSTED. |
| 2669 | * |
| 2670 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2671 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2672 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2673 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2674 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2675 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2676 | * |
| 2677 | * If neither of those is true, then it means the touch can be allowed. |
| 2678 | */ |
| 2679 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2680 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2681 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2682 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2683 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2684 | TouchOcclusionInfo info; |
| 2685 | info.hasBlockingOcclusion = false; |
| 2686 | info.obscuringOpacity = 0; |
| 2687 | info.obscuringUid = -1; |
| 2688 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2689 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2690 | if (windowHandle == otherHandle) { |
| 2691 | break; // All future windows are below us. Exit early. |
| 2692 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2693 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2694 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2695 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2696 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2697 | info.debugInfo.push_back( |
| 2698 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2699 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2700 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2701 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2702 | // window's touch occlusion mode |
| 2703 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2704 | info.hasBlockingOcclusion = true; |
| 2705 | info.obscuringUid = otherInfo->ownerUid; |
| 2706 | info.obscuringPackage = otherInfo->packageName; |
| 2707 | break; |
| 2708 | } |
| 2709 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2710 | uint32_t uid = otherInfo->ownerUid; |
| 2711 | float opacity = |
| 2712 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2713 | // Given windows A and B: |
| 2714 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2715 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2716 | opacityByUid[uid] = opacity; |
| 2717 | if (opacity > info.obscuringOpacity) { |
| 2718 | info.obscuringOpacity = opacity; |
| 2719 | info.obscuringUid = uid; |
| 2720 | info.obscuringPackage = otherInfo->packageName; |
| 2721 | } |
| 2722 | } |
| 2723 | } |
| 2724 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2725 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2726 | info.debugInfo.push_back( |
| 2727 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2728 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2729 | return info; |
| 2730 | } |
| 2731 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2732 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2733 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2734 | return StringPrintf(INDENT2 |
| 2735 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2736 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2737 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2738 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2739 | isTouchedWindow ? "[TOUCHED] " : "", ftl::enum_string(info->type).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2740 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2741 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2742 | info->frameTop, info->frameRight, info->frameBottom, |
| 2743 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2744 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2745 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2746 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2749 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2750 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2751 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2752 | occlusionInfo.obscuringUid); |
| 2753 | return false; |
| 2754 | } |
| 2755 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2756 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2757 | "%.2f, maximum allowed = %.2f)", |
| 2758 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2759 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2760 | return false; |
| 2761 | } |
| 2762 | return true; |
| 2763 | } |
| 2764 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2765 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2766 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2767 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2768 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2769 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2770 | if (windowHandle == otherHandle) { |
| 2771 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2772 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2773 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2774 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2775 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2776 | return true; |
| 2777 | } |
| 2778 | } |
| 2779 | return false; |
| 2780 | } |
| 2781 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2782 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2783 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2784 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2785 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2786 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2787 | if (windowHandle == otherHandle) { |
| 2788 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2789 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2790 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2791 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2792 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2793 | return true; |
| 2794 | } |
| 2795 | } |
| 2796 | return false; |
| 2797 | } |
| 2798 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2799 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2800 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2801 | if (applicationHandle != nullptr) { |
| 2802 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2803 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2804 | } else { |
| 2805 | return applicationHandle->getName(); |
| 2806 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2807 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2808 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2809 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2810 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2811 | } |
| 2812 | } |
| 2813 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2814 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2815 | if (!isUserActivityEvent(eventEntry)) { |
| 2816 | // 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] | 2817 | return; |
| 2818 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2819 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2820 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2821 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2822 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
| 2823 | if (info->inputFeatures.test(WindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2824 | if (DEBUG_DISPATCH_CYCLE) { |
| 2825 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2826 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2827 | return; |
| 2828 | } |
| 2829 | } |
| 2830 | |
| 2831 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2832 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2833 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2834 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2835 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2836 | return; |
| 2837 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2838 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2839 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2840 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2841 | } |
| 2842 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2843 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2844 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2845 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2846 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2847 | return; |
| 2848 | } |
| 2849 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2850 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2851 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2852 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2853 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2854 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2855 | break; |
| 2856 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2857 | } |
| 2858 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2859 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2860 | REQUIRES(mLock) { |
| 2861 | scoped_unlock unlock(mLock); |
| 2862 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2863 | }; |
| 2864 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2868 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2869 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2870 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2871 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2872 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2873 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2874 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2875 | ATRACE_NAME(message.c_str()); |
| 2876 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2877 | if (DEBUG_DISPATCH_CYCLE) { |
| 2878 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2879 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2880 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2881 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2882 | inputTarget.getPointerInfoString().c_str()); |
| 2883 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2884 | |
| 2885 | // Skip this event if the connection status is not normal. |
| 2886 | // 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] | 2887 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2888 | if (DEBUG_DISPATCH_CYCLE) { |
| 2889 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2890 | connection->getInputChannelName().c_str(), |
| 2891 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2892 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2893 | return; |
| 2894 | } |
| 2895 | |
| 2896 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2897 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2898 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2899 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2900 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2901 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2902 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2903 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2904 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2905 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2906 | if (!splitMotionEntry) { |
| 2907 | return; // split event was dropped |
| 2908 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2909 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2910 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2911 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2912 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2913 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2914 | if (DEBUG_FOCUS) { |
| 2915 | ALOGD("channel '%s' ~ Split motion event.", |
| 2916 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2917 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2918 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2919 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2920 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2921 | return; |
| 2922 | } |
| 2923 | } |
| 2924 | |
| 2925 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2926 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2927 | } |
| 2928 | |
| 2929 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2930 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2931 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2932 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2933 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2934 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2935 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2936 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2937 | ATRACE_NAME(message.c_str()); |
| 2938 | } |
| 2939 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2940 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2941 | |
| 2942 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2943 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2944 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2945 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2946 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2947 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2948 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2949 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2950 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2951 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2952 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2953 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2954 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2955 | |
| 2956 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2957 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2958 | startDispatchCycleLocked(currentTime, connection); |
| 2959 | } |
| 2960 | } |
| 2961 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2962 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2963 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2964 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2965 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2966 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2967 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2968 | connection->getInputChannelName().c_str(), |
| 2969 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2970 | ATRACE_NAME(message.c_str()); |
| 2971 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2972 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2973 | if (!(inputTargetFlags & dispatchMode)) { |
| 2974 | return; |
| 2975 | } |
| 2976 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2977 | |
| 2978 | // This is a new event. |
| 2979 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2980 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2981 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2982 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2983 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2984 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2985 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2986 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2987 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2988 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2989 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2990 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2991 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2992 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2993 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2994 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2995 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2996 | if (DEBUG_DISPATCH_CYCLE) { |
| 2997 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 2998 | "event", |
| 2999 | connection->getInputChannelName().c_str()); |
| 3000 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3001 | return; // skip the inconsistent event |
| 3002 | } |
| 3003 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3004 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3005 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3006 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3007 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3008 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3009 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3010 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3011 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3012 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3013 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3014 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 3015 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 3016 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3017 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 3018 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3019 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 3020 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 3021 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 3022 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3023 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3024 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3025 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3026 | } |
| 3027 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3028 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3029 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3030 | if (DEBUG_DISPATCH_CYCLE) { |
| 3031 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3032 | "enter event", |
| 3033 | connection->getInputChannelName().c_str()); |
| 3034 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3035 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3036 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3037 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3038 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3039 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3040 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3041 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3042 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3043 | } |
| 3044 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3045 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3046 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3047 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3048 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3049 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3050 | if (DEBUG_DISPATCH_CYCLE) { |
| 3051 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3052 | "event", |
| 3053 | connection->getInputChannelName().c_str()); |
| 3054 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3055 | return; // skip the inconsistent event |
| 3056 | } |
| 3057 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3058 | dispatchEntry->resolvedEventId = |
| 3059 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3060 | ? mIdGenerator.nextId() |
| 3061 | : motionEntry.id; |
| 3062 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3063 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3064 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3065 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3066 | ATRACE_NAME(message.c_str()); |
| 3067 | } |
| 3068 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3069 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3070 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3071 | // Skip reporting pointer down outside focus to the policy. |
| 3072 | break; |
| 3073 | } |
| 3074 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3075 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3076 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3077 | |
| 3078 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3079 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3080 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3081 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3082 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3083 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3084 | break; |
| 3085 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3086 | case EventEntry::Type::SENSOR: { |
| 3087 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3088 | break; |
| 3089 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3090 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3091 | case EventEntry::Type::DEVICE_RESET: { |
| 3092 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3093 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3094 | break; |
| 3095 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | // Remember that we are waiting for this dispatch to complete. |
| 3099 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3100 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3104 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3105 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3106 | } |
| 3107 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3108 | /** |
| 3109 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3110 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3111 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3112 | * We will see both launcher and wallpaper in that list. |
| 3113 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3114 | * until the set of interacted connections changes. |
| 3115 | * |
| 3116 | * The following items are skipped, to reduce the logspam: |
| 3117 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3118 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3119 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3120 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3121 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3122 | */ |
| 3123 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3124 | const std::vector<InputTarget>& targets) { |
| 3125 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3126 | if (entry.type == EventEntry::Type::KEY) { |
| 3127 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3128 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3129 | return; |
| 3130 | } |
| 3131 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3132 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3133 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3134 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3135 | return; |
| 3136 | } |
| 3137 | } else { |
| 3138 | return; // Not a key or a motion |
| 3139 | } |
| 3140 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3141 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3142 | std::vector<sp<Connection>> newConnections; |
| 3143 | for (const InputTarget& target : targets) { |
| 3144 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3145 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3146 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3147 | } |
| 3148 | |
| 3149 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3150 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3151 | if (connection == nullptr) { |
| 3152 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3153 | } |
| 3154 | newConnectionTokens.insert(std::move(token)); |
| 3155 | newConnections.emplace_back(connection); |
| 3156 | } |
| 3157 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3158 | return; // no change |
| 3159 | } |
| 3160 | mInteractionConnectionTokens = newConnectionTokens; |
| 3161 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3162 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3163 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3164 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3165 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3166 | std::string message = "Interaction with: " + targetList; |
| 3167 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3168 | message += "<none>"; |
| 3169 | } |
| 3170 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3171 | } |
| 3172 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3173 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3174 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3175 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3176 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3177 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3178 | return; |
| 3179 | } |
| 3180 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3181 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3182 | if (focusedToken == token) { |
| 3183 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3184 | return; |
| 3185 | } |
| 3186 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3187 | auto command = [this, token]() REQUIRES(mLock) { |
| 3188 | scoped_unlock unlock(mLock); |
| 3189 | mPolicy->onPointerDownOutsideFocus(token); |
| 3190 | }; |
| 3191 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3195 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3196 | if (ATRACE_ENABLED()) { |
| 3197 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3198 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3199 | ATRACE_NAME(message.c_str()); |
| 3200 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3201 | if (DEBUG_DISPATCH_CYCLE) { |
| 3202 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3203 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3204 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3205 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3206 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3207 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3208 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3209 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3210 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3211 | |
| 3212 | // Publish the event. |
| 3213 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3214 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3215 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3216 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3217 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3218 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3219 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3220 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3221 | status = connection->inputPublisher |
| 3222 | .publishKeyEvent(dispatchEntry->seq, |
| 3223 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3224 | keyEntry.source, keyEntry.displayId, |
| 3225 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3226 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3227 | keyEntry.scanCode, keyEntry.metaState, |
| 3228 | keyEntry.repeatCount, keyEntry.downTime, |
| 3229 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3230 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3231 | } |
| 3232 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3233 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3234 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3235 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3236 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3237 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3238 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3239 | // 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] | 3240 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3241 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3242 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3243 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3244 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3245 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3246 | // Don't apply window scale here since we don't want scale to affect raw |
| 3247 | // coordinates. The scale will be sent back to the client and applied |
| 3248 | // later when requesting relative coordinates. |
| 3249 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3250 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3251 | } |
| 3252 | usingCoords = scaledCoords; |
| 3253 | } |
| 3254 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3255 | // We don't want the dispatch target to know. |
| 3256 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3257 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3258 | scaledCoords[i].clear(); |
| 3259 | } |
| 3260 | usingCoords = scaledCoords; |
| 3261 | } |
| 3262 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3263 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3264 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3265 | |
| 3266 | // Publish the motion event. |
| 3267 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3268 | .publishMotionEvent(dispatchEntry->seq, |
| 3269 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3270 | motionEntry.deviceId, motionEntry.source, |
| 3271 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3272 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3273 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3274 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3275 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3276 | motionEntry.buttonState, |
| 3277 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3278 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3279 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3280 | motionEntry.xCursorPosition, |
| 3281 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3282 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3283 | motionEntry.downTime, motionEntry.eventTime, |
| 3284 | motionEntry.pointerCount, |
| 3285 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3286 | break; |
| 3287 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3288 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3289 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3290 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3291 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3292 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3293 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3294 | break; |
| 3295 | } |
| 3296 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3297 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3298 | const TouchModeEntry& touchModeEntry = |
| 3299 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3300 | status = connection->inputPublisher |
| 3301 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3302 | touchModeEntry.inTouchMode); |
| 3303 | |
| 3304 | break; |
| 3305 | } |
| 3306 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3307 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3308 | const auto& captureEntry = |
| 3309 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3310 | status = connection->inputPublisher |
| 3311 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3312 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3313 | break; |
| 3314 | } |
| 3315 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3316 | case EventEntry::Type::DRAG: { |
| 3317 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3318 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3319 | dragEntry.id, dragEntry.x, |
| 3320 | dragEntry.y, |
| 3321 | dragEntry.isExiting); |
| 3322 | break; |
| 3323 | } |
| 3324 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3325 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3326 | case EventEntry::Type::DEVICE_RESET: |
| 3327 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3328 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3329 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3330 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3331 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | // Check the result. |
| 3335 | if (status) { |
| 3336 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3337 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3338 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3339 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3340 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3341 | "event to it, status=%s(%d)", |
| 3342 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3343 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3344 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3345 | } else { |
| 3346 | // Pipe is full and we are waiting for the app to finish process some events |
| 3347 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3348 | if (DEBUG_DISPATCH_CYCLE) { |
| 3349 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3350 | "waiting for the application to catch up", |
| 3351 | connection->getInputChannelName().c_str()); |
| 3352 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3353 | } |
| 3354 | } else { |
| 3355 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3356 | "status=%s(%d)", |
| 3357 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3358 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3359 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3360 | } |
| 3361 | return; |
| 3362 | } |
| 3363 | |
| 3364 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3365 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3366 | connection->outboundQueue.end(), |
| 3367 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3368 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3369 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3370 | if (connection->responsive) { |
| 3371 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3372 | connection->inputChannel->getConnectionToken()); |
| 3373 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3374 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3375 | } |
| 3376 | } |
| 3377 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3378 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3379 | size_t size; |
| 3380 | switch (event.type) { |
| 3381 | case VerifiedInputEvent::Type::KEY: { |
| 3382 | size = sizeof(VerifiedKeyEvent); |
| 3383 | break; |
| 3384 | } |
| 3385 | case VerifiedInputEvent::Type::MOTION: { |
| 3386 | size = sizeof(VerifiedMotionEvent); |
| 3387 | break; |
| 3388 | } |
| 3389 | } |
| 3390 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3391 | return mHmacKeyManager.sign(start, size); |
| 3392 | } |
| 3393 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3394 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3395 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3396 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3397 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3398 | // Only sign events up and down events as the purely move events |
| 3399 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3400 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3401 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3402 | |
| 3403 | VerifiedMotionEvent verifiedEvent = |
| 3404 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3405 | verifiedEvent.actionMasked = actionMasked; |
| 3406 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3407 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3411 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3412 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3413 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3414 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3415 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3416 | } |
| 3417 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3418 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3419 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3420 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3421 | if (DEBUG_DISPATCH_CYCLE) { |
| 3422 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3423 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3424 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3425 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3426 | if (connection->status == Connection::Status::BROKEN || |
| 3427 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3428 | return; |
| 3429 | } |
| 3430 | |
| 3431 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3432 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3433 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3434 | }; |
| 3435 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3439 | const sp<Connection>& connection, |
| 3440 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3441 | if (DEBUG_DISPATCH_CYCLE) { |
| 3442 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3443 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3444 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3445 | |
| 3446 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3447 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3448 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3449 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3450 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3451 | |
| 3452 | // The connection appears to be unrecoverably broken. |
| 3453 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3454 | if (connection->status == Connection::Status::NORMAL) { |
| 3455 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3456 | |
| 3457 | if (notify) { |
| 3458 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3459 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3460 | connection->getInputChannelName().c_str()); |
| 3461 | |
| 3462 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3463 | scoped_unlock unlock(mLock); |
| 3464 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3465 | }; |
| 3466 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3467 | } |
| 3468 | } |
| 3469 | } |
| 3470 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3471 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3472 | while (!queue.empty()) { |
| 3473 | DispatchEntry* dispatchEntry = queue.front(); |
| 3474 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3475 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3476 | } |
| 3477 | } |
| 3478 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3479 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3480 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3481 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3482 | } |
| 3483 | delete dispatchEntry; |
| 3484 | } |
| 3485 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3486 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3487 | std::scoped_lock _l(mLock); |
| 3488 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3489 | if (connection == nullptr) { |
| 3490 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3491 | connectionToken.get(), events); |
| 3492 | return 0; // remove the callback |
| 3493 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3494 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3495 | bool notify; |
| 3496 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3497 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3498 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3499 | "events=0x%x", |
| 3500 | connection->getInputChannelName().c_str(), events); |
| 3501 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3502 | } |
| 3503 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3504 | nsecs_t currentTime = now(); |
| 3505 | bool gotOne = false; |
| 3506 | status_t status = OK; |
| 3507 | for (;;) { |
| 3508 | Result<InputPublisher::ConsumerResponse> result = |
| 3509 | connection->inputPublisher.receiveConsumerResponse(); |
| 3510 | if (!result.ok()) { |
| 3511 | status = result.error().code(); |
| 3512 | break; |
| 3513 | } |
| 3514 | |
| 3515 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3516 | const InputPublisher::Finished& finish = |
| 3517 | std::get<InputPublisher::Finished>(*result); |
| 3518 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3519 | finish.consumeTime); |
| 3520 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3521 | if (shouldReportMetricsForConnection(*connection)) { |
| 3522 | const InputPublisher::Timeline& timeline = |
| 3523 | std::get<InputPublisher::Timeline>(*result); |
| 3524 | mLatencyTracker |
| 3525 | .trackGraphicsLatency(timeline.inputEventId, |
| 3526 | connection->inputChannel->getConnectionToken(), |
| 3527 | std::move(timeline.graphicsTimeline)); |
| 3528 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3529 | } |
| 3530 | gotOne = true; |
| 3531 | } |
| 3532 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3533 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3534 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3535 | return 1; |
| 3536 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3537 | } |
| 3538 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3539 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3540 | if (notify) { |
| 3541 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3542 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3543 | status); |
| 3544 | } |
| 3545 | } else { |
| 3546 | // Monitor channels are never explicitly unregistered. |
| 3547 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3548 | const bool stillHaveWindowHandle = |
| 3549 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3550 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3551 | if (notify) { |
| 3552 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3553 | connection->getInputChannelName().c_str(), events); |
| 3554 | } |
| 3555 | } |
| 3556 | |
| 3557 | // Remove the channel. |
| 3558 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3559 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3560 | } |
| 3561 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3562 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3563 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3564 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3565 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3566 | } |
| 3567 | } |
| 3568 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3569 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3570 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3571 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3572 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3573 | } |
| 3574 | |
| 3575 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3576 | const CancelationOptions& options, |
| 3577 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3578 | for (const auto& it : monitorsByDisplay) { |
| 3579 | const std::vector<Monitor>& monitors = it.second; |
| 3580 | for (const Monitor& monitor : monitors) { |
| 3581 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3582 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3583 | } |
| 3584 | } |
| 3585 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3586 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3587 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3588 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3589 | if (connection == nullptr) { |
| 3590 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3591 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3592 | |
| 3593 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3597 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3598 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3599 | return; |
| 3600 | } |
| 3601 | |
| 3602 | nsecs_t currentTime = now(); |
| 3603 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3604 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3605 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3606 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3607 | if (cancelationEvents.empty()) { |
| 3608 | return; |
| 3609 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3610 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3611 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3612 | "with reality: %s, mode=%d.", |
| 3613 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3614 | options.mode); |
| 3615 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3616 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3617 | std::string reason = std::string("reason=").append(options.reason); |
| 3618 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3619 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3620 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3621 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3622 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3623 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3624 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3625 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3626 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3627 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3628 | } |
| 3629 | target.inputChannel = connection->inputChannel; |
| 3630 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3631 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3632 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3633 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3634 | switch (cancelationEventEntry->type) { |
| 3635 | case EventEntry::Type::KEY: { |
| 3636 | logOutboundKeyDetails("cancel - ", |
| 3637 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3638 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3639 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3640 | case EventEntry::Type::MOTION: { |
| 3641 | logOutboundMotionDetails("cancel - ", |
| 3642 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3643 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3644 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3645 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3646 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3647 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3648 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3649 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3650 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3651 | break; |
| 3652 | } |
| 3653 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3654 | case EventEntry::Type::DEVICE_RESET: |
| 3655 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3656 | 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] | 3657 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3658 | break; |
| 3659 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3660 | } |
| 3661 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3662 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3663 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3664 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3665 | |
| 3666 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3667 | } |
| 3668 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3669 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3670 | const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3671 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3672 | return; |
| 3673 | } |
| 3674 | |
| 3675 | nsecs_t currentTime = now(); |
| 3676 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3677 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3678 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3679 | |
| 3680 | if (downEvents.empty()) { |
| 3681 | return; |
| 3682 | } |
| 3683 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3684 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3685 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3686 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3687 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3688 | |
| 3689 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3690 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3691 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3692 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3693 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3694 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3695 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3696 | } |
| 3697 | target.inputChannel = connection->inputChannel; |
| 3698 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3699 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3700 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3701 | switch (downEventEntry->type) { |
| 3702 | case EventEntry::Type::MOTION: { |
| 3703 | logOutboundMotionDetails("down - ", |
| 3704 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3705 | break; |
| 3706 | } |
| 3707 | |
| 3708 | case EventEntry::Type::KEY: |
| 3709 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3710 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3711 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3712 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3713 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3714 | case EventEntry::Type::SENSOR: |
| 3715 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3716 | 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] | 3717 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3718 | break; |
| 3719 | } |
| 3720 | } |
| 3721 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3722 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3723 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | startDispatchCycleLocked(currentTime, connection); |
| 3727 | } |
| 3728 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3729 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3730 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3731 | ALOG_ASSERT(pointerIds.value != 0); |
| 3732 | |
| 3733 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3734 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3735 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3736 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3737 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3738 | uint32_t splitPointerCount = 0; |
| 3739 | |
| 3740 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3741 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3742 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3743 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3744 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3745 | if (pointerIds.hasBit(pointerId)) { |
| 3746 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3747 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3748 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3749 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3750 | splitPointerCount += 1; |
| 3751 | } |
| 3752 | } |
| 3753 | |
| 3754 | if (splitPointerCount != pointerIds.count()) { |
| 3755 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3756 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3757 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3758 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3759 | // in this way. |
| 3760 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3761 | "we expected there to be %d pointers. This probably means we received " |
| 3762 | "a broken sequence of pointer ids from the input device.", |
| 3763 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3764 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3765 | } |
| 3766 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3767 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3768 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3769 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3770 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3771 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3772 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3773 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3774 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3775 | if (pointerIds.hasBit(pointerId)) { |
| 3776 | if (pointerIds.count() == 1) { |
| 3777 | // The first/last pointer went down/up. |
| 3778 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3779 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3780 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3781 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3782 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3783 | } else { |
| 3784 | // A secondary pointer went down/up. |
| 3785 | uint32_t splitPointerIndex = 0; |
| 3786 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3787 | splitPointerIndex += 1; |
| 3788 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3789 | action = maskedAction | |
| 3790 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3791 | } |
| 3792 | } else { |
| 3793 | // An unrelated pointer changed. |
| 3794 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3795 | } |
| 3796 | } |
| 3797 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3798 | int32_t newId = mIdGenerator.nextId(); |
| 3799 | if (ATRACE_ENABLED()) { |
| 3800 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3801 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3802 | originalMotionEntry.id, newId); |
| 3803 | ATRACE_NAME(message.c_str()); |
| 3804 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3805 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3806 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3807 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3808 | originalMotionEntry.displayId, |
| 3809 | originalMotionEntry.policyFlags, action, |
| 3810 | originalMotionEntry.actionButton, |
| 3811 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3812 | originalMotionEntry.buttonState, |
| 3813 | originalMotionEntry.classification, |
| 3814 | originalMotionEntry.edgeFlags, |
| 3815 | originalMotionEntry.xPrecision, |
| 3816 | originalMotionEntry.yPrecision, |
| 3817 | originalMotionEntry.xCursorPosition, |
| 3818 | originalMotionEntry.yCursorPosition, |
| 3819 | originalMotionEntry.downTime, splitPointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3820 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3821 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3822 | if (originalMotionEntry.injectionState) { |
| 3823 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3824 | splitMotionEntry->injectionState->refCount += 1; |
| 3825 | } |
| 3826 | |
| 3827 | return splitMotionEntry; |
| 3828 | } |
| 3829 | |
| 3830 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3831 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3832 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3833 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3834 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3835 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3836 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3837 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3838 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3839 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3840 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3841 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3842 | } // release lock |
| 3843 | |
| 3844 | if (needWake) { |
| 3845 | mLooper->wake(); |
| 3846 | } |
| 3847 | } |
| 3848 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3849 | /** |
| 3850 | * If one of the meta shortcuts is detected, process them here: |
| 3851 | * Meta + Backspace -> generate BACK |
| 3852 | * Meta + Enter -> generate HOME |
| 3853 | * This will potentially overwrite keyCode and metaState. |
| 3854 | */ |
| 3855 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3856 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3857 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3858 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3859 | if (keyCode == AKEYCODE_DEL) { |
| 3860 | newKeyCode = AKEYCODE_BACK; |
| 3861 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3862 | newKeyCode = AKEYCODE_HOME; |
| 3863 | } |
| 3864 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3865 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3866 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3867 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3868 | keyCode = newKeyCode; |
| 3869 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3870 | } |
| 3871 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3872 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3873 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3874 | // 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] | 3875 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3876 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3877 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3878 | if (replacementIt != mReplacedKeys.end()) { |
| 3879 | keyCode = replacementIt->second; |
| 3880 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3881 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3882 | } |
| 3883 | } |
| 3884 | } |
| 3885 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3886 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3887 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3888 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3889 | "policyFlags=0x%x, action=0x%x, " |
| 3890 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3891 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3892 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3893 | args->downTime); |
| 3894 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3895 | if (!validateKeyEvent(args->action)) { |
| 3896 | return; |
| 3897 | } |
| 3898 | |
| 3899 | uint32_t policyFlags = args->policyFlags; |
| 3900 | int32_t flags = args->flags; |
| 3901 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3902 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3903 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3904 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3905 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3906 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3907 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3908 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3909 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3910 | metaState |= AMETA_FUNCTION_ON; |
| 3911 | } |
| 3912 | |
| 3913 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3914 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3915 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3916 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3917 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3918 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3919 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3920 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3921 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3922 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3923 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3924 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3925 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3926 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3927 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3928 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3929 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3930 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3931 | { // acquire lock |
| 3932 | mLock.lock(); |
| 3933 | |
| 3934 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3935 | mLock.unlock(); |
| 3936 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3937 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3938 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3939 | return; // event was consumed by the filter |
| 3940 | } |
| 3941 | |
| 3942 | mLock.lock(); |
| 3943 | } |
| 3944 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3945 | std::unique_ptr<KeyEntry> newEntry = |
| 3946 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3947 | args->displayId, policyFlags, args->action, flags, |
| 3948 | keyCode, args->scanCode, metaState, repeatCount, |
| 3949 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3950 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3951 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3952 | mLock.unlock(); |
| 3953 | } // release lock |
| 3954 | |
| 3955 | if (needWake) { |
| 3956 | mLooper->wake(); |
| 3957 | } |
| 3958 | } |
| 3959 | |
| 3960 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3961 | return mInputFilterEnabled; |
| 3962 | } |
| 3963 | |
| 3964 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3965 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3966 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3967 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 3968 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3969 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 3970 | "yCursorPosition=%f, downTime=%" PRId64, |
| 3971 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3972 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3973 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3974 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 3975 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3976 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 3977 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3978 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3979 | "orientation=%f", |
| 3980 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3981 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3982 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3983 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3984 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3985 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3986 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3987 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3988 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3989 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 3990 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3991 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3992 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3993 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3994 | return; |
| 3995 | } |
| 3996 | |
| 3997 | uint32_t policyFlags = args->policyFlags; |
| 3998 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3999 | |
| 4000 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4001 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4002 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4003 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4004 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4005 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4006 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4007 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4008 | { // acquire lock |
| 4009 | mLock.lock(); |
| 4010 | |
| 4011 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4012 | ui::Transform displayTransform; |
| 4013 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4014 | displayTransform = it->second.transform; |
| 4015 | } |
| 4016 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4017 | mLock.unlock(); |
| 4018 | |
| 4019 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4020 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4021 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4022 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4023 | displayTransform, args->xPrecision, args->yPrecision, |
| 4024 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4025 | args->downTime, args->eventTime, args->pointerCount, |
| 4026 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4027 | |
| 4028 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4029 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4030 | return; // event was consumed by the filter |
| 4031 | } |
| 4032 | |
| 4033 | mLock.lock(); |
| 4034 | } |
| 4035 | |
| 4036 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4037 | std::unique_ptr<MotionEntry> newEntry = |
| 4038 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4039 | args->source, args->displayId, policyFlags, |
| 4040 | args->action, args->actionButton, args->flags, |
| 4041 | args->metaState, args->buttonState, |
| 4042 | args->classification, args->edgeFlags, |
| 4043 | args->xPrecision, args->yPrecision, |
| 4044 | args->xCursorPosition, args->yCursorPosition, |
| 4045 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4046 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4047 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4048 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4049 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4050 | !mInputFilterEnabled) { |
| 4051 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4052 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4053 | } |
| 4054 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4055 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4056 | mLock.unlock(); |
| 4057 | } // release lock |
| 4058 | |
| 4059 | if (needWake) { |
| 4060 | mLooper->wake(); |
| 4061 | } |
| 4062 | } |
| 4063 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4064 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4065 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4066 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4067 | " sensorType=%s", |
| 4068 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4069 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4070 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4071 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4072 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4073 | { // acquire lock |
| 4074 | mLock.lock(); |
| 4075 | |
| 4076 | // Just enqueue a new sensor event. |
| 4077 | std::unique_ptr<SensorEntry> newEntry = |
| 4078 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4079 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4080 | args->sensorType, args->accuracy, |
| 4081 | args->accuracyChanged, args->values); |
| 4082 | |
| 4083 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4084 | mLock.unlock(); |
| 4085 | } // release lock |
| 4086 | |
| 4087 | if (needWake) { |
| 4088 | mLooper->wake(); |
| 4089 | } |
| 4090 | } |
| 4091 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4092 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4093 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4094 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4095 | args->deviceId, args->isOn); |
| 4096 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4097 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4098 | } |
| 4099 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4100 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4101 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4105 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4106 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4107 | "switchMask=0x%08x", |
| 4108 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4109 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4110 | |
| 4111 | uint32_t policyFlags = args->policyFlags; |
| 4112 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4113 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4114 | } |
| 4115 | |
| 4116 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4117 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4118 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4119 | args->deviceId); |
| 4120 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4121 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4122 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4123 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4124 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4125 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4126 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4127 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4128 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4129 | } // release lock |
| 4130 | |
| 4131 | if (needWake) { |
| 4132 | mLooper->wake(); |
| 4133 | } |
| 4134 | } |
| 4135 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4136 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4137 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4138 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4139 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4140 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4141 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4142 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4143 | { // acquire lock |
| 4144 | std::scoped_lock _l(mLock); |
| 4145 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4146 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4147 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4148 | } // release lock |
| 4149 | |
| 4150 | if (needWake) { |
| 4151 | mLooper->wake(); |
| 4152 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4153 | } |
| 4154 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4155 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4156 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4157 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4158 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4159 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 4160 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4161 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
| 4162 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4163 | 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] | 4164 | |
| 4165 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4166 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4167 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4168 | } |
| 4169 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4170 | // 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] | 4171 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4172 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4173 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4174 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4175 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4176 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4177 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4178 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4181 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4182 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4183 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4184 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4185 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4186 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4187 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4188 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4189 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4190 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4191 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4192 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4193 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4194 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4195 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4196 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4197 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4198 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4199 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4200 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4201 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4202 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4203 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4204 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4205 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4206 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4207 | |
| 4208 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4209 | android::base::Timer t; |
| 4210 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4211 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4212 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4213 | std::to_string(t.duration().count()).c_str()); |
| 4214 | } |
| 4215 | } |
| 4216 | |
| 4217 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4218 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4219 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4220 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4221 | incomingKey.getDisplayId(), policyFlags, action, |
| 4222 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4223 | incomingKey.getRepeatCount(), |
| 4224 | incomingKey.getDownTime()); |
| 4225 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4226 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4227 | } |
| 4228 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4229 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4230 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4231 | const int32_t action = motionEvent.getAction(); |
| 4232 | const bool isPointerEvent = |
| 4233 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4234 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4235 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4236 | ? ADISPLAY_ID_DEFAULT |
| 4237 | : event->getDisplayId(); |
| 4238 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4239 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4240 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4241 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4242 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4243 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4247 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4248 | android::base::Timer t; |
| 4249 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4250 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4251 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4252 | std::to_string(t.duration().count()).c_str()); |
| 4253 | } |
| 4254 | } |
| 4255 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4256 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4257 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4258 | } |
| 4259 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4260 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4261 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4262 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4263 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4264 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4265 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4266 | displayId, policyFlags, action, actionButton, |
| 4267 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4268 | motionEvent.getButtonState(), |
| 4269 | motionEvent.getClassification(), |
| 4270 | motionEvent.getEdgeFlags(), |
| 4271 | motionEvent.getXPrecision(), |
| 4272 | motionEvent.getYPrecision(), |
| 4273 | motionEvent.getRawXCursorPosition(), |
| 4274 | motionEvent.getRawYCursorPosition(), |
| 4275 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4276 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4277 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4278 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4279 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4280 | sampleEventTimes += 1; |
| 4281 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4282 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4283 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4284 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4285 | displayId, policyFlags, action, actionButton, |
| 4286 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4287 | motionEvent.getButtonState(), |
| 4288 | motionEvent.getClassification(), |
| 4289 | motionEvent.getEdgeFlags(), |
| 4290 | motionEvent.getXPrecision(), |
| 4291 | motionEvent.getYPrecision(), |
| 4292 | motionEvent.getRawXCursorPosition(), |
| 4293 | motionEvent.getRawYCursorPosition(), |
| 4294 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4295 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4296 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4297 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4298 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4299 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4300 | } |
| 4301 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4302 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4303 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4304 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4305 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4306 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4310 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4311 | injectionState->injectionIsAsync = true; |
| 4312 | } |
| 4313 | |
| 4314 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4315 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4316 | |
| 4317 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4318 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4319 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4320 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | mLock.unlock(); |
| 4324 | |
| 4325 | if (needWake) { |
| 4326 | mLooper->wake(); |
| 4327 | } |
| 4328 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4329 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4330 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4331 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4332 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4333 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4334 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4335 | } else { |
| 4336 | for (;;) { |
| 4337 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4338 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4339 | break; |
| 4340 | } |
| 4341 | |
| 4342 | nsecs_t remainingTimeout = endTime - now(); |
| 4343 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4344 | if (DEBUG_INJECTION) { |
| 4345 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4346 | "to become available."); |
| 4347 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4348 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4349 | break; |
| 4350 | } |
| 4351 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4352 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4353 | } |
| 4354 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4355 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4356 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4357 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4358 | if (DEBUG_INJECTION) { |
| 4359 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4360 | injectionState->pendingForegroundDispatches); |
| 4361 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4362 | nsecs_t remainingTimeout = endTime - now(); |
| 4363 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4364 | if (DEBUG_INJECTION) { |
| 4365 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4366 | "dispatches to finish."); |
| 4367 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4368 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4369 | break; |
| 4370 | } |
| 4371 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4372 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4373 | } |
| 4374 | } |
| 4375 | } |
| 4376 | |
| 4377 | injectionState->release(); |
| 4378 | } // release lock |
| 4379 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4380 | if (DEBUG_INJECTION) { |
| 4381 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
| 4382 | injectionResult, injectorPid, injectorUid); |
| 4383 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4384 | |
| 4385 | return injectionResult; |
| 4386 | } |
| 4387 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4388 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4389 | std::array<uint8_t, 32> calculatedHmac; |
| 4390 | std::unique_ptr<VerifiedInputEvent> result; |
| 4391 | switch (event.getType()) { |
| 4392 | case AINPUT_EVENT_TYPE_KEY: { |
| 4393 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4394 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4395 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4396 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4397 | break; |
| 4398 | } |
| 4399 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4400 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4401 | VerifiedMotionEvent verifiedMotionEvent = |
| 4402 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4403 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4404 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4405 | break; |
| 4406 | } |
| 4407 | default: { |
| 4408 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4409 | return nullptr; |
| 4410 | } |
| 4411 | } |
| 4412 | if (calculatedHmac == INVALID_HMAC) { |
| 4413 | return nullptr; |
| 4414 | } |
| 4415 | if (calculatedHmac != event.getHmac()) { |
| 4416 | return nullptr; |
| 4417 | } |
| 4418 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4419 | } |
| 4420 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4421 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4422 | return injectorUid == 0 || |
| 4423 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4424 | } |
| 4425 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4426 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4427 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4428 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4429 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4430 | if (DEBUG_INJECTION) { |
| 4431 | ALOGD("Setting input event injection result to %d. " |
| 4432 | "injectorPid=%d, injectorUid=%d", |
| 4433 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
| 4434 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4435 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4436 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4437 | // Log the outcome since the injector did not wait for the injection result. |
| 4438 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4439 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4440 | ALOGV("Asynchronous input event injection succeeded."); |
| 4441 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4442 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4443 | ALOGW("Asynchronous input event injection failed."); |
| 4444 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4445 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4446 | ALOGW("Asynchronous input event injection permission denied."); |
| 4447 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4448 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4449 | ALOGW("Asynchronous input event injection timed out."); |
| 4450 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4451 | case InputEventInjectionResult::PENDING: |
| 4452 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4453 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4454 | } |
| 4455 | } |
| 4456 | |
| 4457 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4458 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4459 | } |
| 4460 | } |
| 4461 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4462 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4463 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4464 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4465 | // display space, so we need to transform the injected events accordingly. |
| 4466 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4467 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4468 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4469 | |
| 4470 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 4471 | PointerCoords& pc = entry.pointerCoords[i]; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4472 | // Make a copy of the injected coords. We cannot change them in place because some of them |
| 4473 | // are interdependent (for example, X coordinate might depend on the Y coordinate). |
| 4474 | PointerCoords injectedCoords = entry.pointerCoords[i]; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4475 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4476 | BitSet64 bits(injectedCoords.bits); |
| 4477 | while (!bits.isEmpty()) { |
| 4478 | const auto axis = static_cast<int32_t>(bits.clearFirstMarkedBit()); |
| 4479 | const float value = |
| 4480 | MotionEvent::calculateTransformedAxisValue(axis, entry.source, |
| 4481 | transformToDisplay, injectedCoords); |
| 4482 | pc.setAxisValue(axis, value); |
| 4483 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4484 | } |
| 4485 | } |
| 4486 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4487 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4488 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4489 | if (injectionState) { |
| 4490 | injectionState->pendingForegroundDispatches += 1; |
| 4491 | } |
| 4492 | } |
| 4493 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4494 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4495 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4496 | if (injectionState) { |
| 4497 | injectionState->pendingForegroundDispatches -= 1; |
| 4498 | |
| 4499 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4500 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4501 | } |
| 4502 | } |
| 4503 | } |
| 4504 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4505 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4506 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4507 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4508 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4509 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4510 | } |
| 4511 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4512 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4513 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4514 | if (windowHandleToken == nullptr) { |
| 4515 | return nullptr; |
| 4516 | } |
| 4517 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4518 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4519 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4520 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4521 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4522 | return windowHandle; |
| 4523 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4524 | } |
| 4525 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4526 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4527 | } |
| 4528 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4529 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4530 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4531 | if (windowHandleToken == nullptr) { |
| 4532 | return nullptr; |
| 4533 | } |
| 4534 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4535 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4536 | if (windowHandle->getToken() == windowHandleToken) { |
| 4537 | return windowHandle; |
| 4538 | } |
| 4539 | } |
| 4540 | return nullptr; |
| 4541 | } |
| 4542 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4543 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4544 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4545 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4546 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4547 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4548 | if (handle->getId() == windowHandle->getId() && |
| 4549 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4550 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4551 | ALOGE("Found window %s in display %" PRId32 |
| 4552 | ", but it should belong to display %" PRId32, |
| 4553 | windowHandle->getName().c_str(), it.first, |
| 4554 | windowHandle->getInfo()->displayId); |
| 4555 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4556 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4557 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4558 | } |
| 4559 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4560 | return nullptr; |
| 4561 | } |
| 4562 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4563 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4564 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4565 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4566 | } |
| 4567 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4568 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4569 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4570 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4571 | windowHandle.getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4572 | if (connection != nullptr && noInputChannel) { |
| 4573 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4574 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4575 | return false; |
| 4576 | } |
| 4577 | |
| 4578 | if (connection == nullptr) { |
| 4579 | if (!noInputChannel) { |
| 4580 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4581 | } |
| 4582 | return false; |
| 4583 | } |
| 4584 | if (!connection->responsive) { |
| 4585 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4586 | return false; |
| 4587 | } |
| 4588 | return true; |
| 4589 | } |
| 4590 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4591 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4592 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4593 | auto connectionIt = mConnectionsByToken.find(token); |
| 4594 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4595 | return nullptr; |
| 4596 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4597 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4598 | } |
| 4599 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4600 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4601 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4602 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4603 | // Remove all handles on a display if there are no windows left. |
| 4604 | mWindowHandlesByDisplay.erase(displayId); |
| 4605 | return; |
| 4606 | } |
| 4607 | |
| 4608 | // Since we compare the pointer of input window handles across window updates, we need |
| 4609 | // 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] | 4610 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4611 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4612 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4613 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4614 | } |
| 4615 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4616 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4617 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4618 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4619 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4620 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4621 | info->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4622 | const bool canReceiveInput = !info->flags.test(WindowInfo::Flag::NOT_TOUCHABLE) || |
| 4623 | !info->flags.test(WindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4624 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4625 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4626 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4627 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4628 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | if (info->displayId != displayId) { |
| 4632 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4633 | handle->getName().c_str(), displayId, info->displayId); |
| 4634 | continue; |
| 4635 | } |
| 4636 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4637 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4638 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4639 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4640 | oldHandle->updateFrom(handle); |
| 4641 | newHandles.push_back(oldHandle); |
| 4642 | } else { |
| 4643 | newHandles.push_back(handle); |
| 4644 | } |
| 4645 | } |
| 4646 | |
| 4647 | // Insert or replace |
| 4648 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4649 | } |
| 4650 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4651 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4652 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4653 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4654 | { // acquire lock |
| 4655 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4656 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4657 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4658 | } |
| 4659 | } |
| 4660 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4661 | mLooper->wake(); |
| 4662 | } |
| 4663 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4664 | /** |
| 4665 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4666 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4667 | * If set an empty list, remove all handles from the specific display. |
| 4668 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4669 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4670 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4671 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4672 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4673 | if (DEBUG_FOCUS) { |
| 4674 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4675 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4676 | windowList += iwh->getName() + " "; |
| 4677 | } |
| 4678 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4679 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4680 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4681 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4682 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4683 | const WindowInfo& info = *window->getInfo(); |
| 4684 | |
| 4685 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 4686 | const bool noInputWindow = info.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4687 | if (noInputWindow && window->getToken() != nullptr) { |
| 4688 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4689 | window->getName().c_str()); |
| 4690 | window->releaseChannel(); |
| 4691 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4692 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4693 | // Ensure all spy windows are trusted overlays |
| 4694 | LOG_ALWAYS_FATAL_IF(info.isSpy() && !info.trustedOverlay, |
| 4695 | "%s has feature SPY, but is not a trusted overlay.", |
| 4696 | window->getName().c_str()); |
| 4697 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4698 | // Ensure all stylus interceptors are trusted overlays |
| 4699 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && !info.trustedOverlay, |
| 4700 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4701 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4702 | } |
| 4703 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4704 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4705 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4706 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4707 | // Save the old windows' orientation by ID before it gets updated. |
| 4708 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4709 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4710 | oldWindowOrientations.emplace(handle->getId(), |
| 4711 | handle->getInfo()->transform.getOrientation()); |
| 4712 | } |
| 4713 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4714 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4715 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4716 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4717 | if (mLastHoverWindowHandle && |
| 4718 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4719 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4720 | mLastHoverWindowHandle = nullptr; |
| 4721 | } |
| 4722 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4723 | std::optional<FocusResolver::FocusChanges> changes = |
| 4724 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4725 | if (changes) { |
| 4726 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4727 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4728 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4729 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4730 | mTouchStatesByDisplay.find(displayId); |
| 4731 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4732 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4733 | for (size_t i = 0; i < state.windows.size();) { |
| 4734 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4735 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4736 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4737 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4738 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4739 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4740 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4741 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4742 | if (touchedInputChannel != nullptr) { |
| 4743 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4744 | "touched window was removed"); |
| 4745 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4746 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4747 | // well. |
| 4748 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
| 4749 | touchedWindow.windowHandle->getInfo()->hasWallpaper) { |
| 4750 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4751 | if (wallpaper != nullptr) { |
| 4752 | sp<Connection> wallpaperConnection = |
| 4753 | getConnectionLocked(wallpaper->getToken()); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4754 | if (wallpaperConnection != nullptr) { |
| 4755 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4756 | options); |
| 4757 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4758 | } |
| 4759 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4760 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4761 | state.windows.erase(state.windows.begin() + i); |
| 4762 | } else { |
| 4763 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4764 | } |
| 4765 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4766 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4767 | // 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] | 4768 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4769 | if (mDragState && |
| 4770 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4771 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4772 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4773 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4774 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4775 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4776 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4777 | // pointer events if necessary. |
| 4778 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4779 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4780 | if (newWindowHandle != nullptr && |
| 4781 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4782 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4783 | std::shared_ptr<InputChannel> inputChannel = |
| 4784 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4785 | if (inputChannel != nullptr) { |
| 4786 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4787 | "touched window's orientation changed"); |
| 4788 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4789 | } |
| 4790 | } |
| 4791 | } |
| 4792 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4793 | // Release information for windows that are no longer present. |
| 4794 | // This ensures that unused input channels are released promptly. |
| 4795 | // Otherwise, they might stick around until the window handle is destroyed |
| 4796 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4797 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4798 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4799 | if (DEBUG_FOCUS) { |
| 4800 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4801 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4802 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4803 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4804 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4808 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4809 | if (DEBUG_FOCUS) { |
| 4810 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4811 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4812 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4813 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4814 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4815 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4816 | } // release lock |
| 4817 | |
| 4818 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4819 | mLooper->wake(); |
| 4820 | } |
| 4821 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4822 | void InputDispatcher::setFocusedApplicationLocked( |
| 4823 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4824 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4825 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4826 | |
| 4827 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4828 | return; // This application is already focused. No need to wake up or change anything. |
| 4829 | } |
| 4830 | |
| 4831 | // Set the new application handle. |
| 4832 | if (inputApplicationHandle != nullptr) { |
| 4833 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4834 | } else { |
| 4835 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4836 | } |
| 4837 | |
| 4838 | // No matter what the old focused application was, stop waiting on it because it is |
| 4839 | // no longer focused. |
| 4840 | resetNoFocusedWindowTimeoutLocked(); |
| 4841 | } |
| 4842 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4843 | /** |
| 4844 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4845 | * the display not specified. |
| 4846 | * |
| 4847 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4848 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4849 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4850 | * display. The display-specified events won't be affected. |
| 4851 | */ |
| 4852 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4853 | if (DEBUG_FOCUS) { |
| 4854 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4855 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4856 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4857 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4858 | |
| 4859 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4860 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4861 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4862 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4863 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4864 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4865 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4866 | CancelationOptions |
| 4867 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4868 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4869 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4870 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4871 | } |
| 4872 | } |
| 4873 | mFocusedDisplayId = displayId; |
| 4874 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4875 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4876 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4877 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4878 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4879 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4880 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4881 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4882 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4883 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4884 | } |
| 4885 | } |
| 4886 | } |
| 4887 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4888 | if (DEBUG_FOCUS) { |
| 4889 | logDispatchStateLocked(); |
| 4890 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4891 | } // release lock |
| 4892 | |
| 4893 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4894 | mLooper->wake(); |
| 4895 | } |
| 4896 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4897 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4898 | if (DEBUG_FOCUS) { |
| 4899 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4900 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4901 | |
| 4902 | bool changed; |
| 4903 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4904 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4905 | |
| 4906 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4907 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4908 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | if (mDispatchEnabled && !enabled) { |
| 4912 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4913 | } |
| 4914 | |
| 4915 | mDispatchEnabled = enabled; |
| 4916 | mDispatchFrozen = frozen; |
| 4917 | changed = true; |
| 4918 | } else { |
| 4919 | changed = false; |
| 4920 | } |
| 4921 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4922 | if (DEBUG_FOCUS) { |
| 4923 | logDispatchStateLocked(); |
| 4924 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4925 | } // release lock |
| 4926 | |
| 4927 | if (changed) { |
| 4928 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4929 | mLooper->wake(); |
| 4930 | } |
| 4931 | } |
| 4932 | |
| 4933 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4934 | if (DEBUG_FOCUS) { |
| 4935 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4936 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4937 | |
| 4938 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4939 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4940 | |
| 4941 | if (mInputFilterEnabled == enabled) { |
| 4942 | return; |
| 4943 | } |
| 4944 | |
| 4945 | mInputFilterEnabled = enabled; |
| 4946 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4947 | } // release lock |
| 4948 | |
| 4949 | // Wake up poll loop since there might be work to do to drop everything. |
| 4950 | mLooper->wake(); |
| 4951 | } |
| 4952 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame^] | 4953 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, |
| 4954 | bool hasPermission) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4955 | bool needWake = false; |
| 4956 | { |
| 4957 | std::scoped_lock lock(mLock); |
| 4958 | if (mInTouchMode == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame^] | 4959 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4960 | } |
| 4961 | if (DEBUG_TOUCH_MODE) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame^] | 4962 | ALOGD("Request to change touch mode from %s to %s (calling pid=%d, uid=%d, " |
| 4963 | "hasPermission=%s)", |
| 4964 | toString(mInTouchMode), toString(inTouchMode), pid, uid, toString(hasPermission)); |
| 4965 | } |
| 4966 | if (!hasPermission) { |
| 4967 | const sp<IBinder> focusedToken = |
| 4968 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 4969 | |
| 4970 | // TODO(b/198487159): if no window is currently focused, then we need to check the last |
| 4971 | // interacted window (within 1 second timeout). We should allow touch mode change |
| 4972 | // if the last interacted window owner's pid/uid match the calling ones. |
| 4973 | if (focusedToken == nullptr) { |
| 4974 | return false; |
| 4975 | } |
| 4976 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 4977 | if (windowHandle == nullptr) { |
| 4978 | return false; |
| 4979 | } |
| 4980 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 4981 | if (pid != windowInfo->ownerPid || uid != windowInfo->ownerUid) { |
| 4982 | return false; |
| 4983 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | // TODO(b/198499018): Store touch mode per display. |
| 4987 | mInTouchMode = inTouchMode; |
| 4988 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4989 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 4990 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4991 | } // release lock |
| 4992 | |
| 4993 | if (needWake) { |
| 4994 | mLooper->wake(); |
| 4995 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame^] | 4996 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4997 | } |
| 4998 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4999 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5000 | if (opacity < 0 || opacity > 1) { |
| 5001 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5002 | return; |
| 5003 | } |
| 5004 | |
| 5005 | std::scoped_lock lock(mLock); |
| 5006 | mMaximumObscuringOpacityForTouch = opacity; |
| 5007 | } |
| 5008 | |
| 5009 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 5010 | std::scoped_lock lock(mLock); |
| 5011 | mBlockUntrustedTouchesMode = mode; |
| 5012 | } |
| 5013 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5014 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 5015 | const sp<IBinder>& token) { |
| 5016 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5017 | for (TouchedWindow& w : state.windows) { |
| 5018 | if (w.windowHandle->getToken() == token) { |
| 5019 | return std::make_pair(&state, &w); |
| 5020 | } |
| 5021 | } |
| 5022 | } |
| 5023 | return std::make_pair(nullptr, nullptr); |
| 5024 | } |
| 5025 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5026 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5027 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5028 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5029 | if (DEBUG_FOCUS) { |
| 5030 | ALOGD("Trivial transfer to same window."); |
| 5031 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5032 | return true; |
| 5033 | } |
| 5034 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5035 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5036 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5037 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5038 | // Find the target touch state and touched window by fromToken. |
| 5039 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 5040 | if (state == nullptr || touchedWindow == nullptr) { |
| 5041 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5042 | return false; |
| 5043 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5044 | |
| 5045 | const int32_t displayId = state->displayId; |
| 5046 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5047 | if (toWindowHandle == nullptr) { |
| 5048 | ALOGW("Cannot transfer focus because to window not found."); |
| 5049 | return false; |
| 5050 | } |
| 5051 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5052 | if (DEBUG_FOCUS) { |
| 5053 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5054 | touchedWindow->windowHandle->getName().c_str(), |
| 5055 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5056 | } |
| 5057 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5058 | // Erase old window. |
| 5059 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 5060 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 5061 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5062 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5063 | // Add new window. |
| 5064 | int32_t newTargetFlags = oldTargetFlags & |
| 5065 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 5066 | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5067 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5068 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5069 | // Store the dragging window. |
| 5070 | if (isDragDrop) { |
| 5071 | mDragState = std::make_unique<DragState>(toWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5072 | } |
| 5073 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5074 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5075 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5076 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5077 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5078 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5079 | CancelationOptions |
| 5080 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5081 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5082 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5083 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5084 | } |
| 5085 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5086 | if (DEBUG_FOCUS) { |
| 5087 | logDispatchStateLocked(); |
| 5088 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5089 | } // release lock |
| 5090 | |
| 5091 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5092 | mLooper->wake(); |
| 5093 | return true; |
| 5094 | } |
| 5095 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5096 | // Binder call |
| 5097 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 5098 | sp<IBinder> fromToken; |
| 5099 | { // acquire lock |
| 5100 | std::scoped_lock _l(mLock); |
| 5101 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5102 | auto it = std::find_if(mTouchStatesByDisplay.begin(), mTouchStatesByDisplay.end(), |
| 5103 | [](const auto& pair) { return pair.second.windows.size() == 1; }); |
| 5104 | if (it == mTouchStatesByDisplay.end()) { |
| 5105 | ALOGW("Cannot transfer touch state because there is no exact window being touched"); |
| 5106 | return false; |
| 5107 | } |
| 5108 | const int32_t displayId = it->first; |
| 5109 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5110 | if (toWindowHandle == nullptr) { |
| 5111 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 5112 | return false; |
| 5113 | } |
| 5114 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5115 | TouchState& state = it->second; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5116 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 5117 | fromToken = touchedWindow.windowHandle->getToken(); |
| 5118 | } // release lock |
| 5119 | |
| 5120 | return transferTouchFocus(fromToken, destChannelToken); |
| 5121 | } |
| 5122 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5123 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5124 | if (DEBUG_FOCUS) { |
| 5125 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5126 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5127 | |
| 5128 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5129 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5130 | |
| 5131 | resetKeyRepeatLocked(); |
| 5132 | releasePendingEventLocked(); |
| 5133 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5134 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5135 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5136 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5137 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5138 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5139 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5143 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5144 | dumpDispatchStateLocked(dump); |
| 5145 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5146 | std::istringstream stream(dump); |
| 5147 | std::string line; |
| 5148 | |
| 5149 | while (std::getline(stream, line, '\n')) { |
| 5150 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5151 | } |
| 5152 | } |
| 5153 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5154 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5155 | std::string dump; |
| 5156 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5157 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5158 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5159 | |
| 5160 | std::string windowName = "None"; |
| 5161 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5162 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5163 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5164 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5165 | : "token has capture without window"; |
| 5166 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5167 | 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] | 5168 | |
| 5169 | return dump; |
| 5170 | } |
| 5171 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5172 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5173 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5174 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5175 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5176 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5177 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5178 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5179 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5180 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5181 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5182 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5183 | const std::chrono::duration timeout = |
| 5184 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5185 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5186 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5187 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5188 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5189 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5190 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5191 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5192 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5193 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5194 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5195 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5196 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5197 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5198 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5199 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5200 | 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] | 5201 | state.displayId, toString(state.down), toString(state.split), |
| 5202 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5203 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5204 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5205 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5206 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5207 | dump += StringPrintf(INDENT4 |
| 5208 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5209 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5210 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5211 | } |
| 5212 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5213 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5214 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5215 | } |
| 5216 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5217 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5218 | } |
| 5219 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5220 | if (mDragState) { |
| 5221 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5222 | mDragState->dump(dump, INDENT2); |
| 5223 | } |
| 5224 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5225 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5226 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5227 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5228 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5229 | const auto& displayInfo = it->second; |
| 5230 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5231 | displayInfo.logicalHeight); |
| 5232 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5233 | } else { |
| 5234 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5235 | } |
| 5236 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5237 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5238 | dump += INDENT2 "Windows:\n"; |
| 5239 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5240 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5241 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5242 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5243 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5244 | "paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5245 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5246 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5247 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5248 | "applicationInfo.name=%s, " |
| 5249 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5250 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5251 | i, windowInfo->name.c_str(), windowInfo->id, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5252 | windowInfo->displayId, toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5253 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5254 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5255 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 5256 | windowInfo->flags.string().c_str(), |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 5257 | ftl::enum_string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5258 | windowInfo->frameLeft, windowInfo->frameTop, |
| 5259 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5260 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5261 | windowInfo->applicationInfo.name.c_str(), |
| 5262 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5263 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5264 | dump += StringPrintf(", inputFeatures=%s", |
| 5265 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5266 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5267 | "ms, trustedOverlay=%s, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5268 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5269 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5270 | millis(windowInfo->dispatchingTimeout), |
| 5271 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5272 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5273 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5274 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5275 | } |
| 5276 | } else { |
| 5277 | dump += INDENT2 "Windows: <none>\n"; |
| 5278 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5279 | } |
| 5280 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5281 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5282 | } |
| 5283 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5284 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5285 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5286 | const std::vector<Monitor>& monitors = it.second; |
| 5287 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5288 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5289 | } |
| 5290 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5291 | const std::vector<Monitor>& monitors = it.second; |
| 5292 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5293 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5294 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5295 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5296 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5297 | } |
| 5298 | |
| 5299 | nsecs_t currentTime = now(); |
| 5300 | |
| 5301 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5302 | if (!mRecentQueue.empty()) { |
| 5303 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5304 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5305 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5306 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5307 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5308 | } |
| 5309 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5310 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5311 | } |
| 5312 | |
| 5313 | // Dump event currently being dispatched. |
| 5314 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5315 | dump += INDENT "PendingEvent:\n"; |
| 5316 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5317 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5318 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5319 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5320 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5321 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5322 | } |
| 5323 | |
| 5324 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5325 | if (!mInboundQueue.empty()) { |
| 5326 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5327 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5328 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5329 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5330 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5331 | } |
| 5332 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5333 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5334 | } |
| 5335 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5336 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5337 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5338 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5339 | const KeyReplacement& replacement = pair.first; |
| 5340 | int32_t newKeyCode = pair.second; |
| 5341 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5342 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5343 | } |
| 5344 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5345 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5346 | } |
| 5347 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5348 | if (!mCommandQueue.empty()) { |
| 5349 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5350 | } else { |
| 5351 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5352 | } |
| 5353 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5354 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5355 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5356 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5357 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5358 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5359 | connection->inputChannel->getFd().get(), |
| 5360 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5361 | connection->getWindowName().c_str(), |
| 5362 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5363 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5364 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5365 | if (!connection->outboundQueue.empty()) { |
| 5366 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5367 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5368 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5369 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5370 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5371 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5372 | } |
| 5373 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5374 | if (!connection->waitQueue.empty()) { |
| 5375 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5376 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5377 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5378 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5379 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5380 | } |
| 5381 | } |
| 5382 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5383 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5384 | } |
| 5385 | |
| 5386 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5387 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5388 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5389 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5390 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5391 | } |
| 5392 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5393 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5394 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5395 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5396 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5397 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5398 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5399 | } |
| 5400 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5401 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5402 | const size_t numMonitors = monitors.size(); |
| 5403 | for (size_t i = 0; i < numMonitors; i++) { |
| 5404 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5405 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5406 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5407 | dump += "\n"; |
| 5408 | } |
| 5409 | } |
| 5410 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5411 | class LooperEventCallback : public LooperCallback { |
| 5412 | public: |
| 5413 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5414 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5415 | |
| 5416 | private: |
| 5417 | std::function<int(int events)> mCallback; |
| 5418 | }; |
| 5419 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5420 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5421 | if (DEBUG_CHANNEL_CREATION) { |
| 5422 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5423 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5424 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5425 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5426 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5427 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5428 | |
| 5429 | if (result) { |
| 5430 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5431 | } |
| 5432 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5433 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5434 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5435 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5436 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5437 | sp<Connection> connection = |
| 5438 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5439 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5440 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5441 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5442 | } |
| 5443 | mConnectionsByToken.emplace(token, connection); |
| 5444 | |
| 5445 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5446 | this, std::placeholders::_1, token); |
| 5447 | |
| 5448 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5449 | } // release lock |
| 5450 | |
| 5451 | // Wake the looper because some connections have changed. |
| 5452 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5453 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5454 | } |
| 5455 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5456 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5457 | bool isGestureMonitor, |
| 5458 | const std::string& name, |
| 5459 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5460 | std::shared_ptr<InputChannel> serverChannel; |
| 5461 | std::unique_ptr<InputChannel> clientChannel; |
| 5462 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5463 | if (result) { |
| 5464 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5465 | } |
| 5466 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5467 | { // acquire lock |
| 5468 | std::scoped_lock _l(mLock); |
| 5469 | |
| 5470 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5471 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5472 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5473 | } |
| 5474 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5475 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5476 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5477 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5478 | |
| 5479 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5480 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5481 | } |
| 5482 | mConnectionsByToken.emplace(token, connection); |
| 5483 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5484 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5485 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5486 | auto& monitorsByDisplay = |
| 5487 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5488 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5489 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5490 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5491 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), |
| 5492 | displayId, toString(isGestureMonitor), pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5493 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5494 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5495 | // Wake the looper because some connections have changed. |
| 5496 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5497 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5498 | } |
| 5499 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5500 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5501 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5502 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5503 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5504 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5505 | if (status) { |
| 5506 | return status; |
| 5507 | } |
| 5508 | } // release lock |
| 5509 | |
| 5510 | // Wake the poll loop because removing the connection may have changed the current |
| 5511 | // synchronization state. |
| 5512 | mLooper->wake(); |
| 5513 | return OK; |
| 5514 | } |
| 5515 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5516 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5517 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5518 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5519 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5520 | // 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] | 5521 | return BAD_VALUE; |
| 5522 | } |
| 5523 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5524 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5525 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5526 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5527 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5528 | } |
| 5529 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5530 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5531 | |
| 5532 | nsecs_t currentTime = now(); |
| 5533 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5534 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5535 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5536 | return OK; |
| 5537 | } |
| 5538 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5539 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5540 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5541 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5542 | } |
| 5543 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5544 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5545 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5546 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5547 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5548 | std::vector<Monitor>& monitors = it->second; |
| 5549 | const size_t numMonitors = monitors.size(); |
| 5550 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5551 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5552 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5553 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5554 | monitors.erase(monitors.begin() + i); |
| 5555 | break; |
| 5556 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5557 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5558 | if (monitors.empty()) { |
| 5559 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5560 | } else { |
| 5561 | ++it; |
| 5562 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5563 | } |
| 5564 | } |
| 5565 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5566 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5567 | { // acquire lock |
| 5568 | std::scoped_lock _l(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5569 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5570 | TouchState* statePtr = nullptr; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5571 | std::shared_ptr<InputChannel> requestingChannel; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5572 | int32_t displayId; |
| 5573 | int32_t deviceId; |
| 5574 | const std::optional<int32_t> foundGestureMonitorDisplayId = |
| 5575 | findGestureMonitorDisplayByTokenLocked(token); |
| 5576 | |
| 5577 | // TODO: Optimize this function for pilfering from windows when removing gesture monitors. |
| 5578 | if (foundGestureMonitorDisplayId) { |
| 5579 | // A gesture monitor has requested to pilfer pointers. |
| 5580 | displayId = *foundGestureMonitorDisplayId; |
| 5581 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5582 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5583 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5584 | return BAD_VALUE; |
| 5585 | } |
| 5586 | statePtr = &stateIt->second; |
| 5587 | |
| 5588 | for (const auto& monitor : statePtr->gestureMonitors) { |
| 5589 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 5590 | requestingChannel = monitor.inputChannel; |
| 5591 | deviceId = statePtr->deviceId; |
| 5592 | } |
| 5593 | } |
| 5594 | } else { |
| 5595 | // Check if a window has requested to pilfer pointers. |
| 5596 | for (auto& [curDisplayId, state] : mTouchStatesByDisplay) { |
| 5597 | const sp<WindowInfoHandle>& windowHandle = state.getWindow(token); |
| 5598 | if (windowHandle != nullptr) { |
| 5599 | displayId = curDisplayId; |
| 5600 | requestingChannel = getInputChannelLocked(token); |
| 5601 | deviceId = state.deviceId; |
| 5602 | statePtr = &state; |
| 5603 | break; |
| 5604 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5605 | } |
| 5606 | } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5607 | |
| 5608 | if (requestingChannel == nullptr) { |
| 5609 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5610 | return BAD_VALUE; |
| 5611 | } |
| 5612 | TouchState& state = *statePtr; |
| 5613 | if (!state.down) { |
| 5614 | 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] | 5615 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5616 | return BAD_VALUE; |
| 5617 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5618 | |
| 5619 | // Send cancel events to all the input channels we're stealing from. |
| 5620 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5621 | "input channel stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5622 | options.deviceId = deviceId; |
| 5623 | options.displayId = displayId; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5624 | std::string canceledWindows; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5625 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5626 | std::shared_ptr<InputChannel> channel = |
| 5627 | getInputChannelLocked(window.windowHandle->getToken()); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5628 | if (channel != nullptr && channel->getConnectionToken() != token) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5629 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5630 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5631 | canceledWindows += channel->getName(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5632 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5633 | } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5634 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5635 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5636 | canceledWindows.c_str()); |
| 5637 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5638 | // 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] | 5639 | state.split = false; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 5640 | state.filterWindowsExcept(token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5641 | } |
| 5642 | return OK; |
| 5643 | } |
| 5644 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5645 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5646 | { // acquire lock |
| 5647 | std::scoped_lock _l(mLock); |
| 5648 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5649 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5650 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5651 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5652 | : "token without window"); |
| 5653 | } |
| 5654 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5655 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5656 | if (focusedToken != windowToken) { |
| 5657 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5658 | enabled ? "enable" : "disable"); |
| 5659 | return; |
| 5660 | } |
| 5661 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5662 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5663 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5664 | "window has %s requested pointer capture.", |
| 5665 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5666 | return; |
| 5667 | } |
| 5668 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5669 | if (enabled) { |
| 5670 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5671 | mIneligibleDisplaysForPointerCapture.end(), |
| 5672 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5673 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5674 | return; |
| 5675 | } |
| 5676 | } |
| 5677 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5678 | setPointerCaptureLocked(enabled); |
| 5679 | } // release lock |
| 5680 | |
| 5681 | // Wake the thread to process command entries. |
| 5682 | mLooper->wake(); |
| 5683 | } |
| 5684 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5685 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5686 | { // acquire lock |
| 5687 | std::scoped_lock _l(mLock); |
| 5688 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5689 | if (!isEligible) { |
| 5690 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5691 | } |
| 5692 | } // release lock |
| 5693 | } |
| 5694 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5695 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5696 | const sp<IBinder>& token) { |
| 5697 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5698 | const std::vector<Monitor>& monitors = it.second; |
| 5699 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5700 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5701 | return it.first; |
| 5702 | } |
| 5703 | } |
| 5704 | } |
| 5705 | return std::nullopt; |
| 5706 | } |
| 5707 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5708 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5709 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5710 | if (gesturePid.has_value()) { |
| 5711 | return gesturePid; |
| 5712 | } |
| 5713 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5714 | } |
| 5715 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5716 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5717 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5718 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5719 | } |
| 5720 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5721 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5722 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5723 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5724 | } |
| 5725 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5726 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5727 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5728 | } |
| 5729 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5730 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5731 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5732 | if (connection == nullptr) { |
| 5733 | return "<nullptr>"; |
| 5734 | } |
| 5735 | return connection->getInputChannelName(); |
| 5736 | } |
| 5737 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5738 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5739 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5740 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5741 | } |
| 5742 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5743 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5744 | const sp<Connection>& connection, uint32_t seq, |
| 5745 | bool handled, nsecs_t consumeTime) { |
| 5746 | // Handle post-event policy actions. |
| 5747 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5748 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5749 | return; |
| 5750 | } |
| 5751 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5752 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5753 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5754 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5755 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5756 | } |
| 5757 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5758 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5759 | connection->inputChannel->getConnectionToken(), |
| 5760 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5761 | } |
| 5762 | |
| 5763 | bool restartEvent; |
| 5764 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5765 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5766 | restartEvent = |
| 5767 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5768 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5769 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5770 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5771 | handled); |
| 5772 | } else { |
| 5773 | restartEvent = false; |
| 5774 | } |
| 5775 | |
| 5776 | // Dequeue the event and start the next cycle. |
| 5777 | // Because the lock might have been released, it is possible that the |
| 5778 | // contents of the wait queue to have been drained, so we need to double-check |
| 5779 | // a few things. |
| 5780 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5781 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5782 | dispatchEntry = *dispatchEntryIt; |
| 5783 | connection->waitQueue.erase(dispatchEntryIt); |
| 5784 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5785 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5786 | if (!connection->responsive) { |
| 5787 | connection->responsive = isConnectionResponsive(*connection); |
| 5788 | if (connection->responsive) { |
| 5789 | // The connection was unresponsive, and now it's responsive. |
| 5790 | processConnectionResponsiveLocked(*connection); |
| 5791 | } |
| 5792 | } |
| 5793 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5794 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5795 | connection->outboundQueue.push_front(dispatchEntry); |
| 5796 | traceOutboundQueueLength(*connection); |
| 5797 | } else { |
| 5798 | releaseDispatchEntry(dispatchEntry); |
| 5799 | } |
| 5800 | } |
| 5801 | |
| 5802 | // Start the next dispatch cycle for this connection. |
| 5803 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5804 | } |
| 5805 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5806 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5807 | const sp<IBinder>& newToken) { |
| 5808 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5809 | scoped_unlock unlock(mLock); |
| 5810 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5811 | }; |
| 5812 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5813 | } |
| 5814 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5815 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5816 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5817 | scoped_unlock unlock(mLock); |
| 5818 | mPolicy->notifyDropWindow(token, x, y); |
| 5819 | }; |
| 5820 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5821 | } |
| 5822 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5823 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5824 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5825 | scoped_unlock unlock(mLock); |
| 5826 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5827 | }; |
| 5828 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5829 | } |
| 5830 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5831 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5832 | if (connection == nullptr) { |
| 5833 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5834 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5835 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5836 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5837 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5838 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5839 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5840 | return; |
| 5841 | } |
| 5842 | /** |
| 5843 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5844 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5845 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5846 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5847 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5848 | * most useful. |
| 5849 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5850 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5851 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5852 | std::string reason = |
| 5853 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5854 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5855 | ns2ms(currentWait), |
| 5856 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5857 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5858 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5859 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5860 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5861 | |
| 5862 | // Stop waking up for events on this connection, it is already unresponsive |
| 5863 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5864 | } |
| 5865 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5866 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5867 | std::string reason = |
| 5868 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5869 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5870 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5871 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5872 | scoped_unlock unlock(mLock); |
| 5873 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5874 | }; |
| 5875 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5876 | } |
| 5877 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5878 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5879 | const std::string& reason) { |
| 5880 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5881 | updateLastAnrStateLocked(windowLabel, reason); |
| 5882 | } |
| 5883 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5884 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5885 | const std::string& reason) { |
| 5886 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5887 | updateLastAnrStateLocked(windowLabel, reason); |
| 5888 | } |
| 5889 | |
| 5890 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5891 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5892 | // 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] | 5893 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5894 | struct tm tm; |
| 5895 | localtime_r(&t, &tm); |
| 5896 | char timestr[64]; |
| 5897 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5898 | mLastAnrState.clear(); |
| 5899 | mLastAnrState += INDENT "ANR:\n"; |
| 5900 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5901 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5902 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5903 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5904 | } |
| 5905 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5906 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5907 | KeyEntry& entry) { |
| 5908 | const KeyEvent event = createKeyEvent(entry); |
| 5909 | nsecs_t delay = 0; |
| 5910 | { // release lock |
| 5911 | scoped_unlock unlock(mLock); |
| 5912 | android::base::Timer t; |
| 5913 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5914 | entry.policyFlags); |
| 5915 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5916 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5917 | std::to_string(t.duration().count()).c_str()); |
| 5918 | } |
| 5919 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5920 | |
| 5921 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5922 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5923 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5924 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5925 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5926 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5927 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5928 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5929 | } |
| 5930 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5931 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5932 | auto command = [this, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5933 | scoped_unlock unlock(mLock); |
| 5934 | mPolicy->notifyMonitorUnresponsive(pid, reason); |
| 5935 | }; |
| 5936 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5937 | } |
| 5938 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5939 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5940 | std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5941 | auto command = [this, token, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5942 | scoped_unlock unlock(mLock); |
| 5943 | mPolicy->notifyWindowUnresponsive(token, reason); |
| 5944 | }; |
| 5945 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5946 | } |
| 5947 | |
| 5948 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5949 | auto command = [this, pid]() REQUIRES(mLock) { |
| 5950 | scoped_unlock unlock(mLock); |
| 5951 | mPolicy->notifyMonitorResponsive(pid); |
| 5952 | }; |
| 5953 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5954 | } |
| 5955 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5956 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken) { |
| 5957 | auto command = [this, connectionToken]() REQUIRES(mLock) { |
| 5958 | scoped_unlock unlock(mLock); |
| 5959 | mPolicy->notifyWindowResponsive(connectionToken); |
| 5960 | }; |
| 5961 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5962 | } |
| 5963 | |
| 5964 | /** |
| 5965 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5966 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5967 | * command entry to the command queue. |
| 5968 | */ |
| 5969 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5970 | std::string reason) { |
| 5971 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5972 | if (connection.monitor) { |
| 5973 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5974 | reason.c_str()); |
| 5975 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5976 | if (!pid.has_value()) { |
| 5977 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5978 | connection.inputChannel->getName().c_str()); |
| 5979 | return; |
| 5980 | } |
| 5981 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5982 | return; |
| 5983 | } |
| 5984 | // If not a monitor, must be a window |
| 5985 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5986 | reason.c_str()); |
| 5987 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5988 | } |
| 5989 | |
| 5990 | /** |
| 5991 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5992 | */ |
| 5993 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5994 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5995 | if (connection.monitor) { |
| 5996 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5997 | if (!pid.has_value()) { |
| 5998 | ALOGE("Could not find responsive monitor for connection %s", |
| 5999 | connection.inputChannel->getName().c_str()); |
| 6000 | return; |
| 6001 | } |
| 6002 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 6003 | return; |
| 6004 | } |
| 6005 | // If not a monitor, must be a window |
| 6006 | sendWindowResponsiveCommandLocked(connectionToken); |
| 6007 | } |
| 6008 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6009 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6010 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6011 | KeyEntry& keyEntry, bool handled) { |
| 6012 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6013 | if (!handled) { |
| 6014 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6015 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6016 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6017 | return false; |
| 6018 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6019 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6020 | // Get the fallback key state. |
| 6021 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6022 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6023 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6024 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6025 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6026 | } |
| 6027 | |
| 6028 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6029 | // If the application handles the original key for which we previously |
| 6030 | // generated a fallback or if the window is not a foreground window, |
| 6031 | // then cancel the associated fallback key, if any. |
| 6032 | if (fallbackKeyCode != -1) { |
| 6033 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6034 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6035 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6036 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6037 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6038 | keyEntry.policyFlags); |
| 6039 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6040 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6041 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6042 | |
| 6043 | mLock.unlock(); |
| 6044 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6045 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6046 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6047 | |
| 6048 | mLock.lock(); |
| 6049 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6050 | // Cancel the fallback key. |
| 6051 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6052 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6053 | "application handled the original non-fallback key " |
| 6054 | "or is no longer a foreground target, " |
| 6055 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6056 | options.keyCode = fallbackKeyCode; |
| 6057 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6058 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6059 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6060 | } |
| 6061 | } else { |
| 6062 | // If the application did not handle a non-fallback key, first check |
| 6063 | // that we are in a good state to perform unhandled key event processing |
| 6064 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6065 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6066 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6067 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6068 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6069 | "since this is not an initial down. " |
| 6070 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6071 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6072 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6073 | return false; |
| 6074 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6075 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6076 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6077 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6078 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6079 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6080 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6081 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6082 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6083 | |
| 6084 | mLock.unlock(); |
| 6085 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6086 | bool fallback = |
| 6087 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6088 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6089 | |
| 6090 | mLock.lock(); |
| 6091 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6092 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6093 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6094 | return false; |
| 6095 | } |
| 6096 | |
| 6097 | // Latch the fallback keycode for this key on an initial down. |
| 6098 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6099 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6100 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6101 | fallbackKeyCode = event.getKeyCode(); |
| 6102 | } else { |
| 6103 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6104 | } |
| 6105 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6106 | } |
| 6107 | |
| 6108 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6109 | |
| 6110 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6111 | // We will continue to dispatch the key to the policy but we will no |
| 6112 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6113 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6114 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6115 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6116 | if (fallback) { |
| 6117 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6118 | "as a fallback for %d, but on the DOWN it had requested " |
| 6119 | "to send %d instead. Fallback canceled.", |
| 6120 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6121 | } else { |
| 6122 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6123 | "but on the DOWN it had requested to send %d. " |
| 6124 | "Fallback canceled.", |
| 6125 | originalKeyCode, fallbackKeyCode); |
| 6126 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6127 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6128 | |
| 6129 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6130 | "canceling fallback, policy no longer desires it"); |
| 6131 | options.keyCode = fallbackKeyCode; |
| 6132 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6133 | |
| 6134 | fallback = false; |
| 6135 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6136 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6137 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6138 | } |
| 6139 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6140 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6141 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6142 | { |
| 6143 | std::string msg; |
| 6144 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6145 | connection->inputState.getFallbackKeys(); |
| 6146 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6147 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6148 | } |
| 6149 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6150 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6151 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6152 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6153 | |
| 6154 | if (fallback) { |
| 6155 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6156 | keyEntry.eventTime = event.getEventTime(); |
| 6157 | keyEntry.deviceId = event.getDeviceId(); |
| 6158 | keyEntry.source = event.getSource(); |
| 6159 | keyEntry.displayId = event.getDisplayId(); |
| 6160 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6161 | keyEntry.keyCode = fallbackKeyCode; |
| 6162 | keyEntry.scanCode = event.getScanCode(); |
| 6163 | keyEntry.metaState = event.getMetaState(); |
| 6164 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6165 | keyEntry.downTime = event.getDownTime(); |
| 6166 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6167 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6168 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6169 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6170 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6171 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6172 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6173 | return true; // restart the event |
| 6174 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6175 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6176 | ALOGD("Unhandled key event: No fallback key."); |
| 6177 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6178 | |
| 6179 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6180 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6181 | } |
| 6182 | } |
| 6183 | return false; |
| 6184 | } |
| 6185 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6186 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6187 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6188 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6189 | return false; |
| 6190 | } |
| 6191 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6192 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6193 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6194 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6195 | } |
| 6196 | } |
| 6197 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6198 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6199 | if (ATRACE_ENABLED()) { |
| 6200 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6201 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6202 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6203 | } |
| 6204 | } |
| 6205 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6206 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6207 | if (ATRACE_ENABLED()) { |
| 6208 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6209 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6210 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6211 | } |
| 6212 | } |
| 6213 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6214 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6215 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6216 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6217 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6218 | dumpDispatchStateLocked(dump); |
| 6219 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6220 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6221 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6222 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6223 | } |
| 6224 | } |
| 6225 | |
| 6226 | void InputDispatcher::monitor() { |
| 6227 | // 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] | 6228 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6229 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6230 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6231 | } |
| 6232 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6233 | /** |
| 6234 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6235 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6236 | * this method can be safely called from any thread, as long as you've ensured that |
| 6237 | * the work you are interested in completing has already been queued. |
| 6238 | */ |
| 6239 | bool InputDispatcher::waitForIdle() { |
| 6240 | /** |
| 6241 | * Timeout should represent the longest possible time that a device might spend processing |
| 6242 | * events and commands. |
| 6243 | */ |
| 6244 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6245 | std::unique_lock lock(mLock); |
| 6246 | mLooper->wake(); |
| 6247 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6248 | return result == std::cv_status::no_timeout; |
| 6249 | } |
| 6250 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6251 | /** |
| 6252 | * Sets focus to the window identified by the token. This must be called |
| 6253 | * after updating any input window handles. |
| 6254 | * |
| 6255 | * Params: |
| 6256 | * request.token - input channel token used to identify the window that should gain focus. |
| 6257 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6258 | * specified token does not match the currently focused window, this request will be dropped. |
| 6259 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6260 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6261 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6262 | * when requesting the focus change. This determines which request gets |
| 6263 | * precedence if there is a focus change request from another source such as pointer down. |
| 6264 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6265 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6266 | { // acquire lock |
| 6267 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6268 | std::optional<FocusResolver::FocusChanges> changes = |
| 6269 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6270 | if (changes) { |
| 6271 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6272 | } |
| 6273 | } // release lock |
| 6274 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6275 | mLooper->wake(); |
| 6276 | } |
| 6277 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6278 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6279 | if (changes.oldFocus) { |
| 6280 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6281 | if (focusedInputChannel) { |
| 6282 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6283 | "focus left window"); |
| 6284 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6285 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6286 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6287 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6288 | if (changes.newFocus) { |
| 6289 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6290 | } |
| 6291 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6292 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6293 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6294 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6295 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6296 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6297 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6298 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6299 | // front. |
| 6300 | disablePointerCaptureForcedLocked(); |
| 6301 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6302 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6303 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6304 | } |
| 6305 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6306 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6307 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6308 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6309 | return; |
| 6310 | } |
| 6311 | |
| 6312 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6313 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6314 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6315 | setPointerCaptureLocked(false); |
| 6316 | } |
| 6317 | |
| 6318 | if (!mWindowTokenWithPointerCapture) { |
| 6319 | // No need to send capture changes because no window has capture. |
| 6320 | return; |
| 6321 | } |
| 6322 | |
| 6323 | if (mPendingEvent != nullptr) { |
| 6324 | // Move the pending event to the front of the queue. This will give the chance |
| 6325 | // for the pending event to be dropped if it is a captured event. |
| 6326 | mInboundQueue.push_front(mPendingEvent); |
| 6327 | mPendingEvent = nullptr; |
| 6328 | } |
| 6329 | |
| 6330 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6331 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6332 | mInboundQueue.push_front(std::move(entry)); |
| 6333 | } |
| 6334 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6335 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6336 | mCurrentPointerCaptureRequest.enable = enable; |
| 6337 | mCurrentPointerCaptureRequest.seq++; |
| 6338 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6339 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6340 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6341 | }; |
| 6342 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6343 | } |
| 6344 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6345 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6346 | { // acquire lock |
| 6347 | std::scoped_lock _l(mLock); |
| 6348 | // Set an empty list to remove all handles from the specific display. |
| 6349 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6350 | setFocusedApplicationLocked(displayId, nullptr); |
| 6351 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6352 | // have been removed for the removed display. |
| 6353 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6354 | // Reset pointer capture eligibility, regardless of previous state. |
| 6355 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6356 | } // release lock |
| 6357 | |
| 6358 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6359 | mLooper->wake(); |
| 6360 | } |
| 6361 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6362 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6363 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6364 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6365 | // more convenient parsing. |
| 6366 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6367 | for (const auto& info : windowInfos) { |
| 6368 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6369 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6370 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6371 | |
| 6372 | { // acquire lock |
| 6373 | std::scoped_lock _l(mLock); |
| 6374 | mDisplayInfos.clear(); |
| 6375 | for (const auto& displayInfo : displayInfos) { |
| 6376 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6377 | } |
| 6378 | |
| 6379 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6380 | setInputWindowsLocked(handles, displayId); |
| 6381 | } |
| 6382 | } |
| 6383 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6384 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6385 | } |
| 6386 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6387 | bool InputDispatcher::shouldDropInput( |
| 6388 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
| 6389 | if (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT) || |
| 6390 | (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED) && |
| 6391 | isWindowObscuredLocked(windowHandle))) { |
| 6392 | ALOGW("Dropping %s event targeting %s as requested by input feature %s on display " |
| 6393 | "%" PRId32 ".", |
| 6394 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
| 6395 | windowHandle->getInfo()->inputFeatures.string().c_str(), |
| 6396 | windowHandle->getInfo()->displayId); |
| 6397 | return true; |
| 6398 | } |
| 6399 | return false; |
| 6400 | } |
| 6401 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6402 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6403 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6404 | const std::vector<DisplayInfo>& displayInfos) { |
| 6405 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6406 | } |
| 6407 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6408 | void InputDispatcher::cancelCurrentTouch() { |
| 6409 | { |
| 6410 | std::scoped_lock _l(mLock); |
| 6411 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
| 6412 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 6413 | "cancel current touch"); |
| 6414 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6415 | |
| 6416 | mTouchStatesByDisplay.clear(); |
| 6417 | mLastHoverWindowHandle.clear(); |
| 6418 | } |
| 6419 | // Wake up poll loop since there might be work to do. |
| 6420 | mLooper->wake(); |
| 6421 | } |
| 6422 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6423 | } // namespace android::inputdispatcher |