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 | |
Siarhei Vishniakou | 63138b6 | 2022-03-03 11:33:19 -0800 | [diff] [blame] | 69 | /** |
| 70 | * Log detailed debug messages about each inbound event notification to the dispatcher. |
| 71 | * Enable this via "adb shell setprop log.tag.InputDispatcherInboundEvent DEBUG" (requires restart) |
| 72 | */ |
| 73 | const bool DEBUG_INBOUND_EVENT_DETAILS = |
| 74 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "InboundEvent", ANDROID_LOG_INFO); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 75 | |
Siarhei Vishniakou | 63138b6 | 2022-03-03 11:33:19 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Log detailed debug messages about each outbound event processed by the dispatcher. |
| 78 | * Enable this via "adb shell setprop log.tag.InputDispatcherOutboundEvent DEBUG" (requires restart) |
| 79 | */ |
| 80 | const bool DEBUG_OUTBOUND_EVENT_DETAILS = |
| 81 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "OutboundEvent", ANDROID_LOG_INFO); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 82 | |
Siarhei Vishniakou | 63138b6 | 2022-03-03 11:33:19 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Log debug messages about the dispatch cycle. |
| 85 | * Enable this via "adb shell setprop log.tag.InputDispatcherDispatchCycle DEBUG" (requires restart) |
| 86 | */ |
| 87 | const bool DEBUG_DISPATCH_CYCLE = |
| 88 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "DispatchCycle", ANDROID_LOG_INFO); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 89 | |
Siarhei Vishniakou | 63138b6 | 2022-03-03 11:33:19 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Log debug messages about channel creation |
| 92 | * Enable this via "adb shell setprop log.tag.InputDispatcherChannelCreation DEBUG" (requires |
| 93 | * restart) |
| 94 | */ |
| 95 | const bool DEBUG_CHANNEL_CREATION = |
| 96 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "ChannelCreation", ANDROID_LOG_INFO); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 97 | |
Siarhei Vishniakou | 63138b6 | 2022-03-03 11:33:19 -0800 | [diff] [blame] | 98 | /** |
| 99 | * Log debug messages about input event injection. |
| 100 | * Enable this via "adb shell setprop log.tag.InputDispatcherInjection DEBUG" (requires restart) |
| 101 | */ |
| 102 | const bool DEBUG_INJECTION = |
| 103 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Injection", ANDROID_LOG_INFO); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 104 | |
Siarhei Vishniakou | 63138b6 | 2022-03-03 11:33:19 -0800 | [diff] [blame] | 105 | /** |
| 106 | * Log debug messages about input focus tracking. |
| 107 | * Enable this via "adb shell setprop log.tag.InputDispatcherFocus DEBUG" (requires restart) |
| 108 | */ |
| 109 | const bool DEBUG_FOCUS = |
| 110 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Focus", ANDROID_LOG_INFO); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 111 | |
Siarhei Vishniakou | 63138b6 | 2022-03-03 11:33:19 -0800 | [diff] [blame] | 112 | /** |
| 113 | * Log debug messages about touch mode event |
| 114 | * Enable this via "adb shell setprop log.tag.InputDispatcherTouchMode DEBUG" (requires restart) |
| 115 | */ |
| 116 | const bool DEBUG_TOUCH_MODE = |
| 117 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "TouchMode", ANDROID_LOG_INFO); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 118 | |
Siarhei Vishniakou | 63138b6 | 2022-03-03 11:33:19 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Log debug messages about touch occlusion |
| 121 | * Enable this via "adb shell setprop log.tag.InputDispatcherTouchOcclusion DEBUG" (requires |
| 122 | * restart) |
| 123 | */ |
| 124 | const bool DEBUG_TOUCH_OCCLUSION = |
| 125 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "TouchOcclusion", ANDROID_LOG_INFO); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 126 | |
Siarhei Vishniakou | 63138b6 | 2022-03-03 11:33:19 -0800 | [diff] [blame] | 127 | /** |
| 128 | * Log debug messages about the app switch latency optimization. |
| 129 | * Enable this via "adb shell setprop log.tag.InputDispatcherAppSwitch DEBUG" (requires restart) |
| 130 | */ |
| 131 | const bool DEBUG_APP_SWITCH = |
| 132 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "AppSwitch", ANDROID_LOG_INFO); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 133 | |
Siarhei Vishniakou | 63138b6 | 2022-03-03 11:33:19 -0800 | [diff] [blame] | 134 | /** |
| 135 | * Log debug messages about hover events. |
| 136 | * Enable this via "adb shell setprop log.tag.InputDispatcherHover DEBUG" (requires restart) |
| 137 | */ |
| 138 | const bool DEBUG_HOVER = |
| 139 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Hover", ANDROID_LOG_INFO); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 140 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 141 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 142 | // Named to match std::scoped_lock |
| 143 | class scoped_unlock { |
| 144 | public: |
| 145 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 146 | ~scoped_unlock() { mMutex.lock(); } |
| 147 | |
| 148 | private: |
| 149 | std::mutex& mMutex; |
| 150 | }; |
| 151 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 152 | // Default input dispatching timeout if there is no focused application or paused window |
| 153 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 154 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 155 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 156 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 157 | |
| 158 | // Amount of time to allow for all pending events to be processed when an app switch |
| 159 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 160 | // 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] | 161 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 162 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 163 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 164 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | // 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] | 166 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 167 | |
| 168 | // Log a warning when an interception call takes longer than this to process. |
| 169 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 170 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 171 | // Additional key latency in case a connection is still processing some motion events. |
| 172 | // This will help with the case when a user touched a button that opens a new window, |
| 173 | // and gives us the chance to dispatch the key to this new window. |
| 174 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 175 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 176 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 177 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 178 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 179 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 180 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 181 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 182 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 183 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 184 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 185 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 186 | } |
| 187 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 188 | inline const char* toString(bool value) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 189 | return value ? "true" : "false"; |
| 190 | } |
| 191 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 192 | inline const std::string toString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 193 | if (binder == nullptr) { |
| 194 | return "<null>"; |
| 195 | } |
| 196 | return StringPrintf("%p", binder.get()); |
| 197 | } |
| 198 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 199 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 200 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 201 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 204 | bool isValidKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 205 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 206 | case AKEY_EVENT_ACTION_DOWN: |
| 207 | case AKEY_EVENT_ACTION_UP: |
| 208 | return true; |
| 209 | default: |
| 210 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 214 | bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 215 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 216 | ALOGE("Key event has invalid action code 0x%x", action); |
| 217 | return false; |
| 218 | } |
| 219 | return true; |
| 220 | } |
| 221 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 222 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 223 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 224 | case AMOTION_EVENT_ACTION_DOWN: |
| 225 | case AMOTION_EVENT_ACTION_UP: |
| 226 | case AMOTION_EVENT_ACTION_CANCEL: |
| 227 | case AMOTION_EVENT_ACTION_MOVE: |
| 228 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 229 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 230 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 231 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 232 | case AMOTION_EVENT_ACTION_SCROLL: |
| 233 | return true; |
| 234 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 235 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 236 | int32_t index = getMotionEventActionPointerIndex(action); |
| 237 | return index >= 0 && index < pointerCount; |
| 238 | } |
| 239 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 240 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 241 | return actionButton != 0; |
| 242 | default: |
| 243 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 247 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 248 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 249 | } |
| 250 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 251 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 252 | const PointerProperties* pointerProperties) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 253 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 254 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 255 | return false; |
| 256 | } |
| 257 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 258 | 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] | 259 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | BitSet32 pointerIdBits; |
| 263 | for (size_t i = 0; i < pointerCount; i++) { |
| 264 | int32_t id = pointerProperties[i].id; |
| 265 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 266 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 267 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 268 | return false; |
| 269 | } |
| 270 | if (pointerIdBits.hasBit(id)) { |
| 271 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 272 | return false; |
| 273 | } |
| 274 | pointerIdBits.markBit(id); |
| 275 | } |
| 276 | return true; |
| 277 | } |
| 278 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 279 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 280 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 281 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 284 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 285 | bool first = true; |
| 286 | Region::const_iterator cur = region.begin(); |
| 287 | Region::const_iterator const tail = region.end(); |
| 288 | while (cur != tail) { |
| 289 | if (first) { |
| 290 | first = false; |
| 291 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 292 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 293 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 294 | 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] | 295 | cur++; |
| 296 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 297 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 300 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 301 | constexpr size_t maxEntries = 50; // max events to print |
| 302 | constexpr size_t skipBegin = maxEntries / 2; |
| 303 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 304 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 305 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 306 | |
| 307 | std::string dump; |
| 308 | for (size_t i = 0; i < queue.size(); i++) { |
| 309 | const DispatchEntry& entry = *queue[i]; |
| 310 | if (i >= skipBegin && i < skipEnd) { |
| 311 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 312 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 313 | continue; |
| 314 | } |
| 315 | dump.append(INDENT4); |
| 316 | dump += entry.eventEntry->getDescription(); |
| 317 | dump += StringPrintf(", seq=%" PRIu32 |
| 318 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 319 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 320 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 321 | if (entry.deliveryTime != 0) { |
| 322 | // This entry was delivered, so add information on how long we've been waiting |
| 323 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 324 | } |
| 325 | dump.append("\n"); |
| 326 | } |
| 327 | return dump; |
| 328 | } |
| 329 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 330 | /** |
| 331 | * Find the entry in std::unordered_map by key, and return it. |
| 332 | * If the entry is not found, return a default constructed entry. |
| 333 | * |
| 334 | * Useful when the entries are vectors, since an empty vector will be returned |
| 335 | * if the entry is not found. |
| 336 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 337 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 338 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 339 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 340 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 341 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 342 | } |
| 343 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 344 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 345 | if (first == second) { |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | if (first == nullptr || second == nullptr) { |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | return first->getToken() == second->getToken(); |
| 354 | } |
| 355 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 356 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 357 | if (first == nullptr || second == nullptr) { |
| 358 | return false; |
| 359 | } |
| 360 | return first->applicationInfo.token != nullptr && |
| 361 | first->applicationInfo.token == second->applicationInfo.token; |
| 362 | } |
| 363 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 364 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 365 | std::shared_ptr<EventEntry> eventEntry, |
| 366 | int32_t inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 367 | if (inputTarget.useDefaultPointerTransform()) { |
| 368 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 369 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 370 | inputTarget.displayTransform, |
| 371 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 375 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 376 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 377 | std::vector<PointerCoords> pointerCoords; |
| 378 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 379 | |
| 380 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 381 | // 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] | 382 | // uses the transform for the normalized pointer. |
| 383 | const ui::Transform& firstPointerTransform = |
| 384 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 385 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 386 | |
| 387 | // Iterate through all pointers in the event to normalize against the first. |
| 388 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 389 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 390 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 391 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 392 | |
| 393 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 394 | // First, apply the current pointer's transform to update the coordinates into |
| 395 | // window space. |
| 396 | pointerCoords[pointerIndex].transform(currTransform); |
| 397 | // Next, apply the inverse transform of the normalized coordinates so the |
| 398 | // current coordinates are transformed into the normalized coordinate space. |
| 399 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 402 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 403 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 404 | motionEntry.deviceId, motionEntry.source, |
| 405 | motionEntry.displayId, motionEntry.policyFlags, |
| 406 | motionEntry.action, motionEntry.actionButton, |
| 407 | motionEntry.flags, motionEntry.metaState, |
| 408 | motionEntry.buttonState, motionEntry.classification, |
| 409 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 410 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 411 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 412 | motionEntry.pointerCount, motionEntry.pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 413 | pointerCoords.data()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 414 | |
| 415 | if (motionEntry.injectionState) { |
| 416 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 417 | combinedMotionEntry->injectionState->refCount += 1; |
| 418 | } |
| 419 | |
| 420 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 421 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 422 | firstPointerTransform, inputTarget.displayTransform, |
| 423 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 424 | return dispatchEntry; |
| 425 | } |
| 426 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 427 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 428 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 429 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 430 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 431 | |
| 432 | serverChannel = std::move(uniqueServerChannel); |
| 433 | return result; |
| 434 | } |
| 435 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 436 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 437 | 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] | 438 | if (lhs == nullptr && rhs == nullptr) { |
| 439 | return true; |
| 440 | } |
| 441 | if (lhs == nullptr || rhs == nullptr) { |
| 442 | return false; |
| 443 | } |
| 444 | return *lhs == *rhs; |
| 445 | } |
| 446 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 447 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 448 | KeyEvent event; |
| 449 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 450 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 451 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 452 | return event; |
| 453 | } |
| 454 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 455 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 456 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 457 | // affect the statistics. |
| 458 | if (connection.monitor) { |
| 459 | return false; |
| 460 | } |
| 461 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 462 | if (!connection.responsive) { |
| 463 | return false; |
| 464 | } |
| 465 | return true; |
| 466 | } |
| 467 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 468 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 469 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 470 | const int32_t& inputEventId = eventEntry.id; |
| 471 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 472 | // Event was transmuted |
| 473 | return false; |
| 474 | } |
| 475 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 476 | return false; |
| 477 | } |
| 478 | // Only track latency for events that originated from hardware |
| 479 | if (eventEntry.isSynthesized()) { |
| 480 | return false; |
| 481 | } |
| 482 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 483 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 484 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 485 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 486 | return false; |
| 487 | } |
| 488 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 489 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 490 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 491 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 492 | return false; |
| 493 | } |
| 494 | } else { |
| 495 | // Not a key or a motion |
| 496 | return false; |
| 497 | } |
| 498 | if (!shouldReportMetricsForConnection(connection)) { |
| 499 | return false; |
| 500 | } |
| 501 | return true; |
| 502 | } |
| 503 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 504 | /** |
| 505 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 506 | * current time. |
| 507 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 508 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 509 | const nsecs_t currentTime = now(); |
| 510 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 511 | if (entry->timeoutTime < currentTime) { |
| 512 | return false; |
| 513 | } |
| 514 | } |
| 515 | return true; |
| 516 | } |
| 517 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 518 | // Returns true if the event type passed as argument represents a user activity. |
| 519 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 520 | switch (eventEntry.type) { |
| 521 | case EventEntry::Type::FOCUS: |
| 522 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 523 | case EventEntry::Type::DRAG: |
| 524 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 525 | case EventEntry::Type::SENSOR: |
| 526 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 527 | return false; |
| 528 | case EventEntry::Type::DEVICE_RESET: |
| 529 | case EventEntry::Type::KEY: |
| 530 | case EventEntry::Type::MOTION: |
| 531 | return true; |
| 532 | } |
| 533 | } |
| 534 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 535 | // 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] | 536 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, int32_t x, int32_t y, |
| 537 | bool isStylus) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 538 | const auto inputConfig = windowInfo.inputConfig; |
| 539 | if (windowInfo.displayId != displayId || |
| 540 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 541 | return false; |
| 542 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 543 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 544 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 545 | return false; |
| 546 | } |
Prabir Pradhan | 0634904 | 2022-02-04 09:19:17 -0800 | [diff] [blame] | 547 | if (!windowInfo.touchableRegionContainsPoint(x, y)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 548 | return false; |
| 549 | } |
| 550 | return true; |
| 551 | } |
| 552 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 553 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 554 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
| 555 | (entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || |
| 556 | entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_ERASER); |
| 557 | } |
| 558 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame^] | 559 | // Determines if the given window can be targeted as InputTarget::FLAG_FOREGROUND. |
| 560 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 561 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 562 | // be sent to such a window, but it is not a foreground event and doesn't use |
| 563 | // InputTarget::FLAG_FOREGROUND. |
| 564 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 565 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 566 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 567 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 568 | } |
| 569 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 570 | } // namespace |
| 571 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 572 | // --- InputDispatcher --- |
| 573 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 574 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 575 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} |
| 576 | |
| 577 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy, |
| 578 | std::chrono::nanoseconds staleEventTimeout) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 579 | : mPolicy(policy), |
| 580 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 581 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 582 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 583 | mAppSwitchSawKeyDown(false), |
| 584 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 585 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 586 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 587 | mDispatchEnabled(false), |
| 588 | mDispatchFrozen(false), |
| 589 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 590 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 591 | // To avoid leaking stack in case that call never comes, and for tests, |
| 592 | // initialize it here anyways. |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 593 | mInTouchMode(kDefaultInTouchMode), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 594 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 595 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 596 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 597 | mStaleEventTimeout(staleEventTimeout), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 598 | mLatencyAggregator(), |
Siarhei Vishniakou | bd25272 | 2022-01-06 03:49:35 -0800 | [diff] [blame] | 599 | mLatencyTracker(&mLatencyAggregator) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 600 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 601 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 603 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 604 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 605 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 606 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 607 | |
| 608 | policy->getDispatcherConfiguration(&mConfig); |
| 609 | } |
| 610 | |
| 611 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 612 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 613 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 614 | resetKeyRepeatLocked(); |
| 615 | releasePendingEventLocked(); |
| 616 | drainInboundQueueLocked(); |
| 617 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 618 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 619 | while (!mConnectionsByToken.empty()) { |
| 620 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 621 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 622 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 626 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 627 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 628 | return ALREADY_EXISTS; |
| 629 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 630 | mThread = std::make_unique<InputThread>( |
| 631 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 632 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 636 | if (mThread && mThread->isCallingThread()) { |
| 637 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 638 | return INVALID_OPERATION; |
| 639 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 640 | mThread.reset(); |
| 641 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 644 | void InputDispatcher::dispatchOnce() { |
| 645 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 646 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 647 | std::scoped_lock _l(mLock); |
| 648 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 649 | |
| 650 | // Run a dispatch loop if there are no pending commands. |
| 651 | // The dispatch loop might enqueue commands to run afterwards. |
| 652 | if (!haveCommandsLocked()) { |
| 653 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 654 | } |
| 655 | |
| 656 | // Run all pending commands if there are any. |
| 657 | // 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] | 658 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 659 | nextWakeupTime = LONG_LONG_MIN; |
| 660 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 661 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 662 | // If we are still waiting for ack on some events, |
| 663 | // we might have to wake up earlier to check if an app is anr'ing. |
| 664 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 665 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 666 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 667 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 668 | // pending or queued events |
| 669 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 670 | mDispatcherEnteredIdle.notify_all(); |
| 671 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 672 | } // release lock |
| 673 | |
| 674 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 675 | nsecs_t currentTime = now(); |
| 676 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 677 | mLooper->pollOnce(timeoutMillis); |
| 678 | } |
| 679 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 680 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 681 | * Raise ANR if there is no focused window. |
| 682 | * Before the ANR is raised, do a final state check: |
| 683 | * 1. The currently focused application must be the same one we are waiting for. |
| 684 | * 2. Ensure we still don't have a focused window. |
| 685 | */ |
| 686 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 687 | // Check if the application that we are waiting for is still focused. |
| 688 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 689 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 690 | if (focusedApplication == nullptr || |
| 691 | focusedApplication->getApplicationToken() != |
| 692 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 693 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 694 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 695 | focusedApplication->getName().c_str()); |
| 696 | return; // The focused application has changed. |
| 697 | } |
| 698 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 699 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 700 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 701 | if (focusedWindowHandle != nullptr) { |
| 702 | return; // We now have a focused window. No need for ANR. |
| 703 | } |
| 704 | onAnrLocked(mAwaitedFocusedApplication); |
| 705 | } |
| 706 | |
| 707 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 708 | * Check if any of the connections' wait queues have events that are too old. |
| 709 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 710 | * Return the time at which we should wake up next. |
| 711 | */ |
| 712 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 713 | const nsecs_t currentTime = now(); |
| 714 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 715 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 716 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 717 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 718 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 719 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 720 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 721 | return LONG_LONG_MIN; |
| 722 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 723 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 724 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | // Check if any connection ANRs are due |
| 729 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 730 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 731 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 732 | } |
| 733 | |
| 734 | // If we reached here, we have an unresponsive connection. |
| 735 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 736 | if (connection == nullptr) { |
| 737 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 738 | return nextAnrCheck; |
| 739 | } |
| 740 | connection->responsive = false; |
| 741 | // Stop waking up for this unresponsive connection |
| 742 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 743 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 744 | return LONG_LONG_MIN; |
| 745 | } |
| 746 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 747 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
| 748 | const sp<Connection>& connection) { |
| 749 | if (connection->monitor) { |
| 750 | return mMonitorDispatchingTimeout; |
| 751 | } |
| 752 | const sp<WindowInfoHandle> window = |
| 753 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 754 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 755 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 756 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 757 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 760 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 761 | nsecs_t currentTime = now(); |
| 762 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 763 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 764 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 765 | // repeat if the device is just coming out of sleep. |
| 766 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 767 | resetKeyRepeatLocked(); |
| 768 | } |
| 769 | |
| 770 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 771 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 772 | if (DEBUG_FOCUS) { |
| 773 | ALOGD("Dispatch frozen. Waiting some more."); |
| 774 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 775 | return; |
| 776 | } |
| 777 | |
| 778 | // Optimize latency of app switches. |
| 779 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 780 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 781 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 782 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 783 | *nextWakeupTime = mAppSwitchDueTime; |
| 784 | } |
| 785 | |
| 786 | // Ready to start a new event. |
| 787 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 788 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 789 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 790 | if (isAppSwitchDue) { |
| 791 | // The inbound queue is empty so the app switch key we were waiting |
| 792 | // for will never arrive. Stop waiting for it. |
| 793 | resetPendingAppSwitchLocked(false); |
| 794 | isAppSwitchDue = false; |
| 795 | } |
| 796 | |
| 797 | // Synthesize a key repeat if appropriate. |
| 798 | if (mKeyRepeatState.lastKeyEntry) { |
| 799 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 800 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 801 | } else { |
| 802 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 803 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | // Nothing to do if there is no pending event. |
| 809 | if (!mPendingEvent) { |
| 810 | return; |
| 811 | } |
| 812 | } else { |
| 813 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 814 | mPendingEvent = mInboundQueue.front(); |
| 815 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 816 | traceInboundQueueLengthLocked(); |
| 817 | } |
| 818 | |
| 819 | // Poke user activity for this event. |
| 820 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 821 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 822 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | // Now we have an event to dispatch. |
| 826 | // 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] | 827 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 828 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 829 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 830 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 831 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 832 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 833 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 837 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 841 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 842 | const ConfigurationChangedEntry& typedEntry = |
| 843 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 844 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 845 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 846 | break; |
| 847 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 848 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 849 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 850 | const DeviceResetEntry& typedEntry = |
| 851 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 852 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 853 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 854 | break; |
| 855 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 856 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 857 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 858 | std::shared_ptr<FocusEntry> typedEntry = |
| 859 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 860 | dispatchFocusLocked(currentTime, typedEntry); |
| 861 | done = true; |
| 862 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 863 | break; |
| 864 | } |
| 865 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 866 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 867 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 868 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 869 | done = true; |
| 870 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 871 | break; |
| 872 | } |
| 873 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 874 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 875 | const auto typedEntry = |
| 876 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 877 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 878 | done = true; |
| 879 | break; |
| 880 | } |
| 881 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 882 | case EventEntry::Type::DRAG: { |
| 883 | std::shared_ptr<DragEntry> typedEntry = |
| 884 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 885 | dispatchDragLocked(currentTime, typedEntry); |
| 886 | done = true; |
| 887 | break; |
| 888 | } |
| 889 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 890 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 891 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 892 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 893 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 894 | resetPendingAppSwitchLocked(true); |
| 895 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 896 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 897 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 900 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 901 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 902 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 903 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 904 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 905 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 906 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 907 | break; |
| 908 | } |
| 909 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 910 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 911 | std::shared_ptr<MotionEntry> motionEntry = |
| 912 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 913 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 914 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 915 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 916 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 917 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 918 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 919 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 920 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 921 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 922 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 923 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 924 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 925 | |
| 926 | case EventEntry::Type::SENSOR: { |
| 927 | std::shared_ptr<SensorEntry> sensorEntry = |
| 928 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 929 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 930 | dropReason = DropReason::APP_SWITCH; |
| 931 | } |
| 932 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 933 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 934 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 935 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 936 | dropReason = DropReason::STALE; |
| 937 | } |
| 938 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 939 | done = true; |
| 940 | break; |
| 941 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 945 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 946 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 947 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 948 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 949 | |
| 950 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 951 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 955 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 956 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 957 | } |
| 958 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 959 | /** |
| 960 | * Return true if the events preceding this incoming motion event should be dropped |
| 961 | * Return false otherwise (the default behaviour) |
| 962 | */ |
| 963 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 964 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 965 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 966 | |
| 967 | // Optimize case where the current application is unresponsive and the user |
| 968 | // decides to touch a window in a different application. |
| 969 | // If the application takes too long to catch up then we drop all events preceding |
| 970 | // the touch into the other window. |
| 971 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 972 | int32_t displayId = motionEntry.displayId; |
| 973 | int32_t x = static_cast<int32_t>( |
| 974 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 975 | int32_t y = static_cast<int32_t>( |
| 976 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 977 | |
| 978 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 979 | sp<WindowInfoHandle> touchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 980 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 981 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 982 | touchedWindowHandle->getApplicationToken() != |
| 983 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 984 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 985 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 986 | "for %s", |
| 987 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 988 | return true; |
| 989 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 990 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 991 | // Alternatively, maybe there's a spy window that could handle this event. |
| 992 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 993 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 994 | for (const auto& windowHandle : touchedSpies) { |
| 995 | const sp<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 996 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 997 | // This spy window could take more input. Drop all events preceding this |
| 998 | // 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] | 999 | 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] | 1000 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1001 | mAwaitedFocusedApplication->getName().c_str()); |
| 1002 | return true; |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 1008 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 1009 | // events to be processed before dispatching the key event. This is because these motion events |
| 1010 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 1011 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 1012 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1013 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1014 | "just send the pending key event to the focused window."); |
| 1015 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1016 | } |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1020 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1021 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1022 | mInboundQueue.push_back(std::move(newEntry)); |
| 1023 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1024 | traceInboundQueueLengthLocked(); |
| 1025 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1026 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1027 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1028 | // Optimize app switch latency. |
| 1029 | // If the application takes too long to catch up then we drop all events preceding |
| 1030 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1031 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1032 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1033 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1034 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1035 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1036 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1037 | if (DEBUG_APP_SWITCH) { |
| 1038 | ALOGD("App switch is pending!"); |
| 1039 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1040 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1041 | mAppSwitchSawKeyDown = false; |
| 1042 | needWake = true; |
| 1043 | } |
| 1044 | } |
| 1045 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1046 | |
| 1047 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1048 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1049 | // time for it may have been handled in the policy and could be dropped. |
| 1050 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1051 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1052 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1053 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1054 | pendingKey.interceptKeyResult == |
| 1055 | KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1056 | pendingKey.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1057 | pendingKey.interceptKeyWakeupTime = 0; |
| 1058 | needWake = true; |
| 1059 | } |
| 1060 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1061 | break; |
| 1062 | } |
| 1063 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1064 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1065 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1066 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1067 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1068 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1069 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1070 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1071 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1072 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1073 | break; |
| 1074 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1075 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1076 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1077 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1078 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1079 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1080 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1081 | // nothing to do |
| 1082 | break; |
| 1083 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | return needWake; |
| 1087 | } |
| 1088 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1089 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1090 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1091 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1092 | mRecentQueue.push_back(entry); |
| 1093 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1094 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1095 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1099 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1100 | int32_t y, TouchState* touchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1101 | bool isStylus, |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1102 | bool addOutsideTargets, |
| 1103 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1104 | if (addOutsideTargets && touchState == nullptr) { |
| 1105 | 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] | 1106 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1107 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1108 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1109 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1110 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1111 | continue; |
| 1112 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1113 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1114 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1115 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1116 | return windowHandle; |
| 1117 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1118 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1119 | if (addOutsideTargets && |
| 1120 | info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1121 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1122 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1123 | } |
| 1124 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1125 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1126 | } |
| 1127 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1128 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1129 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1130 | // Traverse windows from front to back and gather the touched spy windows. |
| 1131 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1132 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1133 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1134 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1135 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1136 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1137 | continue; |
| 1138 | } |
| 1139 | if (!info.isSpy()) { |
| 1140 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1141 | return spyWindows; |
| 1142 | } |
| 1143 | spyWindows.push_back(windowHandle); |
| 1144 | } |
| 1145 | return spyWindows; |
| 1146 | } |
| 1147 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1148 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1149 | const char* reason; |
| 1150 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1151 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1152 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1153 | ALOGD("Dropped event because policy consumed it."); |
| 1154 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1155 | reason = "inbound event was dropped because the policy consumed it"; |
| 1156 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1157 | case DropReason::DISABLED: |
| 1158 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1159 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1160 | } |
| 1161 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1162 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1163 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1164 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1165 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1166 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1167 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1168 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1169 | "has started interacting with a different application."); |
| 1170 | reason = "inbound event was dropped because the current application is not responding " |
| 1171 | "and the user has started interacting with a different application"; |
| 1172 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1173 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1174 | ALOGI("Dropped event because it is stale."); |
| 1175 | reason = "inbound event was dropped because it is stale"; |
| 1176 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1177 | case DropReason::NO_POINTER_CAPTURE: |
| 1178 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1179 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1180 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1181 | case DropReason::NOT_DROPPED: { |
| 1182 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1183 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1184 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1187 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1188 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1189 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1190 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1191 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1192 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1193 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1194 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1195 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1196 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1197 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1198 | } else { |
| 1199 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1200 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1201 | } |
| 1202 | break; |
| 1203 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1204 | case EventEntry::Type::SENSOR: { |
| 1205 | break; |
| 1206 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1207 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1208 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1209 | break; |
| 1210 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1211 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1212 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1213 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1214 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1215 | 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] | 1216 | break; |
| 1217 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1221 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1222 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1223 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1224 | } |
| 1225 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1226 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1227 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1228 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1229 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1233 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1234 | } |
| 1235 | |
| 1236 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1237 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1238 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1239 | if (DEBUG_APP_SWITCH) { |
| 1240 | if (handled) { |
| 1241 | ALOGD("App switch has arrived."); |
| 1242 | } else { |
| 1243 | ALOGD("App switch was abandoned."); |
| 1244 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1245 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1246 | } |
| 1247 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1248 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1249 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1252 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1253 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | return false; |
| 1255 | } |
| 1256 | |
| 1257 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1258 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1259 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1260 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1261 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1262 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1263 | return true; |
| 1264 | } |
| 1265 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1266 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1267 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1271 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1272 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1273 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1274 | releaseInboundEventLocked(entry); |
| 1275 | } |
| 1276 | traceInboundQueueLengthLocked(); |
| 1277 | } |
| 1278 | |
| 1279 | void InputDispatcher::releasePendingEventLocked() { |
| 1280 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1281 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1282 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1283 | } |
| 1284 | } |
| 1285 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1286 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1287 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1288 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1289 | if (DEBUG_DISPATCH_CYCLE) { |
| 1290 | ALOGD("Injected inbound event was dropped."); |
| 1291 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1292 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1293 | } |
| 1294 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1295 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1296 | } |
| 1297 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1301 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1302 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1306 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1307 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1308 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1309 | uint32_t policyFlags = entry->policyFlags & |
| 1310 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1311 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1312 | std::shared_ptr<KeyEntry> newEntry = |
| 1313 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1314 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1315 | entry->flags, entry->keyCode, entry->scanCode, |
| 1316 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1317 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1318 | newEntry->syntheticRepeat = true; |
| 1319 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1320 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1321 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1324 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1325 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1326 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1327 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1328 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1329 | |
| 1330 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1331 | resetKeyRepeatLocked(); |
| 1332 | |
| 1333 | // 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] | 1334 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1335 | scoped_unlock unlock(mLock); |
| 1336 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1337 | }; |
| 1338 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1339 | return true; |
| 1340 | } |
| 1341 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1342 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1343 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1344 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1345 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1346 | entry.deviceId); |
| 1347 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1348 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1349 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1350 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1351 | resetKeyRepeatLocked(); |
| 1352 | } |
| 1353 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1354 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1355 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1356 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1357 | return true; |
| 1358 | } |
| 1359 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1360 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1361 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1362 | if (mPendingEvent != nullptr) { |
| 1363 | // Move the pending event to the front of the queue. This will give the chance |
| 1364 | // for the pending event to get dispatched to the newly focused window |
| 1365 | mInboundQueue.push_front(mPendingEvent); |
| 1366 | mPendingEvent = nullptr; |
| 1367 | } |
| 1368 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1369 | std::unique_ptr<FocusEntry> focusEntry = |
| 1370 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1371 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1372 | |
| 1373 | // This event should go to the front of the queue, but behind all other focus events |
| 1374 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1375 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1376 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1377 | [](const std::shared_ptr<EventEntry>& event) { |
| 1378 | return event->type == EventEntry::Type::FOCUS; |
| 1379 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1380 | |
| 1381 | // 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] | 1382 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1383 | } |
| 1384 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1385 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1386 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1387 | if (channel == nullptr) { |
| 1388 | return; // Window has gone away |
| 1389 | } |
| 1390 | InputTarget target; |
| 1391 | target.inputChannel = channel; |
| 1392 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1393 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1394 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1395 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1396 | std::string reason = std::string("reason=").append(entry->reason); |
| 1397 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1398 | dispatchEventLocked(currentTime, entry, {target}); |
| 1399 | } |
| 1400 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1401 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1402 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1403 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1404 | dropReason = DropReason::NOT_DROPPED; |
| 1405 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1406 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1407 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1408 | |
| 1409 | if (entry->pointerCaptureRequest.enable) { |
| 1410 | // Enable Pointer Capture. |
| 1411 | if (haveWindowWithPointerCapture && |
| 1412 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
| 1413 | LOG_ALWAYS_FATAL("This request to enable Pointer Capture has already been dispatched " |
| 1414 | "to the window."); |
| 1415 | } |
| 1416 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1417 | // This can happen if a window requests capture and immediately releases capture. |
| 1418 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1419 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1420 | return; |
| 1421 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1422 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1423 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1424 | return; |
| 1425 | } |
| 1426 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1427 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1428 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1429 | mWindowTokenWithPointerCapture = token; |
| 1430 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1431 | // Disable Pointer Capture. |
| 1432 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1433 | // for two reasons: |
| 1434 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1435 | // to disable capture with the same sequence number: one generated by |
| 1436 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1437 | // Capture being disabled in InputReader. |
| 1438 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1439 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1440 | // in InputReader. |
| 1441 | if (!haveWindowWithPointerCapture) { |
| 1442 | // Pointer capture was already forcefully disabled because of focus change. |
| 1443 | dropReason = DropReason::NOT_DROPPED; |
| 1444 | return; |
| 1445 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1446 | token = mWindowTokenWithPointerCapture; |
| 1447 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1448 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1449 | setPointerCaptureLocked(false); |
| 1450 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | auto channel = getInputChannelLocked(token); |
| 1454 | if (channel == nullptr) { |
| 1455 | // Window has gone away, clean up Pointer Capture state. |
| 1456 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1457 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1458 | setPointerCaptureLocked(false); |
| 1459 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1460 | return; |
| 1461 | } |
| 1462 | InputTarget target; |
| 1463 | target.inputChannel = channel; |
| 1464 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1465 | entry->dispatchInProgress = true; |
| 1466 | dispatchEventLocked(currentTime, entry, {target}); |
| 1467 | |
| 1468 | dropReason = DropReason::NOT_DROPPED; |
| 1469 | } |
| 1470 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1471 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1472 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1473 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1474 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1475 | if (windowHandles.empty()) { |
| 1476 | return; |
| 1477 | } |
| 1478 | const std::vector<InputTarget> inputTargets = |
| 1479 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1480 | if (inputTargets.empty()) { |
| 1481 | return; |
| 1482 | } |
| 1483 | entry->dispatchInProgress = true; |
| 1484 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1485 | } |
| 1486 | |
| 1487 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1488 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1489 | std::vector<InputTarget> inputTargets; |
| 1490 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1491 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1492 | const sp<IBinder>& token = handle->getToken(); |
| 1493 | if (token == nullptr) { |
| 1494 | continue; |
| 1495 | } |
| 1496 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1497 | if (channel == nullptr) { |
| 1498 | continue; // Window has gone away |
| 1499 | } |
| 1500 | InputTarget target; |
| 1501 | target.inputChannel = channel; |
| 1502 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1503 | inputTargets.push_back(target); |
| 1504 | } |
| 1505 | return inputTargets; |
| 1506 | } |
| 1507 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1508 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1509 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1510 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1511 | if (!entry->dispatchInProgress) { |
| 1512 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1513 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1514 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1515 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1516 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1517 | // We have seen two identical key downs in a row which indicates that the device |
| 1518 | // driver is automatically generating key repeats itself. We take note of the |
| 1519 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1520 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1521 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1522 | // Make sure we don't get key down from a different device. If a different |
| 1523 | // device Id has same key pressed down, the new device Id will replace the |
| 1524 | // current one to hold the key repeat with repeat count reset. |
| 1525 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1526 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1527 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1528 | resetKeyRepeatLocked(); |
| 1529 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1530 | } else { |
| 1531 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1532 | resetKeyRepeatLocked(); |
| 1533 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1534 | } |
| 1535 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1536 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1537 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1538 | // 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] | 1539 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1540 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1541 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1542 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1543 | resetKeyRepeatLocked(); |
| 1544 | } |
| 1545 | |
| 1546 | if (entry->repeatCount == 1) { |
| 1547 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1548 | } else { |
| 1549 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1550 | } |
| 1551 | |
| 1552 | entry->dispatchInProgress = true; |
| 1553 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1554 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | // Handle case where the policy asked us to try again later last time. |
| 1558 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1559 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1560 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1561 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1562 | } |
| 1563 | return false; // wait until next wakeup |
| 1564 | } |
| 1565 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1566 | entry->interceptKeyWakeupTime = 0; |
| 1567 | } |
| 1568 | |
| 1569 | // Give the policy a chance to intercept the key. |
| 1570 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1571 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1572 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1573 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1574 | |
| 1575 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1576 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1577 | }; |
| 1578 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1579 | return false; // wait for the command to run |
| 1580 | } else { |
| 1581 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1582 | } |
| 1583 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1584 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1585 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1590 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1591 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1592 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1593 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1594 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1595 | return true; |
| 1596 | } |
| 1597 | |
| 1598 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1599 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1600 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1601 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1602 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1603 | return false; |
| 1604 | } |
| 1605 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1606 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1607 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1608 | return true; |
| 1609 | } |
| 1610 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1611 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1612 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1613 | |
| 1614 | // Dispatch the key. |
| 1615 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1616 | return true; |
| 1617 | } |
| 1618 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1619 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1620 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1621 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1622 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1623 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1624 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1625 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1626 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1627 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1628 | } |
| 1629 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1630 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1631 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1632 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1633 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1634 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1635 | "source=0x%x, sensorType=%s", |
| 1636 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1637 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1638 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1639 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1640 | scoped_unlock unlock(mLock); |
| 1641 | |
| 1642 | if (entry->accuracyChanged) { |
| 1643 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1644 | } |
| 1645 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1646 | entry->hwTimestamp, entry->values); |
| 1647 | }; |
| 1648 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1652 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1653 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1654 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1655 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1656 | { // acquire lock |
| 1657 | std::scoped_lock _l(mLock); |
| 1658 | |
| 1659 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1660 | std::shared_ptr<EventEntry> entry = *it; |
| 1661 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1662 | it = mInboundQueue.erase(it); |
| 1663 | releaseInboundEventLocked(entry); |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | return true; |
| 1668 | } |
| 1669 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1670 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1671 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1672 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1673 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1674 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1675 | entry->dispatchInProgress = true; |
| 1676 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1677 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1681 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1682 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1683 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1684 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1685 | return true; |
| 1686 | } |
| 1687 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1688 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1689 | |
| 1690 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1691 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1692 | |
| 1693 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1694 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1695 | if (isPointerEvent) { |
| 1696 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1697 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1698 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1699 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1700 | } else { |
| 1701 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1702 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1703 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1704 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1705 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1706 | return false; |
| 1707 | } |
| 1708 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1709 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1710 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1711 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1712 | return true; |
| 1713 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1714 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1715 | CancelationOptions::Mode mode(isPointerEvent |
| 1716 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1717 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1718 | CancelationOptions options(mode, "input event injection failed"); |
| 1719 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1720 | return true; |
| 1721 | } |
| 1722 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1723 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1724 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1725 | |
| 1726 | // Dispatch the motion. |
| 1727 | if (conflictingPointerActions) { |
| 1728 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1729 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1730 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1731 | } |
| 1732 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1733 | return true; |
| 1734 | } |
| 1735 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1736 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1737 | bool isExiting, const MotionEntry& motionEntry) { |
| 1738 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1739 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1740 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1741 | PointerCoords pointerCoords; |
| 1742 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1743 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1744 | |
| 1745 | std::unique_ptr<DragEntry> dragEntry = |
| 1746 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1747 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1748 | pointerCoords.getY()); |
| 1749 | |
| 1750 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1751 | } |
| 1752 | |
| 1753 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1754 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1755 | if (channel == nullptr) { |
| 1756 | return; // Window has gone away |
| 1757 | } |
| 1758 | InputTarget target; |
| 1759 | target.inputChannel = channel; |
| 1760 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1761 | entry->dispatchInProgress = true; |
| 1762 | dispatchEventLocked(currentTime, entry, {target}); |
| 1763 | } |
| 1764 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1765 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1766 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1767 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1768 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1769 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1770 | "metaState=0x%x, buttonState=0x%x," |
| 1771 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1772 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1773 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1774 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1775 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1776 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1777 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1778 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1779 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1780 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1781 | "orientation=%f", |
| 1782 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1783 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1784 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1785 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1786 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1787 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1788 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1789 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1790 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1791 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1792 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1793 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1794 | } |
| 1795 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1796 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1797 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1798 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1799 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1800 | if (DEBUG_DISPATCH_CYCLE) { |
| 1801 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1802 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1803 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1804 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1805 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1806 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1807 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1808 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1809 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1810 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1811 | sp<Connection> connection = |
| 1812 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1813 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1814 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1815 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1816 | if (DEBUG_FOCUS) { |
| 1817 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1818 | "is no longer registered with the input dispatcher.", |
| 1819 | inputTarget.inputChannel->getName().c_str()); |
| 1820 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1821 | } |
| 1822 | } |
| 1823 | } |
| 1824 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1825 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1826 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1827 | // If the policy decides to close the app, we will get a channel removal event via |
| 1828 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1829 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1830 | // pile up. |
| 1831 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1832 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1833 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1834 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1835 | "application not responding"); |
| 1836 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1840 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1841 | if (DEBUG_FOCUS) { |
| 1842 | ALOGD("Resetting ANR timeouts."); |
| 1843 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1844 | |
| 1845 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1846 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1847 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1848 | } |
| 1849 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1850 | /** |
| 1851 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1852 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1853 | * Focused display is the display that the user most recently interacted with. |
| 1854 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1855 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1856 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1857 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1858 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1859 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1860 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1861 | break; |
| 1862 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1863 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1864 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1865 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1866 | break; |
| 1867 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1868 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1869 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1870 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1871 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1872 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1873 | case EventEntry::Type::SENSOR: |
| 1874 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1875 | 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] | 1876 | return ADISPLAY_ID_NONE; |
| 1877 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1878 | } |
| 1879 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1880 | } |
| 1881 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1882 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1883 | const char* focusedWindowName) { |
| 1884 | if (mAnrTracker.empty()) { |
| 1885 | // already processed all events that we waited for |
| 1886 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1887 | return false; |
| 1888 | } |
| 1889 | |
| 1890 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1891 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1892 | // 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] | 1893 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1894 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1895 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1896 | return true; |
| 1897 | } |
| 1898 | |
| 1899 | // We still have pending events, and already started the timer |
| 1900 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1901 | return true; // Still waiting |
| 1902 | } |
| 1903 | |
| 1904 | // Waited too long, and some connection still hasn't processed all motions |
| 1905 | // Just send the key to the focused window |
| 1906 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1907 | focusedWindowName); |
| 1908 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1909 | return false; |
| 1910 | } |
| 1911 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1912 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1913 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1914 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1915 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1916 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1917 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1918 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1919 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1920 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1921 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1922 | // If there is no currently focused window and no focused application |
| 1923 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1924 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1925 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1926 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1927 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1928 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1929 | } |
| 1930 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1931 | // Drop key events if requested by input feature |
| 1932 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1933 | return InputEventInjectionResult::FAILED; |
| 1934 | } |
| 1935 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1936 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1937 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1938 | // start interacting with another application via touch (app switch). This code can be removed |
| 1939 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1940 | // an app is expected to have a focused window. |
| 1941 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1942 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1943 | // 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] | 1944 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1945 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1946 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1947 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1948 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1949 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1950 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1951 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1952 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1953 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1954 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1955 | // Already raised ANR. Drop the event |
| 1956 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1957 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1958 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1959 | } else { |
| 1960 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1961 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | // we have a valid, non-null focused window |
| 1966 | resetNoFocusedWindowTimeoutLocked(); |
| 1967 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1968 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1969 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1970 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1971 | } |
| 1972 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1973 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 1974 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1975 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1976 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | // If the event is a key event, then we must wait for all previous events to |
| 1980 | // complete before delivering it because previous events may have the |
| 1981 | // side-effect of transferring focus to a different window and we want to |
| 1982 | // ensure that the following keys are sent to the new window. |
| 1983 | // |
| 1984 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1985 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1986 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1987 | // often anticipate pending UI changes when typing on a keyboard. |
| 1988 | // To obtain this behavior, we must serialize key events with respect to all |
| 1989 | // prior input events. |
| 1990 | if (entry.type == EventEntry::Type::KEY) { |
| 1991 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1992 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1993 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1994 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1998 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1999 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2000 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2001 | |
| 2002 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2003 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2004 | } |
| 2005 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2006 | /** |
| 2007 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2008 | * that are currently unresponsive. |
| 2009 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2010 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2011 | const std::vector<Monitor>& monitors) const { |
| 2012 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2013 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2014 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 2015 | sp<Connection> connection = |
| 2016 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2017 | if (connection == nullptr) { |
| 2018 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2019 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2020 | return false; |
| 2021 | } |
| 2022 | if (!connection->responsive) { |
| 2023 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2024 | connection->inputChannel->getName().c_str()); |
| 2025 | return false; |
| 2026 | } |
| 2027 | return true; |
| 2028 | }); |
| 2029 | return responsiveMonitors; |
| 2030 | } |
| 2031 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2032 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 2033 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 2034 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2035 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2036 | enum InjectionPermission { |
| 2037 | INJECTION_PERMISSION_UNKNOWN, |
| 2038 | INJECTION_PERMISSION_GRANTED, |
| 2039 | INJECTION_PERMISSION_DENIED |
| 2040 | }; |
| 2041 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2042 | // For security reasons, we defer updating the touch state until we are sure that |
| 2043 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2044 | const int32_t displayId = entry.displayId; |
| 2045 | const int32_t action = entry.action; |
| 2046 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2047 | |
| 2048 | // 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] | 2049 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2050 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2051 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 2052 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2053 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2054 | // Copy current touch state into tempTouchState. |
| 2055 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2056 | // 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] | 2057 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2058 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2059 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2060 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2061 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2062 | } |
| 2063 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2064 | bool isSplit = tempTouchState.split; |
| 2065 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 2066 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 2067 | tempTouchState.displayId != displayId); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2068 | |
| 2069 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2070 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2071 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2072 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2073 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2074 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2075 | bool wrongDevice = false; |
| 2076 | if (newGesture) { |
| 2077 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2078 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2079 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2080 | "in display %" PRId32, |
| 2081 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2082 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2083 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2084 | switchedDevice = false; |
| 2085 | wrongDevice = true; |
| 2086 | goto Failed; |
| 2087 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2088 | tempTouchState.reset(); |
| 2089 | tempTouchState.down = down; |
| 2090 | tempTouchState.deviceId = entry.deviceId; |
| 2091 | tempTouchState.source = entry.source; |
| 2092 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2093 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2094 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2095 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2096 | "in display %" PRId32, |
| 2097 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2098 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2099 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2100 | switchedDevice = false; |
| 2101 | wrongDevice = true; |
| 2102 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2106 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2107 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2108 | int32_t x; |
| 2109 | int32_t y; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2110 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2111 | // Always dispatch mouse events to cursor position. |
| 2112 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2113 | x = int32_t(entry.xCursorPosition); |
| 2114 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2115 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2116 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2117 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2118 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2119 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2120 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2121 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2122 | isStylus, isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2123 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2124 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2125 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2126 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2127 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2128 | // 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] | 2129 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2132 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2133 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2134 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2135 | // New window supports splitting, but we should never split mouse events. |
| 2136 | isSplit = !isFromMouse; |
| 2137 | } else if (isSplit) { |
| 2138 | // New window does not support splitting but we have already split events. |
| 2139 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2140 | newTouchedWindowHandle = nullptr; |
| 2141 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2142 | } else { |
| 2143 | // 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] | 2144 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2145 | // should never be split. |
| 2146 | tempTouchState.split = isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2149 | // Update hover state. |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2150 | if (newTouchedWindowHandle != nullptr) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2151 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2152 | newHoverWindowHandle = nullptr; |
| 2153 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2154 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2155 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2156 | } |
| 2157 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2158 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2159 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2160 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2161 | // Process the foreground window first so that it is the first to receive the event. |
| 2162 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2163 | } |
| 2164 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2165 | if (newTouchedWindows.empty()) { |
| 2166 | ALOGI("Dropping event because there is no touchable window at (%d, %d) on display %d.", |
| 2167 | x, y, displayId); |
| 2168 | injectionResult = InputEventInjectionResult::FAILED; |
| 2169 | goto Failed; |
| 2170 | } |
| 2171 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2172 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
| 2173 | const WindowInfo& info = *windowHandle->getInfo(); |
| 2174 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2175 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2176 | ALOGI("Not sending touch event to %s because it is paused", |
| 2177 | windowHandle->getName().c_str()); |
| 2178 | continue; |
| 2179 | } |
| 2180 | |
| 2181 | // Ensure the window has a connection and the connection is responsive |
| 2182 | const bool isResponsive = hasResponsiveConnectionLocked(*windowHandle); |
| 2183 | if (!isResponsive) { |
| 2184 | ALOGW("Not sending touch gesture to %s because it is not responsive", |
| 2185 | windowHandle->getName().c_str()); |
| 2186 | continue; |
| 2187 | } |
| 2188 | |
| 2189 | // Drop events that can't be trusted due to occlusion |
| 2190 | if (mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2191 | TouchOcclusionInfo occlusionInfo = |
| 2192 | computeTouchOcclusionInfoLocked(windowHandle, x, y); |
| 2193 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 2194 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2195 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2196 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2197 | ALOGD("%s", log.c_str()); |
| 2198 | } |
| 2199 | } |
| 2200 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
| 2201 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2202 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2203 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2204 | continue; |
| 2205 | } |
| 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | // Drop touch events if requested by input feature |
| 2210 | if (shouldDropInput(entry, windowHandle)) { |
| 2211 | continue; |
| 2212 | } |
| 2213 | |
| 2214 | // Set target flags. |
| 2215 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2216 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame^] | 2217 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2218 | // There should only be one touched window that can be "foreground" for the pointer. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2219 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2220 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2221 | |
| 2222 | if (isSplit) { |
| 2223 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2224 | } |
| 2225 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
| 2226 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2227 | } else if (isWindowObscuredLocked(windowHandle)) { |
| 2228 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2229 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2230 | |
| 2231 | // Update the temporary touch state. |
| 2232 | BitSet32 pointerIds; |
| 2233 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2234 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2235 | pointerIds.markBit(pointerId); |
| 2236 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2237 | |
| 2238 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2239 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2240 | } else { |
| 2241 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2242 | |
| 2243 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2244 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2245 | if (DEBUG_FOCUS) { |
| 2246 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2247 | "dropped the pointer down event in display %" PRId32, |
| 2248 | displayId); |
| 2249 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2250 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2251 | goto Failed; |
| 2252 | } |
| 2253 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2254 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2255 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2256 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2257 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2258 | tempTouchState.isSlippery()) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2259 | const int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2260 | 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] | 2261 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2262 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2263 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2264 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2265 | newTouchedWindowHandle = |
| 2266 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2267 | |
| 2268 | // Drop touch events if requested by input feature |
| 2269 | if (newTouchedWindowHandle != nullptr && |
| 2270 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2271 | newTouchedWindowHandle = nullptr; |
| 2272 | } |
| 2273 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2274 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2275 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2276 | if (DEBUG_FOCUS) { |
| 2277 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2278 | oldTouchedWindowHandle->getName().c_str(), |
| 2279 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2280 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2281 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2282 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2283 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2284 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2285 | |
| 2286 | // Make a slippery entrance into the new window. |
| 2287 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2288 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2289 | } |
| 2290 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame^] | 2291 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
| 2292 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
| 2293 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2294 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2295 | if (isSplit) { |
| 2296 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2297 | } |
| 2298 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2299 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2300 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2301 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | BitSet32 pointerIds; |
| 2305 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2306 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2307 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2308 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2309 | } |
| 2310 | } |
| 2311 | } |
| 2312 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2313 | // Update dispatching for hover enter and exit. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2314 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2315 | // Let the previous window know that the hover sequence is over, unless we already did |
| 2316 | // it when dispatching it as is to newTouchedWindowHandle. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2317 | if (mLastHoverWindowHandle != nullptr && |
| 2318 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2319 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2320 | if (DEBUG_HOVER) { |
| 2321 | ALOGD("Sending hover exit event to window %s.", |
| 2322 | mLastHoverWindowHandle->getName().c_str()); |
| 2323 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2324 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2325 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2326 | } |
| 2327 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2328 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2329 | // when dispatching it as is to newTouchedWindowHandle. |
| 2330 | if (newHoverWindowHandle != nullptr && |
| 2331 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2332 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2333 | if (DEBUG_HOVER) { |
| 2334 | ALOGD("Sending hover enter event to window %s.", |
| 2335 | newHoverWindowHandle->getName().c_str()); |
| 2336 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2337 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2338 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2339 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2340 | } |
| 2341 | } |
| 2342 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame^] | 2343 | // Ensure that we have at least one foreground window or at least one window that cannot be a |
| 2344 | // foreground target. If we only have windows that are not receiving foreground touches (e.g. we |
| 2345 | // only have windows getting ACTION_OUTSIDE), then drop the event, because there is no window |
| 2346 | // that is actually receiving the entire gesture. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2347 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2348 | [](const TouchedWindow& touchedWindow) { |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame^] | 2349 | return !canReceiveForegroundTouches( |
| 2350 | *touchedWindow.windowHandle->getInfo()) || |
| 2351 | (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2352 | })) { |
| 2353 | ALOGI("Dropping event because there is no touched window on display %d to receive it.", |
| 2354 | displayId); |
| 2355 | injectionResult = InputEventInjectionResult::FAILED; |
| 2356 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2357 | } |
| 2358 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2359 | // Check permission to inject into all touched foreground windows. |
| 2360 | if (std::any_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2361 | [this, &entry](const TouchedWindow& touchedWindow) { |
| 2362 | return (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0 && |
| 2363 | !checkInjectionPermission(touchedWindow.windowHandle, |
| 2364 | entry.injectionState); |
| 2365 | })) { |
| 2366 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
| 2367 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2368 | goto Failed; |
| 2369 | } |
| 2370 | // Permission granted to inject into all touched foreground windows. |
| 2371 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2372 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2373 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2374 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2375 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2376 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2377 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2378 | if (foregroundWindowHandle) { |
| 2379 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2380 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2381 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2382 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2383 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2384 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2385 | InputTarget::FLAG_ZERO_COORDS, |
| 2386 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2387 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2388 | } |
| 2389 | } |
| 2390 | } |
| 2391 | } |
| 2392 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2393 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2394 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2395 | // of the touch gesture. |
| 2396 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2397 | // engine only supports touch events. We would need to add a mechanism similar |
| 2398 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2399 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2400 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2401 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2402 | if (foregroundWindowHandle && |
| 2403 | foregroundWindowHandle->getInfo()->inputConfig.test( |
| 2404 | WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2405 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2406 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2407 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2408 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2409 | if (info->displayId == displayId && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2410 | windowHandle->getInfo()->inputConfig.test( |
| 2411 | WindowInfo::InputConfig::IS_WALLPAPER)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2412 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2413 | .addOrUpdateWindow(windowHandle, |
| 2414 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2415 | InputTarget:: |
| 2416 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2417 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2418 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2419 | } |
| 2420 | } |
| 2421 | } |
| 2422 | } |
| 2423 | |
| 2424 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2425 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2426 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2427 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2428 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2429 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | // Drop the outside or hover touch windows since we will not care about them |
| 2433 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2434 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2435 | |
| 2436 | Failed: |
| 2437 | // Check injection permission once and for all. |
| 2438 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2439 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2440 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2441 | } else { |
| 2442 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2443 | } |
| 2444 | } |
| 2445 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2446 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2447 | return injectionResult; |
| 2448 | } |
| 2449 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2450 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2451 | if (!wrongDevice) { |
| 2452 | if (switchedDevice) { |
| 2453 | if (DEBUG_FOCUS) { |
| 2454 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2455 | } |
| 2456 | *outConflictingPointerActions = true; |
| 2457 | } |
| 2458 | |
| 2459 | if (isHoverAction) { |
| 2460 | // Started hovering, therefore no longer down. |
| 2461 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2462 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2463 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2464 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2465 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2466 | *outConflictingPointerActions = true; |
| 2467 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2468 | tempTouchState.reset(); |
| 2469 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2470 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2471 | tempTouchState.deviceId = entry.deviceId; |
| 2472 | tempTouchState.source = entry.source; |
| 2473 | tempTouchState.displayId = displayId; |
| 2474 | } |
| 2475 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2476 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2477 | // All pointers up or canceled. |
| 2478 | tempTouchState.reset(); |
| 2479 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2480 | // First pointer went down. |
| 2481 | if (oldState && oldState->down) { |
| 2482 | if (DEBUG_FOCUS) { |
| 2483 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2484 | } |
| 2485 | *outConflictingPointerActions = true; |
| 2486 | } |
| 2487 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2488 | // One pointer went up. |
| 2489 | if (isSplit) { |
| 2490 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2491 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2492 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2493 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2494 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2495 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2496 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2497 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2498 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2499 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2500 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2501 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2502 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2503 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2504 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2505 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2506 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2507 | // Save changes unless the action was scroll in which case the temporary touch |
| 2508 | // state was only valid for this one action. |
| 2509 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2510 | if (tempTouchState.displayId >= 0) { |
| 2511 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2512 | } else { |
| 2513 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2514 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2515 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2516 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2517 | // Update hover state. |
| 2518 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2519 | } |
| 2520 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2521 | return injectionResult; |
| 2522 | } |
| 2523 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2524 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2525 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2526 | // have an explicit reason to support it. |
| 2527 | constexpr bool isStylus = false; |
| 2528 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2529 | const sp<WindowInfoHandle> dropWindow = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2530 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2531 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2532 | if (dropWindow) { |
| 2533 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2534 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2535 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2536 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2537 | } |
| 2538 | mDragState.reset(); |
| 2539 | } |
| 2540 | |
| 2541 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2542 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2543 | return; |
| 2544 | } |
| 2545 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2546 | if (!mDragState->isStartDrag) { |
| 2547 | mDragState->isStartDrag = true; |
| 2548 | mDragState->isStylusButtonDownAtStart = |
| 2549 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2550 | } |
| 2551 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2552 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2553 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2554 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2555 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2556 | // Handle the special case : stylus button no longer pressed. |
| 2557 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2558 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2559 | finishDragAndDrop(entry.displayId, x, y); |
| 2560 | return; |
| 2561 | } |
| 2562 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2563 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until |
| 2564 | // we have an explicit reason to support it. |
| 2565 | constexpr bool isStylus = false; |
| 2566 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2567 | const sp<WindowInfoHandle> hoverWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2568 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2569 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2570 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2571 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2572 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2573 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2574 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2575 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2576 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2577 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2578 | } |
| 2579 | // enqueue drag location if needed. |
| 2580 | if (hoverWindowHandle != nullptr) { |
| 2581 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2582 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2583 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2584 | finishDragAndDrop(entry.displayId, x, y); |
| 2585 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2586 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2587 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2588 | } |
| 2589 | } |
| 2590 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2591 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2592 | int32_t targetFlags, BitSet32 pointerIds, |
| 2593 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2594 | std::vector<InputTarget>::iterator it = |
| 2595 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2596 | [&windowHandle](const InputTarget& inputTarget) { |
| 2597 | return inputTarget.inputChannel->getConnectionToken() == |
| 2598 | windowHandle->getToken(); |
| 2599 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2600 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2601 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2602 | |
| 2603 | if (it == inputTargets.end()) { |
| 2604 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2605 | std::shared_ptr<InputChannel> inputChannel = |
| 2606 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2607 | if (inputChannel == nullptr) { |
| 2608 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2609 | return; |
| 2610 | } |
| 2611 | inputTarget.inputChannel = inputChannel; |
| 2612 | inputTarget.flags = targetFlags; |
| 2613 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2614 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2615 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2616 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2617 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2618 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2619 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2620 | inputTargets.push_back(inputTarget); |
| 2621 | it = inputTargets.end() - 1; |
| 2622 | } |
| 2623 | |
| 2624 | ALOG_ASSERT(it->flags == targetFlags); |
| 2625 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2626 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2627 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2628 | } |
| 2629 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2630 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2631 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2632 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2633 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2634 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2635 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2636 | InputTarget target; |
| 2637 | target.inputChannel = monitor.inputChannel; |
| 2638 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2639 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2640 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2641 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2642 | target.setDefaultPointerTransform(target.displayTransform); |
| 2643 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2644 | } |
| 2645 | } |
| 2646 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2647 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2648 | const InjectionState* injectionState) { |
| 2649 | if (injectionState && |
| 2650 | (windowHandle == nullptr || |
| 2651 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2652 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2653 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2654 | 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] | 2655 | "owned by uid %d", |
| 2656 | injectionState->injectorPid, injectionState->injectorUid, |
| 2657 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2658 | } else { |
| 2659 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2660 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2661 | } |
| 2662 | return false; |
| 2663 | } |
| 2664 | return true; |
| 2665 | } |
| 2666 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2667 | /** |
| 2668 | * Indicate whether one window handle should be considered as obscuring |
| 2669 | * another window handle. We only check a few preconditions. Actually |
| 2670 | * checking the bounds is left to the caller. |
| 2671 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2672 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2673 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2674 | // Compare by token so cloned layers aren't counted |
| 2675 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2676 | return false; |
| 2677 | } |
| 2678 | auto info = windowHandle->getInfo(); |
| 2679 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2680 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2681 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2682 | } else if (otherInfo->alpha == 0 && |
| 2683 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2684 | // Those act as if they were invisible, so we don't need to flag them. |
| 2685 | // We do want to potentially flag touchable windows even if they have 0 |
| 2686 | // opacity, since they can consume touches and alter the effects of the |
| 2687 | // user interaction (eg. apps that rely on |
| 2688 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2689 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2690 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2691 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2692 | // If ownerUid is the same we don't generate occlusion events as there |
| 2693 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2694 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2695 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2696 | return false; |
| 2697 | } else if (otherInfo->displayId != info->displayId) { |
| 2698 | return false; |
| 2699 | } |
| 2700 | return true; |
| 2701 | } |
| 2702 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2703 | /** |
| 2704 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2705 | * untrusted, one should check: |
| 2706 | * |
| 2707 | * 1. If result.hasBlockingOcclusion is true. |
| 2708 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2709 | * BLOCK_UNTRUSTED. |
| 2710 | * |
| 2711 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2712 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2713 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2714 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2715 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2716 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2717 | * |
| 2718 | * If neither of those is true, then it means the touch can be allowed. |
| 2719 | */ |
| 2720 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2721 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2722 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2723 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2724 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2725 | TouchOcclusionInfo info; |
| 2726 | info.hasBlockingOcclusion = false; |
| 2727 | info.obscuringOpacity = 0; |
| 2728 | info.obscuringUid = -1; |
| 2729 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2730 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2731 | if (windowHandle == otherHandle) { |
| 2732 | break; // All future windows are below us. Exit early. |
| 2733 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2734 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2735 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2736 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2737 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2738 | info.debugInfo.push_back( |
| 2739 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2740 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2741 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2742 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2743 | // window's touch occlusion mode |
| 2744 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2745 | info.hasBlockingOcclusion = true; |
| 2746 | info.obscuringUid = otherInfo->ownerUid; |
| 2747 | info.obscuringPackage = otherInfo->packageName; |
| 2748 | break; |
| 2749 | } |
| 2750 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2751 | uint32_t uid = otherInfo->ownerUid; |
| 2752 | float opacity = |
| 2753 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2754 | // Given windows A and B: |
| 2755 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2756 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2757 | opacityByUid[uid] = opacity; |
| 2758 | if (opacity > info.obscuringOpacity) { |
| 2759 | info.obscuringOpacity = opacity; |
| 2760 | info.obscuringUid = uid; |
| 2761 | info.obscuringPackage = otherInfo->packageName; |
| 2762 | } |
| 2763 | } |
| 2764 | } |
| 2765 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2766 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2767 | info.debugInfo.push_back( |
| 2768 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2769 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2770 | return info; |
| 2771 | } |
| 2772 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2773 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2774 | bool isTouchedWindow) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2775 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2776 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2777 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 2778 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2779 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 2780 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 2781 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 2782 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 2783 | info->name.c_str(), info->inputConfig.string().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2784 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2785 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2788 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2789 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2790 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2791 | occlusionInfo.obscuringUid); |
| 2792 | return false; |
| 2793 | } |
| 2794 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2795 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2796 | "%.2f, maximum allowed = %.2f)", |
| 2797 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2798 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2799 | return false; |
| 2800 | } |
| 2801 | return true; |
| 2802 | } |
| 2803 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2804 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2805 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2806 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2807 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2808 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2809 | if (windowHandle == otherHandle) { |
| 2810 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2811 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2812 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2813 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2814 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2815 | return true; |
| 2816 | } |
| 2817 | } |
| 2818 | return false; |
| 2819 | } |
| 2820 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2821 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2822 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2823 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2824 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2825 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2826 | if (windowHandle == otherHandle) { |
| 2827 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2828 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2829 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2830 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2831 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2832 | return true; |
| 2833 | } |
| 2834 | } |
| 2835 | return false; |
| 2836 | } |
| 2837 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2838 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2839 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2840 | if (applicationHandle != nullptr) { |
| 2841 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2842 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2843 | } else { |
| 2844 | return applicationHandle->getName(); |
| 2845 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2846 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2847 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2848 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2849 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2850 | } |
| 2851 | } |
| 2852 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2853 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2854 | if (!isUserActivityEvent(eventEntry)) { |
| 2855 | // 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] | 2856 | return; |
| 2857 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2858 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2859 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2860 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2861 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2862 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2863 | if (DEBUG_DISPATCH_CYCLE) { |
| 2864 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2865 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2866 | return; |
| 2867 | } |
| 2868 | } |
| 2869 | |
| 2870 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2871 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2872 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2873 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2874 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2875 | return; |
| 2876 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2877 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2878 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2879 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2880 | } |
| 2881 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2882 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2883 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2884 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2885 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2886 | return; |
| 2887 | } |
| 2888 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2889 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2890 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2891 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2892 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2893 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2894 | break; |
| 2895 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2896 | } |
| 2897 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2898 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2899 | REQUIRES(mLock) { |
| 2900 | scoped_unlock unlock(mLock); |
| 2901 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2902 | }; |
| 2903 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2907 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2908 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2909 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2910 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2911 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2912 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2913 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2914 | ATRACE_NAME(message.c_str()); |
| 2915 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2916 | if (DEBUG_DISPATCH_CYCLE) { |
| 2917 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2918 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2919 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2920 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2921 | inputTarget.getPointerInfoString().c_str()); |
| 2922 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2923 | |
| 2924 | // Skip this event if the connection status is not normal. |
| 2925 | // 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] | 2926 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2927 | if (DEBUG_DISPATCH_CYCLE) { |
| 2928 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2929 | connection->getInputChannelName().c_str(), |
| 2930 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2931 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2932 | return; |
| 2933 | } |
| 2934 | |
| 2935 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2936 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2937 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2938 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2939 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2940 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2941 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2942 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2943 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2944 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2945 | if (!splitMotionEntry) { |
| 2946 | return; // split event was dropped |
| 2947 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2948 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2949 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2950 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2951 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2952 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2953 | if (DEBUG_FOCUS) { |
| 2954 | ALOGD("channel '%s' ~ Split motion event.", |
| 2955 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2956 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2957 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2958 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2959 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2960 | return; |
| 2961 | } |
| 2962 | } |
| 2963 | |
| 2964 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2965 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2966 | } |
| 2967 | |
| 2968 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2969 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2970 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2971 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2972 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2973 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2974 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2975 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2976 | ATRACE_NAME(message.c_str()); |
| 2977 | } |
| 2978 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2979 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2980 | |
| 2981 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2982 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2983 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2984 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2985 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2986 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2987 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2988 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2989 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2990 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2991 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2992 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2993 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2994 | |
| 2995 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2996 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2997 | startDispatchCycleLocked(currentTime, connection); |
| 2998 | } |
| 2999 | } |
| 3000 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3001 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3002 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3003 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3004 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3005 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3006 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 3007 | connection->getInputChannelName().c_str(), |
| 3008 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3009 | ATRACE_NAME(message.c_str()); |
| 3010 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3011 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3012 | if (!(inputTargetFlags & dispatchMode)) { |
| 3013 | return; |
| 3014 | } |
| 3015 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 3016 | |
| 3017 | // This is a new event. |
| 3018 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3019 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3020 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3021 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3022 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3023 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3024 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3025 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3026 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3027 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3028 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3029 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3030 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3031 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3032 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3033 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3034 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3035 | if (DEBUG_DISPATCH_CYCLE) { |
| 3036 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3037 | "event", |
| 3038 | connection->getInputChannelName().c_str()); |
| 3039 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3040 | return; // skip the inconsistent event |
| 3041 | } |
| 3042 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3043 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3044 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3045 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3046 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3047 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3048 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3049 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3050 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3051 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3052 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3053 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 3054 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 3055 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3056 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 3057 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3058 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 3059 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 3060 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 3061 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3062 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3063 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3064 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3065 | } |
| 3066 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3067 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3068 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3069 | if (DEBUG_DISPATCH_CYCLE) { |
| 3070 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3071 | "enter event", |
| 3072 | connection->getInputChannelName().c_str()); |
| 3073 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3074 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3075 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3076 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3077 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3078 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3079 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3080 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3081 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3082 | } |
| 3083 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3084 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3085 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3086 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3087 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3088 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3089 | if (DEBUG_DISPATCH_CYCLE) { |
| 3090 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3091 | "event", |
| 3092 | connection->getInputChannelName().c_str()); |
| 3093 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3094 | return; // skip the inconsistent event |
| 3095 | } |
| 3096 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3097 | dispatchEntry->resolvedEventId = |
| 3098 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3099 | ? mIdGenerator.nextId() |
| 3100 | : motionEntry.id; |
| 3101 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3102 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3103 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3104 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3105 | ATRACE_NAME(message.c_str()); |
| 3106 | } |
| 3107 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3108 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3109 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3110 | // Skip reporting pointer down outside focus to the policy. |
| 3111 | break; |
| 3112 | } |
| 3113 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3114 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3115 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3116 | |
| 3117 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3118 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3119 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3120 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3121 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3122 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3123 | break; |
| 3124 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3125 | case EventEntry::Type::SENSOR: { |
| 3126 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3127 | break; |
| 3128 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3129 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3130 | case EventEntry::Type::DEVICE_RESET: { |
| 3131 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3132 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3133 | break; |
| 3134 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | // Remember that we are waiting for this dispatch to complete. |
| 3138 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3139 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3143 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3144 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3145 | } |
| 3146 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3147 | /** |
| 3148 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3149 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3150 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3151 | * We will see both launcher and wallpaper in that list. |
| 3152 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3153 | * until the set of interacted connections changes. |
| 3154 | * |
| 3155 | * The following items are skipped, to reduce the logspam: |
| 3156 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3157 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3158 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3159 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3160 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3161 | */ |
| 3162 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3163 | const std::vector<InputTarget>& targets) { |
| 3164 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3165 | if (entry.type == EventEntry::Type::KEY) { |
| 3166 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3167 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3168 | return; |
| 3169 | } |
| 3170 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3171 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3172 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3173 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3174 | return; |
| 3175 | } |
| 3176 | } else { |
| 3177 | return; // Not a key or a motion |
| 3178 | } |
| 3179 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3180 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3181 | std::vector<sp<Connection>> newConnections; |
| 3182 | for (const InputTarget& target : targets) { |
| 3183 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3184 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3185 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3186 | } |
| 3187 | |
| 3188 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3189 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3190 | if (connection == nullptr) { |
| 3191 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3192 | } |
| 3193 | newConnectionTokens.insert(std::move(token)); |
| 3194 | newConnections.emplace_back(connection); |
| 3195 | } |
| 3196 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3197 | return; // no change |
| 3198 | } |
| 3199 | mInteractionConnectionTokens = newConnectionTokens; |
| 3200 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3201 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3202 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3203 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3204 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3205 | std::string message = "Interaction with: " + targetList; |
| 3206 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3207 | message += "<none>"; |
| 3208 | } |
| 3209 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3210 | } |
| 3211 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3212 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3213 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3214 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3215 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3216 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3217 | return; |
| 3218 | } |
| 3219 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3220 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3221 | if (focusedToken == token) { |
| 3222 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3223 | return; |
| 3224 | } |
| 3225 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3226 | auto command = [this, token]() REQUIRES(mLock) { |
| 3227 | scoped_unlock unlock(mLock); |
| 3228 | mPolicy->onPointerDownOutsideFocus(token); |
| 3229 | }; |
| 3230 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3234 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3235 | if (ATRACE_ENABLED()) { |
| 3236 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3237 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3238 | ATRACE_NAME(message.c_str()); |
| 3239 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3240 | if (DEBUG_DISPATCH_CYCLE) { |
| 3241 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3242 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3243 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3244 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3245 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3246 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3247 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3248 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3249 | |
| 3250 | // Publish the event. |
| 3251 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3252 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3253 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3254 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3255 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3256 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3257 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3258 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3259 | status = connection->inputPublisher |
| 3260 | .publishKeyEvent(dispatchEntry->seq, |
| 3261 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3262 | keyEntry.source, keyEntry.displayId, |
| 3263 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3264 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3265 | keyEntry.scanCode, keyEntry.metaState, |
| 3266 | keyEntry.repeatCount, keyEntry.downTime, |
| 3267 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3268 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3269 | } |
| 3270 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3271 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3272 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3273 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3274 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3275 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3276 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3277 | // 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] | 3278 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3279 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3280 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3281 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3282 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3283 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3284 | // Don't apply window scale here since we don't want scale to affect raw |
| 3285 | // coordinates. The scale will be sent back to the client and applied |
| 3286 | // later when requesting relative coordinates. |
| 3287 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3288 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3289 | } |
| 3290 | usingCoords = scaledCoords; |
| 3291 | } |
| 3292 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3293 | // We don't want the dispatch target to know. |
| 3294 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3295 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3296 | scaledCoords[i].clear(); |
| 3297 | } |
| 3298 | usingCoords = scaledCoords; |
| 3299 | } |
| 3300 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3301 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3302 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3303 | |
| 3304 | // Publish the motion event. |
| 3305 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3306 | .publishMotionEvent(dispatchEntry->seq, |
| 3307 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3308 | motionEntry.deviceId, motionEntry.source, |
| 3309 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3310 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3311 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3312 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3313 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3314 | motionEntry.buttonState, |
| 3315 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3316 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3317 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3318 | motionEntry.xCursorPosition, |
| 3319 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3320 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3321 | motionEntry.downTime, motionEntry.eventTime, |
| 3322 | motionEntry.pointerCount, |
| 3323 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3324 | break; |
| 3325 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3326 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3327 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3328 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3329 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3330 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3331 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3332 | break; |
| 3333 | } |
| 3334 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3335 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3336 | const TouchModeEntry& touchModeEntry = |
| 3337 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3338 | status = connection->inputPublisher |
| 3339 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3340 | touchModeEntry.inTouchMode); |
| 3341 | |
| 3342 | break; |
| 3343 | } |
| 3344 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3345 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3346 | const auto& captureEntry = |
| 3347 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3348 | status = connection->inputPublisher |
| 3349 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3350 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3351 | break; |
| 3352 | } |
| 3353 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3354 | case EventEntry::Type::DRAG: { |
| 3355 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3356 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3357 | dragEntry.id, dragEntry.x, |
| 3358 | dragEntry.y, |
| 3359 | dragEntry.isExiting); |
| 3360 | break; |
| 3361 | } |
| 3362 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3363 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3364 | case EventEntry::Type::DEVICE_RESET: |
| 3365 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3366 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3367 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3368 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3369 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | // Check the result. |
| 3373 | if (status) { |
| 3374 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3375 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3376 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3377 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3378 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3379 | "event to it, status=%s(%d)", |
| 3380 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3381 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3382 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3383 | } else { |
| 3384 | // Pipe is full and we are waiting for the app to finish process some events |
| 3385 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3386 | if (DEBUG_DISPATCH_CYCLE) { |
| 3387 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3388 | "waiting for the application to catch up", |
| 3389 | connection->getInputChannelName().c_str()); |
| 3390 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3391 | } |
| 3392 | } else { |
| 3393 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3394 | "status=%s(%d)", |
| 3395 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3396 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3397 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3398 | } |
| 3399 | return; |
| 3400 | } |
| 3401 | |
| 3402 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3403 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3404 | connection->outboundQueue.end(), |
| 3405 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3406 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3407 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3408 | if (connection->responsive) { |
| 3409 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3410 | connection->inputChannel->getConnectionToken()); |
| 3411 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3412 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3413 | } |
| 3414 | } |
| 3415 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3416 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3417 | size_t size; |
| 3418 | switch (event.type) { |
| 3419 | case VerifiedInputEvent::Type::KEY: { |
| 3420 | size = sizeof(VerifiedKeyEvent); |
| 3421 | break; |
| 3422 | } |
| 3423 | case VerifiedInputEvent::Type::MOTION: { |
| 3424 | size = sizeof(VerifiedMotionEvent); |
| 3425 | break; |
| 3426 | } |
| 3427 | } |
| 3428 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3429 | return mHmacKeyManager.sign(start, size); |
| 3430 | } |
| 3431 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3432 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3433 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3434 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3435 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3436 | // Only sign events up and down events as the purely move events |
| 3437 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3438 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3439 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3440 | |
| 3441 | VerifiedMotionEvent verifiedEvent = |
| 3442 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3443 | verifiedEvent.actionMasked = actionMasked; |
| 3444 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3445 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3446 | } |
| 3447 | |
| 3448 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3449 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3450 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3451 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3452 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3453 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3454 | } |
| 3455 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3456 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3457 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3458 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3459 | if (DEBUG_DISPATCH_CYCLE) { |
| 3460 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3461 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3462 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3463 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3464 | if (connection->status == Connection::Status::BROKEN || |
| 3465 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3466 | return; |
| 3467 | } |
| 3468 | |
| 3469 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3470 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3471 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3472 | }; |
| 3473 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3474 | } |
| 3475 | |
| 3476 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3477 | const sp<Connection>& connection, |
| 3478 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3479 | if (DEBUG_DISPATCH_CYCLE) { |
| 3480 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3481 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3482 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3483 | |
| 3484 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3485 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3486 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3487 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3488 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3489 | |
| 3490 | // The connection appears to be unrecoverably broken. |
| 3491 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3492 | if (connection->status == Connection::Status::NORMAL) { |
| 3493 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3494 | |
| 3495 | if (notify) { |
| 3496 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3497 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3498 | connection->getInputChannelName().c_str()); |
| 3499 | |
| 3500 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3501 | scoped_unlock unlock(mLock); |
| 3502 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3503 | }; |
| 3504 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3505 | } |
| 3506 | } |
| 3507 | } |
| 3508 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3509 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3510 | while (!queue.empty()) { |
| 3511 | DispatchEntry* dispatchEntry = queue.front(); |
| 3512 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3513 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3514 | } |
| 3515 | } |
| 3516 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3517 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3518 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3519 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3520 | } |
| 3521 | delete dispatchEntry; |
| 3522 | } |
| 3523 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3524 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3525 | std::scoped_lock _l(mLock); |
| 3526 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3527 | if (connection == nullptr) { |
| 3528 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3529 | connectionToken.get(), events); |
| 3530 | return 0; // remove the callback |
| 3531 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3532 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3533 | bool notify; |
| 3534 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3535 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3536 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3537 | "events=0x%x", |
| 3538 | connection->getInputChannelName().c_str(), events); |
| 3539 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3540 | } |
| 3541 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3542 | nsecs_t currentTime = now(); |
| 3543 | bool gotOne = false; |
| 3544 | status_t status = OK; |
| 3545 | for (;;) { |
| 3546 | Result<InputPublisher::ConsumerResponse> result = |
| 3547 | connection->inputPublisher.receiveConsumerResponse(); |
| 3548 | if (!result.ok()) { |
| 3549 | status = result.error().code(); |
| 3550 | break; |
| 3551 | } |
| 3552 | |
| 3553 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3554 | const InputPublisher::Finished& finish = |
| 3555 | std::get<InputPublisher::Finished>(*result); |
| 3556 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3557 | finish.consumeTime); |
| 3558 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3559 | if (shouldReportMetricsForConnection(*connection)) { |
| 3560 | const InputPublisher::Timeline& timeline = |
| 3561 | std::get<InputPublisher::Timeline>(*result); |
| 3562 | mLatencyTracker |
| 3563 | .trackGraphicsLatency(timeline.inputEventId, |
| 3564 | connection->inputChannel->getConnectionToken(), |
| 3565 | std::move(timeline.graphicsTimeline)); |
| 3566 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3567 | } |
| 3568 | gotOne = true; |
| 3569 | } |
| 3570 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3571 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3572 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3573 | return 1; |
| 3574 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3575 | } |
| 3576 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3577 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3578 | if (notify) { |
| 3579 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3580 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3581 | status); |
| 3582 | } |
| 3583 | } else { |
| 3584 | // Monitor channels are never explicitly unregistered. |
| 3585 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3586 | const bool stillHaveWindowHandle = |
| 3587 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3588 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3589 | if (notify) { |
| 3590 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3591 | connection->getInputChannelName().c_str(), events); |
| 3592 | } |
| 3593 | } |
| 3594 | |
| 3595 | // Remove the channel. |
| 3596 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3597 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3598 | } |
| 3599 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3600 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3601 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3602 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3603 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3604 | } |
| 3605 | } |
| 3606 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3607 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3608 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3609 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3610 | for (const Monitor& monitor : monitors) { |
| 3611 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3612 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3613 | } |
| 3614 | } |
| 3615 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3616 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3617 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3618 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3619 | if (connection == nullptr) { |
| 3620 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3621 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3622 | |
| 3623 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3627 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3628 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3629 | return; |
| 3630 | } |
| 3631 | |
| 3632 | nsecs_t currentTime = now(); |
| 3633 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3634 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3635 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3636 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3637 | if (cancelationEvents.empty()) { |
| 3638 | return; |
| 3639 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3640 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3641 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3642 | "with reality: %s, mode=%d.", |
| 3643 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3644 | options.mode); |
| 3645 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3646 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3647 | std::string reason = std::string("reason=").append(options.reason); |
| 3648 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3649 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3650 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3651 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3652 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3653 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3654 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3655 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3656 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3657 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3658 | } |
| 3659 | target.inputChannel = connection->inputChannel; |
| 3660 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3661 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3662 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3663 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3664 | switch (cancelationEventEntry->type) { |
| 3665 | case EventEntry::Type::KEY: { |
| 3666 | logOutboundKeyDetails("cancel - ", |
| 3667 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3668 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3669 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3670 | case EventEntry::Type::MOTION: { |
| 3671 | logOutboundMotionDetails("cancel - ", |
| 3672 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3673 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3674 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3675 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3676 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3677 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3678 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3679 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3680 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3681 | break; |
| 3682 | } |
| 3683 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3684 | case EventEntry::Type::DEVICE_RESET: |
| 3685 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3686 | 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] | 3687 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3688 | break; |
| 3689 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3690 | } |
| 3691 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3692 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3693 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3694 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3695 | |
| 3696 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3697 | } |
| 3698 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3699 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3700 | const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3701 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3702 | return; |
| 3703 | } |
| 3704 | |
| 3705 | nsecs_t currentTime = now(); |
| 3706 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3707 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3708 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3709 | |
| 3710 | if (downEvents.empty()) { |
| 3711 | return; |
| 3712 | } |
| 3713 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3714 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3715 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3716 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3717 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3718 | |
| 3719 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3720 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3721 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3722 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3723 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3724 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3725 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3726 | } |
| 3727 | target.inputChannel = connection->inputChannel; |
| 3728 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3729 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3730 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3731 | switch (downEventEntry->type) { |
| 3732 | case EventEntry::Type::MOTION: { |
| 3733 | logOutboundMotionDetails("down - ", |
| 3734 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3735 | break; |
| 3736 | } |
| 3737 | |
| 3738 | case EventEntry::Type::KEY: |
| 3739 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3740 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3741 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3742 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3743 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3744 | case EventEntry::Type::SENSOR: |
| 3745 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3746 | 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] | 3747 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3748 | break; |
| 3749 | } |
| 3750 | } |
| 3751 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3752 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3753 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | startDispatchCycleLocked(currentTime, connection); |
| 3757 | } |
| 3758 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3759 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3760 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3761 | ALOG_ASSERT(pointerIds.value != 0); |
| 3762 | |
| 3763 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3764 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3765 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3766 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3767 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3768 | uint32_t splitPointerCount = 0; |
| 3769 | |
| 3770 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3771 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3772 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3773 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3774 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3775 | if (pointerIds.hasBit(pointerId)) { |
| 3776 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3777 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3778 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3779 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3780 | splitPointerCount += 1; |
| 3781 | } |
| 3782 | } |
| 3783 | |
| 3784 | if (splitPointerCount != pointerIds.count()) { |
| 3785 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3786 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3787 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3788 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3789 | // in this way. |
| 3790 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3791 | "we expected there to be %d pointers. This probably means we received " |
| 3792 | "a broken sequence of pointer ids from the input device.", |
| 3793 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3794 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3795 | } |
| 3796 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3797 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3798 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3799 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3800 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3801 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3802 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3803 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3804 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3805 | if (pointerIds.hasBit(pointerId)) { |
| 3806 | if (pointerIds.count() == 1) { |
| 3807 | // The first/last pointer went down/up. |
| 3808 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3809 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3810 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3811 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3812 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3813 | } else { |
| 3814 | // A secondary pointer went down/up. |
| 3815 | uint32_t splitPointerIndex = 0; |
| 3816 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3817 | splitPointerIndex += 1; |
| 3818 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3819 | action = maskedAction | |
| 3820 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3821 | } |
| 3822 | } else { |
| 3823 | // An unrelated pointer changed. |
| 3824 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3825 | } |
| 3826 | } |
| 3827 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3828 | int32_t newId = mIdGenerator.nextId(); |
| 3829 | if (ATRACE_ENABLED()) { |
| 3830 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3831 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3832 | originalMotionEntry.id, newId); |
| 3833 | ATRACE_NAME(message.c_str()); |
| 3834 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3835 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3836 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3837 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3838 | originalMotionEntry.displayId, |
| 3839 | originalMotionEntry.policyFlags, action, |
| 3840 | originalMotionEntry.actionButton, |
| 3841 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3842 | originalMotionEntry.buttonState, |
| 3843 | originalMotionEntry.classification, |
| 3844 | originalMotionEntry.edgeFlags, |
| 3845 | originalMotionEntry.xPrecision, |
| 3846 | originalMotionEntry.yPrecision, |
| 3847 | originalMotionEntry.xCursorPosition, |
| 3848 | originalMotionEntry.yCursorPosition, |
| 3849 | originalMotionEntry.downTime, splitPointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3850 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3851 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3852 | if (originalMotionEntry.injectionState) { |
| 3853 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3854 | splitMotionEntry->injectionState->refCount += 1; |
| 3855 | } |
| 3856 | |
| 3857 | return splitMotionEntry; |
| 3858 | } |
| 3859 | |
| 3860 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3861 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3862 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3863 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3864 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3865 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3866 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3867 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3868 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3869 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3870 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3871 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3872 | } // release lock |
| 3873 | |
| 3874 | if (needWake) { |
| 3875 | mLooper->wake(); |
| 3876 | } |
| 3877 | } |
| 3878 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3879 | /** |
| 3880 | * If one of the meta shortcuts is detected, process them here: |
| 3881 | * Meta + Backspace -> generate BACK |
| 3882 | * Meta + Enter -> generate HOME |
| 3883 | * This will potentially overwrite keyCode and metaState. |
| 3884 | */ |
| 3885 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3886 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3887 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3888 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3889 | if (keyCode == AKEYCODE_DEL) { |
| 3890 | newKeyCode = AKEYCODE_BACK; |
| 3891 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3892 | newKeyCode = AKEYCODE_HOME; |
| 3893 | } |
| 3894 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3895 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3896 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3897 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3898 | keyCode = newKeyCode; |
| 3899 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3900 | } |
| 3901 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3902 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3903 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3904 | // 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] | 3905 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3906 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3907 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3908 | if (replacementIt != mReplacedKeys.end()) { |
| 3909 | keyCode = replacementIt->second; |
| 3910 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3911 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3912 | } |
| 3913 | } |
| 3914 | } |
| 3915 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3916 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3917 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3918 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3919 | "policyFlags=0x%x, action=0x%x, " |
| 3920 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3921 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3922 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3923 | args->downTime); |
| 3924 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3925 | if (!validateKeyEvent(args->action)) { |
| 3926 | return; |
| 3927 | } |
| 3928 | |
| 3929 | uint32_t policyFlags = args->policyFlags; |
| 3930 | int32_t flags = args->flags; |
| 3931 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3932 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3933 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3934 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3935 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3936 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3937 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3938 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3939 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3940 | metaState |= AMETA_FUNCTION_ON; |
| 3941 | } |
| 3942 | |
| 3943 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3944 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3945 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3946 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3947 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3948 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3949 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3950 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3951 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3952 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3953 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3954 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3955 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3956 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3957 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3958 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3959 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3960 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3961 | { // acquire lock |
| 3962 | mLock.lock(); |
| 3963 | |
| 3964 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3965 | mLock.unlock(); |
| 3966 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3967 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3968 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3969 | return; // event was consumed by the filter |
| 3970 | } |
| 3971 | |
| 3972 | mLock.lock(); |
| 3973 | } |
| 3974 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3975 | std::unique_ptr<KeyEntry> newEntry = |
| 3976 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3977 | args->displayId, policyFlags, args->action, flags, |
| 3978 | keyCode, args->scanCode, metaState, repeatCount, |
| 3979 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3980 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3981 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3982 | mLock.unlock(); |
| 3983 | } // release lock |
| 3984 | |
| 3985 | if (needWake) { |
| 3986 | mLooper->wake(); |
| 3987 | } |
| 3988 | } |
| 3989 | |
| 3990 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3991 | return mInputFilterEnabled; |
| 3992 | } |
| 3993 | |
| 3994 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3995 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3996 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3997 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 3998 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3999 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4000 | "yCursorPosition=%f, downTime=%" PRId64, |
| 4001 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 4002 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 4003 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 4004 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 4005 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 4006 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 4007 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 4008 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 4009 | "orientation=%f", |
| 4010 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 4011 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4012 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4013 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4014 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4015 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4016 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4017 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4018 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4019 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 4020 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4021 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4022 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 4023 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4024 | return; |
| 4025 | } |
| 4026 | |
| 4027 | uint32_t policyFlags = args->policyFlags; |
| 4028 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4029 | |
| 4030 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4031 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4032 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4033 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4034 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4035 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4036 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4037 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4038 | { // acquire lock |
| 4039 | mLock.lock(); |
| 4040 | |
| 4041 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4042 | ui::Transform displayTransform; |
| 4043 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4044 | displayTransform = it->second.transform; |
| 4045 | } |
| 4046 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4047 | mLock.unlock(); |
| 4048 | |
| 4049 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4050 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4051 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4052 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4053 | displayTransform, args->xPrecision, args->yPrecision, |
| 4054 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4055 | args->downTime, args->eventTime, args->pointerCount, |
| 4056 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4057 | |
| 4058 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4059 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4060 | return; // event was consumed by the filter |
| 4061 | } |
| 4062 | |
| 4063 | mLock.lock(); |
| 4064 | } |
| 4065 | |
| 4066 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4067 | std::unique_ptr<MotionEntry> newEntry = |
| 4068 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4069 | args->source, args->displayId, policyFlags, |
| 4070 | args->action, args->actionButton, args->flags, |
| 4071 | args->metaState, args->buttonState, |
| 4072 | args->classification, args->edgeFlags, |
| 4073 | args->xPrecision, args->yPrecision, |
| 4074 | args->xCursorPosition, args->yCursorPosition, |
| 4075 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4076 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4077 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4078 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4079 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4080 | !mInputFilterEnabled) { |
| 4081 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4082 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4083 | } |
| 4084 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4085 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4086 | mLock.unlock(); |
| 4087 | } // release lock |
| 4088 | |
| 4089 | if (needWake) { |
| 4090 | mLooper->wake(); |
| 4091 | } |
| 4092 | } |
| 4093 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4094 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4095 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4096 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4097 | " sensorType=%s", |
| 4098 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4099 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4100 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4101 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4102 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4103 | { // acquire lock |
| 4104 | mLock.lock(); |
| 4105 | |
| 4106 | // Just enqueue a new sensor event. |
| 4107 | std::unique_ptr<SensorEntry> newEntry = |
| 4108 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4109 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4110 | args->sensorType, args->accuracy, |
| 4111 | args->accuracyChanged, args->values); |
| 4112 | |
| 4113 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4114 | mLock.unlock(); |
| 4115 | } // release lock |
| 4116 | |
| 4117 | if (needWake) { |
| 4118 | mLooper->wake(); |
| 4119 | } |
| 4120 | } |
| 4121 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4122 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4123 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4124 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4125 | args->deviceId, args->isOn); |
| 4126 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4127 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4128 | } |
| 4129 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4130 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4131 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4135 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4136 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4137 | "switchMask=0x%08x", |
| 4138 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4139 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4140 | |
| 4141 | uint32_t policyFlags = args->policyFlags; |
| 4142 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4143 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4147 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4148 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4149 | args->deviceId); |
| 4150 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4151 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4152 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4153 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4154 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4155 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4156 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4157 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4158 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4159 | } // release lock |
| 4160 | |
| 4161 | if (needWake) { |
| 4162 | mLooper->wake(); |
| 4163 | } |
| 4164 | } |
| 4165 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4166 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4167 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4168 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4169 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4170 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4171 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4172 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4173 | { // acquire lock |
| 4174 | std::scoped_lock _l(mLock); |
| 4175 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4176 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4177 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4178 | } // release lock |
| 4179 | |
| 4180 | if (needWake) { |
| 4181 | mLooper->wake(); |
| 4182 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4183 | } |
| 4184 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4185 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4186 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4187 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4188 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4189 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 4190 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4191 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
| 4192 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4193 | 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] | 4194 | |
| 4195 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4196 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4197 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4198 | } |
| 4199 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4200 | // 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] | 4201 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4202 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4203 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4204 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4205 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4206 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4207 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4208 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4211 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4212 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4213 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4214 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4215 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4216 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4217 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4218 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4219 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4220 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4221 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4222 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4223 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4224 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4225 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4226 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4227 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4228 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4229 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4230 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4231 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4232 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4233 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4234 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4235 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4236 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4237 | |
| 4238 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4239 | android::base::Timer t; |
| 4240 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4241 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4242 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4243 | std::to_string(t.duration().count()).c_str()); |
| 4244 | } |
| 4245 | } |
| 4246 | |
| 4247 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4248 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4249 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4250 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4251 | incomingKey.getDisplayId(), policyFlags, action, |
| 4252 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4253 | incomingKey.getRepeatCount(), |
| 4254 | incomingKey.getDownTime()); |
| 4255 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4256 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4257 | } |
| 4258 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4259 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4260 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4261 | const int32_t action = motionEvent.getAction(); |
| 4262 | const bool isPointerEvent = |
| 4263 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4264 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4265 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4266 | ? ADISPLAY_ID_DEFAULT |
| 4267 | : event->getDisplayId(); |
| 4268 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4269 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4270 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4271 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4272 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4273 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4274 | } |
| 4275 | |
| 4276 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4277 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4278 | android::base::Timer t; |
| 4279 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4280 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4281 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4282 | std::to_string(t.duration().count()).c_str()); |
| 4283 | } |
| 4284 | } |
| 4285 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4286 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4287 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4288 | } |
| 4289 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4290 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4291 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4292 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4293 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4294 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4295 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4296 | displayId, policyFlags, action, actionButton, |
| 4297 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4298 | motionEvent.getButtonState(), |
| 4299 | motionEvent.getClassification(), |
| 4300 | motionEvent.getEdgeFlags(), |
| 4301 | motionEvent.getXPrecision(), |
| 4302 | motionEvent.getYPrecision(), |
| 4303 | motionEvent.getRawXCursorPosition(), |
| 4304 | motionEvent.getRawYCursorPosition(), |
| 4305 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4306 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4307 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4308 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4309 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4310 | sampleEventTimes += 1; |
| 4311 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4312 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4313 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4314 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4315 | displayId, policyFlags, action, actionButton, |
| 4316 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4317 | motionEvent.getButtonState(), |
| 4318 | motionEvent.getClassification(), |
| 4319 | motionEvent.getEdgeFlags(), |
| 4320 | motionEvent.getXPrecision(), |
| 4321 | motionEvent.getYPrecision(), |
| 4322 | motionEvent.getRawXCursorPosition(), |
| 4323 | motionEvent.getRawYCursorPosition(), |
| 4324 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4325 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4326 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4327 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4328 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4329 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4330 | } |
| 4331 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4332 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4333 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4334 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4335 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4336 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4337 | } |
| 4338 | |
| 4339 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4340 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4341 | injectionState->injectionIsAsync = true; |
| 4342 | } |
| 4343 | |
| 4344 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4345 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4346 | |
| 4347 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4348 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4349 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4350 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4351 | } |
| 4352 | |
| 4353 | mLock.unlock(); |
| 4354 | |
| 4355 | if (needWake) { |
| 4356 | mLooper->wake(); |
| 4357 | } |
| 4358 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4359 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4360 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4361 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4362 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4363 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4364 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4365 | } else { |
| 4366 | for (;;) { |
| 4367 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4368 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4369 | break; |
| 4370 | } |
| 4371 | |
| 4372 | nsecs_t remainingTimeout = endTime - now(); |
| 4373 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4374 | if (DEBUG_INJECTION) { |
| 4375 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4376 | "to become available."); |
| 4377 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4378 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4379 | break; |
| 4380 | } |
| 4381 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4382 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4383 | } |
| 4384 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4385 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4386 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4387 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4388 | if (DEBUG_INJECTION) { |
| 4389 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4390 | injectionState->pendingForegroundDispatches); |
| 4391 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4392 | nsecs_t remainingTimeout = endTime - now(); |
| 4393 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4394 | if (DEBUG_INJECTION) { |
| 4395 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4396 | "dispatches to finish."); |
| 4397 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4398 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4399 | break; |
| 4400 | } |
| 4401 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4402 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | injectionState->release(); |
| 4408 | } // release lock |
| 4409 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4410 | if (DEBUG_INJECTION) { |
| 4411 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
| 4412 | injectionResult, injectorPid, injectorUid); |
| 4413 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4414 | |
| 4415 | return injectionResult; |
| 4416 | } |
| 4417 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4418 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4419 | std::array<uint8_t, 32> calculatedHmac; |
| 4420 | std::unique_ptr<VerifiedInputEvent> result; |
| 4421 | switch (event.getType()) { |
| 4422 | case AINPUT_EVENT_TYPE_KEY: { |
| 4423 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4424 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4425 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4426 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4427 | break; |
| 4428 | } |
| 4429 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4430 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4431 | VerifiedMotionEvent verifiedMotionEvent = |
| 4432 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4433 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4434 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4435 | break; |
| 4436 | } |
| 4437 | default: { |
| 4438 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4439 | return nullptr; |
| 4440 | } |
| 4441 | } |
| 4442 | if (calculatedHmac == INVALID_HMAC) { |
| 4443 | return nullptr; |
| 4444 | } |
| 4445 | if (calculatedHmac != event.getHmac()) { |
| 4446 | return nullptr; |
| 4447 | } |
| 4448 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4449 | } |
| 4450 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4451 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4452 | return injectorUid == 0 || |
| 4453 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4454 | } |
| 4455 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4456 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4457 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4458 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4459 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4460 | if (DEBUG_INJECTION) { |
| 4461 | ALOGD("Setting input event injection result to %d. " |
| 4462 | "injectorPid=%d, injectorUid=%d", |
| 4463 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
| 4464 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4465 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4466 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4467 | // Log the outcome since the injector did not wait for the injection result. |
| 4468 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4469 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4470 | ALOGV("Asynchronous input event injection succeeded."); |
| 4471 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4472 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4473 | ALOGW("Asynchronous input event injection failed."); |
| 4474 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4475 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4476 | ALOGW("Asynchronous input event injection permission denied."); |
| 4477 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4478 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4479 | ALOGW("Asynchronous input event injection timed out."); |
| 4480 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4481 | case InputEventInjectionResult::PENDING: |
| 4482 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4483 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4484 | } |
| 4485 | } |
| 4486 | |
| 4487 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4488 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4489 | } |
| 4490 | } |
| 4491 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4492 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4493 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4494 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4495 | // display space, so we need to transform the injected events accordingly. |
| 4496 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4497 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4498 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4499 | |
| 4500 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4501 | entry.pointerCoords[i] = |
| 4502 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4503 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4504 | } |
| 4505 | } |
| 4506 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4507 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4508 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4509 | if (injectionState) { |
| 4510 | injectionState->pendingForegroundDispatches += 1; |
| 4511 | } |
| 4512 | } |
| 4513 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4514 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4515 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4516 | if (injectionState) { |
| 4517 | injectionState->pendingForegroundDispatches -= 1; |
| 4518 | |
| 4519 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4520 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4521 | } |
| 4522 | } |
| 4523 | } |
| 4524 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4525 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4526 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4527 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4528 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4529 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4530 | } |
| 4531 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4532 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4533 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4534 | if (windowHandleToken == nullptr) { |
| 4535 | return nullptr; |
| 4536 | } |
| 4537 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4538 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4539 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4540 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4541 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4542 | return windowHandle; |
| 4543 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4544 | } |
| 4545 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4546 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4547 | } |
| 4548 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4549 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4550 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4551 | if (windowHandleToken == nullptr) { |
| 4552 | return nullptr; |
| 4553 | } |
| 4554 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4555 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4556 | if (windowHandle->getToken() == windowHandleToken) { |
| 4557 | return windowHandle; |
| 4558 | } |
| 4559 | } |
| 4560 | return nullptr; |
| 4561 | } |
| 4562 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4563 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4564 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4565 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4566 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4567 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4568 | if (handle->getId() == windowHandle->getId() && |
| 4569 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4570 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4571 | ALOGE("Found window %s in display %" PRId32 |
| 4572 | ", but it should belong to display %" PRId32, |
| 4573 | windowHandle->getName().c_str(), it.first, |
| 4574 | windowHandle->getInfo()->displayId); |
| 4575 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4576 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4577 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4578 | } |
| 4579 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4580 | return nullptr; |
| 4581 | } |
| 4582 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4583 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4584 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4585 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4586 | } |
| 4587 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4588 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4589 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4590 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4591 | windowHandle.getInfo()->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4592 | if (connection != nullptr && noInputChannel) { |
| 4593 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4594 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4595 | return false; |
| 4596 | } |
| 4597 | |
| 4598 | if (connection == nullptr) { |
| 4599 | if (!noInputChannel) { |
| 4600 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4601 | } |
| 4602 | return false; |
| 4603 | } |
| 4604 | if (!connection->responsive) { |
| 4605 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4606 | return false; |
| 4607 | } |
| 4608 | return true; |
| 4609 | } |
| 4610 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4611 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4612 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4613 | auto connectionIt = mConnectionsByToken.find(token); |
| 4614 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4615 | return nullptr; |
| 4616 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4617 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4618 | } |
| 4619 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4620 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4621 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4622 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4623 | // Remove all handles on a display if there are no windows left. |
| 4624 | mWindowHandlesByDisplay.erase(displayId); |
| 4625 | return; |
| 4626 | } |
| 4627 | |
| 4628 | // Since we compare the pointer of input window handles across window updates, we need |
| 4629 | // 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] | 4630 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4631 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4632 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4633 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4634 | } |
| 4635 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4636 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4637 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4638 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4639 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4640 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4641 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4642 | const bool canReceiveInput = |
| 4643 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4644 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4645 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4646 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4647 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4648 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4649 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | if (info->displayId != displayId) { |
| 4653 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4654 | handle->getName().c_str(), displayId, info->displayId); |
| 4655 | continue; |
| 4656 | } |
| 4657 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4658 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4659 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4660 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4661 | oldHandle->updateFrom(handle); |
| 4662 | newHandles.push_back(oldHandle); |
| 4663 | } else { |
| 4664 | newHandles.push_back(handle); |
| 4665 | } |
| 4666 | } |
| 4667 | |
| 4668 | // Insert or replace |
| 4669 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4670 | } |
| 4671 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4672 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4673 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4674 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4675 | { // acquire lock |
| 4676 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4677 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4678 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4679 | } |
| 4680 | } |
| 4681 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4682 | mLooper->wake(); |
| 4683 | } |
| 4684 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4685 | /** |
| 4686 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4687 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4688 | * If set an empty list, remove all handles from the specific display. |
| 4689 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4690 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4691 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4692 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4693 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4694 | if (DEBUG_FOCUS) { |
| 4695 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4696 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4697 | windowList += iwh->getName() + " "; |
| 4698 | } |
| 4699 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4700 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4701 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4702 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4703 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4704 | const WindowInfo& info = *window->getInfo(); |
| 4705 | |
| 4706 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4707 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4708 | if (noInputWindow && window->getToken() != nullptr) { |
| 4709 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4710 | window->getName().c_str()); |
| 4711 | window->releaseChannel(); |
| 4712 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4713 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4714 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4715 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 4716 | !info.inputConfig.test( |
| 4717 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4718 | "%s has feature SPY, but is not a trusted overlay.", |
| 4719 | window->getName().c_str()); |
| 4720 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4721 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4722 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 4723 | !info.inputConfig.test( |
| 4724 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4725 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4726 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4727 | } |
| 4728 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4729 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4730 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4731 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4732 | // Save the old windows' orientation by ID before it gets updated. |
| 4733 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4734 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4735 | oldWindowOrientations.emplace(handle->getId(), |
| 4736 | handle->getInfo()->transform.getOrientation()); |
| 4737 | } |
| 4738 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4739 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4740 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4741 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4742 | if (mLastHoverWindowHandle && |
| 4743 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4744 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4745 | mLastHoverWindowHandle = nullptr; |
| 4746 | } |
| 4747 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4748 | std::optional<FocusResolver::FocusChanges> changes = |
| 4749 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4750 | if (changes) { |
| 4751 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4752 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4753 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4754 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4755 | mTouchStatesByDisplay.find(displayId); |
| 4756 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4757 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4758 | for (size_t i = 0; i < state.windows.size();) { |
| 4759 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4760 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4761 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4762 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4763 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4764 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4765 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4766 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4767 | if (touchedInputChannel != nullptr) { |
| 4768 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4769 | "touched window was removed"); |
| 4770 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4771 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4772 | // well. |
| 4773 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4774 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 4775 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4776 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4777 | if (wallpaper != nullptr) { |
| 4778 | sp<Connection> wallpaperConnection = |
| 4779 | getConnectionLocked(wallpaper->getToken()); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4780 | if (wallpaperConnection != nullptr) { |
| 4781 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4782 | options); |
| 4783 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4784 | } |
| 4785 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4786 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4787 | state.windows.erase(state.windows.begin() + i); |
| 4788 | } else { |
| 4789 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4790 | } |
| 4791 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4792 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4793 | // 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] | 4794 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4795 | if (mDragState && |
| 4796 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4797 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4798 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4799 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4800 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4801 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4802 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4803 | // pointer events if necessary. |
| 4804 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4805 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4806 | if (newWindowHandle != nullptr && |
| 4807 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4808 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4809 | std::shared_ptr<InputChannel> inputChannel = |
| 4810 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4811 | if (inputChannel != nullptr) { |
| 4812 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4813 | "touched window's orientation changed"); |
| 4814 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4815 | } |
| 4816 | } |
| 4817 | } |
| 4818 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4819 | // Release information for windows that are no longer present. |
| 4820 | // This ensures that unused input channels are released promptly. |
| 4821 | // Otherwise, they might stick around until the window handle is destroyed |
| 4822 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4823 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4824 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4825 | if (DEBUG_FOCUS) { |
| 4826 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4827 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4828 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4829 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4830 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4831 | } |
| 4832 | |
| 4833 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4834 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4835 | if (DEBUG_FOCUS) { |
| 4836 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4837 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4838 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4839 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4840 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4841 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4842 | } // release lock |
| 4843 | |
| 4844 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4845 | mLooper->wake(); |
| 4846 | } |
| 4847 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4848 | void InputDispatcher::setFocusedApplicationLocked( |
| 4849 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4850 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4851 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4852 | |
| 4853 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4854 | return; // This application is already focused. No need to wake up or change anything. |
| 4855 | } |
| 4856 | |
| 4857 | // Set the new application handle. |
| 4858 | if (inputApplicationHandle != nullptr) { |
| 4859 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4860 | } else { |
| 4861 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4862 | } |
| 4863 | |
| 4864 | // No matter what the old focused application was, stop waiting on it because it is |
| 4865 | // no longer focused. |
| 4866 | resetNoFocusedWindowTimeoutLocked(); |
| 4867 | } |
| 4868 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4869 | /** |
| 4870 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4871 | * the display not specified. |
| 4872 | * |
| 4873 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4874 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4875 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4876 | * display. The display-specified events won't be affected. |
| 4877 | */ |
| 4878 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4879 | if (DEBUG_FOCUS) { |
| 4880 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4881 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4882 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4883 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4884 | |
| 4885 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4886 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4887 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4888 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4889 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4890 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4891 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4892 | CancelationOptions |
| 4893 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4894 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4895 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4896 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4897 | } |
| 4898 | } |
| 4899 | mFocusedDisplayId = displayId; |
| 4900 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4901 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4902 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4903 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4904 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4905 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4906 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4907 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4908 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4909 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4910 | } |
| 4911 | } |
| 4912 | } |
| 4913 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4914 | if (DEBUG_FOCUS) { |
| 4915 | logDispatchStateLocked(); |
| 4916 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4917 | } // release lock |
| 4918 | |
| 4919 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4920 | mLooper->wake(); |
| 4921 | } |
| 4922 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4923 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4924 | if (DEBUG_FOCUS) { |
| 4925 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4926 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4927 | |
| 4928 | bool changed; |
| 4929 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4930 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4931 | |
| 4932 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4933 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4934 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | if (mDispatchEnabled && !enabled) { |
| 4938 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4939 | } |
| 4940 | |
| 4941 | mDispatchEnabled = enabled; |
| 4942 | mDispatchFrozen = frozen; |
| 4943 | changed = true; |
| 4944 | } else { |
| 4945 | changed = false; |
| 4946 | } |
| 4947 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4948 | if (DEBUG_FOCUS) { |
| 4949 | logDispatchStateLocked(); |
| 4950 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4951 | } // release lock |
| 4952 | |
| 4953 | if (changed) { |
| 4954 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4955 | mLooper->wake(); |
| 4956 | } |
| 4957 | } |
| 4958 | |
| 4959 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4960 | if (DEBUG_FOCUS) { |
| 4961 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4962 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4963 | |
| 4964 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4965 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4966 | |
| 4967 | if (mInputFilterEnabled == enabled) { |
| 4968 | return; |
| 4969 | } |
| 4970 | |
| 4971 | mInputFilterEnabled = enabled; |
| 4972 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4973 | } // release lock |
| 4974 | |
| 4975 | // Wake up poll loop since there might be work to do to drop everything. |
| 4976 | mLooper->wake(); |
| 4977 | } |
| 4978 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4979 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, |
| 4980 | bool hasPermission) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4981 | bool needWake = false; |
| 4982 | { |
| 4983 | std::scoped_lock lock(mLock); |
| 4984 | if (mInTouchMode == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4985 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4986 | } |
| 4987 | if (DEBUG_TOUCH_MODE) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4988 | ALOGD("Request to change touch mode from %s to %s (calling pid=%d, uid=%d, " |
| 4989 | "hasPermission=%s)", |
| 4990 | toString(mInTouchMode), toString(inTouchMode), pid, uid, toString(hasPermission)); |
| 4991 | } |
| 4992 | if (!hasPermission) { |
| 4993 | const sp<IBinder> focusedToken = |
| 4994 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 4995 | |
Antonio Kantek | 019eb66 | 2022-02-08 13:41:52 -0800 | [diff] [blame] | 4996 | // TODO(b/218541064): if no window is currently focused, then we need to check the last |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4997 | // interacted window (within 1 second timeout). We should allow touch mode change |
| 4998 | // if the last interacted window owner's pid/uid match the calling ones. |
| 4999 | if (focusedToken == nullptr) { |
| 5000 | return false; |
| 5001 | } |
| 5002 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5003 | if (windowHandle == nullptr) { |
| 5004 | return false; |
| 5005 | } |
| 5006 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 5007 | if (pid != windowInfo->ownerPid || uid != windowInfo->ownerUid) { |
| 5008 | return false; |
| 5009 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
| 5012 | // TODO(b/198499018): Store touch mode per display. |
| 5013 | mInTouchMode = inTouchMode; |
| 5014 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5015 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 5016 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5017 | } // release lock |
| 5018 | |
| 5019 | if (needWake) { |
| 5020 | mLooper->wake(); |
| 5021 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5022 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5023 | } |
| 5024 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5025 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5026 | if (opacity < 0 || opacity > 1) { |
| 5027 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5028 | return; |
| 5029 | } |
| 5030 | |
| 5031 | std::scoped_lock lock(mLock); |
| 5032 | mMaximumObscuringOpacityForTouch = opacity; |
| 5033 | } |
| 5034 | |
| 5035 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 5036 | std::scoped_lock lock(mLock); |
| 5037 | mBlockUntrustedTouchesMode = mode; |
| 5038 | } |
| 5039 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5040 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 5041 | const sp<IBinder>& token) { |
| 5042 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5043 | for (TouchedWindow& w : state.windows) { |
| 5044 | if (w.windowHandle->getToken() == token) { |
| 5045 | return std::make_pair(&state, &w); |
| 5046 | } |
| 5047 | } |
| 5048 | } |
| 5049 | return std::make_pair(nullptr, nullptr); |
| 5050 | } |
| 5051 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5052 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5053 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5054 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5055 | if (DEBUG_FOCUS) { |
| 5056 | ALOGD("Trivial transfer to same window."); |
| 5057 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5058 | return true; |
| 5059 | } |
| 5060 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5061 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5062 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5063 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5064 | // Find the target touch state and touched window by fromToken. |
| 5065 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 5066 | if (state == nullptr || touchedWindow == nullptr) { |
| 5067 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5068 | return false; |
| 5069 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5070 | |
| 5071 | const int32_t displayId = state->displayId; |
| 5072 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5073 | if (toWindowHandle == nullptr) { |
| 5074 | ALOGW("Cannot transfer focus because to window not found."); |
| 5075 | return false; |
| 5076 | } |
| 5077 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5078 | if (DEBUG_FOCUS) { |
| 5079 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5080 | touchedWindow->windowHandle->getName().c_str(), |
| 5081 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5082 | } |
| 5083 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5084 | // Erase old window. |
| 5085 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 5086 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 5087 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5088 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5089 | // Add new window. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame^] | 5090 | int32_t newTargetFlags = |
| 5091 | oldTargetFlags & (InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5092 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
| 5093 | newTargetFlags |= InputTarget::FLAG_FOREGROUND; |
| 5094 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5095 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5096 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5097 | // Store the dragging window. |
| 5098 | if (isDragDrop) { |
| 5099 | mDragState = std::make_unique<DragState>(toWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5100 | } |
| 5101 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5102 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5103 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5104 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5105 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5106 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5107 | CancelationOptions |
| 5108 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5109 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5110 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5111 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5112 | } |
| 5113 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5114 | if (DEBUG_FOCUS) { |
| 5115 | logDispatchStateLocked(); |
| 5116 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5117 | } // release lock |
| 5118 | |
| 5119 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5120 | mLooper->wake(); |
| 5121 | return true; |
| 5122 | } |
| 5123 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5124 | // Binder call |
| 5125 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 5126 | sp<IBinder> fromToken; |
| 5127 | { // acquire lock |
| 5128 | std::scoped_lock _l(mLock); |
| 5129 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5130 | auto it = std::find_if(mTouchStatesByDisplay.begin(), mTouchStatesByDisplay.end(), |
| 5131 | [](const auto& pair) { return pair.second.windows.size() == 1; }); |
| 5132 | if (it == mTouchStatesByDisplay.end()) { |
| 5133 | ALOGW("Cannot transfer touch state because there is no exact window being touched"); |
| 5134 | return false; |
| 5135 | } |
| 5136 | const int32_t displayId = it->first; |
| 5137 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5138 | if (toWindowHandle == nullptr) { |
| 5139 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 5140 | return false; |
| 5141 | } |
| 5142 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5143 | TouchState& state = it->second; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5144 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 5145 | fromToken = touchedWindow.windowHandle->getToken(); |
| 5146 | } // release lock |
| 5147 | |
| 5148 | return transferTouchFocus(fromToken, destChannelToken); |
| 5149 | } |
| 5150 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5151 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5152 | if (DEBUG_FOCUS) { |
| 5153 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5154 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5155 | |
| 5156 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5157 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5158 | |
| 5159 | resetKeyRepeatLocked(); |
| 5160 | releasePendingEventLocked(); |
| 5161 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5162 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5163 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5164 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5165 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5166 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5167 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5168 | } |
| 5169 | |
| 5170 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5171 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5172 | dumpDispatchStateLocked(dump); |
| 5173 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5174 | std::istringstream stream(dump); |
| 5175 | std::string line; |
| 5176 | |
| 5177 | while (std::getline(stream, line, '\n')) { |
| 5178 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5179 | } |
| 5180 | } |
| 5181 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5182 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5183 | std::string dump; |
| 5184 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5185 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5186 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5187 | |
| 5188 | std::string windowName = "None"; |
| 5189 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5190 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5191 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5192 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5193 | : "token has capture without window"; |
| 5194 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5195 | 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] | 5196 | |
| 5197 | return dump; |
| 5198 | } |
| 5199 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5200 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5201 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5202 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5203 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5204 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5205 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5206 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5207 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5208 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5209 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5210 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5211 | const std::chrono::duration timeout = |
| 5212 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5213 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5214 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5215 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5216 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5217 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5218 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5219 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5220 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5221 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5222 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5223 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5224 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5225 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5226 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5227 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5228 | 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] | 5229 | state.displayId, toString(state.down), toString(state.split), |
| 5230 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5231 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5232 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5233 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5234 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5235 | dump += StringPrintf(INDENT4 |
| 5236 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5237 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5238 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5239 | } |
| 5240 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5241 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5242 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5243 | } |
| 5244 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5245 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5246 | } |
| 5247 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5248 | if (mDragState) { |
| 5249 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5250 | mDragState->dump(dump, INDENT2); |
| 5251 | } |
| 5252 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5253 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5254 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5255 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5256 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5257 | const auto& displayInfo = it->second; |
| 5258 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5259 | displayInfo.logicalHeight); |
| 5260 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5261 | } else { |
| 5262 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5263 | } |
| 5264 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5265 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5266 | dump += INDENT2 "Windows:\n"; |
| 5267 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5268 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5269 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5270 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5271 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5272 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5273 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5274 | "applicationInfo.name=%s, " |
| 5275 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5276 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5277 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5278 | windowInfo->displayId, |
| 5279 | windowInfo->inputConfig.string().c_str(), |
| 5280 | windowInfo->alpha, windowInfo->frameLeft, |
| 5281 | windowInfo->frameTop, windowInfo->frameRight, |
| 5282 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5283 | windowInfo->applicationInfo.name.c_str(), |
| 5284 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5285 | dump += dumpRegion(windowInfo->touchableRegion); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5286 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5287 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5288 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5289 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5290 | millis(windowInfo->dispatchingTimeout), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5291 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5292 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5293 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5294 | } |
| 5295 | } else { |
| 5296 | dump += INDENT2 "Windows: <none>\n"; |
| 5297 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5298 | } |
| 5299 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5300 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5301 | } |
| 5302 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5303 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5304 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5305 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5306 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5307 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5308 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5309 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5310 | } |
| 5311 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5312 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5313 | |
| 5314 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5315 | if (!mRecentQueue.empty()) { |
| 5316 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5317 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5318 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5319 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5320 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5321 | } |
| 5322 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5323 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | // Dump event currently being dispatched. |
| 5327 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5328 | dump += INDENT "PendingEvent:\n"; |
| 5329 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5330 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5331 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5332 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5333 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5334 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5335 | } |
| 5336 | |
| 5337 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5338 | if (!mInboundQueue.empty()) { |
| 5339 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5340 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5341 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5342 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5343 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5344 | } |
| 5345 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5346 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5347 | } |
| 5348 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5349 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5350 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5351 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5352 | const KeyReplacement& replacement = pair.first; |
| 5353 | int32_t newKeyCode = pair.second; |
| 5354 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5355 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5356 | } |
| 5357 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5358 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5359 | } |
| 5360 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5361 | if (!mCommandQueue.empty()) { |
| 5362 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5363 | } else { |
| 5364 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5365 | } |
| 5366 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5367 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5368 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5369 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5370 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5371 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5372 | connection->inputChannel->getFd().get(), |
| 5373 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5374 | connection->getWindowName().c_str(), |
| 5375 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5376 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5377 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5378 | if (!connection->outboundQueue.empty()) { |
| 5379 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5380 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5381 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5382 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5383 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5384 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5385 | } |
| 5386 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5387 | if (!connection->waitQueue.empty()) { |
| 5388 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5389 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5390 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5391 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5392 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5393 | } |
| 5394 | } |
| 5395 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5396 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5400 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5401 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5402 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5403 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5404 | } |
| 5405 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5406 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5407 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5408 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5409 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5410 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5411 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5412 | } |
| 5413 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5414 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5415 | const size_t numMonitors = monitors.size(); |
| 5416 | for (size_t i = 0; i < numMonitors; i++) { |
| 5417 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5418 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5419 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5420 | dump += "\n"; |
| 5421 | } |
| 5422 | } |
| 5423 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5424 | class LooperEventCallback : public LooperCallback { |
| 5425 | public: |
| 5426 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5427 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5428 | |
| 5429 | private: |
| 5430 | std::function<int(int events)> mCallback; |
| 5431 | }; |
| 5432 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5433 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5434 | if (DEBUG_CHANNEL_CREATION) { |
| 5435 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5436 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5437 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5438 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5439 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5440 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5441 | |
| 5442 | if (result) { |
| 5443 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5444 | } |
| 5445 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5446 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5447 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5448 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5449 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5450 | sp<Connection> connection = |
| 5451 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5452 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5453 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5454 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5455 | } |
| 5456 | mConnectionsByToken.emplace(token, connection); |
| 5457 | |
| 5458 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5459 | this, std::placeholders::_1, token); |
| 5460 | |
| 5461 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5462 | } // release lock |
| 5463 | |
| 5464 | // Wake the looper because some connections have changed. |
| 5465 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5466 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5467 | } |
| 5468 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5469 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5470 | const std::string& name, |
| 5471 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5472 | std::shared_ptr<InputChannel> serverChannel; |
| 5473 | std::unique_ptr<InputChannel> clientChannel; |
| 5474 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5475 | if (result) { |
| 5476 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5477 | } |
| 5478 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5479 | { // acquire lock |
| 5480 | std::scoped_lock _l(mLock); |
| 5481 | |
| 5482 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5483 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5484 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5485 | } |
| 5486 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5487 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5488 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5489 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5490 | |
| 5491 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5492 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5493 | } |
| 5494 | mConnectionsByToken.emplace(token, connection); |
| 5495 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5496 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5497 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5498 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5499 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5500 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5501 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5502 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5503 | // Wake the looper because some connections have changed. |
| 5504 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5505 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5508 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5509 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5510 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5511 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5512 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5513 | if (status) { |
| 5514 | return status; |
| 5515 | } |
| 5516 | } // release lock |
| 5517 | |
| 5518 | // Wake the poll loop because removing the connection may have changed the current |
| 5519 | // synchronization state. |
| 5520 | mLooper->wake(); |
| 5521 | return OK; |
| 5522 | } |
| 5523 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5524 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5525 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5526 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5527 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5528 | // 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] | 5529 | return BAD_VALUE; |
| 5530 | } |
| 5531 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5532 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5533 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5534 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5535 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5536 | } |
| 5537 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5538 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5539 | |
| 5540 | nsecs_t currentTime = now(); |
| 5541 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5542 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5543 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5544 | return OK; |
| 5545 | } |
| 5546 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5547 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5548 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5549 | auto& [displayId, monitors] = *it; |
| 5550 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5551 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5552 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5553 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5554 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5555 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5556 | } else { |
| 5557 | ++it; |
| 5558 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5559 | } |
| 5560 | } |
| 5561 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5562 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5563 | std::scoped_lock _l(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5564 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5565 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5566 | if (!requestingChannel) { |
| 5567 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5568 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5569 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5570 | |
| 5571 | auto [statePtr, windowPtr] = findTouchStateAndWindowLocked(token); |
| 5572 | if (statePtr == nullptr || windowPtr == nullptr || !statePtr->down) { |
| 5573 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5574 | " Ignoring."); |
| 5575 | return BAD_VALUE; |
| 5576 | } |
| 5577 | |
| 5578 | TouchState& state = *statePtr; |
| 5579 | |
| 5580 | // Send cancel events to all the input channels we're stealing from. |
| 5581 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5582 | "input channel stole pointer stream"); |
| 5583 | options.deviceId = state.deviceId; |
| 5584 | options.displayId = state.displayId; |
| 5585 | std::string canceledWindows; |
| 5586 | for (const TouchedWindow& window : state.windows) { |
| 5587 | const std::shared_ptr<InputChannel> channel = |
| 5588 | getInputChannelLocked(window.windowHandle->getToken()); |
| 5589 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5590 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5591 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5592 | canceledWindows += channel->getName(); |
| 5593 | } |
| 5594 | } |
| 5595 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5596 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5597 | canceledWindows.c_str()); |
| 5598 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5599 | // Prevent the gesture from being sent to any other windows. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5600 | state.filterWindowsExcept(token); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5601 | state.preventNewTargets = true; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5602 | return OK; |
| 5603 | } |
| 5604 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5605 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5606 | { // acquire lock |
| 5607 | std::scoped_lock _l(mLock); |
| 5608 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5609 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5610 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5611 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5612 | : "token without window"); |
| 5613 | } |
| 5614 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5615 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5616 | if (focusedToken != windowToken) { |
| 5617 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5618 | enabled ? "enable" : "disable"); |
| 5619 | return; |
| 5620 | } |
| 5621 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5622 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5623 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5624 | "window has %s requested pointer capture.", |
| 5625 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5626 | return; |
| 5627 | } |
| 5628 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5629 | if (enabled) { |
| 5630 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5631 | mIneligibleDisplaysForPointerCapture.end(), |
| 5632 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5633 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5634 | return; |
| 5635 | } |
| 5636 | } |
| 5637 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5638 | setPointerCaptureLocked(enabled); |
| 5639 | } // release lock |
| 5640 | |
| 5641 | // Wake the thread to process command entries. |
| 5642 | mLooper->wake(); |
| 5643 | } |
| 5644 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5645 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5646 | { // acquire lock |
| 5647 | std::scoped_lock _l(mLock); |
| 5648 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5649 | if (!isEligible) { |
| 5650 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5651 | } |
| 5652 | } // release lock |
| 5653 | } |
| 5654 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5655 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5656 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5657 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5658 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5659 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5660 | } |
| 5661 | } |
| 5662 | } |
| 5663 | return std::nullopt; |
| 5664 | } |
| 5665 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5666 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5667 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5668 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5669 | } |
| 5670 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5671 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5672 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5673 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5674 | } |
| 5675 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5676 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5677 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5678 | } |
| 5679 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5680 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5681 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5682 | if (connection == nullptr) { |
| 5683 | return "<nullptr>"; |
| 5684 | } |
| 5685 | return connection->getInputChannelName(); |
| 5686 | } |
| 5687 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5688 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5689 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5690 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5691 | } |
| 5692 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5693 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5694 | const sp<Connection>& connection, uint32_t seq, |
| 5695 | bool handled, nsecs_t consumeTime) { |
| 5696 | // Handle post-event policy actions. |
| 5697 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5698 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5699 | return; |
| 5700 | } |
| 5701 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5702 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5703 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5704 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5705 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5706 | } |
| 5707 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5708 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5709 | connection->inputChannel->getConnectionToken(), |
| 5710 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5711 | } |
| 5712 | |
| 5713 | bool restartEvent; |
| 5714 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5715 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5716 | restartEvent = |
| 5717 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5718 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5719 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5720 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5721 | handled); |
| 5722 | } else { |
| 5723 | restartEvent = false; |
| 5724 | } |
| 5725 | |
| 5726 | // Dequeue the event and start the next cycle. |
| 5727 | // Because the lock might have been released, it is possible that the |
| 5728 | // contents of the wait queue to have been drained, so we need to double-check |
| 5729 | // a few things. |
| 5730 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5731 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5732 | dispatchEntry = *dispatchEntryIt; |
| 5733 | connection->waitQueue.erase(dispatchEntryIt); |
| 5734 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5735 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5736 | if (!connection->responsive) { |
| 5737 | connection->responsive = isConnectionResponsive(*connection); |
| 5738 | if (connection->responsive) { |
| 5739 | // The connection was unresponsive, and now it's responsive. |
| 5740 | processConnectionResponsiveLocked(*connection); |
| 5741 | } |
| 5742 | } |
| 5743 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5744 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5745 | connection->outboundQueue.push_front(dispatchEntry); |
| 5746 | traceOutboundQueueLength(*connection); |
| 5747 | } else { |
| 5748 | releaseDispatchEntry(dispatchEntry); |
| 5749 | } |
| 5750 | } |
| 5751 | |
| 5752 | // Start the next dispatch cycle for this connection. |
| 5753 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5754 | } |
| 5755 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5756 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5757 | const sp<IBinder>& newToken) { |
| 5758 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5759 | scoped_unlock unlock(mLock); |
| 5760 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5761 | }; |
| 5762 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5763 | } |
| 5764 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5765 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5766 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5767 | scoped_unlock unlock(mLock); |
| 5768 | mPolicy->notifyDropWindow(token, x, y); |
| 5769 | }; |
| 5770 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5771 | } |
| 5772 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5773 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5774 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5775 | scoped_unlock unlock(mLock); |
| 5776 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5777 | }; |
| 5778 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5779 | } |
| 5780 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5781 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5782 | if (connection == nullptr) { |
| 5783 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5784 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5785 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5786 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5787 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5788 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5789 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5790 | return; |
| 5791 | } |
| 5792 | /** |
| 5793 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5794 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5795 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5796 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5797 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5798 | * most useful. |
| 5799 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5800 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5801 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5802 | std::string reason = |
| 5803 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5804 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5805 | ns2ms(currentWait), |
| 5806 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5807 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5808 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5809 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5810 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5811 | |
| 5812 | // Stop waking up for events on this connection, it is already unresponsive |
| 5813 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5814 | } |
| 5815 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5816 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5817 | std::string reason = |
| 5818 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5819 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5820 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5821 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5822 | scoped_unlock unlock(mLock); |
| 5823 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5824 | }; |
| 5825 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5826 | } |
| 5827 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5828 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5829 | const std::string& reason) { |
| 5830 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5831 | updateLastAnrStateLocked(windowLabel, reason); |
| 5832 | } |
| 5833 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5834 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5835 | const std::string& reason) { |
| 5836 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5837 | updateLastAnrStateLocked(windowLabel, reason); |
| 5838 | } |
| 5839 | |
| 5840 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5841 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5842 | // 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] | 5843 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5844 | struct tm tm; |
| 5845 | localtime_r(&t, &tm); |
| 5846 | char timestr[64]; |
| 5847 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5848 | mLastAnrState.clear(); |
| 5849 | mLastAnrState += INDENT "ANR:\n"; |
| 5850 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5851 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5852 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5853 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5854 | } |
| 5855 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5856 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5857 | KeyEntry& entry) { |
| 5858 | const KeyEvent event = createKeyEvent(entry); |
| 5859 | nsecs_t delay = 0; |
| 5860 | { // release lock |
| 5861 | scoped_unlock unlock(mLock); |
| 5862 | android::base::Timer t; |
| 5863 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5864 | entry.policyFlags); |
| 5865 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5866 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5867 | std::to_string(t.duration().count()).c_str()); |
| 5868 | } |
| 5869 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5870 | |
| 5871 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5872 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5873 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5874 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5875 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5876 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5877 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5878 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5879 | } |
| 5880 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5881 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5882 | std::optional<int32_t> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5883 | std::string reason) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5884 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5885 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5886 | mPolicy->notifyWindowUnresponsive(token, pid, reason); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5887 | }; |
| 5888 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5889 | } |
| 5890 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5891 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
| 5892 | std::optional<int32_t> pid) { |
| 5893 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5894 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5895 | mPolicy->notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5896 | }; |
| 5897 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
| 5900 | /** |
| 5901 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5902 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5903 | * command entry to the command queue. |
| 5904 | */ |
| 5905 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5906 | std::string reason) { |
| 5907 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5908 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5909 | if (connection.monitor) { |
| 5910 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5911 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5912 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 5913 | } else { |
| 5914 | // The connection is a window |
| 5915 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5916 | reason.c_str()); |
| 5917 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 5918 | if (handle != nullptr) { |
| 5919 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5920 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5921 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5922 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5923 | } |
| 5924 | |
| 5925 | /** |
| 5926 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5927 | */ |
| 5928 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5929 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5930 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5931 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5932 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 5933 | } else { |
| 5934 | // The connection is a window |
| 5935 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 5936 | if (handle != nullptr) { |
| 5937 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5938 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5939 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5940 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5941 | } |
| 5942 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5943 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5944 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5945 | KeyEntry& keyEntry, bool handled) { |
| 5946 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5947 | if (!handled) { |
| 5948 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5949 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5950 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5951 | return false; |
| 5952 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5953 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5954 | // Get the fallback key state. |
| 5955 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5956 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5957 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5958 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5959 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5960 | } |
| 5961 | |
| 5962 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5963 | // If the application handles the original key for which we previously |
| 5964 | // generated a fallback or if the window is not a foreground window, |
| 5965 | // then cancel the associated fallback key, if any. |
| 5966 | if (fallbackKeyCode != -1) { |
| 5967 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5968 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5969 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 5970 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5971 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 5972 | keyEntry.policyFlags); |
| 5973 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5974 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5975 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5976 | |
| 5977 | mLock.unlock(); |
| 5978 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5979 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5980 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5981 | |
| 5982 | mLock.lock(); |
| 5983 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5984 | // Cancel the fallback key. |
| 5985 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5986 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5987 | "application handled the original non-fallback key " |
| 5988 | "or is no longer a foreground target, " |
| 5989 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5990 | options.keyCode = fallbackKeyCode; |
| 5991 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5992 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5993 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5994 | } |
| 5995 | } else { |
| 5996 | // If the application did not handle a non-fallback key, first check |
| 5997 | // that we are in a good state to perform unhandled key event processing |
| 5998 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5999 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6000 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6001 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6002 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6003 | "since this is not an initial down. " |
| 6004 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6005 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6006 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6007 | return false; |
| 6008 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6009 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6010 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6011 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6012 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6013 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6014 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6015 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6016 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6017 | |
| 6018 | mLock.unlock(); |
| 6019 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6020 | bool fallback = |
| 6021 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6022 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6023 | |
| 6024 | mLock.lock(); |
| 6025 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6026 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6027 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6028 | return false; |
| 6029 | } |
| 6030 | |
| 6031 | // Latch the fallback keycode for this key on an initial down. |
| 6032 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6033 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6034 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6035 | fallbackKeyCode = event.getKeyCode(); |
| 6036 | } else { |
| 6037 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6038 | } |
| 6039 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6040 | } |
| 6041 | |
| 6042 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6043 | |
| 6044 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6045 | // We will continue to dispatch the key to the policy but we will no |
| 6046 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6047 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6048 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6049 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6050 | if (fallback) { |
| 6051 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6052 | "as a fallback for %d, but on the DOWN it had requested " |
| 6053 | "to send %d instead. Fallback canceled.", |
| 6054 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6055 | } else { |
| 6056 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6057 | "but on the DOWN it had requested to send %d. " |
| 6058 | "Fallback canceled.", |
| 6059 | originalKeyCode, fallbackKeyCode); |
| 6060 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6061 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6062 | |
| 6063 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6064 | "canceling fallback, policy no longer desires it"); |
| 6065 | options.keyCode = fallbackKeyCode; |
| 6066 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6067 | |
| 6068 | fallback = false; |
| 6069 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6070 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6071 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6072 | } |
| 6073 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6074 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6075 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6076 | { |
| 6077 | std::string msg; |
| 6078 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6079 | connection->inputState.getFallbackKeys(); |
| 6080 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6081 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6082 | } |
| 6083 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6084 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6085 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6086 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6087 | |
| 6088 | if (fallback) { |
| 6089 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6090 | keyEntry.eventTime = event.getEventTime(); |
| 6091 | keyEntry.deviceId = event.getDeviceId(); |
| 6092 | keyEntry.source = event.getSource(); |
| 6093 | keyEntry.displayId = event.getDisplayId(); |
| 6094 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6095 | keyEntry.keyCode = fallbackKeyCode; |
| 6096 | keyEntry.scanCode = event.getScanCode(); |
| 6097 | keyEntry.metaState = event.getMetaState(); |
| 6098 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6099 | keyEntry.downTime = event.getDownTime(); |
| 6100 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6101 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6102 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6103 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6104 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6105 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6106 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6107 | return true; // restart the event |
| 6108 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6109 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6110 | ALOGD("Unhandled key event: No fallback key."); |
| 6111 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6112 | |
| 6113 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6114 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6115 | } |
| 6116 | } |
| 6117 | return false; |
| 6118 | } |
| 6119 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6120 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6121 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6122 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6123 | return false; |
| 6124 | } |
| 6125 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6126 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6127 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6128 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6129 | } |
| 6130 | } |
| 6131 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6132 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6133 | if (ATRACE_ENABLED()) { |
| 6134 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6135 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6136 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6137 | } |
| 6138 | } |
| 6139 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6140 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6141 | if (ATRACE_ENABLED()) { |
| 6142 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6143 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6144 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6145 | } |
| 6146 | } |
| 6147 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6148 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6149 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6150 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6151 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6152 | dumpDispatchStateLocked(dump); |
| 6153 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6154 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6155 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6156 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6157 | } |
| 6158 | } |
| 6159 | |
| 6160 | void InputDispatcher::monitor() { |
| 6161 | // 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] | 6162 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6163 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6164 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6165 | } |
| 6166 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6167 | /** |
| 6168 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6169 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6170 | * this method can be safely called from any thread, as long as you've ensured that |
| 6171 | * the work you are interested in completing has already been queued. |
| 6172 | */ |
| 6173 | bool InputDispatcher::waitForIdle() { |
| 6174 | /** |
| 6175 | * Timeout should represent the longest possible time that a device might spend processing |
| 6176 | * events and commands. |
| 6177 | */ |
| 6178 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6179 | std::unique_lock lock(mLock); |
| 6180 | mLooper->wake(); |
| 6181 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6182 | return result == std::cv_status::no_timeout; |
| 6183 | } |
| 6184 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6185 | /** |
| 6186 | * Sets focus to the window identified by the token. This must be called |
| 6187 | * after updating any input window handles. |
| 6188 | * |
| 6189 | * Params: |
| 6190 | * request.token - input channel token used to identify the window that should gain focus. |
| 6191 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6192 | * specified token does not match the currently focused window, this request will be dropped. |
| 6193 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6194 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6195 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6196 | * when requesting the focus change. This determines which request gets |
| 6197 | * precedence if there is a focus change request from another source such as pointer down. |
| 6198 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6199 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6200 | { // acquire lock |
| 6201 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6202 | std::optional<FocusResolver::FocusChanges> changes = |
| 6203 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6204 | if (changes) { |
| 6205 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6206 | } |
| 6207 | } // release lock |
| 6208 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6209 | mLooper->wake(); |
| 6210 | } |
| 6211 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6212 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6213 | if (changes.oldFocus) { |
| 6214 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6215 | if (focusedInputChannel) { |
| 6216 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6217 | "focus left window"); |
| 6218 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6219 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6220 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6221 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6222 | if (changes.newFocus) { |
| 6223 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6224 | } |
| 6225 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6226 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6227 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6228 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6229 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6230 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6231 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6232 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6233 | // front. |
| 6234 | disablePointerCaptureForcedLocked(); |
| 6235 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6236 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6237 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6238 | } |
| 6239 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6240 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6241 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6242 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6243 | return; |
| 6244 | } |
| 6245 | |
| 6246 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6247 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6248 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6249 | setPointerCaptureLocked(false); |
| 6250 | } |
| 6251 | |
| 6252 | if (!mWindowTokenWithPointerCapture) { |
| 6253 | // No need to send capture changes because no window has capture. |
| 6254 | return; |
| 6255 | } |
| 6256 | |
| 6257 | if (mPendingEvent != nullptr) { |
| 6258 | // Move the pending event to the front of the queue. This will give the chance |
| 6259 | // for the pending event to be dropped if it is a captured event. |
| 6260 | mInboundQueue.push_front(mPendingEvent); |
| 6261 | mPendingEvent = nullptr; |
| 6262 | } |
| 6263 | |
| 6264 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6265 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6266 | mInboundQueue.push_front(std::move(entry)); |
| 6267 | } |
| 6268 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6269 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6270 | mCurrentPointerCaptureRequest.enable = enable; |
| 6271 | mCurrentPointerCaptureRequest.seq++; |
| 6272 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6273 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6274 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6275 | }; |
| 6276 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6277 | } |
| 6278 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6279 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6280 | { // acquire lock |
| 6281 | std::scoped_lock _l(mLock); |
| 6282 | // Set an empty list to remove all handles from the specific display. |
| 6283 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6284 | setFocusedApplicationLocked(displayId, nullptr); |
| 6285 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6286 | // have been removed for the removed display. |
| 6287 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6288 | // Reset pointer capture eligibility, regardless of previous state. |
| 6289 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6290 | } // release lock |
| 6291 | |
| 6292 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6293 | mLooper->wake(); |
| 6294 | } |
| 6295 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6296 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6297 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6298 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6299 | // more convenient parsing. |
| 6300 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6301 | for (const auto& info : windowInfos) { |
| 6302 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6303 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6304 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6305 | |
| 6306 | { // acquire lock |
| 6307 | std::scoped_lock _l(mLock); |
| 6308 | mDisplayInfos.clear(); |
| 6309 | for (const auto& displayInfo : displayInfos) { |
| 6310 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6311 | } |
| 6312 | |
| 6313 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6314 | setInputWindowsLocked(handles, displayId); |
| 6315 | } |
| 6316 | } |
| 6317 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6318 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6319 | } |
| 6320 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6321 | bool InputDispatcher::shouldDropInput( |
| 6322 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6323 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6324 | (windowHandle->getInfo()->inputConfig.test( |
| 6325 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6326 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6327 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6328 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6329 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6330 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6331 | windowHandle->getInfo()->displayId); |
| 6332 | return true; |
| 6333 | } |
| 6334 | return false; |
| 6335 | } |
| 6336 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6337 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6338 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6339 | const std::vector<DisplayInfo>& displayInfos) { |
| 6340 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6341 | } |
| 6342 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6343 | void InputDispatcher::cancelCurrentTouch() { |
| 6344 | { |
| 6345 | std::scoped_lock _l(mLock); |
| 6346 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
| 6347 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 6348 | "cancel current touch"); |
| 6349 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6350 | |
| 6351 | mTouchStatesByDisplay.clear(); |
| 6352 | mLastHoverWindowHandle.clear(); |
| 6353 | } |
| 6354 | // Wake up poll loop since there might be work to do. |
| 6355 | mLooper->wake(); |
| 6356 | } |
| 6357 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6358 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6359 | std::scoped_lock _l(mLock); |
| 6360 | mMonitorDispatchingTimeout = timeout; |
| 6361 | } |
| 6362 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6363 | } // namespace android::inputdispatcher |