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 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 421 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 422 | // affect the statistics. |
| 423 | if (connection.monitor) { |
| 424 | return false; |
| 425 | } |
| 426 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 427 | if (!connection.responsive) { |
| 428 | return false; |
| 429 | } |
| 430 | return true; |
| 431 | } |
| 432 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 433 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 434 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 435 | const int32_t& inputEventId = eventEntry.id; |
| 436 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 437 | // Event was transmuted |
| 438 | return false; |
| 439 | } |
| 440 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 441 | return false; |
| 442 | } |
| 443 | // Only track latency for events that originated from hardware |
| 444 | if (eventEntry.isSynthesized()) { |
| 445 | return false; |
| 446 | } |
| 447 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 448 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 449 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 450 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 451 | return false; |
| 452 | } |
| 453 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 454 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 455 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 456 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 457 | return false; |
| 458 | } |
| 459 | } else { |
| 460 | // Not a key or a motion |
| 461 | return false; |
| 462 | } |
| 463 | if (!shouldReportMetricsForConnection(connection)) { |
| 464 | return false; |
| 465 | } |
| 466 | return true; |
| 467 | } |
| 468 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 469 | /** |
| 470 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 471 | * current time. |
| 472 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 473 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 474 | const nsecs_t currentTime = now(); |
| 475 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 476 | if (entry->timeoutTime < currentTime) { |
| 477 | return false; |
| 478 | } |
| 479 | } |
| 480 | return true; |
| 481 | } |
| 482 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 483 | // Returns true if the event type passed as argument represents a user activity. |
| 484 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 485 | switch (eventEntry.type) { |
| 486 | case EventEntry::Type::FOCUS: |
| 487 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 488 | case EventEntry::Type::DRAG: |
| 489 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 490 | case EventEntry::Type::SENSOR: |
| 491 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 492 | return false; |
| 493 | case EventEntry::Type::DEVICE_RESET: |
| 494 | case EventEntry::Type::KEY: |
| 495 | case EventEntry::Type::MOTION: |
| 496 | return true; |
| 497 | } |
| 498 | } |
| 499 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 500 | // 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] | 501 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, int32_t x, int32_t y, |
| 502 | bool isStylus) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 503 | const auto inputConfig = windowInfo.inputConfig; |
| 504 | if (windowInfo.displayId != displayId || |
| 505 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 506 | return false; |
| 507 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 508 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 509 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 510 | return false; |
| 511 | } |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 512 | const bool isModalWindow = !inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE) && |
| 513 | !inputConfig.test(WindowInfo::InputConfig::NOT_TOUCH_MODAL); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 514 | if (!isModalWindow && !windowInfo.touchableRegionContainsPoint(x, y)) { |
| 515 | return false; |
| 516 | } |
| 517 | return true; |
| 518 | } |
| 519 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 520 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 521 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
| 522 | (entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || |
| 523 | entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_ERASER); |
| 524 | } |
| 525 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 526 | } // namespace |
| 527 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 528 | // --- InputDispatcher --- |
| 529 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 530 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 531 | : mPolicy(policy), |
| 532 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 533 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 534 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 535 | mAppSwitchSawKeyDown(false), |
| 536 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 537 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 538 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 539 | mDispatchEnabled(false), |
| 540 | mDispatchFrozen(false), |
| 541 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 542 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 543 | // To avoid leaking stack in case that call never comes, and for tests, |
| 544 | // initialize it here anyways. |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 545 | mInTouchMode(kDefaultInTouchMode), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 546 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 547 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 548 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 549 | mLatencyAggregator(), |
Siarhei Vishniakou | bd25272 | 2022-01-06 03:49:35 -0800 | [diff] [blame] | 550 | mLatencyTracker(&mLatencyAggregator) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 551 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 552 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 553 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 554 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 555 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 556 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 557 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 558 | |
| 559 | policy->getDispatcherConfiguration(&mConfig); |
| 560 | } |
| 561 | |
| 562 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 563 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 564 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 565 | resetKeyRepeatLocked(); |
| 566 | releasePendingEventLocked(); |
| 567 | drainInboundQueueLocked(); |
| 568 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 569 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 570 | while (!mConnectionsByToken.empty()) { |
| 571 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 572 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 573 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 577 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 578 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 579 | return ALREADY_EXISTS; |
| 580 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 581 | mThread = std::make_unique<InputThread>( |
| 582 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 583 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 587 | if (mThread && mThread->isCallingThread()) { |
| 588 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 589 | return INVALID_OPERATION; |
| 590 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 591 | mThread.reset(); |
| 592 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 595 | void InputDispatcher::dispatchOnce() { |
| 596 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 597 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 598 | std::scoped_lock _l(mLock); |
| 599 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 600 | |
| 601 | // Run a dispatch loop if there are no pending commands. |
| 602 | // The dispatch loop might enqueue commands to run afterwards. |
| 603 | if (!haveCommandsLocked()) { |
| 604 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 605 | } |
| 606 | |
| 607 | // Run all pending commands if there are any. |
| 608 | // If any commands were run then force the next poll to wake up immediately. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 609 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 610 | nextWakeupTime = LONG_LONG_MIN; |
| 611 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 612 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 613 | // If we are still waiting for ack on some events, |
| 614 | // we might have to wake up earlier to check if an app is anr'ing. |
| 615 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 616 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 617 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 618 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 619 | // pending or queued events |
| 620 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 621 | mDispatcherEnteredIdle.notify_all(); |
| 622 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | } // release lock |
| 624 | |
| 625 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 626 | nsecs_t currentTime = now(); |
| 627 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 628 | mLooper->pollOnce(timeoutMillis); |
| 629 | } |
| 630 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 631 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 632 | * Raise ANR if there is no focused window. |
| 633 | * Before the ANR is raised, do a final state check: |
| 634 | * 1. The currently focused application must be the same one we are waiting for. |
| 635 | * 2. Ensure we still don't have a focused window. |
| 636 | */ |
| 637 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 638 | // Check if the application that we are waiting for is still focused. |
| 639 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 640 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 641 | if (focusedApplication == nullptr || |
| 642 | focusedApplication->getApplicationToken() != |
| 643 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 644 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 645 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 646 | focusedApplication->getName().c_str()); |
| 647 | return; // The focused application has changed. |
| 648 | } |
| 649 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 650 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 651 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 652 | if (focusedWindowHandle != nullptr) { |
| 653 | return; // We now have a focused window. No need for ANR. |
| 654 | } |
| 655 | onAnrLocked(mAwaitedFocusedApplication); |
| 656 | } |
| 657 | |
| 658 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 659 | * Check if any of the connections' wait queues have events that are too old. |
| 660 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 661 | * Return the time at which we should wake up next. |
| 662 | */ |
| 663 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 664 | const nsecs_t currentTime = now(); |
| 665 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 666 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 667 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 668 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 669 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 670 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 671 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 672 | return LONG_LONG_MIN; |
| 673 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 674 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 675 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | // Check if any connection ANRs are due |
| 680 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 681 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 682 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 683 | } |
| 684 | |
| 685 | // If we reached here, we have an unresponsive connection. |
| 686 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 687 | if (connection == nullptr) { |
| 688 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 689 | return nextAnrCheck; |
| 690 | } |
| 691 | connection->responsive = false; |
| 692 | // Stop waking up for this unresponsive connection |
| 693 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 694 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 695 | return LONG_LONG_MIN; |
| 696 | } |
| 697 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 698 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
| 699 | const sp<Connection>& connection) { |
| 700 | if (connection->monitor) { |
| 701 | return mMonitorDispatchingTimeout; |
| 702 | } |
| 703 | const sp<WindowInfoHandle> window = |
| 704 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 705 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 706 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 707 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 708 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 711 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 712 | nsecs_t currentTime = now(); |
| 713 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 714 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 715 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 716 | // repeat if the device is just coming out of sleep. |
| 717 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 718 | resetKeyRepeatLocked(); |
| 719 | } |
| 720 | |
| 721 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 722 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 723 | if (DEBUG_FOCUS) { |
| 724 | ALOGD("Dispatch frozen. Waiting some more."); |
| 725 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 726 | return; |
| 727 | } |
| 728 | |
| 729 | // Optimize latency of app switches. |
| 730 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 731 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 732 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 733 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 734 | *nextWakeupTime = mAppSwitchDueTime; |
| 735 | } |
| 736 | |
| 737 | // Ready to start a new event. |
| 738 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 739 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 740 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 741 | if (isAppSwitchDue) { |
| 742 | // The inbound queue is empty so the app switch key we were waiting |
| 743 | // for will never arrive. Stop waiting for it. |
| 744 | resetPendingAppSwitchLocked(false); |
| 745 | isAppSwitchDue = false; |
| 746 | } |
| 747 | |
| 748 | // Synthesize a key repeat if appropriate. |
| 749 | if (mKeyRepeatState.lastKeyEntry) { |
| 750 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 751 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 752 | } else { |
| 753 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 754 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | // Nothing to do if there is no pending event. |
| 760 | if (!mPendingEvent) { |
| 761 | return; |
| 762 | } |
| 763 | } else { |
| 764 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 765 | mPendingEvent = mInboundQueue.front(); |
| 766 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 767 | traceInboundQueueLengthLocked(); |
| 768 | } |
| 769 | |
| 770 | // Poke user activity for this event. |
| 771 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 772 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 773 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | // Now we have an event to dispatch. |
| 777 | // 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] | 778 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 779 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 780 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 781 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 782 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 783 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 784 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 788 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 792 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 793 | const ConfigurationChangedEntry& typedEntry = |
| 794 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 795 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 796 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 797 | break; |
| 798 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 799 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 800 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 801 | const DeviceResetEntry& typedEntry = |
| 802 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 803 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 804 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 805 | break; |
| 806 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 807 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 808 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 809 | std::shared_ptr<FocusEntry> typedEntry = |
| 810 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 811 | dispatchFocusLocked(currentTime, typedEntry); |
| 812 | done = true; |
| 813 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 814 | break; |
| 815 | } |
| 816 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 817 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 818 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 819 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 820 | done = true; |
| 821 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 822 | break; |
| 823 | } |
| 824 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 825 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 826 | const auto typedEntry = |
| 827 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 828 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 829 | done = true; |
| 830 | break; |
| 831 | } |
| 832 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 833 | case EventEntry::Type::DRAG: { |
| 834 | std::shared_ptr<DragEntry> typedEntry = |
| 835 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 836 | dispatchDragLocked(currentTime, typedEntry); |
| 837 | done = true; |
| 838 | break; |
| 839 | } |
| 840 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 841 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 842 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 843 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 844 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 845 | resetPendingAppSwitchLocked(true); |
| 846 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 847 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 848 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 851 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 852 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 853 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 854 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 855 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 856 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 857 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 858 | break; |
| 859 | } |
| 860 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 861 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 862 | std::shared_ptr<MotionEntry> motionEntry = |
| 863 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 864 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 865 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 866 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 867 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 868 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 869 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 870 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 871 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 872 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 873 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 874 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 875 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 876 | |
| 877 | case EventEntry::Type::SENSOR: { |
| 878 | std::shared_ptr<SensorEntry> sensorEntry = |
| 879 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 880 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 881 | dropReason = DropReason::APP_SWITCH; |
| 882 | } |
| 883 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 884 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 885 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 886 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 887 | dropReason = DropReason::STALE; |
| 888 | } |
| 889 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 890 | done = true; |
| 891 | break; |
| 892 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 896 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 897 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 898 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 899 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 900 | |
| 901 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 902 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 906 | /** |
| 907 | * Return true if the events preceding this incoming motion event should be dropped |
| 908 | * Return false otherwise (the default behaviour) |
| 909 | */ |
| 910 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 911 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 912 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 913 | |
| 914 | // Optimize case where the current application is unresponsive and the user |
| 915 | // decides to touch a window in a different application. |
| 916 | // If the application takes too long to catch up then we drop all events preceding |
| 917 | // the touch into the other window. |
| 918 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 919 | int32_t displayId = motionEntry.displayId; |
| 920 | int32_t x = static_cast<int32_t>( |
| 921 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 922 | int32_t y = static_cast<int32_t>( |
| 923 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 924 | |
| 925 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 926 | sp<WindowInfoHandle> touchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 927 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 928 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 929 | touchedWindowHandle->getApplicationToken() != |
| 930 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 931 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 932 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 933 | "for %s", |
| 934 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 935 | return true; |
| 936 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 937 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 938 | // Alternatively, maybe there's a spy window that could handle this event. |
| 939 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 940 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 941 | for (const auto& windowHandle : touchedSpies) { |
| 942 | const sp<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 943 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 944 | // This spy window could take more input. Drop all events preceding this |
| 945 | // event, so that the spy window can get a chance to receive the stream. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 946 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 947 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 948 | mAwaitedFocusedApplication->getName().c_str()); |
| 949 | return true; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 955 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 956 | // events to be processed before dispatching the key event. This is because these motion events |
| 957 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 958 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 959 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 960 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 961 | "just send the pending key event to the focused window."); |
| 962 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 963 | } |
| 964 | return false; |
| 965 | } |
| 966 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 967 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 968 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 969 | mInboundQueue.push_back(std::move(newEntry)); |
| 970 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 971 | traceInboundQueueLengthLocked(); |
| 972 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 973 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 974 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 975 | // Optimize app switch latency. |
| 976 | // If the application takes too long to catch up then we drop all events preceding |
| 977 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 978 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 979 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 980 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 981 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 982 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 983 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 984 | if (DEBUG_APP_SWITCH) { |
| 985 | ALOGD("App switch is pending!"); |
| 986 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 987 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 988 | mAppSwitchSawKeyDown = false; |
| 989 | needWake = true; |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | break; |
| 994 | } |
| 995 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 996 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 997 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 998 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 999 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1000 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1001 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1002 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1003 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1004 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1005 | break; |
| 1006 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1007 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1008 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1009 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1010 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1011 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1012 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1013 | // nothing to do |
| 1014 | break; |
| 1015 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | return needWake; |
| 1019 | } |
| 1020 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1021 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1022 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1023 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1024 | mRecentQueue.push_back(entry); |
| 1025 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1026 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1027 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1031 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1032 | int32_t y, TouchState* touchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1033 | bool isStylus, |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1034 | bool addOutsideTargets, |
| 1035 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1036 | if (addOutsideTargets && touchState == nullptr) { |
| 1037 | 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] | 1038 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1039 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1040 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1041 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1042 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1043 | continue; |
| 1044 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1045 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1046 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1047 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1048 | return windowHandle; |
| 1049 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1050 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 1051 | if (addOutsideTargets && |
| 1052 | info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1053 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1054 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1055 | } |
| 1056 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1057 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1060 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1061 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1062 | // Traverse windows from front to back and gather the touched spy windows. |
| 1063 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1064 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1065 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1066 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1067 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1068 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1069 | continue; |
| 1070 | } |
| 1071 | if (!info.isSpy()) { |
| 1072 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1073 | return spyWindows; |
| 1074 | } |
| 1075 | spyWindows.push_back(windowHandle); |
| 1076 | } |
| 1077 | return spyWindows; |
| 1078 | } |
| 1079 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1080 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1081 | const char* reason; |
| 1082 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1083 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1084 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1085 | ALOGD("Dropped event because policy consumed it."); |
| 1086 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1087 | reason = "inbound event was dropped because the policy consumed it"; |
| 1088 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1089 | case DropReason::DISABLED: |
| 1090 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1091 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1092 | } |
| 1093 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1094 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1095 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1096 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1097 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1098 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1099 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1100 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1101 | "has started interacting with a different application."); |
| 1102 | reason = "inbound event was dropped because the current application is not responding " |
| 1103 | "and the user has started interacting with a different application"; |
| 1104 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1105 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1106 | ALOGI("Dropped event because it is stale."); |
| 1107 | reason = "inbound event was dropped because it is stale"; |
| 1108 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1109 | case DropReason::NO_POINTER_CAPTURE: |
| 1110 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1111 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1112 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1113 | case DropReason::NOT_DROPPED: { |
| 1114 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1115 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1116 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1117 | } |
| 1118 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1119 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1120 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1121 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1122 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1123 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1124 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1125 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1126 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1127 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1128 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1129 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1130 | } else { |
| 1131 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1132 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1133 | } |
| 1134 | break; |
| 1135 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1136 | case EventEntry::Type::SENSOR: { |
| 1137 | break; |
| 1138 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1139 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1140 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1141 | break; |
| 1142 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1143 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1144 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1145 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1146 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1147 | 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] | 1148 | break; |
| 1149 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1153 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1154 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1155 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1158 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1159 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1160 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1161 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1165 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1166 | } |
| 1167 | |
| 1168 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1169 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1170 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1171 | if (DEBUG_APP_SWITCH) { |
| 1172 | if (handled) { |
| 1173 | ALOGD("App switch has arrived."); |
| 1174 | } else { |
| 1175 | ALOGD("App switch was abandoned."); |
| 1176 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1177 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1180 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1181 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1184 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1185 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1186 | return false; |
| 1187 | } |
| 1188 | |
| 1189 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1190 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1191 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1192 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1193 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1194 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1195 | return true; |
| 1196 | } |
| 1197 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1198 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1199 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1203 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1204 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1205 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1206 | releaseInboundEventLocked(entry); |
| 1207 | } |
| 1208 | traceInboundQueueLengthLocked(); |
| 1209 | } |
| 1210 | |
| 1211 | void InputDispatcher::releasePendingEventLocked() { |
| 1212 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1213 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1214 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1218 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1219 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1220 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1221 | if (DEBUG_DISPATCH_CYCLE) { |
| 1222 | ALOGD("Injected inbound event was dropped."); |
| 1223 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1224 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1225 | } |
| 1226 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1227 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1228 | } |
| 1229 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1233 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1234 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1238 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1239 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1240 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1241 | uint32_t policyFlags = entry->policyFlags & |
| 1242 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1243 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1244 | std::shared_ptr<KeyEntry> newEntry = |
| 1245 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1246 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1247 | entry->flags, entry->keyCode, entry->scanCode, |
| 1248 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1249 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1250 | newEntry->syntheticRepeat = true; |
| 1251 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1252 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1253 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1256 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1257 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1258 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1259 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1260 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1261 | |
| 1262 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1263 | resetKeyRepeatLocked(); |
| 1264 | |
| 1265 | // 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] | 1266 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1267 | scoped_unlock unlock(mLock); |
| 1268 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1269 | }; |
| 1270 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1271 | return true; |
| 1272 | } |
| 1273 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1274 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1275 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1276 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1277 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1278 | entry.deviceId); |
| 1279 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1280 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1281 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1282 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1283 | resetKeyRepeatLocked(); |
| 1284 | } |
| 1285 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1286 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1287 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1288 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1289 | return true; |
| 1290 | } |
| 1291 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1292 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1293 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1294 | if (mPendingEvent != nullptr) { |
| 1295 | // Move the pending event to the front of the queue. This will give the chance |
| 1296 | // for the pending event to get dispatched to the newly focused window |
| 1297 | mInboundQueue.push_front(mPendingEvent); |
| 1298 | mPendingEvent = nullptr; |
| 1299 | } |
| 1300 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1301 | std::unique_ptr<FocusEntry> focusEntry = |
| 1302 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1303 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1304 | |
| 1305 | // This event should go to the front of the queue, but behind all other focus events |
| 1306 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1307 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1308 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1309 | [](const std::shared_ptr<EventEntry>& event) { |
| 1310 | return event->type == EventEntry::Type::FOCUS; |
| 1311 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1312 | |
| 1313 | // 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] | 1314 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1315 | } |
| 1316 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1317 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1318 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1319 | if (channel == nullptr) { |
| 1320 | return; // Window has gone away |
| 1321 | } |
| 1322 | InputTarget target; |
| 1323 | target.inputChannel = channel; |
| 1324 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1325 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1326 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1327 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1328 | std::string reason = std::string("reason=").append(entry->reason); |
| 1329 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1330 | dispatchEventLocked(currentTime, entry, {target}); |
| 1331 | } |
| 1332 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1333 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1334 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1335 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1336 | dropReason = DropReason::NOT_DROPPED; |
| 1337 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1338 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1339 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1340 | |
| 1341 | if (entry->pointerCaptureRequest.enable) { |
| 1342 | // Enable Pointer Capture. |
| 1343 | if (haveWindowWithPointerCapture && |
| 1344 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
| 1345 | LOG_ALWAYS_FATAL("This request to enable Pointer Capture has already been dispatched " |
| 1346 | "to the window."); |
| 1347 | } |
| 1348 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1349 | // This can happen if a window requests capture and immediately releases capture. |
| 1350 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1351 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1352 | return; |
| 1353 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1354 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1355 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1356 | return; |
| 1357 | } |
| 1358 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1359 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1360 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1361 | mWindowTokenWithPointerCapture = token; |
| 1362 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1363 | // Disable Pointer Capture. |
| 1364 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1365 | // for two reasons: |
| 1366 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1367 | // to disable capture with the same sequence number: one generated by |
| 1368 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1369 | // Capture being disabled in InputReader. |
| 1370 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1371 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1372 | // in InputReader. |
| 1373 | if (!haveWindowWithPointerCapture) { |
| 1374 | // Pointer capture was already forcefully disabled because of focus change. |
| 1375 | dropReason = DropReason::NOT_DROPPED; |
| 1376 | return; |
| 1377 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1378 | token = mWindowTokenWithPointerCapture; |
| 1379 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1380 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1381 | setPointerCaptureLocked(false); |
| 1382 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | auto channel = getInputChannelLocked(token); |
| 1386 | if (channel == nullptr) { |
| 1387 | // Window has gone away, clean up Pointer Capture state. |
| 1388 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1389 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1390 | setPointerCaptureLocked(false); |
| 1391 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1392 | return; |
| 1393 | } |
| 1394 | InputTarget target; |
| 1395 | target.inputChannel = channel; |
| 1396 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1397 | entry->dispatchInProgress = true; |
| 1398 | dispatchEventLocked(currentTime, entry, {target}); |
| 1399 | |
| 1400 | dropReason = DropReason::NOT_DROPPED; |
| 1401 | } |
| 1402 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1403 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1404 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1405 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1406 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1407 | if (windowHandles.empty()) { |
| 1408 | return; |
| 1409 | } |
| 1410 | const std::vector<InputTarget> inputTargets = |
| 1411 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1412 | if (inputTargets.empty()) { |
| 1413 | return; |
| 1414 | } |
| 1415 | entry->dispatchInProgress = true; |
| 1416 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1417 | } |
| 1418 | |
| 1419 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1420 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1421 | std::vector<InputTarget> inputTargets; |
| 1422 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1423 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1424 | const sp<IBinder>& token = handle->getToken(); |
| 1425 | if (token == nullptr) { |
| 1426 | continue; |
| 1427 | } |
| 1428 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1429 | if (channel == nullptr) { |
| 1430 | continue; // Window has gone away |
| 1431 | } |
| 1432 | InputTarget target; |
| 1433 | target.inputChannel = channel; |
| 1434 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1435 | inputTargets.push_back(target); |
| 1436 | } |
| 1437 | return inputTargets; |
| 1438 | } |
| 1439 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1440 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1441 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1442 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1443 | if (!entry->dispatchInProgress) { |
| 1444 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1445 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1446 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1447 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1448 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1449 | // We have seen two identical key downs in a row which indicates that the device |
| 1450 | // driver is automatically generating key repeats itself. We take note of the |
| 1451 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1452 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1453 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1454 | // Make sure we don't get key down from a different device. If a different |
| 1455 | // device Id has same key pressed down, the new device Id will replace the |
| 1456 | // current one to hold the key repeat with repeat count reset. |
| 1457 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1458 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1459 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1460 | resetKeyRepeatLocked(); |
| 1461 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1462 | } else { |
| 1463 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1464 | resetKeyRepeatLocked(); |
| 1465 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1466 | } |
| 1467 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1468 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1469 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1470 | // 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] | 1471 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1472 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1473 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1474 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1475 | resetKeyRepeatLocked(); |
| 1476 | } |
| 1477 | |
| 1478 | if (entry->repeatCount == 1) { |
| 1479 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1480 | } else { |
| 1481 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1482 | } |
| 1483 | |
| 1484 | entry->dispatchInProgress = true; |
| 1485 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1486 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | // Handle case where the policy asked us to try again later last time. |
| 1490 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1491 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1492 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1493 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1494 | } |
| 1495 | return false; // wait until next wakeup |
| 1496 | } |
| 1497 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1498 | entry->interceptKeyWakeupTime = 0; |
| 1499 | } |
| 1500 | |
| 1501 | // Give the policy a chance to intercept the key. |
| 1502 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1503 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1504 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1505 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1506 | |
| 1507 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1508 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1509 | }; |
| 1510 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1511 | return false; // wait for the command to run |
| 1512 | } else { |
| 1513 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1514 | } |
| 1515 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1516 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1517 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1522 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1523 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1524 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1525 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1526 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1527 | return true; |
| 1528 | } |
| 1529 | |
| 1530 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1531 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1532 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1533 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1534 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1535 | return false; |
| 1536 | } |
| 1537 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1538 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1539 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1540 | return true; |
| 1541 | } |
| 1542 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1543 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1544 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1545 | |
| 1546 | // Dispatch the key. |
| 1547 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1548 | return true; |
| 1549 | } |
| 1550 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1551 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1552 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1553 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1554 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1555 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1556 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1557 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1558 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1559 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1560 | } |
| 1561 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1562 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1563 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1564 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1565 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1566 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1567 | "source=0x%x, sensorType=%s", |
| 1568 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1569 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1570 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1571 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1572 | scoped_unlock unlock(mLock); |
| 1573 | |
| 1574 | if (entry->accuracyChanged) { |
| 1575 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1576 | } |
| 1577 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1578 | entry->hwTimestamp, entry->values); |
| 1579 | }; |
| 1580 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1584 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1585 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1586 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1587 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1588 | { // acquire lock |
| 1589 | std::scoped_lock _l(mLock); |
| 1590 | |
| 1591 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1592 | std::shared_ptr<EventEntry> entry = *it; |
| 1593 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1594 | it = mInboundQueue.erase(it); |
| 1595 | releaseInboundEventLocked(entry); |
| 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | return true; |
| 1600 | } |
| 1601 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1602 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1603 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1604 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1605 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1606 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1607 | entry->dispatchInProgress = true; |
| 1608 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1609 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1613 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1614 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1615 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1616 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1617 | return true; |
| 1618 | } |
| 1619 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1620 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1621 | |
| 1622 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1623 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1624 | |
| 1625 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1626 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1627 | if (isPointerEvent) { |
| 1628 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1629 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1630 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1631 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1632 | } else { |
| 1633 | // Non touch event. (eg. trackball) |
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 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1636 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1637 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1638 | return false; |
| 1639 | } |
| 1640 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1641 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1642 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1643 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1644 | return true; |
| 1645 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1646 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1647 | CancelationOptions::Mode mode(isPointerEvent |
| 1648 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1649 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1650 | CancelationOptions options(mode, "input event injection failed"); |
| 1651 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1652 | return true; |
| 1653 | } |
| 1654 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1655 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1656 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1657 | |
| 1658 | // Dispatch the motion. |
| 1659 | if (conflictingPointerActions) { |
| 1660 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1661 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1662 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1663 | } |
| 1664 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1665 | return true; |
| 1666 | } |
| 1667 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1668 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1669 | bool isExiting, const MotionEntry& motionEntry) { |
| 1670 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1671 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1672 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1673 | PointerCoords pointerCoords; |
| 1674 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1675 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1676 | |
| 1677 | std::unique_ptr<DragEntry> dragEntry = |
| 1678 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1679 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1680 | pointerCoords.getY()); |
| 1681 | |
| 1682 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1683 | } |
| 1684 | |
| 1685 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1686 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1687 | if (channel == nullptr) { |
| 1688 | return; // Window has gone away |
| 1689 | } |
| 1690 | InputTarget target; |
| 1691 | target.inputChannel = channel; |
| 1692 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1693 | entry->dispatchInProgress = true; |
| 1694 | dispatchEventLocked(currentTime, entry, {target}); |
| 1695 | } |
| 1696 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1697 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1698 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1699 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1700 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1701 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1702 | "metaState=0x%x, buttonState=0x%x," |
| 1703 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1704 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1705 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1706 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1707 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1708 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1709 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1710 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1711 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1712 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1713 | "orientation=%f", |
| 1714 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1715 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1716 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1717 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1718 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1719 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1720 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1721 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1722 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1723 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1724 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1725 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1726 | } |
| 1727 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1728 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1729 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1730 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1731 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1732 | if (DEBUG_DISPATCH_CYCLE) { |
| 1733 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1734 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1735 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1736 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1737 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1738 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1739 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1740 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1741 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1742 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1743 | sp<Connection> connection = |
| 1744 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1745 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1746 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1747 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1748 | if (DEBUG_FOCUS) { |
| 1749 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1750 | "is no longer registered with the input dispatcher.", |
| 1751 | inputTarget.inputChannel->getName().c_str()); |
| 1752 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1757 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1758 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1759 | // If the policy decides to close the app, we will get a channel removal event via |
| 1760 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1761 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1762 | // pile up. |
| 1763 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1764 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1765 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1766 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1767 | "application not responding"); |
| 1768 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1769 | } |
| 1770 | } |
| 1771 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1772 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1773 | if (DEBUG_FOCUS) { |
| 1774 | ALOGD("Resetting ANR timeouts."); |
| 1775 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1776 | |
| 1777 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1778 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1779 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1782 | /** |
| 1783 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1784 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1785 | * Focused display is the display that the user most recently interacted with. |
| 1786 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1787 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1788 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1789 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1790 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1791 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1792 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1793 | break; |
| 1794 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1795 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1796 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1797 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1798 | break; |
| 1799 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1800 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1801 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1802 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1803 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1804 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1805 | case EventEntry::Type::SENSOR: |
| 1806 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1807 | 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] | 1808 | return ADISPLAY_ID_NONE; |
| 1809 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1810 | } |
| 1811 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1812 | } |
| 1813 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1814 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1815 | const char* focusedWindowName) { |
| 1816 | if (mAnrTracker.empty()) { |
| 1817 | // already processed all events that we waited for |
| 1818 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1819 | return false; |
| 1820 | } |
| 1821 | |
| 1822 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1823 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1824 | // 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] | 1825 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1826 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1827 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1828 | return true; |
| 1829 | } |
| 1830 | |
| 1831 | // We still have pending events, and already started the timer |
| 1832 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1833 | return true; // Still waiting |
| 1834 | } |
| 1835 | |
| 1836 | // Waited too long, and some connection still hasn't processed all motions |
| 1837 | // Just send the key to the focused window |
| 1838 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1839 | focusedWindowName); |
| 1840 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1841 | return false; |
| 1842 | } |
| 1843 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1844 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1845 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1846 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1847 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1848 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1849 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1850 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1851 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1852 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1853 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1854 | // If there is no currently focused window and no focused application |
| 1855 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1856 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1857 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1858 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1859 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1860 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1861 | } |
| 1862 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1863 | // Drop key events if requested by input feature |
| 1864 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1865 | return InputEventInjectionResult::FAILED; |
| 1866 | } |
| 1867 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1868 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1869 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1870 | // start interacting with another application via touch (app switch). This code can be removed |
| 1871 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1872 | // an app is expected to have a focused window. |
| 1873 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1874 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1875 | // 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] | 1876 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1877 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1878 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1879 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1880 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1881 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1882 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1883 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1884 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1885 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1886 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1887 | // Already raised ANR. Drop the event |
| 1888 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1889 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1890 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1891 | } else { |
| 1892 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1893 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | // we have a valid, non-null focused window |
| 1898 | resetNoFocusedWindowTimeoutLocked(); |
| 1899 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1900 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1901 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1902 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1903 | } |
| 1904 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 1905 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 1906 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1907 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1908 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | // If the event is a key event, then we must wait for all previous events to |
| 1912 | // complete before delivering it because previous events may have the |
| 1913 | // side-effect of transferring focus to a different window and we want to |
| 1914 | // ensure that the following keys are sent to the new window. |
| 1915 | // |
| 1916 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1917 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1918 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1919 | // often anticipate pending UI changes when typing on a keyboard. |
| 1920 | // To obtain this behavior, we must serialize key events with respect to all |
| 1921 | // prior input events. |
| 1922 | if (entry.type == EventEntry::Type::KEY) { |
| 1923 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1924 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1925 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1926 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1930 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1931 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1932 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1933 | |
| 1934 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1935 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1936 | } |
| 1937 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1938 | /** |
| 1939 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1940 | * that are currently unresponsive. |
| 1941 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1942 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1943 | const std::vector<Monitor>& monitors) const { |
| 1944 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1945 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1946 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 1947 | sp<Connection> connection = |
| 1948 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1949 | if (connection == nullptr) { |
| 1950 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1951 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1952 | return false; |
| 1953 | } |
| 1954 | if (!connection->responsive) { |
| 1955 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1956 | connection->inputChannel->getName().c_str()); |
| 1957 | return false; |
| 1958 | } |
| 1959 | return true; |
| 1960 | }); |
| 1961 | return responsiveMonitors; |
| 1962 | } |
| 1963 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1964 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1965 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1966 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1967 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1968 | enum InjectionPermission { |
| 1969 | INJECTION_PERMISSION_UNKNOWN, |
| 1970 | INJECTION_PERMISSION_GRANTED, |
| 1971 | INJECTION_PERMISSION_DENIED |
| 1972 | }; |
| 1973 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1974 | // For security reasons, we defer updating the touch state until we are sure that |
| 1975 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1976 | const int32_t displayId = entry.displayId; |
| 1977 | const int32_t action = entry.action; |
| 1978 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1979 | |
| 1980 | // 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] | 1981 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1982 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1983 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1984 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1985 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1986 | // Copy current touch state into tempTouchState. |
| 1987 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1988 | // 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] | 1989 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1990 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1991 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 1992 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 1993 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1994 | } |
| 1995 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1996 | bool isSplit = tempTouchState.split; |
| 1997 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1998 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1999 | tempTouchState.displayId != displayId); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2000 | |
| 2001 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2002 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2003 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2004 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2005 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2006 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2007 | bool wrongDevice = false; |
| 2008 | if (newGesture) { |
| 2009 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2010 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2011 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2012 | "in display %" PRId32, |
| 2013 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2014 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2015 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2016 | switchedDevice = false; |
| 2017 | wrongDevice = true; |
| 2018 | goto Failed; |
| 2019 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2020 | tempTouchState.reset(); |
| 2021 | tempTouchState.down = down; |
| 2022 | tempTouchState.deviceId = entry.deviceId; |
| 2023 | tempTouchState.source = entry.source; |
| 2024 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2025 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2026 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2027 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2028 | "in display %" PRId32, |
| 2029 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2030 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2031 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2032 | switchedDevice = false; |
| 2033 | wrongDevice = true; |
| 2034 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2038 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2039 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2040 | int32_t x; |
| 2041 | int32_t y; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2042 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2043 | // Always dispatch mouse events to cursor position. |
| 2044 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2045 | x = int32_t(entry.xCursorPosition); |
| 2046 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2047 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2048 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2049 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2050 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2051 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2052 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2053 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2054 | isStylus, isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2055 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2056 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2057 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2058 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2059 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2060 | // 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] | 2061 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2064 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2065 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2066 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2067 | // New window supports splitting, but we should never split mouse events. |
| 2068 | isSplit = !isFromMouse; |
| 2069 | } else if (isSplit) { |
| 2070 | // New window does not support splitting but we have already split events. |
| 2071 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2072 | newTouchedWindowHandle = nullptr; |
| 2073 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2074 | } else { |
| 2075 | // 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] | 2076 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2077 | // should never be split. |
| 2078 | tempTouchState.split = isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2081 | // Update hover state. |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2082 | if (newTouchedWindowHandle != nullptr) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2083 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2084 | newHoverWindowHandle = nullptr; |
| 2085 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2086 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2087 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2088 | } |
| 2089 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2090 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2091 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2092 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2093 | // Process the foreground window first so that it is the first to receive the event. |
| 2094 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2095 | } |
| 2096 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2097 | if (newTouchedWindows.empty()) { |
| 2098 | ALOGI("Dropping event because there is no touchable window at (%d, %d) on display %d.", |
| 2099 | x, y, displayId); |
| 2100 | injectionResult = InputEventInjectionResult::FAILED; |
| 2101 | goto Failed; |
| 2102 | } |
| 2103 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2104 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
| 2105 | const WindowInfo& info = *windowHandle->getInfo(); |
| 2106 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 2107 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2108 | ALOGI("Not sending touch event to %s because it is paused", |
| 2109 | windowHandle->getName().c_str()); |
| 2110 | continue; |
| 2111 | } |
| 2112 | |
| 2113 | // Ensure the window has a connection and the connection is responsive |
| 2114 | const bool isResponsive = hasResponsiveConnectionLocked(*windowHandle); |
| 2115 | if (!isResponsive) { |
| 2116 | ALOGW("Not sending touch gesture to %s because it is not responsive", |
| 2117 | windowHandle->getName().c_str()); |
| 2118 | continue; |
| 2119 | } |
| 2120 | |
| 2121 | // Drop events that can't be trusted due to occlusion |
| 2122 | if (mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2123 | TouchOcclusionInfo occlusionInfo = |
| 2124 | computeTouchOcclusionInfoLocked(windowHandle, x, y); |
| 2125 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 2126 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2127 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2128 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2129 | ALOGD("%s", log.c_str()); |
| 2130 | } |
| 2131 | } |
| 2132 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
| 2133 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2134 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2135 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2136 | continue; |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | // Drop touch events if requested by input feature |
| 2142 | if (shouldDropInput(entry, windowHandle)) { |
| 2143 | continue; |
| 2144 | } |
| 2145 | |
| 2146 | // Set target flags. |
| 2147 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2148 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2149 | if (!info.isSpy()) { |
| 2150 | // There should only be one new foreground (non-spy) window at this location. |
| 2151 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2152 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2153 | |
| 2154 | if (isSplit) { |
| 2155 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2156 | } |
| 2157 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
| 2158 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2159 | } else if (isWindowObscuredLocked(windowHandle)) { |
| 2160 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2161 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2162 | |
| 2163 | // Update the temporary touch state. |
| 2164 | BitSet32 pointerIds; |
| 2165 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2166 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2167 | pointerIds.markBit(pointerId); |
| 2168 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2169 | |
| 2170 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2171 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2172 | } else { |
| 2173 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2174 | |
| 2175 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2176 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2177 | if (DEBUG_FOCUS) { |
| 2178 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2179 | "dropped the pointer down event in display %" PRId32, |
| 2180 | displayId); |
| 2181 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2182 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2183 | goto Failed; |
| 2184 | } |
| 2185 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2186 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2187 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2188 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2189 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2190 | tempTouchState.isSlippery()) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2191 | const int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2192 | 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] | 2193 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2194 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2195 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2196 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2197 | newTouchedWindowHandle = |
| 2198 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2199 | |
| 2200 | // Drop touch events if requested by input feature |
| 2201 | if (newTouchedWindowHandle != nullptr && |
| 2202 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2203 | newTouchedWindowHandle = nullptr; |
| 2204 | } |
| 2205 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2206 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2207 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2208 | if (DEBUG_FOCUS) { |
| 2209 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2210 | oldTouchedWindowHandle->getName().c_str(), |
| 2211 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2212 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2213 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2214 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2215 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2216 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2217 | |
| 2218 | // Make a slippery entrance into the new window. |
| 2219 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2220 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2221 | } |
| 2222 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2223 | int32_t targetFlags = |
| 2224 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2225 | if (isSplit) { |
| 2226 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2227 | } |
| 2228 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2229 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2230 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2231 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | BitSet32 pointerIds; |
| 2235 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2236 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2237 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2238 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2239 | } |
| 2240 | } |
| 2241 | } |
| 2242 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2243 | // Update dispatching for hover enter and exit. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2244 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2245 | // Let the previous window know that the hover sequence is over, unless we already did |
| 2246 | // it when dispatching it as is to newTouchedWindowHandle. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2247 | if (mLastHoverWindowHandle != nullptr && |
| 2248 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2249 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2250 | if (DEBUG_HOVER) { |
| 2251 | ALOGD("Sending hover exit event to window %s.", |
| 2252 | mLastHoverWindowHandle->getName().c_str()); |
| 2253 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2254 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2255 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2256 | } |
| 2257 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2258 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2259 | // when dispatching it as is to newTouchedWindowHandle. |
| 2260 | if (newHoverWindowHandle != nullptr && |
| 2261 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2262 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2263 | if (DEBUG_HOVER) { |
| 2264 | ALOGD("Sending hover enter event to window %s.", |
| 2265 | newHoverWindowHandle->getName().c_str()); |
| 2266 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2267 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2268 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2269 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2270 | } |
| 2271 | } |
| 2272 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2273 | // Ensure that we have at least one foreground or spy window. It's possible that we dropped some |
| 2274 | // of the touched windows we previously found if they became paused or unresponsive or were |
| 2275 | // removed. |
| 2276 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2277 | [](const TouchedWindow& touchedWindow) { |
| 2278 | return (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0 || |
| 2279 | touchedWindow.windowHandle->getInfo()->isSpy(); |
| 2280 | })) { |
| 2281 | ALOGI("Dropping event because there is no touched window on display %d to receive it.", |
| 2282 | displayId); |
| 2283 | injectionResult = InputEventInjectionResult::FAILED; |
| 2284 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2285 | } |
| 2286 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2287 | // Check permission to inject into all touched foreground windows. |
| 2288 | if (std::any_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2289 | [this, &entry](const TouchedWindow& touchedWindow) { |
| 2290 | return (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0 && |
| 2291 | !checkInjectionPermission(touchedWindow.windowHandle, |
| 2292 | entry.injectionState); |
| 2293 | })) { |
| 2294 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
| 2295 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2296 | goto Failed; |
| 2297 | } |
| 2298 | // Permission granted to inject into all touched foreground windows. |
| 2299 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2300 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2301 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2302 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2303 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2304 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2305 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2306 | if (foregroundWindowHandle) { |
| 2307 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2308 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2309 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2310 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2311 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2312 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2313 | InputTarget::FLAG_ZERO_COORDS, |
| 2314 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2315 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2316 | } |
| 2317 | } |
| 2318 | } |
| 2319 | } |
| 2320 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2321 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2322 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2323 | // of the touch gesture. |
| 2324 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2325 | // engine only supports touch events. We would need to add a mechanism similar |
| 2326 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2327 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2328 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2329 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 2330 | if (foregroundWindowHandle && |
| 2331 | foregroundWindowHandle->getInfo()->inputConfig.test( |
| 2332 | WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2333 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2334 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2335 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2336 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2337 | if (info->displayId == displayId && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 2338 | windowHandle->getInfo()->inputConfig.test( |
| 2339 | WindowInfo::InputConfig::IS_WALLPAPER)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2340 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2341 | .addOrUpdateWindow(windowHandle, |
| 2342 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2343 | InputTarget:: |
| 2344 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2345 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2346 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2347 | } |
| 2348 | } |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2353 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2354 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2355 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2356 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2357 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | // Drop the outside or hover touch windows since we will not care about them |
| 2361 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2362 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2363 | |
| 2364 | Failed: |
| 2365 | // Check injection permission once and for all. |
| 2366 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2367 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2368 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2369 | } else { |
| 2370 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2371 | } |
| 2372 | } |
| 2373 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2374 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2375 | return injectionResult; |
| 2376 | } |
| 2377 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2378 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2379 | if (!wrongDevice) { |
| 2380 | if (switchedDevice) { |
| 2381 | if (DEBUG_FOCUS) { |
| 2382 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2383 | } |
| 2384 | *outConflictingPointerActions = true; |
| 2385 | } |
| 2386 | |
| 2387 | if (isHoverAction) { |
| 2388 | // Started hovering, therefore no longer down. |
| 2389 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2390 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2391 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2392 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2393 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2394 | *outConflictingPointerActions = true; |
| 2395 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2396 | tempTouchState.reset(); |
| 2397 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2398 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2399 | tempTouchState.deviceId = entry.deviceId; |
| 2400 | tempTouchState.source = entry.source; |
| 2401 | tempTouchState.displayId = displayId; |
| 2402 | } |
| 2403 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2404 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2405 | // All pointers up or canceled. |
| 2406 | tempTouchState.reset(); |
| 2407 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2408 | // First pointer went down. |
| 2409 | if (oldState && oldState->down) { |
| 2410 | if (DEBUG_FOCUS) { |
| 2411 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2412 | } |
| 2413 | *outConflictingPointerActions = true; |
| 2414 | } |
| 2415 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2416 | // One pointer went up. |
| 2417 | if (isSplit) { |
| 2418 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2419 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2420 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2421 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2422 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2423 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2424 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2425 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2426 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2427 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2428 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2429 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2430 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2431 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2432 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2433 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2434 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2435 | // Save changes unless the action was scroll in which case the temporary touch |
| 2436 | // state was only valid for this one action. |
| 2437 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2438 | if (tempTouchState.displayId >= 0) { |
| 2439 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2440 | } else { |
| 2441 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2442 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2443 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2444 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2445 | // Update hover state. |
| 2446 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2447 | } |
| 2448 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2449 | return injectionResult; |
| 2450 | } |
| 2451 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2452 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2453 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2454 | // have an explicit reason to support it. |
| 2455 | constexpr bool isStylus = false; |
| 2456 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2457 | const sp<WindowInfoHandle> dropWindow = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2458 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2459 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2460 | if (dropWindow) { |
| 2461 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2462 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2463 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2464 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2465 | } |
| 2466 | mDragState.reset(); |
| 2467 | } |
| 2468 | |
| 2469 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2470 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2471 | return; |
| 2472 | } |
| 2473 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2474 | if (!mDragState->isStartDrag) { |
| 2475 | mDragState->isStartDrag = true; |
| 2476 | mDragState->isStylusButtonDownAtStart = |
| 2477 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2478 | } |
| 2479 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2480 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2481 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2482 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2483 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2484 | // Handle the special case : stylus button no longer pressed. |
| 2485 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2486 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2487 | finishDragAndDrop(entry.displayId, x, y); |
| 2488 | return; |
| 2489 | } |
| 2490 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2491 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until |
| 2492 | // we have an explicit reason to support it. |
| 2493 | constexpr bool isStylus = false; |
| 2494 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2495 | const sp<WindowInfoHandle> hoverWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2496 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2497 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2498 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2499 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2500 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2501 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2502 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2503 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2504 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2505 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2506 | } |
| 2507 | // enqueue drag location if needed. |
| 2508 | if (hoverWindowHandle != nullptr) { |
| 2509 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2510 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2511 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2512 | finishDragAndDrop(entry.displayId, x, y); |
| 2513 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2514 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2515 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2516 | } |
| 2517 | } |
| 2518 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2519 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2520 | int32_t targetFlags, BitSet32 pointerIds, |
| 2521 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2522 | std::vector<InputTarget>::iterator it = |
| 2523 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2524 | [&windowHandle](const InputTarget& inputTarget) { |
| 2525 | return inputTarget.inputChannel->getConnectionToken() == |
| 2526 | windowHandle->getToken(); |
| 2527 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2528 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2529 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2530 | |
| 2531 | if (it == inputTargets.end()) { |
| 2532 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2533 | std::shared_ptr<InputChannel> inputChannel = |
| 2534 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2535 | if (inputChannel == nullptr) { |
| 2536 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2537 | return; |
| 2538 | } |
| 2539 | inputTarget.inputChannel = inputChannel; |
| 2540 | inputTarget.flags = targetFlags; |
| 2541 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2542 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2543 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2544 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2545 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2546 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2547 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2548 | inputTargets.push_back(inputTarget); |
| 2549 | it = inputTargets.end() - 1; |
| 2550 | } |
| 2551 | |
| 2552 | ALOG_ASSERT(it->flags == targetFlags); |
| 2553 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2554 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2555 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2556 | } |
| 2557 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2558 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2559 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2560 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2561 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2562 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2563 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2564 | InputTarget target; |
| 2565 | target.inputChannel = monitor.inputChannel; |
| 2566 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2567 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2568 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2569 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2570 | target.setDefaultPointerTransform(target.displayTransform); |
| 2571 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2572 | } |
| 2573 | } |
| 2574 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2575 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2576 | const InjectionState* injectionState) { |
| 2577 | if (injectionState && |
| 2578 | (windowHandle == nullptr || |
| 2579 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2580 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2581 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2582 | 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] | 2583 | "owned by uid %d", |
| 2584 | injectionState->injectorPid, injectionState->injectorUid, |
| 2585 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2586 | } else { |
| 2587 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2588 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2589 | } |
| 2590 | return false; |
| 2591 | } |
| 2592 | return true; |
| 2593 | } |
| 2594 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2595 | /** |
| 2596 | * Indicate whether one window handle should be considered as obscuring |
| 2597 | * another window handle. We only check a few preconditions. Actually |
| 2598 | * checking the bounds is left to the caller. |
| 2599 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2600 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2601 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2602 | // Compare by token so cloned layers aren't counted |
| 2603 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2604 | return false; |
| 2605 | } |
| 2606 | auto info = windowHandle->getInfo(); |
| 2607 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 2608 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2609 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 2610 | } else if (otherInfo->alpha == 0 && |
| 2611 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2612 | // Those act as if they were invisible, so we don't need to flag them. |
| 2613 | // We do want to potentially flag touchable windows even if they have 0 |
| 2614 | // opacity, since they can consume touches and alter the effects of the |
| 2615 | // user interaction (eg. apps that rely on |
| 2616 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2617 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2618 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2619 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2620 | // If ownerUid is the same we don't generate occlusion events as there |
| 2621 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2622 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 2623 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2624 | return false; |
| 2625 | } else if (otherInfo->displayId != info->displayId) { |
| 2626 | return false; |
| 2627 | } |
| 2628 | return true; |
| 2629 | } |
| 2630 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2631 | /** |
| 2632 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2633 | * untrusted, one should check: |
| 2634 | * |
| 2635 | * 1. If result.hasBlockingOcclusion is true. |
| 2636 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2637 | * BLOCK_UNTRUSTED. |
| 2638 | * |
| 2639 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2640 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2641 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2642 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2643 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2644 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2645 | * |
| 2646 | * If neither of those is true, then it means the touch can be allowed. |
| 2647 | */ |
| 2648 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2649 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2650 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2651 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2652 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2653 | TouchOcclusionInfo info; |
| 2654 | info.hasBlockingOcclusion = false; |
| 2655 | info.obscuringOpacity = 0; |
| 2656 | info.obscuringUid = -1; |
| 2657 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2658 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2659 | if (windowHandle == otherHandle) { |
| 2660 | break; // All future windows are below us. Exit early. |
| 2661 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2662 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2663 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2664 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2665 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2666 | info.debugInfo.push_back( |
| 2667 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2668 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2669 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2670 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2671 | // window's touch occlusion mode |
| 2672 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2673 | info.hasBlockingOcclusion = true; |
| 2674 | info.obscuringUid = otherInfo->ownerUid; |
| 2675 | info.obscuringPackage = otherInfo->packageName; |
| 2676 | break; |
| 2677 | } |
| 2678 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2679 | uint32_t uid = otherInfo->ownerUid; |
| 2680 | float opacity = |
| 2681 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2682 | // Given windows A and B: |
| 2683 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2684 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2685 | opacityByUid[uid] = opacity; |
| 2686 | if (opacity > info.obscuringOpacity) { |
| 2687 | info.obscuringOpacity = opacity; |
| 2688 | info.obscuringUid = uid; |
| 2689 | info.obscuringPackage = otherInfo->packageName; |
| 2690 | } |
| 2691 | } |
| 2692 | } |
| 2693 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2694 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2695 | info.debugInfo.push_back( |
| 2696 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2697 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2698 | return info; |
| 2699 | } |
| 2700 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2701 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2702 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2703 | return StringPrintf(INDENT2 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 2704 | "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2705 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 2706 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, inputFeatures={%s}, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2707 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 2708 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 2709 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 2710 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 2711 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 2712 | info->name.c_str(), info->inputConfig.string().c_str(), |
| 2713 | info->inputFeatures.string().c_str(), toString(info->token != nullptr), |
| 2714 | info->applicationInfo.name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2715 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2718 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2719 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2720 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2721 | occlusionInfo.obscuringUid); |
| 2722 | return false; |
| 2723 | } |
| 2724 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2725 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2726 | "%.2f, maximum allowed = %.2f)", |
| 2727 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2728 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2729 | return false; |
| 2730 | } |
| 2731 | return true; |
| 2732 | } |
| 2733 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2734 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2735 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2736 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2737 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2738 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2739 | if (windowHandle == otherHandle) { |
| 2740 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2741 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2742 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2743 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2744 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2745 | return true; |
| 2746 | } |
| 2747 | } |
| 2748 | return false; |
| 2749 | } |
| 2750 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2751 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2752 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2753 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2754 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2755 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2756 | if (windowHandle == otherHandle) { |
| 2757 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2758 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2759 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2760 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2761 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2762 | return true; |
| 2763 | } |
| 2764 | } |
| 2765 | return false; |
| 2766 | } |
| 2767 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2768 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2769 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2770 | if (applicationHandle != nullptr) { |
| 2771 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2772 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2773 | } else { |
| 2774 | return applicationHandle->getName(); |
| 2775 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2776 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2777 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2778 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2779 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2780 | } |
| 2781 | } |
| 2782 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2783 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2784 | if (!isUserActivityEvent(eventEntry)) { |
| 2785 | // 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] | 2786 | return; |
| 2787 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2788 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2789 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2790 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2791 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
| 2792 | if (info->inputFeatures.test(WindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2793 | if (DEBUG_DISPATCH_CYCLE) { |
| 2794 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2795 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2796 | return; |
| 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2801 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2802 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2803 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2804 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2805 | return; |
| 2806 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2807 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2808 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2809 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2810 | } |
| 2811 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2812 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2813 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2814 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2815 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2816 | return; |
| 2817 | } |
| 2818 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2819 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2820 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2821 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2822 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2823 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2824 | break; |
| 2825 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2826 | } |
| 2827 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2828 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2829 | REQUIRES(mLock) { |
| 2830 | scoped_unlock unlock(mLock); |
| 2831 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2832 | }; |
| 2833 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2837 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2838 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2839 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2840 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2841 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2842 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2843 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2844 | ATRACE_NAME(message.c_str()); |
| 2845 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2846 | if (DEBUG_DISPATCH_CYCLE) { |
| 2847 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2848 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2849 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2850 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2851 | inputTarget.getPointerInfoString().c_str()); |
| 2852 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2853 | |
| 2854 | // Skip this event if the connection status is not normal. |
| 2855 | // 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] | 2856 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2857 | if (DEBUG_DISPATCH_CYCLE) { |
| 2858 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2859 | connection->getInputChannelName().c_str(), |
| 2860 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2861 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2862 | return; |
| 2863 | } |
| 2864 | |
| 2865 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2866 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2867 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2868 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2869 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2870 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2871 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2872 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2873 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2874 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2875 | if (!splitMotionEntry) { |
| 2876 | return; // split event was dropped |
| 2877 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2878 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2879 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2880 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2881 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2882 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2883 | if (DEBUG_FOCUS) { |
| 2884 | ALOGD("channel '%s' ~ Split motion event.", |
| 2885 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2886 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2887 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2888 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2889 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2890 | return; |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2895 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2896 | } |
| 2897 | |
| 2898 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2899 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2900 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2901 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2902 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2903 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2904 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2905 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2906 | ATRACE_NAME(message.c_str()); |
| 2907 | } |
| 2908 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2909 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2910 | |
| 2911 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2912 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2913 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2914 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2915 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2916 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2917 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2918 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2919 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2920 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2921 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2922 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2923 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2924 | |
| 2925 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2926 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2927 | startDispatchCycleLocked(currentTime, connection); |
| 2928 | } |
| 2929 | } |
| 2930 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2931 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2932 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2933 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2934 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2935 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2936 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2937 | connection->getInputChannelName().c_str(), |
| 2938 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2939 | ATRACE_NAME(message.c_str()); |
| 2940 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2941 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2942 | if (!(inputTargetFlags & dispatchMode)) { |
| 2943 | return; |
| 2944 | } |
| 2945 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2946 | |
| 2947 | // This is a new event. |
| 2948 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2949 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2950 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2951 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2952 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2953 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2954 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2955 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2956 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2957 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2958 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2959 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2960 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2961 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2962 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2963 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2964 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2965 | if (DEBUG_DISPATCH_CYCLE) { |
| 2966 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 2967 | "event", |
| 2968 | connection->getInputChannelName().c_str()); |
| 2969 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2970 | return; // skip the inconsistent event |
| 2971 | } |
| 2972 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2973 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2974 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2975 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2976 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2977 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2978 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2979 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2980 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2981 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2982 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2983 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2984 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2985 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2986 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2987 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2988 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2989 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2990 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2991 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2992 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2993 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2994 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2995 | } |
| 2996 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2997 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2998 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2999 | if (DEBUG_DISPATCH_CYCLE) { |
| 3000 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3001 | "enter event", |
| 3002 | connection->getInputChannelName().c_str()); |
| 3003 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3004 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3005 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3006 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3007 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3008 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3009 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3010 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3011 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3012 | } |
| 3013 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3014 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3015 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3016 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3017 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3018 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3019 | if (DEBUG_DISPATCH_CYCLE) { |
| 3020 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3021 | "event", |
| 3022 | connection->getInputChannelName().c_str()); |
| 3023 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3024 | return; // skip the inconsistent event |
| 3025 | } |
| 3026 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3027 | dispatchEntry->resolvedEventId = |
| 3028 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3029 | ? mIdGenerator.nextId() |
| 3030 | : motionEntry.id; |
| 3031 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3032 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3033 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3034 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3035 | ATRACE_NAME(message.c_str()); |
| 3036 | } |
| 3037 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3038 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3039 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3040 | // Skip reporting pointer down outside focus to the policy. |
| 3041 | break; |
| 3042 | } |
| 3043 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3044 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3045 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3046 | |
| 3047 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3048 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3049 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3050 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3051 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3052 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3053 | break; |
| 3054 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3055 | case EventEntry::Type::SENSOR: { |
| 3056 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3057 | break; |
| 3058 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3059 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3060 | case EventEntry::Type::DEVICE_RESET: { |
| 3061 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3062 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3063 | break; |
| 3064 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | // Remember that we are waiting for this dispatch to complete. |
| 3068 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3069 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3073 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3074 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3075 | } |
| 3076 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3077 | /** |
| 3078 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3079 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3080 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3081 | * We will see both launcher and wallpaper in that list. |
| 3082 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3083 | * until the set of interacted connections changes. |
| 3084 | * |
| 3085 | * The following items are skipped, to reduce the logspam: |
| 3086 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3087 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3088 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3089 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3090 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3091 | */ |
| 3092 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3093 | const std::vector<InputTarget>& targets) { |
| 3094 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3095 | if (entry.type == EventEntry::Type::KEY) { |
| 3096 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3097 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3098 | return; |
| 3099 | } |
| 3100 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3101 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3102 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3103 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3104 | return; |
| 3105 | } |
| 3106 | } else { |
| 3107 | return; // Not a key or a motion |
| 3108 | } |
| 3109 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3110 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3111 | std::vector<sp<Connection>> newConnections; |
| 3112 | for (const InputTarget& target : targets) { |
| 3113 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3114 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3115 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3116 | } |
| 3117 | |
| 3118 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3119 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3120 | if (connection == nullptr) { |
| 3121 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3122 | } |
| 3123 | newConnectionTokens.insert(std::move(token)); |
| 3124 | newConnections.emplace_back(connection); |
| 3125 | } |
| 3126 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3127 | return; // no change |
| 3128 | } |
| 3129 | mInteractionConnectionTokens = newConnectionTokens; |
| 3130 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3131 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3132 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3133 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3134 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3135 | std::string message = "Interaction with: " + targetList; |
| 3136 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3137 | message += "<none>"; |
| 3138 | } |
| 3139 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3140 | } |
| 3141 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3142 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3143 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3144 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3145 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3146 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3147 | return; |
| 3148 | } |
| 3149 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3150 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3151 | if (focusedToken == token) { |
| 3152 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3153 | return; |
| 3154 | } |
| 3155 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3156 | auto command = [this, token]() REQUIRES(mLock) { |
| 3157 | scoped_unlock unlock(mLock); |
| 3158 | mPolicy->onPointerDownOutsideFocus(token); |
| 3159 | }; |
| 3160 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3164 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3165 | if (ATRACE_ENABLED()) { |
| 3166 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3167 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3168 | ATRACE_NAME(message.c_str()); |
| 3169 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3170 | if (DEBUG_DISPATCH_CYCLE) { |
| 3171 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3172 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3173 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3174 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3175 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3176 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3177 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3178 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3179 | |
| 3180 | // Publish the event. |
| 3181 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3182 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3183 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3184 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3185 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3186 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3187 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3188 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3189 | status = connection->inputPublisher |
| 3190 | .publishKeyEvent(dispatchEntry->seq, |
| 3191 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3192 | keyEntry.source, keyEntry.displayId, |
| 3193 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3194 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3195 | keyEntry.scanCode, keyEntry.metaState, |
| 3196 | keyEntry.repeatCount, keyEntry.downTime, |
| 3197 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3198 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3199 | } |
| 3200 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3201 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3202 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3203 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3204 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3205 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3206 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3207 | // 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] | 3208 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3209 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3210 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3211 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3212 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3213 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3214 | // Don't apply window scale here since we don't want scale to affect raw |
| 3215 | // coordinates. The scale will be sent back to the client and applied |
| 3216 | // later when requesting relative coordinates. |
| 3217 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3218 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3219 | } |
| 3220 | usingCoords = scaledCoords; |
| 3221 | } |
| 3222 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3223 | // We don't want the dispatch target to know. |
| 3224 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3225 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3226 | scaledCoords[i].clear(); |
| 3227 | } |
| 3228 | usingCoords = scaledCoords; |
| 3229 | } |
| 3230 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3231 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3232 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3233 | |
| 3234 | // Publish the motion event. |
| 3235 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3236 | .publishMotionEvent(dispatchEntry->seq, |
| 3237 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3238 | motionEntry.deviceId, motionEntry.source, |
| 3239 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3240 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3241 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3242 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3243 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3244 | motionEntry.buttonState, |
| 3245 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3246 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3247 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3248 | motionEntry.xCursorPosition, |
| 3249 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3250 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3251 | motionEntry.downTime, motionEntry.eventTime, |
| 3252 | motionEntry.pointerCount, |
| 3253 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3254 | break; |
| 3255 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3256 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3257 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3258 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3259 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3260 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3261 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3262 | break; |
| 3263 | } |
| 3264 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3265 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3266 | const TouchModeEntry& touchModeEntry = |
| 3267 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3268 | status = connection->inputPublisher |
| 3269 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3270 | touchModeEntry.inTouchMode); |
| 3271 | |
| 3272 | break; |
| 3273 | } |
| 3274 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3275 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3276 | const auto& captureEntry = |
| 3277 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3278 | status = connection->inputPublisher |
| 3279 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3280 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3281 | break; |
| 3282 | } |
| 3283 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3284 | case EventEntry::Type::DRAG: { |
| 3285 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3286 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3287 | dragEntry.id, dragEntry.x, |
| 3288 | dragEntry.y, |
| 3289 | dragEntry.isExiting); |
| 3290 | break; |
| 3291 | } |
| 3292 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3293 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3294 | case EventEntry::Type::DEVICE_RESET: |
| 3295 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3296 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3297 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3298 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3299 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | // Check the result. |
| 3303 | if (status) { |
| 3304 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3305 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3306 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3307 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3308 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3309 | "event to it, status=%s(%d)", |
| 3310 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3311 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3312 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3313 | } else { |
| 3314 | // Pipe is full and we are waiting for the app to finish process some events |
| 3315 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3316 | if (DEBUG_DISPATCH_CYCLE) { |
| 3317 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3318 | "waiting for the application to catch up", |
| 3319 | connection->getInputChannelName().c_str()); |
| 3320 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3321 | } |
| 3322 | } else { |
| 3323 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3324 | "status=%s(%d)", |
| 3325 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3326 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3327 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3328 | } |
| 3329 | return; |
| 3330 | } |
| 3331 | |
| 3332 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3333 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3334 | connection->outboundQueue.end(), |
| 3335 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3336 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3337 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3338 | if (connection->responsive) { |
| 3339 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3340 | connection->inputChannel->getConnectionToken()); |
| 3341 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3342 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3343 | } |
| 3344 | } |
| 3345 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3346 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3347 | size_t size; |
| 3348 | switch (event.type) { |
| 3349 | case VerifiedInputEvent::Type::KEY: { |
| 3350 | size = sizeof(VerifiedKeyEvent); |
| 3351 | break; |
| 3352 | } |
| 3353 | case VerifiedInputEvent::Type::MOTION: { |
| 3354 | size = sizeof(VerifiedMotionEvent); |
| 3355 | break; |
| 3356 | } |
| 3357 | } |
| 3358 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3359 | return mHmacKeyManager.sign(start, size); |
| 3360 | } |
| 3361 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3362 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3363 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3364 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3365 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3366 | // Only sign events up and down events as the purely move events |
| 3367 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3368 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3369 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3370 | |
| 3371 | VerifiedMotionEvent verifiedEvent = |
| 3372 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3373 | verifiedEvent.actionMasked = actionMasked; |
| 3374 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3375 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3376 | } |
| 3377 | |
| 3378 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3379 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3380 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3381 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3382 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3383 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3384 | } |
| 3385 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3386 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3387 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3388 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3389 | if (DEBUG_DISPATCH_CYCLE) { |
| 3390 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3391 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3392 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3393 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3394 | if (connection->status == Connection::Status::BROKEN || |
| 3395 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3396 | return; |
| 3397 | } |
| 3398 | |
| 3399 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3400 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3401 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3402 | }; |
| 3403 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3407 | const sp<Connection>& connection, |
| 3408 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3409 | if (DEBUG_DISPATCH_CYCLE) { |
| 3410 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3411 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3412 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3413 | |
| 3414 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3415 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3416 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3417 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3418 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3419 | |
| 3420 | // The connection appears to be unrecoverably broken. |
| 3421 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3422 | if (connection->status == Connection::Status::NORMAL) { |
| 3423 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3424 | |
| 3425 | if (notify) { |
| 3426 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3427 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3428 | connection->getInputChannelName().c_str()); |
| 3429 | |
| 3430 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3431 | scoped_unlock unlock(mLock); |
| 3432 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3433 | }; |
| 3434 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3435 | } |
| 3436 | } |
| 3437 | } |
| 3438 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3439 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3440 | while (!queue.empty()) { |
| 3441 | DispatchEntry* dispatchEntry = queue.front(); |
| 3442 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3443 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3444 | } |
| 3445 | } |
| 3446 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3447 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3448 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3449 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3450 | } |
| 3451 | delete dispatchEntry; |
| 3452 | } |
| 3453 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3454 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3455 | std::scoped_lock _l(mLock); |
| 3456 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3457 | if (connection == nullptr) { |
| 3458 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3459 | connectionToken.get(), events); |
| 3460 | return 0; // remove the callback |
| 3461 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3462 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3463 | bool notify; |
| 3464 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3465 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3466 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3467 | "events=0x%x", |
| 3468 | connection->getInputChannelName().c_str(), events); |
| 3469 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3470 | } |
| 3471 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3472 | nsecs_t currentTime = now(); |
| 3473 | bool gotOne = false; |
| 3474 | status_t status = OK; |
| 3475 | for (;;) { |
| 3476 | Result<InputPublisher::ConsumerResponse> result = |
| 3477 | connection->inputPublisher.receiveConsumerResponse(); |
| 3478 | if (!result.ok()) { |
| 3479 | status = result.error().code(); |
| 3480 | break; |
| 3481 | } |
| 3482 | |
| 3483 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3484 | const InputPublisher::Finished& finish = |
| 3485 | std::get<InputPublisher::Finished>(*result); |
| 3486 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3487 | finish.consumeTime); |
| 3488 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3489 | if (shouldReportMetricsForConnection(*connection)) { |
| 3490 | const InputPublisher::Timeline& timeline = |
| 3491 | std::get<InputPublisher::Timeline>(*result); |
| 3492 | mLatencyTracker |
| 3493 | .trackGraphicsLatency(timeline.inputEventId, |
| 3494 | connection->inputChannel->getConnectionToken(), |
| 3495 | std::move(timeline.graphicsTimeline)); |
| 3496 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3497 | } |
| 3498 | gotOne = true; |
| 3499 | } |
| 3500 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3501 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3502 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3503 | return 1; |
| 3504 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3505 | } |
| 3506 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3507 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3508 | if (notify) { |
| 3509 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3510 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3511 | status); |
| 3512 | } |
| 3513 | } else { |
| 3514 | // Monitor channels are never explicitly unregistered. |
| 3515 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3516 | const bool stillHaveWindowHandle = |
| 3517 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3518 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3519 | if (notify) { |
| 3520 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3521 | connection->getInputChannelName().c_str(), events); |
| 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | // Remove the channel. |
| 3526 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3527 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3528 | } |
| 3529 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3530 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3531 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3532 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3533 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3534 | } |
| 3535 | } |
| 3536 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3537 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3538 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3539 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3540 | for (const Monitor& monitor : monitors) { |
| 3541 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3542 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3543 | } |
| 3544 | } |
| 3545 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3546 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3547 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3548 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3549 | if (connection == nullptr) { |
| 3550 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3551 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3552 | |
| 3553 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3557 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3558 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3559 | return; |
| 3560 | } |
| 3561 | |
| 3562 | nsecs_t currentTime = now(); |
| 3563 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3564 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3565 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3566 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3567 | if (cancelationEvents.empty()) { |
| 3568 | return; |
| 3569 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3570 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3571 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3572 | "with reality: %s, mode=%d.", |
| 3573 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3574 | options.mode); |
| 3575 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3576 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3577 | std::string reason = std::string("reason=").append(options.reason); |
| 3578 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3579 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3580 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3581 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3582 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3583 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3584 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3585 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3586 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3587 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3588 | } |
| 3589 | target.inputChannel = connection->inputChannel; |
| 3590 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3591 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3592 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3593 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3594 | switch (cancelationEventEntry->type) { |
| 3595 | case EventEntry::Type::KEY: { |
| 3596 | logOutboundKeyDetails("cancel - ", |
| 3597 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3598 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3599 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3600 | case EventEntry::Type::MOTION: { |
| 3601 | logOutboundMotionDetails("cancel - ", |
| 3602 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3603 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3604 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3605 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3606 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3607 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3608 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3609 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3610 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3611 | break; |
| 3612 | } |
| 3613 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3614 | case EventEntry::Type::DEVICE_RESET: |
| 3615 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3616 | 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] | 3617 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3618 | break; |
| 3619 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3620 | } |
| 3621 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3622 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3623 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3624 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3625 | |
| 3626 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3627 | } |
| 3628 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3629 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3630 | const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3631 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3632 | return; |
| 3633 | } |
| 3634 | |
| 3635 | nsecs_t currentTime = now(); |
| 3636 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3637 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3638 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3639 | |
| 3640 | if (downEvents.empty()) { |
| 3641 | return; |
| 3642 | } |
| 3643 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3644 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3645 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3646 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3647 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3648 | |
| 3649 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3650 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3651 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3652 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3653 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3654 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3655 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3656 | } |
| 3657 | target.inputChannel = connection->inputChannel; |
| 3658 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3659 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3660 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3661 | switch (downEventEntry->type) { |
| 3662 | case EventEntry::Type::MOTION: { |
| 3663 | logOutboundMotionDetails("down - ", |
| 3664 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3665 | break; |
| 3666 | } |
| 3667 | |
| 3668 | case EventEntry::Type::KEY: |
| 3669 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3670 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3671 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3672 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3673 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3674 | case EventEntry::Type::SENSOR: |
| 3675 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3676 | 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] | 3677 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3678 | break; |
| 3679 | } |
| 3680 | } |
| 3681 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3682 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3683 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | startDispatchCycleLocked(currentTime, connection); |
| 3687 | } |
| 3688 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3689 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3690 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3691 | ALOG_ASSERT(pointerIds.value != 0); |
| 3692 | |
| 3693 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3694 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3695 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3696 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3697 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3698 | uint32_t splitPointerCount = 0; |
| 3699 | |
| 3700 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3701 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3702 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3703 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3704 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3705 | if (pointerIds.hasBit(pointerId)) { |
| 3706 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3707 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3708 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3709 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3710 | splitPointerCount += 1; |
| 3711 | } |
| 3712 | } |
| 3713 | |
| 3714 | if (splitPointerCount != pointerIds.count()) { |
| 3715 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3716 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3717 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3718 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3719 | // in this way. |
| 3720 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3721 | "we expected there to be %d pointers. This probably means we received " |
| 3722 | "a broken sequence of pointer ids from the input device.", |
| 3723 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3724 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3725 | } |
| 3726 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3727 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3728 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3729 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3730 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3731 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3732 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3733 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3734 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3735 | if (pointerIds.hasBit(pointerId)) { |
| 3736 | if (pointerIds.count() == 1) { |
| 3737 | // The first/last pointer went down/up. |
| 3738 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3739 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3740 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3741 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3742 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3743 | } else { |
| 3744 | // A secondary pointer went down/up. |
| 3745 | uint32_t splitPointerIndex = 0; |
| 3746 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3747 | splitPointerIndex += 1; |
| 3748 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3749 | action = maskedAction | |
| 3750 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3751 | } |
| 3752 | } else { |
| 3753 | // An unrelated pointer changed. |
| 3754 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3755 | } |
| 3756 | } |
| 3757 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3758 | int32_t newId = mIdGenerator.nextId(); |
| 3759 | if (ATRACE_ENABLED()) { |
| 3760 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3761 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3762 | originalMotionEntry.id, newId); |
| 3763 | ATRACE_NAME(message.c_str()); |
| 3764 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3765 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3766 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3767 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3768 | originalMotionEntry.displayId, |
| 3769 | originalMotionEntry.policyFlags, action, |
| 3770 | originalMotionEntry.actionButton, |
| 3771 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3772 | originalMotionEntry.buttonState, |
| 3773 | originalMotionEntry.classification, |
| 3774 | originalMotionEntry.edgeFlags, |
| 3775 | originalMotionEntry.xPrecision, |
| 3776 | originalMotionEntry.yPrecision, |
| 3777 | originalMotionEntry.xCursorPosition, |
| 3778 | originalMotionEntry.yCursorPosition, |
| 3779 | originalMotionEntry.downTime, splitPointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3780 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3781 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3782 | if (originalMotionEntry.injectionState) { |
| 3783 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3784 | splitMotionEntry->injectionState->refCount += 1; |
| 3785 | } |
| 3786 | |
| 3787 | return splitMotionEntry; |
| 3788 | } |
| 3789 | |
| 3790 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3791 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3792 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3793 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3794 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3795 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3796 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3797 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3798 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3799 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3800 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3801 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3802 | } // release lock |
| 3803 | |
| 3804 | if (needWake) { |
| 3805 | mLooper->wake(); |
| 3806 | } |
| 3807 | } |
| 3808 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3809 | /** |
| 3810 | * If one of the meta shortcuts is detected, process them here: |
| 3811 | * Meta + Backspace -> generate BACK |
| 3812 | * Meta + Enter -> generate HOME |
| 3813 | * This will potentially overwrite keyCode and metaState. |
| 3814 | */ |
| 3815 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3816 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3817 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3818 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3819 | if (keyCode == AKEYCODE_DEL) { |
| 3820 | newKeyCode = AKEYCODE_BACK; |
| 3821 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3822 | newKeyCode = AKEYCODE_HOME; |
| 3823 | } |
| 3824 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3825 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3826 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3827 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3828 | keyCode = newKeyCode; |
| 3829 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3830 | } |
| 3831 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3832 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3833 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3834 | // 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] | 3835 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3836 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3837 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3838 | if (replacementIt != mReplacedKeys.end()) { |
| 3839 | keyCode = replacementIt->second; |
| 3840 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3841 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3842 | } |
| 3843 | } |
| 3844 | } |
| 3845 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3846 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3847 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3848 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3849 | "policyFlags=0x%x, action=0x%x, " |
| 3850 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3851 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3852 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3853 | args->downTime); |
| 3854 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3855 | if (!validateKeyEvent(args->action)) { |
| 3856 | return; |
| 3857 | } |
| 3858 | |
| 3859 | uint32_t policyFlags = args->policyFlags; |
| 3860 | int32_t flags = args->flags; |
| 3861 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3862 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3863 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3864 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3865 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3866 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3867 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3868 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3869 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3870 | metaState |= AMETA_FUNCTION_ON; |
| 3871 | } |
| 3872 | |
| 3873 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3874 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3875 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3876 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3877 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3878 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3879 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3880 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3881 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3882 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3883 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3884 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3885 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3886 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3887 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3888 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3889 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3890 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3891 | { // acquire lock |
| 3892 | mLock.lock(); |
| 3893 | |
| 3894 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3895 | mLock.unlock(); |
| 3896 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3897 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3898 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3899 | return; // event was consumed by the filter |
| 3900 | } |
| 3901 | |
| 3902 | mLock.lock(); |
| 3903 | } |
| 3904 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3905 | std::unique_ptr<KeyEntry> newEntry = |
| 3906 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3907 | args->displayId, policyFlags, args->action, flags, |
| 3908 | keyCode, args->scanCode, metaState, repeatCount, |
| 3909 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3910 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3911 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3912 | mLock.unlock(); |
| 3913 | } // release lock |
| 3914 | |
| 3915 | if (needWake) { |
| 3916 | mLooper->wake(); |
| 3917 | } |
| 3918 | } |
| 3919 | |
| 3920 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3921 | return mInputFilterEnabled; |
| 3922 | } |
| 3923 | |
| 3924 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3925 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3926 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3927 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 3928 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3929 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 3930 | "yCursorPosition=%f, downTime=%" PRId64, |
| 3931 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3932 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3933 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3934 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 3935 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3936 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 3937 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3938 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3939 | "orientation=%f", |
| 3940 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3941 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3942 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3943 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3944 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3945 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3946 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3947 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3948 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3949 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 3950 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3951 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3952 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3953 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3954 | return; |
| 3955 | } |
| 3956 | |
| 3957 | uint32_t policyFlags = args->policyFlags; |
| 3958 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3959 | |
| 3960 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3961 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3962 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3963 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3964 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3965 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3966 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3967 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3968 | { // acquire lock |
| 3969 | mLock.lock(); |
| 3970 | |
| 3971 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3972 | ui::Transform displayTransform; |
| 3973 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 3974 | displayTransform = it->second.transform; |
| 3975 | } |
| 3976 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3977 | mLock.unlock(); |
| 3978 | |
| 3979 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3980 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3981 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3982 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3983 | displayTransform, args->xPrecision, args->yPrecision, |
| 3984 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3985 | args->downTime, args->eventTime, args->pointerCount, |
| 3986 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3987 | |
| 3988 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3989 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3990 | return; // event was consumed by the filter |
| 3991 | } |
| 3992 | |
| 3993 | mLock.lock(); |
| 3994 | } |
| 3995 | |
| 3996 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3997 | std::unique_ptr<MotionEntry> newEntry = |
| 3998 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 3999 | args->source, args->displayId, policyFlags, |
| 4000 | args->action, args->actionButton, args->flags, |
| 4001 | args->metaState, args->buttonState, |
| 4002 | args->classification, args->edgeFlags, |
| 4003 | args->xPrecision, args->yPrecision, |
| 4004 | args->xCursorPosition, args->yCursorPosition, |
| 4005 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4006 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4007 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4008 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4009 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4010 | !mInputFilterEnabled) { |
| 4011 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4012 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4013 | } |
| 4014 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4015 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4016 | mLock.unlock(); |
| 4017 | } // release lock |
| 4018 | |
| 4019 | if (needWake) { |
| 4020 | mLooper->wake(); |
| 4021 | } |
| 4022 | } |
| 4023 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4024 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4025 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4026 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4027 | " sensorType=%s", |
| 4028 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4029 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4030 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4031 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4032 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4033 | { // acquire lock |
| 4034 | mLock.lock(); |
| 4035 | |
| 4036 | // Just enqueue a new sensor event. |
| 4037 | std::unique_ptr<SensorEntry> newEntry = |
| 4038 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4039 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4040 | args->sensorType, args->accuracy, |
| 4041 | args->accuracyChanged, args->values); |
| 4042 | |
| 4043 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4044 | mLock.unlock(); |
| 4045 | } // release lock |
| 4046 | |
| 4047 | if (needWake) { |
| 4048 | mLooper->wake(); |
| 4049 | } |
| 4050 | } |
| 4051 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4052 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4053 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4054 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4055 | args->deviceId, args->isOn); |
| 4056 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4057 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4058 | } |
| 4059 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4060 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4061 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4065 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4066 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4067 | "switchMask=0x%08x", |
| 4068 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4069 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4070 | |
| 4071 | uint32_t policyFlags = args->policyFlags; |
| 4072 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4073 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4074 | } |
| 4075 | |
| 4076 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4077 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4078 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4079 | args->deviceId); |
| 4080 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4081 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4082 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4083 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4084 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4085 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4086 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4087 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4088 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4089 | } // release lock |
| 4090 | |
| 4091 | if (needWake) { |
| 4092 | mLooper->wake(); |
| 4093 | } |
| 4094 | } |
| 4095 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4096 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4097 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4098 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4099 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4100 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4101 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4102 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4103 | { // acquire lock |
| 4104 | std::scoped_lock _l(mLock); |
| 4105 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4106 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4107 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4108 | } // release lock |
| 4109 | |
| 4110 | if (needWake) { |
| 4111 | mLooper->wake(); |
| 4112 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4113 | } |
| 4114 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4115 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4116 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4117 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4118 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4119 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 4120 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4121 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
| 4122 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4123 | 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] | 4124 | |
| 4125 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4126 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4127 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4128 | } |
| 4129 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4130 | // 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] | 4131 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4132 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4133 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4134 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4135 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4136 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4137 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4138 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4141 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4142 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4143 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4144 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4145 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4146 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4147 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4148 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4149 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4150 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4151 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4152 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4153 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4154 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4155 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4156 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4157 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4158 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4159 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4160 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4161 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4162 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4163 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4164 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4165 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4166 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4167 | |
| 4168 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4169 | android::base::Timer t; |
| 4170 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4171 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4172 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4173 | std::to_string(t.duration().count()).c_str()); |
| 4174 | } |
| 4175 | } |
| 4176 | |
| 4177 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4178 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4179 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4180 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4181 | incomingKey.getDisplayId(), policyFlags, action, |
| 4182 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4183 | incomingKey.getRepeatCount(), |
| 4184 | incomingKey.getDownTime()); |
| 4185 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4186 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4187 | } |
| 4188 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4189 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4190 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4191 | const int32_t action = motionEvent.getAction(); |
| 4192 | const bool isPointerEvent = |
| 4193 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4194 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4195 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4196 | ? ADISPLAY_ID_DEFAULT |
| 4197 | : event->getDisplayId(); |
| 4198 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4199 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4200 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4201 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4202 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4203 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4204 | } |
| 4205 | |
| 4206 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4207 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4208 | android::base::Timer t; |
| 4209 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4210 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4211 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4212 | std::to_string(t.duration().count()).c_str()); |
| 4213 | } |
| 4214 | } |
| 4215 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4216 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4217 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4218 | } |
| 4219 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4220 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4221 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4222 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4223 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4224 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4225 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4226 | displayId, policyFlags, action, actionButton, |
| 4227 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4228 | motionEvent.getButtonState(), |
| 4229 | motionEvent.getClassification(), |
| 4230 | motionEvent.getEdgeFlags(), |
| 4231 | motionEvent.getXPrecision(), |
| 4232 | motionEvent.getYPrecision(), |
| 4233 | motionEvent.getRawXCursorPosition(), |
| 4234 | motionEvent.getRawYCursorPosition(), |
| 4235 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4236 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4237 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4238 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4239 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4240 | sampleEventTimes += 1; |
| 4241 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4242 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4243 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4244 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4245 | displayId, policyFlags, action, actionButton, |
| 4246 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4247 | motionEvent.getButtonState(), |
| 4248 | motionEvent.getClassification(), |
| 4249 | motionEvent.getEdgeFlags(), |
| 4250 | motionEvent.getXPrecision(), |
| 4251 | motionEvent.getYPrecision(), |
| 4252 | motionEvent.getRawXCursorPosition(), |
| 4253 | motionEvent.getRawYCursorPosition(), |
| 4254 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4255 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4256 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4257 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4258 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4259 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4260 | } |
| 4261 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4262 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4263 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4264 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4265 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4266 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4270 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4271 | injectionState->injectionIsAsync = true; |
| 4272 | } |
| 4273 | |
| 4274 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4275 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4276 | |
| 4277 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4278 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4279 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4280 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | mLock.unlock(); |
| 4284 | |
| 4285 | if (needWake) { |
| 4286 | mLooper->wake(); |
| 4287 | } |
| 4288 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4289 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4290 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4291 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4292 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4293 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4294 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4295 | } else { |
| 4296 | for (;;) { |
| 4297 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4298 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4299 | break; |
| 4300 | } |
| 4301 | |
| 4302 | nsecs_t remainingTimeout = endTime - now(); |
| 4303 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4304 | if (DEBUG_INJECTION) { |
| 4305 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4306 | "to become available."); |
| 4307 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4308 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4309 | break; |
| 4310 | } |
| 4311 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4312 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4313 | } |
| 4314 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4315 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4316 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4317 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4318 | if (DEBUG_INJECTION) { |
| 4319 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4320 | injectionState->pendingForegroundDispatches); |
| 4321 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4322 | nsecs_t remainingTimeout = endTime - now(); |
| 4323 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4324 | if (DEBUG_INJECTION) { |
| 4325 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4326 | "dispatches to finish."); |
| 4327 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4328 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4329 | break; |
| 4330 | } |
| 4331 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4332 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4333 | } |
| 4334 | } |
| 4335 | } |
| 4336 | |
| 4337 | injectionState->release(); |
| 4338 | } // release lock |
| 4339 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4340 | if (DEBUG_INJECTION) { |
| 4341 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
| 4342 | injectionResult, injectorPid, injectorUid); |
| 4343 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4344 | |
| 4345 | return injectionResult; |
| 4346 | } |
| 4347 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4348 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4349 | std::array<uint8_t, 32> calculatedHmac; |
| 4350 | std::unique_ptr<VerifiedInputEvent> result; |
| 4351 | switch (event.getType()) { |
| 4352 | case AINPUT_EVENT_TYPE_KEY: { |
| 4353 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4354 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4355 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4356 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4357 | break; |
| 4358 | } |
| 4359 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4360 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4361 | VerifiedMotionEvent verifiedMotionEvent = |
| 4362 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4363 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4364 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4365 | break; |
| 4366 | } |
| 4367 | default: { |
| 4368 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4369 | return nullptr; |
| 4370 | } |
| 4371 | } |
| 4372 | if (calculatedHmac == INVALID_HMAC) { |
| 4373 | return nullptr; |
| 4374 | } |
| 4375 | if (calculatedHmac != event.getHmac()) { |
| 4376 | return nullptr; |
| 4377 | } |
| 4378 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4379 | } |
| 4380 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4381 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4382 | return injectorUid == 0 || |
| 4383 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4384 | } |
| 4385 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4386 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4387 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4388 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4389 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4390 | if (DEBUG_INJECTION) { |
| 4391 | ALOGD("Setting input event injection result to %d. " |
| 4392 | "injectorPid=%d, injectorUid=%d", |
| 4393 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
| 4394 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4395 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4396 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4397 | // Log the outcome since the injector did not wait for the injection result. |
| 4398 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4399 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4400 | ALOGV("Asynchronous input event injection succeeded."); |
| 4401 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4402 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4403 | ALOGW("Asynchronous input event injection failed."); |
| 4404 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4405 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4406 | ALOGW("Asynchronous input event injection permission denied."); |
| 4407 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4408 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4409 | ALOGW("Asynchronous input event injection timed out."); |
| 4410 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4411 | case InputEventInjectionResult::PENDING: |
| 4412 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4413 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4414 | } |
| 4415 | } |
| 4416 | |
| 4417 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4418 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4419 | } |
| 4420 | } |
| 4421 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4422 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4423 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4424 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4425 | // display space, so we need to transform the injected events accordingly. |
| 4426 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4427 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4428 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4429 | |
| 4430 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 4431 | PointerCoords& pc = entry.pointerCoords[i]; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4432 | // Make a copy of the injected coords. We cannot change them in place because some of them |
| 4433 | // are interdependent (for example, X coordinate might depend on the Y coordinate). |
| 4434 | PointerCoords injectedCoords = entry.pointerCoords[i]; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4435 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4436 | BitSet64 bits(injectedCoords.bits); |
| 4437 | while (!bits.isEmpty()) { |
| 4438 | const auto axis = static_cast<int32_t>(bits.clearFirstMarkedBit()); |
| 4439 | const float value = |
| 4440 | MotionEvent::calculateTransformedAxisValue(axis, entry.source, |
| 4441 | transformToDisplay, injectedCoords); |
| 4442 | pc.setAxisValue(axis, value); |
| 4443 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4444 | } |
| 4445 | } |
| 4446 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4447 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4448 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4449 | if (injectionState) { |
| 4450 | injectionState->pendingForegroundDispatches += 1; |
| 4451 | } |
| 4452 | } |
| 4453 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4454 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4455 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4456 | if (injectionState) { |
| 4457 | injectionState->pendingForegroundDispatches -= 1; |
| 4458 | |
| 4459 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4460 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4461 | } |
| 4462 | } |
| 4463 | } |
| 4464 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4465 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4466 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4467 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4468 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4469 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4470 | } |
| 4471 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4472 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4473 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4474 | if (windowHandleToken == nullptr) { |
| 4475 | return nullptr; |
| 4476 | } |
| 4477 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4478 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4479 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4480 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4481 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4482 | return windowHandle; |
| 4483 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4484 | } |
| 4485 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4486 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4487 | } |
| 4488 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4489 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4490 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4491 | if (windowHandleToken == nullptr) { |
| 4492 | return nullptr; |
| 4493 | } |
| 4494 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4495 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4496 | if (windowHandle->getToken() == windowHandleToken) { |
| 4497 | return windowHandle; |
| 4498 | } |
| 4499 | } |
| 4500 | return nullptr; |
| 4501 | } |
| 4502 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4503 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4504 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4505 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4506 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4507 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4508 | if (handle->getId() == windowHandle->getId() && |
| 4509 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4510 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4511 | ALOGE("Found window %s in display %" PRId32 |
| 4512 | ", but it should belong to display %" PRId32, |
| 4513 | windowHandle->getName().c_str(), it.first, |
| 4514 | windowHandle->getInfo()->displayId); |
| 4515 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4516 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4517 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4518 | } |
| 4519 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4520 | return nullptr; |
| 4521 | } |
| 4522 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4523 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4524 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4525 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4526 | } |
| 4527 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4528 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4529 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4530 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4531 | windowHandle.getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4532 | if (connection != nullptr && noInputChannel) { |
| 4533 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4534 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4535 | return false; |
| 4536 | } |
| 4537 | |
| 4538 | if (connection == nullptr) { |
| 4539 | if (!noInputChannel) { |
| 4540 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4541 | } |
| 4542 | return false; |
| 4543 | } |
| 4544 | if (!connection->responsive) { |
| 4545 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4546 | return false; |
| 4547 | } |
| 4548 | return true; |
| 4549 | } |
| 4550 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4551 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4552 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4553 | auto connectionIt = mConnectionsByToken.find(token); |
| 4554 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4555 | return nullptr; |
| 4556 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4557 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4558 | } |
| 4559 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4560 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4561 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4562 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4563 | // Remove all handles on a display if there are no windows left. |
| 4564 | mWindowHandlesByDisplay.erase(displayId); |
| 4565 | return; |
| 4566 | } |
| 4567 | |
| 4568 | // Since we compare the pointer of input window handles across window updates, we need |
| 4569 | // 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] | 4570 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4571 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4572 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4573 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4574 | } |
| 4575 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4576 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4577 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4578 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4579 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4580 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4581 | info->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 4582 | const bool canReceiveInput = |
| 4583 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4584 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4585 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4586 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4587 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4588 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4589 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4590 | } |
| 4591 | |
| 4592 | if (info->displayId != displayId) { |
| 4593 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4594 | handle->getName().c_str(), displayId, info->displayId); |
| 4595 | continue; |
| 4596 | } |
| 4597 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4598 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4599 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4600 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4601 | oldHandle->updateFrom(handle); |
| 4602 | newHandles.push_back(oldHandle); |
| 4603 | } else { |
| 4604 | newHandles.push_back(handle); |
| 4605 | } |
| 4606 | } |
| 4607 | |
| 4608 | // Insert or replace |
| 4609 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4610 | } |
| 4611 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4612 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4613 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4614 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4615 | { // acquire lock |
| 4616 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4617 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4618 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4619 | } |
| 4620 | } |
| 4621 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4622 | mLooper->wake(); |
| 4623 | } |
| 4624 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4625 | /** |
| 4626 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4627 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4628 | * If set an empty list, remove all handles from the specific display. |
| 4629 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4630 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4631 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4632 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4633 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4634 | if (DEBUG_FOCUS) { |
| 4635 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4636 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4637 | windowList += iwh->getName() + " "; |
| 4638 | } |
| 4639 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4640 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4641 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4642 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4643 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4644 | const WindowInfo& info = *window->getInfo(); |
| 4645 | |
| 4646 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 4647 | const bool noInputWindow = info.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4648 | if (noInputWindow && window->getToken() != nullptr) { |
| 4649 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4650 | window->getName().c_str()); |
| 4651 | window->releaseChannel(); |
| 4652 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4653 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4654 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 4655 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 4656 | !info.inputConfig.test( |
| 4657 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4658 | "%s has feature SPY, but is not a trusted overlay.", |
| 4659 | window->getName().c_str()); |
| 4660 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4661 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 4662 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 4663 | !info.inputConfig.test( |
| 4664 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4665 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4666 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4667 | } |
| 4668 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4669 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4670 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4671 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4672 | // Save the old windows' orientation by ID before it gets updated. |
| 4673 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4674 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4675 | oldWindowOrientations.emplace(handle->getId(), |
| 4676 | handle->getInfo()->transform.getOrientation()); |
| 4677 | } |
| 4678 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4679 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4680 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4681 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4682 | if (mLastHoverWindowHandle && |
| 4683 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4684 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4685 | mLastHoverWindowHandle = nullptr; |
| 4686 | } |
| 4687 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4688 | std::optional<FocusResolver::FocusChanges> changes = |
| 4689 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4690 | if (changes) { |
| 4691 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4692 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4693 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4694 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4695 | mTouchStatesByDisplay.find(displayId); |
| 4696 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4697 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4698 | for (size_t i = 0; i < state.windows.size();) { |
| 4699 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4700 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4701 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4702 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4703 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4704 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4705 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4706 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4707 | if (touchedInputChannel != nullptr) { |
| 4708 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4709 | "touched window was removed"); |
| 4710 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4711 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4712 | // well. |
| 4713 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 4714 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 4715 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4716 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4717 | if (wallpaper != nullptr) { |
| 4718 | sp<Connection> wallpaperConnection = |
| 4719 | getConnectionLocked(wallpaper->getToken()); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4720 | if (wallpaperConnection != nullptr) { |
| 4721 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4722 | options); |
| 4723 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4724 | } |
| 4725 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4726 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4727 | state.windows.erase(state.windows.begin() + i); |
| 4728 | } else { |
| 4729 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4730 | } |
| 4731 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4732 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4733 | // 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] | 4734 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4735 | if (mDragState && |
| 4736 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4737 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4738 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4739 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4740 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4741 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4742 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4743 | // pointer events if necessary. |
| 4744 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4745 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4746 | if (newWindowHandle != nullptr && |
| 4747 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4748 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4749 | std::shared_ptr<InputChannel> inputChannel = |
| 4750 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4751 | if (inputChannel != nullptr) { |
| 4752 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4753 | "touched window's orientation changed"); |
| 4754 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4755 | } |
| 4756 | } |
| 4757 | } |
| 4758 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4759 | // Release information for windows that are no longer present. |
| 4760 | // This ensures that unused input channels are released promptly. |
| 4761 | // Otherwise, they might stick around until the window handle is destroyed |
| 4762 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4763 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4764 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4765 | if (DEBUG_FOCUS) { |
| 4766 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4767 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4768 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4769 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4770 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4774 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4775 | if (DEBUG_FOCUS) { |
| 4776 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4777 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4778 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4779 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4780 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4781 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4782 | } // release lock |
| 4783 | |
| 4784 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4785 | mLooper->wake(); |
| 4786 | } |
| 4787 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4788 | void InputDispatcher::setFocusedApplicationLocked( |
| 4789 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4790 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4791 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4792 | |
| 4793 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4794 | return; // This application is already focused. No need to wake up or change anything. |
| 4795 | } |
| 4796 | |
| 4797 | // Set the new application handle. |
| 4798 | if (inputApplicationHandle != nullptr) { |
| 4799 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4800 | } else { |
| 4801 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4802 | } |
| 4803 | |
| 4804 | // No matter what the old focused application was, stop waiting on it because it is |
| 4805 | // no longer focused. |
| 4806 | resetNoFocusedWindowTimeoutLocked(); |
| 4807 | } |
| 4808 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4809 | /** |
| 4810 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4811 | * the display not specified. |
| 4812 | * |
| 4813 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4814 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4815 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4816 | * display. The display-specified events won't be affected. |
| 4817 | */ |
| 4818 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4819 | if (DEBUG_FOCUS) { |
| 4820 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4821 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4822 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4823 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4824 | |
| 4825 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4826 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4827 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4828 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4829 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4830 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4831 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4832 | CancelationOptions |
| 4833 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4834 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4835 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4836 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4837 | } |
| 4838 | } |
| 4839 | mFocusedDisplayId = displayId; |
| 4840 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4841 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4842 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4843 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4844 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4845 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4846 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4847 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4848 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4849 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4850 | } |
| 4851 | } |
| 4852 | } |
| 4853 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4854 | if (DEBUG_FOCUS) { |
| 4855 | logDispatchStateLocked(); |
| 4856 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4857 | } // release lock |
| 4858 | |
| 4859 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4860 | mLooper->wake(); |
| 4861 | } |
| 4862 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4863 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4864 | if (DEBUG_FOCUS) { |
| 4865 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4866 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4867 | |
| 4868 | bool changed; |
| 4869 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4870 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4871 | |
| 4872 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4873 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4874 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | if (mDispatchEnabled && !enabled) { |
| 4878 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4879 | } |
| 4880 | |
| 4881 | mDispatchEnabled = enabled; |
| 4882 | mDispatchFrozen = frozen; |
| 4883 | changed = true; |
| 4884 | } else { |
| 4885 | changed = false; |
| 4886 | } |
| 4887 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4888 | if (DEBUG_FOCUS) { |
| 4889 | logDispatchStateLocked(); |
| 4890 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4891 | } // release lock |
| 4892 | |
| 4893 | if (changed) { |
| 4894 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4895 | mLooper->wake(); |
| 4896 | } |
| 4897 | } |
| 4898 | |
| 4899 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4900 | if (DEBUG_FOCUS) { |
| 4901 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4902 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4903 | |
| 4904 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4905 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4906 | |
| 4907 | if (mInputFilterEnabled == enabled) { |
| 4908 | return; |
| 4909 | } |
| 4910 | |
| 4911 | mInputFilterEnabled = enabled; |
| 4912 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4913 | } // release lock |
| 4914 | |
| 4915 | // Wake up poll loop since there might be work to do to drop everything. |
| 4916 | mLooper->wake(); |
| 4917 | } |
| 4918 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4919 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, |
| 4920 | bool hasPermission) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4921 | bool needWake = false; |
| 4922 | { |
| 4923 | std::scoped_lock lock(mLock); |
| 4924 | if (mInTouchMode == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4925 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4926 | } |
| 4927 | if (DEBUG_TOUCH_MODE) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4928 | ALOGD("Request to change touch mode from %s to %s (calling pid=%d, uid=%d, " |
| 4929 | "hasPermission=%s)", |
| 4930 | toString(mInTouchMode), toString(inTouchMode), pid, uid, toString(hasPermission)); |
| 4931 | } |
| 4932 | if (!hasPermission) { |
| 4933 | const sp<IBinder> focusedToken = |
| 4934 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 4935 | |
| 4936 | // TODO(b/198487159): if no window is currently focused, then we need to check the last |
| 4937 | // interacted window (within 1 second timeout). We should allow touch mode change |
| 4938 | // if the last interacted window owner's pid/uid match the calling ones. |
| 4939 | if (focusedToken == nullptr) { |
| 4940 | return false; |
| 4941 | } |
| 4942 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 4943 | if (windowHandle == nullptr) { |
| 4944 | return false; |
| 4945 | } |
| 4946 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 4947 | if (pid != windowInfo->ownerPid || uid != windowInfo->ownerUid) { |
| 4948 | return false; |
| 4949 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | // TODO(b/198499018): Store touch mode per display. |
| 4953 | mInTouchMode = inTouchMode; |
| 4954 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4955 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 4956 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4957 | } // release lock |
| 4958 | |
| 4959 | if (needWake) { |
| 4960 | mLooper->wake(); |
| 4961 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4962 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4963 | } |
| 4964 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4965 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4966 | if (opacity < 0 || opacity > 1) { |
| 4967 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4968 | return; |
| 4969 | } |
| 4970 | |
| 4971 | std::scoped_lock lock(mLock); |
| 4972 | mMaximumObscuringOpacityForTouch = opacity; |
| 4973 | } |
| 4974 | |
| 4975 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4976 | std::scoped_lock lock(mLock); |
| 4977 | mBlockUntrustedTouchesMode = mode; |
| 4978 | } |
| 4979 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4980 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 4981 | const sp<IBinder>& token) { |
| 4982 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 4983 | for (TouchedWindow& w : state.windows) { |
| 4984 | if (w.windowHandle->getToken() == token) { |
| 4985 | return std::make_pair(&state, &w); |
| 4986 | } |
| 4987 | } |
| 4988 | } |
| 4989 | return std::make_pair(nullptr, nullptr); |
| 4990 | } |
| 4991 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4992 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 4993 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4994 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4995 | if (DEBUG_FOCUS) { |
| 4996 | ALOGD("Trivial transfer to same window."); |
| 4997 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4998 | return true; |
| 4999 | } |
| 5000 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5001 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5002 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5003 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5004 | // Find the target touch state and touched window by fromToken. |
| 5005 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 5006 | if (state == nullptr || touchedWindow == nullptr) { |
| 5007 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5008 | return false; |
| 5009 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5010 | |
| 5011 | const int32_t displayId = state->displayId; |
| 5012 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5013 | if (toWindowHandle == nullptr) { |
| 5014 | ALOGW("Cannot transfer focus because to window not found."); |
| 5015 | return false; |
| 5016 | } |
| 5017 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5018 | if (DEBUG_FOCUS) { |
| 5019 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5020 | touchedWindow->windowHandle->getName().c_str(), |
| 5021 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5022 | } |
| 5023 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5024 | // Erase old window. |
| 5025 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 5026 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 5027 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5028 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5029 | // Add new window. |
| 5030 | int32_t newTargetFlags = oldTargetFlags & |
| 5031 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 5032 | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5033 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5034 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5035 | // Store the dragging window. |
| 5036 | if (isDragDrop) { |
| 5037 | mDragState = std::make_unique<DragState>(toWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5038 | } |
| 5039 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5040 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5041 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5042 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5043 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5044 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5045 | CancelationOptions |
| 5046 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5047 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5048 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5049 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5050 | } |
| 5051 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5052 | if (DEBUG_FOCUS) { |
| 5053 | logDispatchStateLocked(); |
| 5054 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5055 | } // release lock |
| 5056 | |
| 5057 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5058 | mLooper->wake(); |
| 5059 | return true; |
| 5060 | } |
| 5061 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5062 | // Binder call |
| 5063 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 5064 | sp<IBinder> fromToken; |
| 5065 | { // acquire lock |
| 5066 | std::scoped_lock _l(mLock); |
| 5067 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5068 | auto it = std::find_if(mTouchStatesByDisplay.begin(), mTouchStatesByDisplay.end(), |
| 5069 | [](const auto& pair) { return pair.second.windows.size() == 1; }); |
| 5070 | if (it == mTouchStatesByDisplay.end()) { |
| 5071 | ALOGW("Cannot transfer touch state because there is no exact window being touched"); |
| 5072 | return false; |
| 5073 | } |
| 5074 | const int32_t displayId = it->first; |
| 5075 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5076 | if (toWindowHandle == nullptr) { |
| 5077 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 5078 | return false; |
| 5079 | } |
| 5080 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5081 | TouchState& state = it->second; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5082 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 5083 | fromToken = touchedWindow.windowHandle->getToken(); |
| 5084 | } // release lock |
| 5085 | |
| 5086 | return transferTouchFocus(fromToken, destChannelToken); |
| 5087 | } |
| 5088 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5089 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5090 | if (DEBUG_FOCUS) { |
| 5091 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5092 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5093 | |
| 5094 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5095 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5096 | |
| 5097 | resetKeyRepeatLocked(); |
| 5098 | releasePendingEventLocked(); |
| 5099 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5100 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5101 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5102 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5103 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5104 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5105 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5109 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5110 | dumpDispatchStateLocked(dump); |
| 5111 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5112 | std::istringstream stream(dump); |
| 5113 | std::string line; |
| 5114 | |
| 5115 | while (std::getline(stream, line, '\n')) { |
| 5116 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5117 | } |
| 5118 | } |
| 5119 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5120 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5121 | std::string dump; |
| 5122 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5123 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5124 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5125 | |
| 5126 | std::string windowName = "None"; |
| 5127 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5128 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5129 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5130 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5131 | : "token has capture without window"; |
| 5132 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5133 | 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] | 5134 | |
| 5135 | return dump; |
| 5136 | } |
| 5137 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5138 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5139 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5140 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5141 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5142 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5143 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5144 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5145 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5146 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5147 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5148 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5149 | const std::chrono::duration timeout = |
| 5150 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5151 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5152 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5153 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5154 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5155 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5156 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5157 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5158 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5159 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5160 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5161 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5162 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5163 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5164 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5165 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5166 | 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] | 5167 | state.displayId, toString(state.down), toString(state.split), |
| 5168 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5169 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5170 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5171 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5172 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5173 | dump += StringPrintf(INDENT4 |
| 5174 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5175 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5176 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5177 | } |
| 5178 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5179 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5180 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5181 | } |
| 5182 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5183 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5184 | } |
| 5185 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5186 | if (mDragState) { |
| 5187 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5188 | mDragState->dump(dump, INDENT2); |
| 5189 | } |
| 5190 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5191 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5192 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5193 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5194 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5195 | const auto& displayInfo = it->second; |
| 5196 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5197 | displayInfo.logicalHeight); |
| 5198 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5199 | } else { |
| 5200 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5201 | } |
| 5202 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5203 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5204 | dump += INDENT2 "Windows:\n"; |
| 5205 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5206 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5207 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5208 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5209 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 5210 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5211 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5212 | "applicationInfo.name=%s, " |
| 5213 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5214 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5215 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 5216 | windowInfo->displayId, |
| 5217 | windowInfo->inputConfig.string().c_str(), |
| 5218 | windowInfo->alpha, windowInfo->frameLeft, |
| 5219 | windowInfo->frameTop, windowInfo->frameRight, |
| 5220 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5221 | windowInfo->applicationInfo.name.c_str(), |
| 5222 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5223 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5224 | dump += StringPrintf(", inputFeatures=%s", |
| 5225 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5226 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame^] | 5227 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5228 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5229 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5230 | millis(windowInfo->dispatchingTimeout), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5231 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5232 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5233 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5234 | } |
| 5235 | } else { |
| 5236 | dump += INDENT2 "Windows: <none>\n"; |
| 5237 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5238 | } |
| 5239 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5240 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5241 | } |
| 5242 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5243 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5244 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5245 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5246 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5247 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5248 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5249 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5250 | } |
| 5251 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5252 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5253 | |
| 5254 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5255 | if (!mRecentQueue.empty()) { |
| 5256 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5257 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5258 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5259 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5260 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5261 | } |
| 5262 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5263 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5264 | } |
| 5265 | |
| 5266 | // Dump event currently being dispatched. |
| 5267 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5268 | dump += INDENT "PendingEvent:\n"; |
| 5269 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5270 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5271 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5272 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5273 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5274 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5275 | } |
| 5276 | |
| 5277 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5278 | if (!mInboundQueue.empty()) { |
| 5279 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5280 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5281 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5282 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5283 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5284 | } |
| 5285 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5286 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5287 | } |
| 5288 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5289 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5290 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5291 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5292 | const KeyReplacement& replacement = pair.first; |
| 5293 | int32_t newKeyCode = pair.second; |
| 5294 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5295 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5296 | } |
| 5297 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5298 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5299 | } |
| 5300 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5301 | if (!mCommandQueue.empty()) { |
| 5302 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5303 | } else { |
| 5304 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5305 | } |
| 5306 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5307 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5308 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5309 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5310 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5311 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5312 | connection->inputChannel->getFd().get(), |
| 5313 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5314 | connection->getWindowName().c_str(), |
| 5315 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5316 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5317 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5318 | if (!connection->outboundQueue.empty()) { |
| 5319 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5320 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5321 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5322 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5323 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5324 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5325 | } |
| 5326 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5327 | if (!connection->waitQueue.empty()) { |
| 5328 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5329 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5330 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5331 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5332 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5333 | } |
| 5334 | } |
| 5335 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5336 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5337 | } |
| 5338 | |
| 5339 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5340 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5341 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5342 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5343 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5344 | } |
| 5345 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5346 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5347 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5348 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5349 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5350 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5351 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5352 | } |
| 5353 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5354 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5355 | const size_t numMonitors = monitors.size(); |
| 5356 | for (size_t i = 0; i < numMonitors; i++) { |
| 5357 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5358 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5359 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5360 | dump += "\n"; |
| 5361 | } |
| 5362 | } |
| 5363 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5364 | class LooperEventCallback : public LooperCallback { |
| 5365 | public: |
| 5366 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5367 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5368 | |
| 5369 | private: |
| 5370 | std::function<int(int events)> mCallback; |
| 5371 | }; |
| 5372 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5373 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5374 | if (DEBUG_CHANNEL_CREATION) { |
| 5375 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5376 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5377 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5378 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5379 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5380 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5381 | |
| 5382 | if (result) { |
| 5383 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5384 | } |
| 5385 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5386 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5387 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5388 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5389 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5390 | sp<Connection> connection = |
| 5391 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5392 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5393 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5394 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5395 | } |
| 5396 | mConnectionsByToken.emplace(token, connection); |
| 5397 | |
| 5398 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5399 | this, std::placeholders::_1, token); |
| 5400 | |
| 5401 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5402 | } // release lock |
| 5403 | |
| 5404 | // Wake the looper because some connections have changed. |
| 5405 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5406 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5407 | } |
| 5408 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5409 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5410 | const std::string& name, |
| 5411 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5412 | std::shared_ptr<InputChannel> serverChannel; |
| 5413 | std::unique_ptr<InputChannel> clientChannel; |
| 5414 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5415 | if (result) { |
| 5416 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5417 | } |
| 5418 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5419 | { // acquire lock |
| 5420 | std::scoped_lock _l(mLock); |
| 5421 | |
| 5422 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5423 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5424 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5425 | } |
| 5426 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5427 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5428 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5429 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5430 | |
| 5431 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5432 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5433 | } |
| 5434 | mConnectionsByToken.emplace(token, connection); |
| 5435 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5436 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5437 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5438 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5439 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5440 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5441 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5442 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5443 | // Wake the looper because some connections have changed. |
| 5444 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5445 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5448 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5449 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5450 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5451 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5452 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5453 | if (status) { |
| 5454 | return status; |
| 5455 | } |
| 5456 | } // release lock |
| 5457 | |
| 5458 | // Wake the poll loop because removing the connection may have changed the current |
| 5459 | // synchronization state. |
| 5460 | mLooper->wake(); |
| 5461 | return OK; |
| 5462 | } |
| 5463 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5464 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5465 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5466 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5467 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5468 | // 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] | 5469 | return BAD_VALUE; |
| 5470 | } |
| 5471 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5472 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5473 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5474 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5475 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5476 | } |
| 5477 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5478 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5479 | |
| 5480 | nsecs_t currentTime = now(); |
| 5481 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5482 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5483 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5484 | return OK; |
| 5485 | } |
| 5486 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5487 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5488 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5489 | auto& [displayId, monitors] = *it; |
| 5490 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5491 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5492 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5493 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5494 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5495 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5496 | } else { |
| 5497 | ++it; |
| 5498 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5499 | } |
| 5500 | } |
| 5501 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5502 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5503 | std::scoped_lock _l(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5504 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5505 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5506 | if (!requestingChannel) { |
| 5507 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5508 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5509 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5510 | |
| 5511 | auto [statePtr, windowPtr] = findTouchStateAndWindowLocked(token); |
| 5512 | if (statePtr == nullptr || windowPtr == nullptr || !statePtr->down) { |
| 5513 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5514 | " Ignoring."); |
| 5515 | return BAD_VALUE; |
| 5516 | } |
| 5517 | |
| 5518 | TouchState& state = *statePtr; |
| 5519 | |
| 5520 | // Send cancel events to all the input channels we're stealing from. |
| 5521 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5522 | "input channel stole pointer stream"); |
| 5523 | options.deviceId = state.deviceId; |
| 5524 | options.displayId = state.displayId; |
| 5525 | std::string canceledWindows; |
| 5526 | for (const TouchedWindow& window : state.windows) { |
| 5527 | const std::shared_ptr<InputChannel> channel = |
| 5528 | getInputChannelLocked(window.windowHandle->getToken()); |
| 5529 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5530 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5531 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5532 | canceledWindows += channel->getName(); |
| 5533 | } |
| 5534 | } |
| 5535 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5536 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5537 | canceledWindows.c_str()); |
| 5538 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5539 | // Prevent the gesture from being sent to any other windows. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5540 | state.filterWindowsExcept(token); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5541 | state.preventNewTargets = true; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5542 | return OK; |
| 5543 | } |
| 5544 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5545 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5546 | { // acquire lock |
| 5547 | std::scoped_lock _l(mLock); |
| 5548 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5549 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5550 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5551 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5552 | : "token without window"); |
| 5553 | } |
| 5554 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5555 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5556 | if (focusedToken != windowToken) { |
| 5557 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5558 | enabled ? "enable" : "disable"); |
| 5559 | return; |
| 5560 | } |
| 5561 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5562 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5563 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5564 | "window has %s requested pointer capture.", |
| 5565 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5566 | return; |
| 5567 | } |
| 5568 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5569 | if (enabled) { |
| 5570 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5571 | mIneligibleDisplaysForPointerCapture.end(), |
| 5572 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5573 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5574 | return; |
| 5575 | } |
| 5576 | } |
| 5577 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5578 | setPointerCaptureLocked(enabled); |
| 5579 | } // release lock |
| 5580 | |
| 5581 | // Wake the thread to process command entries. |
| 5582 | mLooper->wake(); |
| 5583 | } |
| 5584 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5585 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5586 | { // acquire lock |
| 5587 | std::scoped_lock _l(mLock); |
| 5588 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5589 | if (!isEligible) { |
| 5590 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5591 | } |
| 5592 | } // release lock |
| 5593 | } |
| 5594 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5595 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5596 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5597 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5598 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5599 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5600 | } |
| 5601 | } |
| 5602 | } |
| 5603 | return std::nullopt; |
| 5604 | } |
| 5605 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5606 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5607 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5608 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5609 | } |
| 5610 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5611 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5612 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5613 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5614 | } |
| 5615 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5616 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5617 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5618 | } |
| 5619 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5620 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5621 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5622 | if (connection == nullptr) { |
| 5623 | return "<nullptr>"; |
| 5624 | } |
| 5625 | return connection->getInputChannelName(); |
| 5626 | } |
| 5627 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5628 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5629 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5630 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5631 | } |
| 5632 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5633 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5634 | const sp<Connection>& connection, uint32_t seq, |
| 5635 | bool handled, nsecs_t consumeTime) { |
| 5636 | // Handle post-event policy actions. |
| 5637 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5638 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5639 | return; |
| 5640 | } |
| 5641 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5642 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5643 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5644 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5645 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5646 | } |
| 5647 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5648 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5649 | connection->inputChannel->getConnectionToken(), |
| 5650 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5651 | } |
| 5652 | |
| 5653 | bool restartEvent; |
| 5654 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5655 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5656 | restartEvent = |
| 5657 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5658 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5659 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5660 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5661 | handled); |
| 5662 | } else { |
| 5663 | restartEvent = false; |
| 5664 | } |
| 5665 | |
| 5666 | // Dequeue the event and start the next cycle. |
| 5667 | // Because the lock might have been released, it is possible that the |
| 5668 | // contents of the wait queue to have been drained, so we need to double-check |
| 5669 | // a few things. |
| 5670 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5671 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5672 | dispatchEntry = *dispatchEntryIt; |
| 5673 | connection->waitQueue.erase(dispatchEntryIt); |
| 5674 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5675 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5676 | if (!connection->responsive) { |
| 5677 | connection->responsive = isConnectionResponsive(*connection); |
| 5678 | if (connection->responsive) { |
| 5679 | // The connection was unresponsive, and now it's responsive. |
| 5680 | processConnectionResponsiveLocked(*connection); |
| 5681 | } |
| 5682 | } |
| 5683 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5684 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5685 | connection->outboundQueue.push_front(dispatchEntry); |
| 5686 | traceOutboundQueueLength(*connection); |
| 5687 | } else { |
| 5688 | releaseDispatchEntry(dispatchEntry); |
| 5689 | } |
| 5690 | } |
| 5691 | |
| 5692 | // Start the next dispatch cycle for this connection. |
| 5693 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5694 | } |
| 5695 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5696 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5697 | const sp<IBinder>& newToken) { |
| 5698 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5699 | scoped_unlock unlock(mLock); |
| 5700 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5701 | }; |
| 5702 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5703 | } |
| 5704 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5705 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5706 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5707 | scoped_unlock unlock(mLock); |
| 5708 | mPolicy->notifyDropWindow(token, x, y); |
| 5709 | }; |
| 5710 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5711 | } |
| 5712 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5713 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5714 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5715 | scoped_unlock unlock(mLock); |
| 5716 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5717 | }; |
| 5718 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5719 | } |
| 5720 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5721 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5722 | if (connection == nullptr) { |
| 5723 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5724 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5725 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5726 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5727 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5728 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5729 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5730 | return; |
| 5731 | } |
| 5732 | /** |
| 5733 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5734 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5735 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5736 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5737 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5738 | * most useful. |
| 5739 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5740 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5741 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5742 | std::string reason = |
| 5743 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5744 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5745 | ns2ms(currentWait), |
| 5746 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5747 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5748 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5749 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5750 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5751 | |
| 5752 | // Stop waking up for events on this connection, it is already unresponsive |
| 5753 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5754 | } |
| 5755 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5756 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5757 | std::string reason = |
| 5758 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5759 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5760 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5761 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5762 | scoped_unlock unlock(mLock); |
| 5763 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5764 | }; |
| 5765 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5766 | } |
| 5767 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5768 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5769 | const std::string& reason) { |
| 5770 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5771 | updateLastAnrStateLocked(windowLabel, reason); |
| 5772 | } |
| 5773 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5774 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5775 | const std::string& reason) { |
| 5776 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5777 | updateLastAnrStateLocked(windowLabel, reason); |
| 5778 | } |
| 5779 | |
| 5780 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5781 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5782 | // 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] | 5783 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5784 | struct tm tm; |
| 5785 | localtime_r(&t, &tm); |
| 5786 | char timestr[64]; |
| 5787 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5788 | mLastAnrState.clear(); |
| 5789 | mLastAnrState += INDENT "ANR:\n"; |
| 5790 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5791 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5792 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5793 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5794 | } |
| 5795 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5796 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5797 | KeyEntry& entry) { |
| 5798 | const KeyEvent event = createKeyEvent(entry); |
| 5799 | nsecs_t delay = 0; |
| 5800 | { // release lock |
| 5801 | scoped_unlock unlock(mLock); |
| 5802 | android::base::Timer t; |
| 5803 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5804 | entry.policyFlags); |
| 5805 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5806 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5807 | std::to_string(t.duration().count()).c_str()); |
| 5808 | } |
| 5809 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5810 | |
| 5811 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5812 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5813 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5814 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5815 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5816 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5817 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5818 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5819 | } |
| 5820 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5821 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5822 | auto command = [this, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5823 | scoped_unlock unlock(mLock); |
| 5824 | mPolicy->notifyMonitorUnresponsive(pid, reason); |
| 5825 | }; |
| 5826 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5827 | } |
| 5828 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5829 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5830 | std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5831 | auto command = [this, token, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5832 | scoped_unlock unlock(mLock); |
| 5833 | mPolicy->notifyWindowUnresponsive(token, reason); |
| 5834 | }; |
| 5835 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5836 | } |
| 5837 | |
| 5838 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5839 | auto command = [this, pid]() REQUIRES(mLock) { |
| 5840 | scoped_unlock unlock(mLock); |
| 5841 | mPolicy->notifyMonitorResponsive(pid); |
| 5842 | }; |
| 5843 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5844 | } |
| 5845 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5846 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken) { |
| 5847 | auto command = [this, connectionToken]() REQUIRES(mLock) { |
| 5848 | scoped_unlock unlock(mLock); |
| 5849 | mPolicy->notifyWindowResponsive(connectionToken); |
| 5850 | }; |
| 5851 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5852 | } |
| 5853 | |
| 5854 | /** |
| 5855 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5856 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5857 | * command entry to the command queue. |
| 5858 | */ |
| 5859 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5860 | std::string reason) { |
| 5861 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5862 | if (connection.monitor) { |
| 5863 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5864 | reason.c_str()); |
| 5865 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5866 | if (!pid.has_value()) { |
| 5867 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5868 | connection.inputChannel->getName().c_str()); |
| 5869 | return; |
| 5870 | } |
| 5871 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5872 | return; |
| 5873 | } |
| 5874 | // If not a monitor, must be a window |
| 5875 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5876 | reason.c_str()); |
| 5877 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5878 | } |
| 5879 | |
| 5880 | /** |
| 5881 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5882 | */ |
| 5883 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5884 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5885 | if (connection.monitor) { |
| 5886 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5887 | if (!pid.has_value()) { |
| 5888 | ALOGE("Could not find responsive monitor for connection %s", |
| 5889 | connection.inputChannel->getName().c_str()); |
| 5890 | return; |
| 5891 | } |
| 5892 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5893 | return; |
| 5894 | } |
| 5895 | // If not a monitor, must be a window |
| 5896 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5897 | } |
| 5898 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5899 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5900 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5901 | KeyEntry& keyEntry, bool handled) { |
| 5902 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5903 | if (!handled) { |
| 5904 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5905 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5906 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5907 | return false; |
| 5908 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5909 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5910 | // Get the fallback key state. |
| 5911 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5912 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5913 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5914 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5915 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5916 | } |
| 5917 | |
| 5918 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5919 | // If the application handles the original key for which we previously |
| 5920 | // generated a fallback or if the window is not a foreground window, |
| 5921 | // then cancel the associated fallback key, if any. |
| 5922 | if (fallbackKeyCode != -1) { |
| 5923 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5924 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5925 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 5926 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5927 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 5928 | keyEntry.policyFlags); |
| 5929 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5930 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5931 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5932 | |
| 5933 | mLock.unlock(); |
| 5934 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5935 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5936 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5937 | |
| 5938 | mLock.lock(); |
| 5939 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5940 | // Cancel the fallback key. |
| 5941 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5942 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5943 | "application handled the original non-fallback key " |
| 5944 | "or is no longer a foreground target, " |
| 5945 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5946 | options.keyCode = fallbackKeyCode; |
| 5947 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5948 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5949 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5950 | } |
| 5951 | } else { |
| 5952 | // If the application did not handle a non-fallback key, first check |
| 5953 | // that we are in a good state to perform unhandled key event processing |
| 5954 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5955 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5956 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5957 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5958 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 5959 | "since this is not an initial down. " |
| 5960 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5961 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 5962 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5963 | return false; |
| 5964 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5965 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5966 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5967 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5968 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 5969 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5970 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 5971 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5972 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5973 | |
| 5974 | mLock.unlock(); |
| 5975 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5976 | bool fallback = |
| 5977 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5978 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5979 | |
| 5980 | mLock.lock(); |
| 5981 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5982 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5983 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5984 | return false; |
| 5985 | } |
| 5986 | |
| 5987 | // Latch the fallback keycode for this key on an initial down. |
| 5988 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5989 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5990 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5991 | fallbackKeyCode = event.getKeyCode(); |
| 5992 | } else { |
| 5993 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5994 | } |
| 5995 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5996 | } |
| 5997 | |
| 5998 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5999 | |
| 6000 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6001 | // We will continue to dispatch the key to the policy but we will no |
| 6002 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6003 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6004 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6005 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6006 | if (fallback) { |
| 6007 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6008 | "as a fallback for %d, but on the DOWN it had requested " |
| 6009 | "to send %d instead. Fallback canceled.", |
| 6010 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6011 | } else { |
| 6012 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6013 | "but on the DOWN it had requested to send %d. " |
| 6014 | "Fallback canceled.", |
| 6015 | originalKeyCode, fallbackKeyCode); |
| 6016 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6017 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6018 | |
| 6019 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6020 | "canceling fallback, policy no longer desires it"); |
| 6021 | options.keyCode = fallbackKeyCode; |
| 6022 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6023 | |
| 6024 | fallback = false; |
| 6025 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6026 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6027 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6028 | } |
| 6029 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6030 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6031 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6032 | { |
| 6033 | std::string msg; |
| 6034 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6035 | connection->inputState.getFallbackKeys(); |
| 6036 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6037 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6038 | } |
| 6039 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6040 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6041 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6042 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6043 | |
| 6044 | if (fallback) { |
| 6045 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6046 | keyEntry.eventTime = event.getEventTime(); |
| 6047 | keyEntry.deviceId = event.getDeviceId(); |
| 6048 | keyEntry.source = event.getSource(); |
| 6049 | keyEntry.displayId = event.getDisplayId(); |
| 6050 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6051 | keyEntry.keyCode = fallbackKeyCode; |
| 6052 | keyEntry.scanCode = event.getScanCode(); |
| 6053 | keyEntry.metaState = event.getMetaState(); |
| 6054 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6055 | keyEntry.downTime = event.getDownTime(); |
| 6056 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6057 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6058 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6059 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6060 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6061 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6062 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6063 | return true; // restart the event |
| 6064 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6065 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6066 | ALOGD("Unhandled key event: No fallback key."); |
| 6067 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6068 | |
| 6069 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6070 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6071 | } |
| 6072 | } |
| 6073 | return false; |
| 6074 | } |
| 6075 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6076 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6077 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6078 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6079 | return false; |
| 6080 | } |
| 6081 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6082 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6083 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6084 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6085 | } |
| 6086 | } |
| 6087 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6088 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6089 | if (ATRACE_ENABLED()) { |
| 6090 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6091 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6092 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6093 | } |
| 6094 | } |
| 6095 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6096 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6097 | if (ATRACE_ENABLED()) { |
| 6098 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6099 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6100 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6101 | } |
| 6102 | } |
| 6103 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6104 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6105 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6106 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6107 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6108 | dumpDispatchStateLocked(dump); |
| 6109 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6110 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6111 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6112 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6113 | } |
| 6114 | } |
| 6115 | |
| 6116 | void InputDispatcher::monitor() { |
| 6117 | // 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] | 6118 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6119 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6120 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6121 | } |
| 6122 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6123 | /** |
| 6124 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6125 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6126 | * this method can be safely called from any thread, as long as you've ensured that |
| 6127 | * the work you are interested in completing has already been queued. |
| 6128 | */ |
| 6129 | bool InputDispatcher::waitForIdle() { |
| 6130 | /** |
| 6131 | * Timeout should represent the longest possible time that a device might spend processing |
| 6132 | * events and commands. |
| 6133 | */ |
| 6134 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6135 | std::unique_lock lock(mLock); |
| 6136 | mLooper->wake(); |
| 6137 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6138 | return result == std::cv_status::no_timeout; |
| 6139 | } |
| 6140 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6141 | /** |
| 6142 | * Sets focus to the window identified by the token. This must be called |
| 6143 | * after updating any input window handles. |
| 6144 | * |
| 6145 | * Params: |
| 6146 | * request.token - input channel token used to identify the window that should gain focus. |
| 6147 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6148 | * specified token does not match the currently focused window, this request will be dropped. |
| 6149 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6150 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6151 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6152 | * when requesting the focus change. This determines which request gets |
| 6153 | * precedence if there is a focus change request from another source such as pointer down. |
| 6154 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6155 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6156 | { // acquire lock |
| 6157 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6158 | std::optional<FocusResolver::FocusChanges> changes = |
| 6159 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6160 | if (changes) { |
| 6161 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6162 | } |
| 6163 | } // release lock |
| 6164 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6165 | mLooper->wake(); |
| 6166 | } |
| 6167 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6168 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6169 | if (changes.oldFocus) { |
| 6170 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6171 | if (focusedInputChannel) { |
| 6172 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6173 | "focus left window"); |
| 6174 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6175 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6176 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6177 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6178 | if (changes.newFocus) { |
| 6179 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6180 | } |
| 6181 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6182 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6183 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6184 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6185 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6186 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6187 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6188 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6189 | // front. |
| 6190 | disablePointerCaptureForcedLocked(); |
| 6191 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6192 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6193 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6194 | } |
| 6195 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6196 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6197 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6198 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6199 | return; |
| 6200 | } |
| 6201 | |
| 6202 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6203 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6204 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6205 | setPointerCaptureLocked(false); |
| 6206 | } |
| 6207 | |
| 6208 | if (!mWindowTokenWithPointerCapture) { |
| 6209 | // No need to send capture changes because no window has capture. |
| 6210 | return; |
| 6211 | } |
| 6212 | |
| 6213 | if (mPendingEvent != nullptr) { |
| 6214 | // Move the pending event to the front of the queue. This will give the chance |
| 6215 | // for the pending event to be dropped if it is a captured event. |
| 6216 | mInboundQueue.push_front(mPendingEvent); |
| 6217 | mPendingEvent = nullptr; |
| 6218 | } |
| 6219 | |
| 6220 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6221 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6222 | mInboundQueue.push_front(std::move(entry)); |
| 6223 | } |
| 6224 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6225 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6226 | mCurrentPointerCaptureRequest.enable = enable; |
| 6227 | mCurrentPointerCaptureRequest.seq++; |
| 6228 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6229 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6230 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6231 | }; |
| 6232 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6233 | } |
| 6234 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6235 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6236 | { // acquire lock |
| 6237 | std::scoped_lock _l(mLock); |
| 6238 | // Set an empty list to remove all handles from the specific display. |
| 6239 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6240 | setFocusedApplicationLocked(displayId, nullptr); |
| 6241 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6242 | // have been removed for the removed display. |
| 6243 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6244 | // Reset pointer capture eligibility, regardless of previous state. |
| 6245 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6246 | } // release lock |
| 6247 | |
| 6248 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6249 | mLooper->wake(); |
| 6250 | } |
| 6251 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6252 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6253 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6254 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6255 | // more convenient parsing. |
| 6256 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6257 | for (const auto& info : windowInfos) { |
| 6258 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6259 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6260 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6261 | |
| 6262 | { // acquire lock |
| 6263 | std::scoped_lock _l(mLock); |
| 6264 | mDisplayInfos.clear(); |
| 6265 | for (const auto& displayInfo : displayInfos) { |
| 6266 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6267 | } |
| 6268 | |
| 6269 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6270 | setInputWindowsLocked(handles, displayId); |
| 6271 | } |
| 6272 | } |
| 6273 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6274 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6275 | } |
| 6276 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6277 | bool InputDispatcher::shouldDropInput( |
| 6278 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
| 6279 | if (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT) || |
| 6280 | (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED) && |
| 6281 | isWindowObscuredLocked(windowHandle))) { |
| 6282 | ALOGW("Dropping %s event targeting %s as requested by input feature %s on display " |
| 6283 | "%" PRId32 ".", |
| 6284 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
| 6285 | windowHandle->getInfo()->inputFeatures.string().c_str(), |
| 6286 | windowHandle->getInfo()->displayId); |
| 6287 | return true; |
| 6288 | } |
| 6289 | return false; |
| 6290 | } |
| 6291 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6292 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6293 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6294 | const std::vector<DisplayInfo>& displayInfos) { |
| 6295 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6296 | } |
| 6297 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6298 | void InputDispatcher::cancelCurrentTouch() { |
| 6299 | { |
| 6300 | std::scoped_lock _l(mLock); |
| 6301 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
| 6302 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 6303 | "cancel current touch"); |
| 6304 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6305 | |
| 6306 | mTouchStatesByDisplay.clear(); |
| 6307 | mLastHoverWindowHandle.clear(); |
| 6308 | } |
| 6309 | // Wake up poll loop since there might be work to do. |
| 6310 | mLooper->wake(); |
| 6311 | } |
| 6312 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6313 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6314 | std::scoped_lock _l(mLock); |
| 6315 | mMonitorDispatchingTimeout = timeout; |
| 6316 | } |
| 6317 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6318 | } // namespace android::inputdispatcher |