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 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 570 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, int32_t pid, int32_t uid) { |
| 571 | if (windowHandle == nullptr) { |
| 572 | return false; |
| 573 | } |
| 574 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 575 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 576 | return true; |
| 577 | } |
| 578 | return false; |
| 579 | } |
| 580 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 581 | // Checks targeted injection using the window's owner's uid. |
| 582 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 583 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 584 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 585 | const EventEntry& entry) { |
| 586 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 587 | // The event was not injected, or the injected event does not target a window. |
| 588 | return {}; |
| 589 | } |
| 590 | const int32_t uid = *entry.injectionState->targetUid; |
| 591 | if (window == nullptr) { |
| 592 | return StringPrintf("No valid window target for injection into uid %d.", uid); |
| 593 | } |
| 594 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
| 595 | return StringPrintf("Injected event targeted at uid %d would be dispatched to window '%s' " |
| 596 | "owned by uid %d.", |
| 597 | uid, window->getName().c_str(), window->getInfo()->ownerUid); |
| 598 | } |
| 599 | return {}; |
| 600 | } |
| 601 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 602 | } // namespace |
| 603 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 604 | // --- InputDispatcher --- |
| 605 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 606 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 607 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} |
| 608 | |
| 609 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy, |
| 610 | std::chrono::nanoseconds staleEventTimeout) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 611 | : mPolicy(policy), |
| 612 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 613 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 614 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 615 | mAppSwitchSawKeyDown(false), |
| 616 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 617 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 618 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 619 | mDispatchEnabled(false), |
| 620 | mDispatchFrozen(false), |
| 621 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 622 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 623 | // To avoid leaking stack in case that call never comes, and for tests, |
| 624 | // initialize it here anyways. |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 625 | mInTouchMode(kDefaultInTouchMode), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 626 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 627 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 628 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 629 | mStaleEventTimeout(staleEventTimeout), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 630 | mLatencyAggregator(), |
Siarhei Vishniakou | bd25272 | 2022-01-06 03:49:35 -0800 | [diff] [blame] | 631 | mLatencyTracker(&mLatencyAggregator) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 632 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 633 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 634 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 635 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 636 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 637 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 638 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 639 | |
| 640 | policy->getDispatcherConfiguration(&mConfig); |
| 641 | } |
| 642 | |
| 643 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 644 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 645 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 646 | resetKeyRepeatLocked(); |
| 647 | releasePendingEventLocked(); |
| 648 | drainInboundQueueLocked(); |
| 649 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 650 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 651 | while (!mConnectionsByToken.empty()) { |
| 652 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 653 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 654 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 658 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 659 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 660 | return ALREADY_EXISTS; |
| 661 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 662 | mThread = std::make_unique<InputThread>( |
| 663 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 664 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 668 | if (mThread && mThread->isCallingThread()) { |
| 669 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 670 | return INVALID_OPERATION; |
| 671 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 672 | mThread.reset(); |
| 673 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 676 | void InputDispatcher::dispatchOnce() { |
| 677 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 678 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 679 | std::scoped_lock _l(mLock); |
| 680 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 681 | |
| 682 | // Run a dispatch loop if there are no pending commands. |
| 683 | // The dispatch loop might enqueue commands to run afterwards. |
| 684 | if (!haveCommandsLocked()) { |
| 685 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 686 | } |
| 687 | |
| 688 | // Run all pending commands if there are any. |
| 689 | // 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] | 690 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 691 | nextWakeupTime = LONG_LONG_MIN; |
| 692 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 693 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 694 | // If we are still waiting for ack on some events, |
| 695 | // we might have to wake up earlier to check if an app is anr'ing. |
| 696 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 697 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 698 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 699 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 700 | // pending or queued events |
| 701 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 702 | mDispatcherEnteredIdle.notify_all(); |
| 703 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 704 | } // release lock |
| 705 | |
| 706 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 707 | nsecs_t currentTime = now(); |
| 708 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 709 | mLooper->pollOnce(timeoutMillis); |
| 710 | } |
| 711 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 712 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 713 | * Raise ANR if there is no focused window. |
| 714 | * Before the ANR is raised, do a final state check: |
| 715 | * 1. The currently focused application must be the same one we are waiting for. |
| 716 | * 2. Ensure we still don't have a focused window. |
| 717 | */ |
| 718 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 719 | // Check if the application that we are waiting for is still focused. |
| 720 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 721 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 722 | if (focusedApplication == nullptr || |
| 723 | focusedApplication->getApplicationToken() != |
| 724 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 725 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 726 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 727 | focusedApplication->getName().c_str()); |
| 728 | return; // The focused application has changed. |
| 729 | } |
| 730 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 731 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 732 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 733 | if (focusedWindowHandle != nullptr) { |
| 734 | return; // We now have a focused window. No need for ANR. |
| 735 | } |
| 736 | onAnrLocked(mAwaitedFocusedApplication); |
| 737 | } |
| 738 | |
| 739 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 740 | * Check if any of the connections' wait queues have events that are too old. |
| 741 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 742 | * Return the time at which we should wake up next. |
| 743 | */ |
| 744 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 745 | const nsecs_t currentTime = now(); |
| 746 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 747 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 748 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 749 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 750 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 751 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 752 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 753 | return LONG_LONG_MIN; |
| 754 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 755 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 756 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | // Check if any connection ANRs are due |
| 761 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 762 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 763 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 764 | } |
| 765 | |
| 766 | // If we reached here, we have an unresponsive connection. |
| 767 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 768 | if (connection == nullptr) { |
| 769 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 770 | return nextAnrCheck; |
| 771 | } |
| 772 | connection->responsive = false; |
| 773 | // Stop waking up for this unresponsive connection |
| 774 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 775 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 776 | return LONG_LONG_MIN; |
| 777 | } |
| 778 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 779 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
| 780 | const sp<Connection>& connection) { |
| 781 | if (connection->monitor) { |
| 782 | return mMonitorDispatchingTimeout; |
| 783 | } |
| 784 | const sp<WindowInfoHandle> window = |
| 785 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 786 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 787 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 788 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 789 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 792 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 793 | nsecs_t currentTime = now(); |
| 794 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 795 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 796 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 797 | // repeat if the device is just coming out of sleep. |
| 798 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 799 | resetKeyRepeatLocked(); |
| 800 | } |
| 801 | |
| 802 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 803 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 804 | if (DEBUG_FOCUS) { |
| 805 | ALOGD("Dispatch frozen. Waiting some more."); |
| 806 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 807 | return; |
| 808 | } |
| 809 | |
| 810 | // Optimize latency of app switches. |
| 811 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 812 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 813 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 814 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 815 | *nextWakeupTime = mAppSwitchDueTime; |
| 816 | } |
| 817 | |
| 818 | // Ready to start a new event. |
| 819 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 820 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 821 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 822 | if (isAppSwitchDue) { |
| 823 | // The inbound queue is empty so the app switch key we were waiting |
| 824 | // for will never arrive. Stop waiting for it. |
| 825 | resetPendingAppSwitchLocked(false); |
| 826 | isAppSwitchDue = false; |
| 827 | } |
| 828 | |
| 829 | // Synthesize a key repeat if appropriate. |
| 830 | if (mKeyRepeatState.lastKeyEntry) { |
| 831 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 832 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 833 | } else { |
| 834 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 835 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | // Nothing to do if there is no pending event. |
| 841 | if (!mPendingEvent) { |
| 842 | return; |
| 843 | } |
| 844 | } else { |
| 845 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 846 | mPendingEvent = mInboundQueue.front(); |
| 847 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 848 | traceInboundQueueLengthLocked(); |
| 849 | } |
| 850 | |
| 851 | // Poke user activity for this event. |
| 852 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 853 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 854 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | // Now we have an event to dispatch. |
| 858 | // 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] | 859 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 860 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 861 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 862 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 863 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 864 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 865 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 869 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 873 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 874 | const ConfigurationChangedEntry& typedEntry = |
| 875 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 876 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 877 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 878 | break; |
| 879 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 880 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 881 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 882 | const DeviceResetEntry& typedEntry = |
| 883 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 884 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 885 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 886 | break; |
| 887 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 888 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 889 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 890 | std::shared_ptr<FocusEntry> typedEntry = |
| 891 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 892 | dispatchFocusLocked(currentTime, typedEntry); |
| 893 | done = true; |
| 894 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 895 | break; |
| 896 | } |
| 897 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 898 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 899 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 900 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 901 | done = true; |
| 902 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 903 | break; |
| 904 | } |
| 905 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 906 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 907 | const auto typedEntry = |
| 908 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 909 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 910 | done = true; |
| 911 | break; |
| 912 | } |
| 913 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 914 | case EventEntry::Type::DRAG: { |
| 915 | std::shared_ptr<DragEntry> typedEntry = |
| 916 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 917 | dispatchDragLocked(currentTime, typedEntry); |
| 918 | done = true; |
| 919 | break; |
| 920 | } |
| 921 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 922 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 923 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 924 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 925 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 926 | resetPendingAppSwitchLocked(true); |
| 927 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 928 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 929 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 930 | } |
| 931 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 932 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 933 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 934 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 935 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 936 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 937 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 938 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 939 | break; |
| 940 | } |
| 941 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 942 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 943 | std::shared_ptr<MotionEntry> motionEntry = |
| 944 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 945 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 946 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 947 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 948 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 949 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 950 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 951 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 952 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 953 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 954 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 955 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 956 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 957 | |
| 958 | case EventEntry::Type::SENSOR: { |
| 959 | std::shared_ptr<SensorEntry> sensorEntry = |
| 960 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 961 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 962 | dropReason = DropReason::APP_SWITCH; |
| 963 | } |
| 964 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 965 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 966 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 967 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 968 | dropReason = DropReason::STALE; |
| 969 | } |
| 970 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 971 | done = true; |
| 972 | break; |
| 973 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 977 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 978 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 979 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 980 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 981 | |
| 982 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 983 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 987 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 988 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 989 | } |
| 990 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 991 | /** |
| 992 | * Return true if the events preceding this incoming motion event should be dropped |
| 993 | * Return false otherwise (the default behaviour) |
| 994 | */ |
| 995 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 996 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 997 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 998 | |
| 999 | // Optimize case where the current application is unresponsive and the user |
| 1000 | // decides to touch a window in a different application. |
| 1001 | // If the application takes too long to catch up then we drop all events preceding |
| 1002 | // the touch into the other window. |
| 1003 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1004 | int32_t displayId = motionEntry.displayId; |
| 1005 | int32_t x = static_cast<int32_t>( |
| 1006 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1007 | int32_t y = static_cast<int32_t>( |
| 1008 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1009 | |
| 1010 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1011 | sp<WindowInfoHandle> touchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1012 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1013 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1014 | touchedWindowHandle->getApplicationToken() != |
| 1015 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1016 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1017 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1018 | "for %s", |
| 1019 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1020 | return true; |
| 1021 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1022 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1023 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1024 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1025 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1026 | for (const auto& windowHandle : touchedSpies) { |
| 1027 | const sp<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1028 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1029 | // This spy window could take more input. Drop all events preceding this |
| 1030 | // 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] | 1031 | 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] | 1032 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1033 | mAwaitedFocusedApplication->getName().c_str()); |
| 1034 | return true; |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 1040 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 1041 | // events to be processed before dispatching the key event. This is because these motion events |
| 1042 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 1043 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 1044 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1045 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1046 | "just send the pending key event to the focused window."); |
| 1047 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1048 | } |
| 1049 | return false; |
| 1050 | } |
| 1051 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1052 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1053 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1054 | mInboundQueue.push_back(std::move(newEntry)); |
| 1055 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1056 | traceInboundQueueLengthLocked(); |
| 1057 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1058 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1059 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 1060 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1061 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1062 | // Optimize app switch latency. |
| 1063 | // If the application takes too long to catch up then we drop all events preceding |
| 1064 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1065 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1066 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1067 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1068 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1069 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1070 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1071 | if (DEBUG_APP_SWITCH) { |
| 1072 | ALOGD("App switch is pending!"); |
| 1073 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1074 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1075 | mAppSwitchSawKeyDown = false; |
| 1076 | needWake = true; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1080 | |
| 1081 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1082 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1083 | // time for it may have been handled in the policy and could be dropped. |
| 1084 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1085 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1086 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1087 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1088 | pendingKey.interceptKeyResult == |
| 1089 | KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1090 | pendingKey.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1091 | pendingKey.interceptKeyWakeupTime = 0; |
| 1092 | needWake = true; |
| 1093 | } |
| 1094 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1095 | break; |
| 1096 | } |
| 1097 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1098 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 1099 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1100 | "Unexpected untrusted event."); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1101 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1102 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1103 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1104 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1105 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1106 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1107 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1108 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1109 | break; |
| 1110 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1111 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1112 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1113 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1114 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1115 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1116 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1117 | // nothing to do |
| 1118 | break; |
| 1119 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | return needWake; |
| 1123 | } |
| 1124 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1125 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1126 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1127 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1128 | mRecentQueue.push_back(entry); |
| 1129 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1130 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1131 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1135 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1136 | int32_t y, TouchState* touchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1137 | bool isStylus, |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1138 | bool addOutsideTargets, |
| 1139 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1140 | if (addOutsideTargets && touchState == nullptr) { |
| 1141 | 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] | 1142 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1143 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1144 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1145 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1146 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1147 | continue; |
| 1148 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1149 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1150 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1151 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1152 | return windowHandle; |
| 1153 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1154 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1155 | if (addOutsideTargets && |
| 1156 | info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1157 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1158 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1159 | } |
| 1160 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1161 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1164 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1165 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1166 | // Traverse windows from front to back and gather the touched spy windows. |
| 1167 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1168 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1169 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1170 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1171 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1172 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1173 | continue; |
| 1174 | } |
| 1175 | if (!info.isSpy()) { |
| 1176 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1177 | return spyWindows; |
| 1178 | } |
| 1179 | spyWindows.push_back(windowHandle); |
| 1180 | } |
| 1181 | return spyWindows; |
| 1182 | } |
| 1183 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1184 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1185 | const char* reason; |
| 1186 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1187 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1188 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1189 | ALOGD("Dropped event because policy consumed it."); |
| 1190 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1191 | reason = "inbound event was dropped because the policy consumed it"; |
| 1192 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1193 | case DropReason::DISABLED: |
| 1194 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1195 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1196 | } |
| 1197 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1198 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1199 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1200 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1201 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1202 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1203 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1204 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1205 | "has started interacting with a different application."); |
| 1206 | reason = "inbound event was dropped because the current application is not responding " |
| 1207 | "and the user has started interacting with a different application"; |
| 1208 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1209 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1210 | ALOGI("Dropped event because it is stale."); |
| 1211 | reason = "inbound event was dropped because it is stale"; |
| 1212 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1213 | case DropReason::NO_POINTER_CAPTURE: |
| 1214 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1215 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1216 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1217 | case DropReason::NOT_DROPPED: { |
| 1218 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1219 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1220 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1223 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1224 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1225 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1226 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1227 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1228 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1229 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1230 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1231 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1232 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1233 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1234 | } else { |
| 1235 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1236 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1237 | } |
| 1238 | break; |
| 1239 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1240 | case EventEntry::Type::SENSOR: { |
| 1241 | break; |
| 1242 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1243 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1244 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1245 | break; |
| 1246 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1247 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1248 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1249 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1250 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1251 | 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] | 1252 | break; |
| 1253 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1257 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1258 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1259 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1260 | } |
| 1261 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1262 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1263 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1264 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1265 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1269 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1270 | } |
| 1271 | |
| 1272 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1273 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1274 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1275 | if (DEBUG_APP_SWITCH) { |
| 1276 | if (handled) { |
| 1277 | ALOGD("App switch has arrived."); |
| 1278 | } else { |
| 1279 | ALOGD("App switch was abandoned."); |
| 1280 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1281 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1282 | } |
| 1283 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1284 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1285 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1288 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1289 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1290 | return false; |
| 1291 | } |
| 1292 | |
| 1293 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1294 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1295 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1296 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1297 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1298 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1299 | return true; |
| 1300 | } |
| 1301 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1302 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1303 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1307 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1308 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1309 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1310 | releaseInboundEventLocked(entry); |
| 1311 | } |
| 1312 | traceInboundQueueLengthLocked(); |
| 1313 | } |
| 1314 | |
| 1315 | void InputDispatcher::releasePendingEventLocked() { |
| 1316 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1317 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1318 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1322 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1323 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1324 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1325 | if (DEBUG_DISPATCH_CYCLE) { |
| 1326 | ALOGD("Injected inbound event was dropped."); |
| 1327 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1328 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1329 | } |
| 1330 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1331 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1332 | } |
| 1333 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1337 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1338 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1342 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1343 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1344 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1345 | uint32_t policyFlags = entry->policyFlags & |
| 1346 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1347 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1348 | std::shared_ptr<KeyEntry> newEntry = |
| 1349 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1350 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1351 | entry->flags, entry->keyCode, entry->scanCode, |
| 1352 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1353 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1354 | newEntry->syntheticRepeat = true; |
| 1355 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1356 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1357 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1358 | } |
| 1359 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1360 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1361 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1362 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1363 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1364 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1365 | |
| 1366 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1367 | resetKeyRepeatLocked(); |
| 1368 | |
| 1369 | // 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] | 1370 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1371 | scoped_unlock unlock(mLock); |
| 1372 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1373 | }; |
| 1374 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1375 | return true; |
| 1376 | } |
| 1377 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1378 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1379 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1380 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1381 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1382 | entry.deviceId); |
| 1383 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1384 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1385 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1386 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1387 | resetKeyRepeatLocked(); |
| 1388 | } |
| 1389 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1390 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1391 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1392 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1393 | return true; |
| 1394 | } |
| 1395 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1396 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1397 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1398 | if (mPendingEvent != nullptr) { |
| 1399 | // Move the pending event to the front of the queue. This will give the chance |
| 1400 | // for the pending event to get dispatched to the newly focused window |
| 1401 | mInboundQueue.push_front(mPendingEvent); |
| 1402 | mPendingEvent = nullptr; |
| 1403 | } |
| 1404 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1405 | std::unique_ptr<FocusEntry> focusEntry = |
| 1406 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1407 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1408 | |
| 1409 | // This event should go to the front of the queue, but behind all other focus events |
| 1410 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1411 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1412 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1413 | [](const std::shared_ptr<EventEntry>& event) { |
| 1414 | return event->type == EventEntry::Type::FOCUS; |
| 1415 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1416 | |
| 1417 | // 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] | 1418 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1419 | } |
| 1420 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1421 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1422 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1423 | if (channel == nullptr) { |
| 1424 | return; // Window has gone away |
| 1425 | } |
| 1426 | InputTarget target; |
| 1427 | target.inputChannel = channel; |
| 1428 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1429 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1430 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1431 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1432 | std::string reason = std::string("reason=").append(entry->reason); |
| 1433 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1434 | dispatchEventLocked(currentTime, entry, {target}); |
| 1435 | } |
| 1436 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1437 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1438 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1439 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1440 | dropReason = DropReason::NOT_DROPPED; |
| 1441 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1442 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1443 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1444 | |
| 1445 | if (entry->pointerCaptureRequest.enable) { |
| 1446 | // Enable Pointer Capture. |
| 1447 | if (haveWindowWithPointerCapture && |
| 1448 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
| 1449 | LOG_ALWAYS_FATAL("This request to enable Pointer Capture has already been dispatched " |
| 1450 | "to the window."); |
| 1451 | } |
| 1452 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1453 | // This can happen if a window requests capture and immediately releases capture. |
| 1454 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1455 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1456 | return; |
| 1457 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1458 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1459 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1460 | return; |
| 1461 | } |
| 1462 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1463 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1464 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1465 | mWindowTokenWithPointerCapture = token; |
| 1466 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1467 | // Disable Pointer Capture. |
| 1468 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1469 | // for two reasons: |
| 1470 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1471 | // to disable capture with the same sequence number: one generated by |
| 1472 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1473 | // Capture being disabled in InputReader. |
| 1474 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1475 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1476 | // in InputReader. |
| 1477 | if (!haveWindowWithPointerCapture) { |
| 1478 | // Pointer capture was already forcefully disabled because of focus change. |
| 1479 | dropReason = DropReason::NOT_DROPPED; |
| 1480 | return; |
| 1481 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1482 | token = mWindowTokenWithPointerCapture; |
| 1483 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1484 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1485 | setPointerCaptureLocked(false); |
| 1486 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | auto channel = getInputChannelLocked(token); |
| 1490 | if (channel == nullptr) { |
| 1491 | // Window has gone away, clean up Pointer Capture state. |
| 1492 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1493 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1494 | setPointerCaptureLocked(false); |
| 1495 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1496 | return; |
| 1497 | } |
| 1498 | InputTarget target; |
| 1499 | target.inputChannel = channel; |
| 1500 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1501 | entry->dispatchInProgress = true; |
| 1502 | dispatchEventLocked(currentTime, entry, {target}); |
| 1503 | |
| 1504 | dropReason = DropReason::NOT_DROPPED; |
| 1505 | } |
| 1506 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1507 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1508 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1509 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1510 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1511 | if (windowHandles.empty()) { |
| 1512 | return; |
| 1513 | } |
| 1514 | const std::vector<InputTarget> inputTargets = |
| 1515 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1516 | if (inputTargets.empty()) { |
| 1517 | return; |
| 1518 | } |
| 1519 | entry->dispatchInProgress = true; |
| 1520 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1521 | } |
| 1522 | |
| 1523 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1524 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1525 | std::vector<InputTarget> inputTargets; |
| 1526 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1527 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1528 | const sp<IBinder>& token = handle->getToken(); |
| 1529 | if (token == nullptr) { |
| 1530 | continue; |
| 1531 | } |
| 1532 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1533 | if (channel == nullptr) { |
| 1534 | continue; // Window has gone away |
| 1535 | } |
| 1536 | InputTarget target; |
| 1537 | target.inputChannel = channel; |
| 1538 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1539 | inputTargets.push_back(target); |
| 1540 | } |
| 1541 | return inputTargets; |
| 1542 | } |
| 1543 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1544 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1545 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1546 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1547 | if (!entry->dispatchInProgress) { |
| 1548 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1549 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1550 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1551 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1552 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1553 | // We have seen two identical key downs in a row which indicates that the device |
| 1554 | // driver is automatically generating key repeats itself. We take note of the |
| 1555 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1556 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1557 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1558 | // Make sure we don't get key down from a different device. If a different |
| 1559 | // device Id has same key pressed down, the new device Id will replace the |
| 1560 | // current one to hold the key repeat with repeat count reset. |
| 1561 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1562 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1563 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1564 | resetKeyRepeatLocked(); |
| 1565 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1566 | } else { |
| 1567 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1568 | resetKeyRepeatLocked(); |
| 1569 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1570 | } |
| 1571 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1572 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1573 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1574 | // 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] | 1575 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1576 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1577 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1578 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1579 | resetKeyRepeatLocked(); |
| 1580 | } |
| 1581 | |
| 1582 | if (entry->repeatCount == 1) { |
| 1583 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1584 | } else { |
| 1585 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1586 | } |
| 1587 | |
| 1588 | entry->dispatchInProgress = true; |
| 1589 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1590 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | // Handle case where the policy asked us to try again later last time. |
| 1594 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1595 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1596 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1597 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1598 | } |
| 1599 | return false; // wait until next wakeup |
| 1600 | } |
| 1601 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1602 | entry->interceptKeyWakeupTime = 0; |
| 1603 | } |
| 1604 | |
| 1605 | // Give the policy a chance to intercept the key. |
| 1606 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1607 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1608 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1609 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1610 | |
| 1611 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1612 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1613 | }; |
| 1614 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1615 | return false; // wait for the command to run |
| 1616 | } else { |
| 1617 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1618 | } |
| 1619 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1620 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1621 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1626 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1627 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1628 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1629 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1630 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1631 | return true; |
| 1632 | } |
| 1633 | |
| 1634 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1635 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1636 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1637 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1638 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1639 | return false; |
| 1640 | } |
| 1641 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1642 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1643 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1644 | return true; |
| 1645 | } |
| 1646 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1647 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1648 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1649 | |
| 1650 | // Dispatch the key. |
| 1651 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1652 | return true; |
| 1653 | } |
| 1654 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1655 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1656 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1657 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1658 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1659 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1660 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1661 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1662 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1663 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1666 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1667 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1668 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1669 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1670 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1671 | "source=0x%x, sensorType=%s", |
| 1672 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1673 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1674 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1675 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1676 | scoped_unlock unlock(mLock); |
| 1677 | |
| 1678 | if (entry->accuracyChanged) { |
| 1679 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1680 | } |
| 1681 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1682 | entry->hwTimestamp, entry->values); |
| 1683 | }; |
| 1684 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1688 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1689 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1690 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1691 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1692 | { // acquire lock |
| 1693 | std::scoped_lock _l(mLock); |
| 1694 | |
| 1695 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1696 | std::shared_ptr<EventEntry> entry = *it; |
| 1697 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1698 | it = mInboundQueue.erase(it); |
| 1699 | releaseInboundEventLocked(entry); |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 | return true; |
| 1704 | } |
| 1705 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1706 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1707 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1708 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1709 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1710 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1711 | entry->dispatchInProgress = true; |
| 1712 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1713 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1717 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1718 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1719 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1720 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1721 | return true; |
| 1722 | } |
| 1723 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1724 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1725 | |
| 1726 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1727 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1728 | |
| 1729 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1730 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1731 | if (isPointerEvent) { |
| 1732 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1733 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1734 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1735 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1736 | } else { |
| 1737 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1738 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1739 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1740 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1741 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1742 | return false; |
| 1743 | } |
| 1744 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1745 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 1746 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1747 | return true; |
| 1748 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1749 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1750 | CancelationOptions::Mode mode(isPointerEvent |
| 1751 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1752 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1753 | CancelationOptions options(mode, "input event injection failed"); |
| 1754 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1755 | return true; |
| 1756 | } |
| 1757 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1758 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1759 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1760 | |
| 1761 | // Dispatch the motion. |
| 1762 | if (conflictingPointerActions) { |
| 1763 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1764 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1765 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1766 | } |
| 1767 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1768 | return true; |
| 1769 | } |
| 1770 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1771 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1772 | bool isExiting, const MotionEntry& motionEntry) { |
| 1773 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1774 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1775 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1776 | PointerCoords pointerCoords; |
| 1777 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1778 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1779 | |
| 1780 | std::unique_ptr<DragEntry> dragEntry = |
| 1781 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1782 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1783 | pointerCoords.getY()); |
| 1784 | |
| 1785 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1786 | } |
| 1787 | |
| 1788 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1789 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1790 | if (channel == nullptr) { |
| 1791 | return; // Window has gone away |
| 1792 | } |
| 1793 | InputTarget target; |
| 1794 | target.inputChannel = channel; |
| 1795 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1796 | entry->dispatchInProgress = true; |
| 1797 | dispatchEventLocked(currentTime, entry, {target}); |
| 1798 | } |
| 1799 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1800 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1801 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1802 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1803 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1804 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1805 | "metaState=0x%x, buttonState=0x%x," |
| 1806 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1807 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1808 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1809 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1810 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1811 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1812 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1813 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1814 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1815 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1816 | "orientation=%f", |
| 1817 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1818 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1819 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1820 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1821 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1822 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1823 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1824 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1825 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1826 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1827 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1828 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1829 | } |
| 1830 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1831 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1832 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1833 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1834 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1835 | if (DEBUG_DISPATCH_CYCLE) { |
| 1836 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1837 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1838 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1839 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1840 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1841 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1842 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1843 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1844 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1845 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1846 | sp<Connection> connection = |
| 1847 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1848 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1849 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1850 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1851 | if (DEBUG_FOCUS) { |
| 1852 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1853 | "is no longer registered with the input dispatcher.", |
| 1854 | inputTarget.inputChannel->getName().c_str()); |
| 1855 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1856 | } |
| 1857 | } |
| 1858 | } |
| 1859 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1860 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1861 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1862 | // If the policy decides to close the app, we will get a channel removal event via |
| 1863 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1864 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1865 | // pile up. |
| 1866 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1867 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1868 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1869 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1870 | "application not responding"); |
| 1871 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1875 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1876 | if (DEBUG_FOCUS) { |
| 1877 | ALOGD("Resetting ANR timeouts."); |
| 1878 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1879 | |
| 1880 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1881 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1882 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1883 | } |
| 1884 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1885 | /** |
| 1886 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1887 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1888 | * Focused display is the display that the user most recently interacted with. |
| 1889 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1890 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1891 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1892 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1893 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1894 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1895 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1896 | break; |
| 1897 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1898 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1899 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1900 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1901 | break; |
| 1902 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1903 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1904 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1905 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1906 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1907 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1908 | case EventEntry::Type::SENSOR: |
| 1909 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1910 | 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] | 1911 | return ADISPLAY_ID_NONE; |
| 1912 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1913 | } |
| 1914 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1915 | } |
| 1916 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1917 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1918 | const char* focusedWindowName) { |
| 1919 | if (mAnrTracker.empty()) { |
| 1920 | // already processed all events that we waited for |
| 1921 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1922 | return false; |
| 1923 | } |
| 1924 | |
| 1925 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1926 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1927 | // 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] | 1928 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1929 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1930 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1931 | return true; |
| 1932 | } |
| 1933 | |
| 1934 | // We still have pending events, and already started the timer |
| 1935 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1936 | return true; // Still waiting |
| 1937 | } |
| 1938 | |
| 1939 | // Waited too long, and some connection still hasn't processed all motions |
| 1940 | // Just send the key to the focused window |
| 1941 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1942 | focusedWindowName); |
| 1943 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1944 | return false; |
| 1945 | } |
| 1946 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1947 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1948 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1949 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1950 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1951 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1952 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1953 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1954 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1955 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1956 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1957 | // If there is no currently focused window and no focused application |
| 1958 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1959 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1960 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1961 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1962 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1963 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1964 | } |
| 1965 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1966 | // Drop key events if requested by input feature |
| 1967 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1968 | return InputEventInjectionResult::FAILED; |
| 1969 | } |
| 1970 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1971 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1972 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1973 | // start interacting with another application via touch (app switch). This code can be removed |
| 1974 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1975 | // an app is expected to have a focused window. |
| 1976 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1977 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1978 | // 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] | 1979 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1980 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1981 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1982 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1983 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1984 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1985 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1986 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1987 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1988 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1989 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1990 | // Already raised ANR. Drop the event |
| 1991 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1992 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1993 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1994 | } else { |
| 1995 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1996 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | // we have a valid, non-null focused window |
| 2001 | resetNoFocusedWindowTimeoutLocked(); |
| 2002 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 2003 | // Verify targeted injection. |
| 2004 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2005 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
| 2006 | return InputEventInjectionResult::TARGET_MISMATCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2007 | } |
| 2008 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2009 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2010 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2011 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2012 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | // If the event is a key event, then we must wait for all previous events to |
| 2016 | // complete before delivering it because previous events may have the |
| 2017 | // side-effect of transferring focus to a different window and we want to |
| 2018 | // ensure that the following keys are sent to the new window. |
| 2019 | // |
| 2020 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2021 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2022 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2023 | // often anticipate pending UI changes when typing on a keyboard. |
| 2024 | // To obtain this behavior, we must serialize key events with respect to all |
| 2025 | // prior input events. |
| 2026 | if (entry.type == EventEntry::Type::KEY) { |
| 2027 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 2028 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2029 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2030 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2034 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2035 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2036 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2037 | |
| 2038 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2039 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2040 | } |
| 2041 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2042 | /** |
| 2043 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2044 | * that are currently unresponsive. |
| 2045 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2046 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2047 | const std::vector<Monitor>& monitors) const { |
| 2048 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2049 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2050 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 2051 | sp<Connection> connection = |
| 2052 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2053 | if (connection == nullptr) { |
| 2054 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2055 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2056 | return false; |
| 2057 | } |
| 2058 | if (!connection->responsive) { |
| 2059 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2060 | connection->inputChannel->getName().c_str()); |
| 2061 | return false; |
| 2062 | } |
| 2063 | return true; |
| 2064 | }); |
| 2065 | return responsiveMonitors; |
| 2066 | } |
| 2067 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2068 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 2069 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 2070 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2071 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2072 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2073 | // For security reasons, we defer updating the touch state until we are sure that |
| 2074 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2075 | const int32_t displayId = entry.displayId; |
| 2076 | const int32_t action = entry.action; |
| 2077 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2078 | |
| 2079 | // 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] | 2080 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2081 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 2082 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2083 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2084 | // Copy current touch state into tempTouchState. |
| 2085 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2086 | // 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] | 2087 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2088 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2089 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2090 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2091 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2092 | } |
| 2093 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2094 | bool isSplit = tempTouchState.split; |
| 2095 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 2096 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 2097 | tempTouchState.displayId != displayId); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2098 | |
| 2099 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2100 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2101 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2102 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2103 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2104 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2105 | bool wrongDevice = false; |
| 2106 | if (newGesture) { |
| 2107 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2108 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2109 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2110 | "in display %" PRId32, |
| 2111 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2112 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2113 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2114 | switchedDevice = false; |
| 2115 | wrongDevice = true; |
| 2116 | goto Failed; |
| 2117 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2118 | tempTouchState.reset(); |
| 2119 | tempTouchState.down = down; |
| 2120 | tempTouchState.deviceId = entry.deviceId; |
| 2121 | tempTouchState.source = entry.source; |
| 2122 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2123 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2124 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2125 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2126 | "in display %" PRId32, |
| 2127 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2128 | // TODO: test multiple simultaneous input streams. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 2129 | injectionResult = InputEventInjectionResult::FAILED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2130 | switchedDevice = false; |
| 2131 | wrongDevice = true; |
| 2132 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2136 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2137 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2138 | int32_t x; |
| 2139 | int32_t y; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2140 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2141 | // Always dispatch mouse events to cursor position. |
| 2142 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2143 | x = int32_t(entry.xCursorPosition); |
| 2144 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2145 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2146 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2147 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2148 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2149 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2150 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2151 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2152 | isStylus, isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2153 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2154 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2155 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2156 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2157 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2158 | // 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] | 2159 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 2162 | // Verify targeted injection. |
| 2163 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2164 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
| 2165 | injectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
| 2166 | newTouchedWindowHandle = nullptr; |
| 2167 | goto Failed; |
| 2168 | } |
| 2169 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2170 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2171 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2172 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2173 | // New window supports splitting, but we should never split mouse events. |
| 2174 | isSplit = !isFromMouse; |
| 2175 | } else if (isSplit) { |
| 2176 | // New window does not support splitting but we have already split events. |
| 2177 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2178 | newTouchedWindowHandle = nullptr; |
| 2179 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2180 | } else { |
| 2181 | // 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] | 2182 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2183 | // should never be split. |
| 2184 | tempTouchState.split = isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2187 | // Update hover state. |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2188 | if (newTouchedWindowHandle != nullptr) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2189 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2190 | newHoverWindowHandle = nullptr; |
| 2191 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2192 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2193 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2194 | } |
| 2195 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2196 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2197 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2198 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2199 | // Process the foreground window first so that it is the first to receive the event. |
| 2200 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2201 | } |
| 2202 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2203 | if (newTouchedWindows.empty()) { |
| 2204 | ALOGI("Dropping event because there is no touchable window at (%d, %d) on display %d.", |
| 2205 | x, y, displayId); |
| 2206 | injectionResult = InputEventInjectionResult::FAILED; |
| 2207 | goto Failed; |
| 2208 | } |
| 2209 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2210 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
| 2211 | const WindowInfo& info = *windowHandle->getInfo(); |
| 2212 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 2213 | // Skip spy window targets that are not valid for targeted injection. |
| 2214 | if (const auto err = verifyTargetedInjection(windowHandle, entry); err) { |
| 2215 | continue; |
| 2216 | } |
| 2217 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2218 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2219 | ALOGI("Not sending touch event to %s because it is paused", |
| 2220 | windowHandle->getName().c_str()); |
| 2221 | continue; |
| 2222 | } |
| 2223 | |
| 2224 | // Ensure the window has a connection and the connection is responsive |
| 2225 | const bool isResponsive = hasResponsiveConnectionLocked(*windowHandle); |
| 2226 | if (!isResponsive) { |
| 2227 | ALOGW("Not sending touch gesture to %s because it is not responsive", |
| 2228 | windowHandle->getName().c_str()); |
| 2229 | continue; |
| 2230 | } |
| 2231 | |
| 2232 | // Drop events that can't be trusted due to occlusion |
| 2233 | if (mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2234 | TouchOcclusionInfo occlusionInfo = |
| 2235 | computeTouchOcclusionInfoLocked(windowHandle, x, y); |
| 2236 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 2237 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2238 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2239 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2240 | ALOGD("%s", log.c_str()); |
| 2241 | } |
| 2242 | } |
| 2243 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
| 2244 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2245 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2246 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2247 | continue; |
| 2248 | } |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | // Drop touch events if requested by input feature |
| 2253 | if (shouldDropInput(entry, windowHandle)) { |
| 2254 | continue; |
| 2255 | } |
| 2256 | |
| 2257 | // Set target flags. |
| 2258 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2259 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2260 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2261 | // 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] | 2262 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2263 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2264 | |
| 2265 | if (isSplit) { |
| 2266 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2267 | } |
| 2268 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
| 2269 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2270 | } else if (isWindowObscuredLocked(windowHandle)) { |
| 2271 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2272 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2273 | |
| 2274 | // Update the temporary touch state. |
| 2275 | BitSet32 pointerIds; |
| 2276 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2277 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2278 | pointerIds.markBit(pointerId); |
| 2279 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2280 | |
| 2281 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2282 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2283 | } else { |
| 2284 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2285 | |
| 2286 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2287 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2288 | if (DEBUG_FOCUS) { |
| 2289 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2290 | "dropped the pointer down event in display %" PRId32, |
| 2291 | displayId); |
| 2292 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2293 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2294 | goto Failed; |
| 2295 | } |
| 2296 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2297 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2298 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2299 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2300 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2301 | tempTouchState.isSlippery()) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2302 | const int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2303 | 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] | 2304 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2305 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2306 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2307 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2308 | newTouchedWindowHandle = |
| 2309 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2310 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 2311 | // Verify targeted injection. |
| 2312 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2313 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
| 2314 | injectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
| 2315 | newTouchedWindowHandle = nullptr; |
| 2316 | goto Failed; |
| 2317 | } |
| 2318 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2319 | // Drop touch events if requested by input feature |
| 2320 | if (newTouchedWindowHandle != nullptr && |
| 2321 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2322 | newTouchedWindowHandle = nullptr; |
| 2323 | } |
| 2324 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2325 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2326 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2327 | if (DEBUG_FOCUS) { |
| 2328 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2329 | oldTouchedWindowHandle->getName().c_str(), |
| 2330 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2331 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2332 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2333 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2334 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2335 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2336 | |
| 2337 | // Make a slippery entrance into the new window. |
| 2338 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2339 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2340 | } |
| 2341 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2342 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
| 2343 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
| 2344 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2345 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2346 | if (isSplit) { |
| 2347 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2348 | } |
| 2349 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2350 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2351 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2352 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | BitSet32 pointerIds; |
| 2356 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2357 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2358 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2359 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2360 | } |
| 2361 | } |
| 2362 | } |
| 2363 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2364 | // Update dispatching for hover enter and exit. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2365 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2366 | // Let the previous window know that the hover sequence is over, unless we already did |
| 2367 | // it when dispatching it as is to newTouchedWindowHandle. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2368 | if (mLastHoverWindowHandle != nullptr && |
| 2369 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2370 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2371 | if (DEBUG_HOVER) { |
| 2372 | ALOGD("Sending hover exit event to window %s.", |
| 2373 | mLastHoverWindowHandle->getName().c_str()); |
| 2374 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2375 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2376 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2377 | } |
| 2378 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2379 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2380 | // when dispatching it as is to newTouchedWindowHandle. |
| 2381 | if (newHoverWindowHandle != nullptr && |
| 2382 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2383 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2384 | if (DEBUG_HOVER) { |
| 2385 | ALOGD("Sending hover enter event to window %s.", |
| 2386 | newHoverWindowHandle->getName().c_str()); |
| 2387 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2388 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2389 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2390 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2391 | } |
| 2392 | } |
| 2393 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2394 | // Ensure that we have at least one foreground window or at least one window that cannot be a |
| 2395 | // foreground target. If we only have windows that are not receiving foreground touches (e.g. we |
| 2396 | // only have windows getting ACTION_OUTSIDE), then drop the event, because there is no window |
| 2397 | // that is actually receiving the entire gesture. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2398 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2399 | [](const TouchedWindow& touchedWindow) { |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2400 | return !canReceiveForegroundTouches( |
| 2401 | *touchedWindow.windowHandle->getInfo()) || |
| 2402 | (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2403 | })) { |
Siarhei Vishniakou | 1fb1891 | 2022-03-08 10:31:39 -0800 | [diff] [blame] | 2404 | ALOGI("Dropping event because there is no touched window on display %d to receive it: %s", |
| 2405 | displayId, entry.getDescription().c_str()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2406 | injectionResult = InputEventInjectionResult::FAILED; |
| 2407 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2408 | } |
| 2409 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 2410 | // Ensure that all touched windows are valid for injection. |
| 2411 | if (entry.injectionState != nullptr) { |
| 2412 | std::string errs; |
| 2413 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
| 2414 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2415 | // Allow ACTION_OUTSIDE events generated by targeted injection to be |
| 2416 | // dispatched to any uid, since the coords will be zeroed out later. |
| 2417 | continue; |
| 2418 | } |
| 2419 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2420 | if (err) errs += "\n - " + *err; |
| 2421 | } |
| 2422 | if (!errs.empty()) { |
| 2423 | ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " |
| 2424 | "%d:%s", |
| 2425 | *entry.injectionState->targetUid, errs.c_str()); |
| 2426 | injectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2427 | goto Failed; |
| 2428 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2429 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2430 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2431 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2432 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2433 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2434 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2435 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2436 | if (foregroundWindowHandle) { |
| 2437 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2438 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2439 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2440 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2441 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2442 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2443 | InputTarget::FLAG_ZERO_COORDS, |
| 2444 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2445 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2446 | } |
| 2447 | } |
| 2448 | } |
| 2449 | } |
| 2450 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2451 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2452 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2453 | // of the touch gesture. |
| 2454 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2455 | // engine only supports touch events. We would need to add a mechanism similar |
| 2456 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2457 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2458 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2459 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2460 | if (foregroundWindowHandle && |
| 2461 | foregroundWindowHandle->getInfo()->inputConfig.test( |
| 2462 | WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2463 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2464 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2465 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2466 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2467 | if (info->displayId == displayId && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2468 | windowHandle->getInfo()->inputConfig.test( |
| 2469 | WindowInfo::InputConfig::IS_WALLPAPER)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2470 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2471 | .addOrUpdateWindow(windowHandle, |
| 2472 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2473 | InputTarget:: |
| 2474 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2475 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2476 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2477 | } |
| 2478 | } |
| 2479 | } |
| 2480 | } |
| 2481 | |
| 2482 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2483 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2484 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2485 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2486 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2487 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | // Drop the outside or hover touch windows since we will not care about them |
| 2491 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2492 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2493 | |
| 2494 | Failed: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2495 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2496 | if (!wrongDevice) { |
| 2497 | if (switchedDevice) { |
| 2498 | if (DEBUG_FOCUS) { |
| 2499 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2500 | } |
| 2501 | *outConflictingPointerActions = true; |
| 2502 | } |
| 2503 | |
| 2504 | if (isHoverAction) { |
| 2505 | // Started hovering, therefore no longer down. |
| 2506 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2507 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2508 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2509 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2510 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2511 | *outConflictingPointerActions = true; |
| 2512 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2513 | tempTouchState.reset(); |
| 2514 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2515 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2516 | tempTouchState.deviceId = entry.deviceId; |
| 2517 | tempTouchState.source = entry.source; |
| 2518 | tempTouchState.displayId = displayId; |
| 2519 | } |
| 2520 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2521 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2522 | // All pointers up or canceled. |
| 2523 | tempTouchState.reset(); |
| 2524 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2525 | // First pointer went down. |
| 2526 | if (oldState && oldState->down) { |
| 2527 | if (DEBUG_FOCUS) { |
| 2528 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2529 | } |
| 2530 | *outConflictingPointerActions = true; |
| 2531 | } |
| 2532 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2533 | // One pointer went up. |
| 2534 | if (isSplit) { |
| 2535 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2536 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2537 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2538 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2539 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2540 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2541 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2542 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2543 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2544 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2545 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2546 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2547 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2548 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2549 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2550 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2551 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2552 | // Save changes unless the action was scroll in which case the temporary touch |
| 2553 | // state was only valid for this one action. |
| 2554 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2555 | if (tempTouchState.displayId >= 0) { |
| 2556 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2557 | } else { |
| 2558 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2559 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2560 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2561 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2562 | // Update hover state. |
| 2563 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2564 | } |
| 2565 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2566 | return injectionResult; |
| 2567 | } |
| 2568 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2569 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2570 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2571 | // have an explicit reason to support it. |
| 2572 | constexpr bool isStylus = false; |
| 2573 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2574 | const sp<WindowInfoHandle> dropWindow = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2575 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2576 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2577 | if (dropWindow) { |
| 2578 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2579 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2580 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2581 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2582 | } |
| 2583 | mDragState.reset(); |
| 2584 | } |
| 2585 | |
| 2586 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2587 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2588 | return; |
| 2589 | } |
| 2590 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2591 | if (!mDragState->isStartDrag) { |
| 2592 | mDragState->isStartDrag = true; |
| 2593 | mDragState->isStylusButtonDownAtStart = |
| 2594 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2595 | } |
| 2596 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2597 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2598 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2599 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2600 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2601 | // Handle the special case : stylus button no longer pressed. |
| 2602 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2603 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2604 | finishDragAndDrop(entry.displayId, x, y); |
| 2605 | return; |
| 2606 | } |
| 2607 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2608 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until |
| 2609 | // we have an explicit reason to support it. |
| 2610 | constexpr bool isStylus = false; |
| 2611 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2612 | const sp<WindowInfoHandle> hoverWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2613 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2614 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2615 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2616 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2617 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2618 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2619 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2620 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2621 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2622 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2623 | } |
| 2624 | // enqueue drag location if needed. |
| 2625 | if (hoverWindowHandle != nullptr) { |
| 2626 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2627 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2628 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2629 | finishDragAndDrop(entry.displayId, x, y); |
| 2630 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2631 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2632 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2633 | } |
| 2634 | } |
| 2635 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2636 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2637 | int32_t targetFlags, BitSet32 pointerIds, |
| 2638 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2639 | std::vector<InputTarget>::iterator it = |
| 2640 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2641 | [&windowHandle](const InputTarget& inputTarget) { |
| 2642 | return inputTarget.inputChannel->getConnectionToken() == |
| 2643 | windowHandle->getToken(); |
| 2644 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2645 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2646 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2647 | |
| 2648 | if (it == inputTargets.end()) { |
| 2649 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2650 | std::shared_ptr<InputChannel> inputChannel = |
| 2651 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2652 | if (inputChannel == nullptr) { |
| 2653 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2654 | return; |
| 2655 | } |
| 2656 | inputTarget.inputChannel = inputChannel; |
| 2657 | inputTarget.flags = targetFlags; |
| 2658 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2659 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2660 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2661 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2662 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2663 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2664 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2665 | inputTargets.push_back(inputTarget); |
| 2666 | it = inputTargets.end() - 1; |
| 2667 | } |
| 2668 | |
| 2669 | ALOG_ASSERT(it->flags == targetFlags); |
| 2670 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2671 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2672 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2673 | } |
| 2674 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2675 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2676 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2677 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2678 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2679 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2680 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2681 | InputTarget target; |
| 2682 | target.inputChannel = monitor.inputChannel; |
| 2683 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2684 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2685 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2686 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2687 | target.setDefaultPointerTransform(target.displayTransform); |
| 2688 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2689 | } |
| 2690 | } |
| 2691 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2692 | /** |
| 2693 | * Indicate whether one window handle should be considered as obscuring |
| 2694 | * another window handle. We only check a few preconditions. Actually |
| 2695 | * checking the bounds is left to the caller. |
| 2696 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2697 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2698 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2699 | // Compare by token so cloned layers aren't counted |
| 2700 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2701 | return false; |
| 2702 | } |
| 2703 | auto info = windowHandle->getInfo(); |
| 2704 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2705 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2706 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2707 | } else if (otherInfo->alpha == 0 && |
| 2708 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2709 | // Those act as if they were invisible, so we don't need to flag them. |
| 2710 | // We do want to potentially flag touchable windows even if they have 0 |
| 2711 | // opacity, since they can consume touches and alter the effects of the |
| 2712 | // user interaction (eg. apps that rely on |
| 2713 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2714 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2715 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2716 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2717 | // If ownerUid is the same we don't generate occlusion events as there |
| 2718 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2719 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2720 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2721 | return false; |
| 2722 | } else if (otherInfo->displayId != info->displayId) { |
| 2723 | return false; |
| 2724 | } |
| 2725 | return true; |
| 2726 | } |
| 2727 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2728 | /** |
| 2729 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2730 | * untrusted, one should check: |
| 2731 | * |
| 2732 | * 1. If result.hasBlockingOcclusion is true. |
| 2733 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2734 | * BLOCK_UNTRUSTED. |
| 2735 | * |
| 2736 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2737 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2738 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2739 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2740 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2741 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2742 | * |
| 2743 | * If neither of those is true, then it means the touch can be allowed. |
| 2744 | */ |
| 2745 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2746 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2747 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2748 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2749 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2750 | TouchOcclusionInfo info; |
| 2751 | info.hasBlockingOcclusion = false; |
| 2752 | info.obscuringOpacity = 0; |
| 2753 | info.obscuringUid = -1; |
| 2754 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2755 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2756 | if (windowHandle == otherHandle) { |
| 2757 | break; // All future windows are below us. Exit early. |
| 2758 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2759 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2760 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2761 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2762 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2763 | info.debugInfo.push_back( |
| 2764 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2765 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2766 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2767 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2768 | // window's touch occlusion mode |
| 2769 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2770 | info.hasBlockingOcclusion = true; |
| 2771 | info.obscuringUid = otherInfo->ownerUid; |
| 2772 | info.obscuringPackage = otherInfo->packageName; |
| 2773 | break; |
| 2774 | } |
| 2775 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2776 | uint32_t uid = otherInfo->ownerUid; |
| 2777 | float opacity = |
| 2778 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2779 | // Given windows A and B: |
| 2780 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2781 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2782 | opacityByUid[uid] = opacity; |
| 2783 | if (opacity > info.obscuringOpacity) { |
| 2784 | info.obscuringOpacity = opacity; |
| 2785 | info.obscuringUid = uid; |
| 2786 | info.obscuringPackage = otherInfo->packageName; |
| 2787 | } |
| 2788 | } |
| 2789 | } |
| 2790 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2791 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2792 | info.debugInfo.push_back( |
| 2793 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2794 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2795 | return info; |
| 2796 | } |
| 2797 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2798 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2799 | bool isTouchedWindow) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2800 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2801 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2802 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 2803 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2804 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 2805 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 2806 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 2807 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 2808 | info->name.c_str(), info->inputConfig.string().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2809 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2810 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2813 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2814 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2815 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2816 | occlusionInfo.obscuringUid); |
| 2817 | return false; |
| 2818 | } |
| 2819 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2820 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2821 | "%.2f, maximum allowed = %.2f)", |
| 2822 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2823 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2824 | return false; |
| 2825 | } |
| 2826 | return true; |
| 2827 | } |
| 2828 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2829 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2830 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2831 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2832 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2833 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2834 | if (windowHandle == otherHandle) { |
| 2835 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2836 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2837 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2838 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2839 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2840 | return true; |
| 2841 | } |
| 2842 | } |
| 2843 | return false; |
| 2844 | } |
| 2845 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2846 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2847 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2848 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2849 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2850 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2851 | if (windowHandle == otherHandle) { |
| 2852 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2853 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2854 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2855 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2856 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2857 | return true; |
| 2858 | } |
| 2859 | } |
| 2860 | return false; |
| 2861 | } |
| 2862 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2863 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2864 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2865 | if (applicationHandle != nullptr) { |
| 2866 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2867 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2868 | } else { |
| 2869 | return applicationHandle->getName(); |
| 2870 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2871 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2872 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2873 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2874 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2875 | } |
| 2876 | } |
| 2877 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2878 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2879 | if (!isUserActivityEvent(eventEntry)) { |
| 2880 | // 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] | 2881 | return; |
| 2882 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2883 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2884 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2885 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2886 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2887 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2888 | if (DEBUG_DISPATCH_CYCLE) { |
| 2889 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2890 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2891 | return; |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2896 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2897 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2898 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2899 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2900 | return; |
| 2901 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2902 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2903 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2904 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2905 | } |
| 2906 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2907 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2908 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2909 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2910 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2911 | return; |
| 2912 | } |
| 2913 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2914 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2915 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2916 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2917 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2918 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2919 | break; |
| 2920 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2921 | } |
| 2922 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2923 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2924 | REQUIRES(mLock) { |
| 2925 | scoped_unlock unlock(mLock); |
| 2926 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2927 | }; |
| 2928 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2932 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2933 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2934 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2935 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2936 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2937 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2938 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2939 | ATRACE_NAME(message.c_str()); |
| 2940 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2941 | if (DEBUG_DISPATCH_CYCLE) { |
| 2942 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2943 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2944 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2945 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2946 | inputTarget.getPointerInfoString().c_str()); |
| 2947 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2948 | |
| 2949 | // Skip this event if the connection status is not normal. |
| 2950 | // 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] | 2951 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2952 | if (DEBUG_DISPATCH_CYCLE) { |
| 2953 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2954 | connection->getInputChannelName().c_str(), |
| 2955 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2956 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2957 | return; |
| 2958 | } |
| 2959 | |
| 2960 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2961 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2962 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2963 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2964 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2965 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2966 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2967 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2968 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2969 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2970 | if (!splitMotionEntry) { |
| 2971 | return; // split event was dropped |
| 2972 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2973 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2974 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2975 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2976 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2977 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2978 | if (DEBUG_FOCUS) { |
| 2979 | ALOGD("channel '%s' ~ Split motion event.", |
| 2980 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2981 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2982 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2983 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2984 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2985 | return; |
| 2986 | } |
| 2987 | } |
| 2988 | |
| 2989 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2990 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2991 | } |
| 2992 | |
| 2993 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2994 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2995 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2996 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2997 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2998 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2999 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3000 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3001 | ATRACE_NAME(message.c_str()); |
| 3002 | } |
| 3003 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3004 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3005 | |
| 3006 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3007 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3008 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3009 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3010 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3011 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3012 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3013 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3014 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3015 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3016 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3017 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3018 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3019 | |
| 3020 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3021 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3022 | startDispatchCycleLocked(currentTime, connection); |
| 3023 | } |
| 3024 | } |
| 3025 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3026 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3027 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3028 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3029 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3030 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3031 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 3032 | connection->getInputChannelName().c_str(), |
| 3033 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3034 | ATRACE_NAME(message.c_str()); |
| 3035 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3036 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3037 | if (!(inputTargetFlags & dispatchMode)) { |
| 3038 | return; |
| 3039 | } |
| 3040 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 3041 | |
| 3042 | // This is a new event. |
| 3043 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3044 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3045 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3046 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3047 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3048 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3049 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3050 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3051 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3052 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3053 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3054 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3055 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3056 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3057 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3058 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3059 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3060 | if (DEBUG_DISPATCH_CYCLE) { |
| 3061 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3062 | "event", |
| 3063 | connection->getInputChannelName().c_str()); |
| 3064 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3065 | return; // skip the inconsistent event |
| 3066 | } |
| 3067 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3068 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3069 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3070 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3071 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3072 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3073 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3074 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3075 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3076 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3077 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3078 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 3079 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 3080 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3081 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 3082 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3083 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 3084 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 3085 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 3086 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3087 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3088 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3089 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3090 | } |
| 3091 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3092 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3093 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3094 | if (DEBUG_DISPATCH_CYCLE) { |
| 3095 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3096 | "enter event", |
| 3097 | connection->getInputChannelName().c_str()); |
| 3098 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3099 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3100 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3101 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3102 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3103 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3104 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3105 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3106 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3107 | } |
| 3108 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3109 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3110 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3111 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3112 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3113 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3114 | if (DEBUG_DISPATCH_CYCLE) { |
| 3115 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3116 | "event", |
| 3117 | connection->getInputChannelName().c_str()); |
| 3118 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3119 | return; // skip the inconsistent event |
| 3120 | } |
| 3121 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3122 | dispatchEntry->resolvedEventId = |
| 3123 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3124 | ? mIdGenerator.nextId() |
| 3125 | : motionEntry.id; |
| 3126 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3127 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3128 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3129 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3130 | ATRACE_NAME(message.c_str()); |
| 3131 | } |
| 3132 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3133 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3134 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3135 | // Skip reporting pointer down outside focus to the policy. |
| 3136 | break; |
| 3137 | } |
| 3138 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3139 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3140 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3141 | |
| 3142 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3143 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3144 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3145 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3146 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3147 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3148 | break; |
| 3149 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3150 | case EventEntry::Type::SENSOR: { |
| 3151 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3152 | break; |
| 3153 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3154 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3155 | case EventEntry::Type::DEVICE_RESET: { |
| 3156 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3157 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3158 | break; |
| 3159 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | // Remember that we are waiting for this dispatch to complete. |
| 3163 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3164 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3168 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3169 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3170 | } |
| 3171 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3172 | /** |
| 3173 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3174 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3175 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3176 | * We will see both launcher and wallpaper in that list. |
| 3177 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3178 | * until the set of interacted connections changes. |
| 3179 | * |
| 3180 | * The following items are skipped, to reduce the logspam: |
| 3181 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3182 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3183 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3184 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3185 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3186 | */ |
| 3187 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3188 | const std::vector<InputTarget>& targets) { |
| 3189 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3190 | if (entry.type == EventEntry::Type::KEY) { |
| 3191 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3192 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3193 | return; |
| 3194 | } |
| 3195 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3196 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3197 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3198 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3199 | return; |
| 3200 | } |
| 3201 | } else { |
| 3202 | return; // Not a key or a motion |
| 3203 | } |
| 3204 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3205 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3206 | std::vector<sp<Connection>> newConnections; |
| 3207 | for (const InputTarget& target : targets) { |
| 3208 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3209 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3210 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3211 | } |
| 3212 | |
| 3213 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3214 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3215 | if (connection == nullptr) { |
| 3216 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3217 | } |
| 3218 | newConnectionTokens.insert(std::move(token)); |
| 3219 | newConnections.emplace_back(connection); |
| 3220 | } |
| 3221 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3222 | return; // no change |
| 3223 | } |
| 3224 | mInteractionConnectionTokens = newConnectionTokens; |
| 3225 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3226 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3227 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3228 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3229 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3230 | std::string message = "Interaction with: " + targetList; |
| 3231 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3232 | message += "<none>"; |
| 3233 | } |
| 3234 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3235 | } |
| 3236 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3237 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3238 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3239 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3240 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3241 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3242 | return; |
| 3243 | } |
| 3244 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3245 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3246 | if (focusedToken == token) { |
| 3247 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3248 | return; |
| 3249 | } |
| 3250 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3251 | auto command = [this, token]() REQUIRES(mLock) { |
| 3252 | scoped_unlock unlock(mLock); |
| 3253 | mPolicy->onPointerDownOutsideFocus(token); |
| 3254 | }; |
| 3255 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3259 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3260 | if (ATRACE_ENABLED()) { |
| 3261 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3262 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3263 | ATRACE_NAME(message.c_str()); |
| 3264 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3265 | if (DEBUG_DISPATCH_CYCLE) { |
| 3266 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3267 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3268 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3269 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3270 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3271 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3272 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3273 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3274 | |
| 3275 | // Publish the event. |
| 3276 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3277 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3278 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3279 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3280 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3281 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3282 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3283 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3284 | status = connection->inputPublisher |
| 3285 | .publishKeyEvent(dispatchEntry->seq, |
| 3286 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3287 | keyEntry.source, keyEntry.displayId, |
| 3288 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3289 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3290 | keyEntry.scanCode, keyEntry.metaState, |
| 3291 | keyEntry.repeatCount, keyEntry.downTime, |
| 3292 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3293 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3294 | } |
| 3295 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3296 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3297 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3298 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3299 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3300 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3301 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3302 | // 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] | 3303 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3304 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3305 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3306 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3307 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3308 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3309 | // Don't apply window scale here since we don't want scale to affect raw |
| 3310 | // coordinates. The scale will be sent back to the client and applied |
| 3311 | // later when requesting relative coordinates. |
| 3312 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3313 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3314 | } |
| 3315 | usingCoords = scaledCoords; |
| 3316 | } |
| 3317 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3318 | // We don't want the dispatch target to know. |
| 3319 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3320 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3321 | scaledCoords[i].clear(); |
| 3322 | } |
| 3323 | usingCoords = scaledCoords; |
| 3324 | } |
| 3325 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3326 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3327 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3328 | |
| 3329 | // Publish the motion event. |
| 3330 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3331 | .publishMotionEvent(dispatchEntry->seq, |
| 3332 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3333 | motionEntry.deviceId, motionEntry.source, |
| 3334 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3335 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3336 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3337 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3338 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3339 | motionEntry.buttonState, |
| 3340 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3341 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3342 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3343 | motionEntry.xCursorPosition, |
| 3344 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3345 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3346 | motionEntry.downTime, motionEntry.eventTime, |
| 3347 | motionEntry.pointerCount, |
| 3348 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3349 | break; |
| 3350 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3351 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3352 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3353 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3354 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3355 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3356 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3357 | break; |
| 3358 | } |
| 3359 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3360 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3361 | const TouchModeEntry& touchModeEntry = |
| 3362 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3363 | status = connection->inputPublisher |
| 3364 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3365 | touchModeEntry.inTouchMode); |
| 3366 | |
| 3367 | break; |
| 3368 | } |
| 3369 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3370 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3371 | const auto& captureEntry = |
| 3372 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3373 | status = connection->inputPublisher |
| 3374 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3375 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3376 | break; |
| 3377 | } |
| 3378 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3379 | case EventEntry::Type::DRAG: { |
| 3380 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3381 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3382 | dragEntry.id, dragEntry.x, |
| 3383 | dragEntry.y, |
| 3384 | dragEntry.isExiting); |
| 3385 | break; |
| 3386 | } |
| 3387 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3388 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3389 | case EventEntry::Type::DEVICE_RESET: |
| 3390 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3391 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3392 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3393 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3394 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | // Check the result. |
| 3398 | if (status) { |
| 3399 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3400 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3401 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3402 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3403 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3404 | "event to it, status=%s(%d)", |
| 3405 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3406 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3407 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3408 | } else { |
| 3409 | // Pipe is full and we are waiting for the app to finish process some events |
| 3410 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3411 | if (DEBUG_DISPATCH_CYCLE) { |
| 3412 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3413 | "waiting for the application to catch up", |
| 3414 | connection->getInputChannelName().c_str()); |
| 3415 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3416 | } |
| 3417 | } else { |
| 3418 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3419 | "status=%s(%d)", |
| 3420 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3421 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3422 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3423 | } |
| 3424 | return; |
| 3425 | } |
| 3426 | |
| 3427 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3428 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3429 | connection->outboundQueue.end(), |
| 3430 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3431 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3432 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3433 | if (connection->responsive) { |
| 3434 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3435 | connection->inputChannel->getConnectionToken()); |
| 3436 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3437 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3438 | } |
| 3439 | } |
| 3440 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3441 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3442 | size_t size; |
| 3443 | switch (event.type) { |
| 3444 | case VerifiedInputEvent::Type::KEY: { |
| 3445 | size = sizeof(VerifiedKeyEvent); |
| 3446 | break; |
| 3447 | } |
| 3448 | case VerifiedInputEvent::Type::MOTION: { |
| 3449 | size = sizeof(VerifiedMotionEvent); |
| 3450 | break; |
| 3451 | } |
| 3452 | } |
| 3453 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3454 | return mHmacKeyManager.sign(start, size); |
| 3455 | } |
| 3456 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3457 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3458 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3459 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3460 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3461 | // Only sign events up and down events as the purely move events |
| 3462 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3463 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3464 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3465 | |
| 3466 | VerifiedMotionEvent verifiedEvent = |
| 3467 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3468 | verifiedEvent.actionMasked = actionMasked; |
| 3469 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3470 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3471 | } |
| 3472 | |
| 3473 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3474 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3475 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3476 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3477 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3478 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3479 | } |
| 3480 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3481 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3482 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3483 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3484 | if (DEBUG_DISPATCH_CYCLE) { |
| 3485 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3486 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3487 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3488 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3489 | if (connection->status == Connection::Status::BROKEN || |
| 3490 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3491 | return; |
| 3492 | } |
| 3493 | |
| 3494 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3495 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3496 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3497 | }; |
| 3498 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3499 | } |
| 3500 | |
| 3501 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3502 | const sp<Connection>& connection, |
| 3503 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3504 | if (DEBUG_DISPATCH_CYCLE) { |
| 3505 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3506 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3507 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3508 | |
| 3509 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3510 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3511 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3512 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3513 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3514 | |
| 3515 | // The connection appears to be unrecoverably broken. |
| 3516 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3517 | if (connection->status == Connection::Status::NORMAL) { |
| 3518 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3519 | |
| 3520 | if (notify) { |
| 3521 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3522 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3523 | connection->getInputChannelName().c_str()); |
| 3524 | |
| 3525 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3526 | scoped_unlock unlock(mLock); |
| 3527 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3528 | }; |
| 3529 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3530 | } |
| 3531 | } |
| 3532 | } |
| 3533 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3534 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3535 | while (!queue.empty()) { |
| 3536 | DispatchEntry* dispatchEntry = queue.front(); |
| 3537 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3538 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3539 | } |
| 3540 | } |
| 3541 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3542 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3543 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3544 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3545 | } |
| 3546 | delete dispatchEntry; |
| 3547 | } |
| 3548 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3549 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3550 | std::scoped_lock _l(mLock); |
| 3551 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3552 | if (connection == nullptr) { |
| 3553 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3554 | connectionToken.get(), events); |
| 3555 | return 0; // remove the callback |
| 3556 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3557 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3558 | bool notify; |
| 3559 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3560 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3561 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3562 | "events=0x%x", |
| 3563 | connection->getInputChannelName().c_str(), events); |
| 3564 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3565 | } |
| 3566 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3567 | nsecs_t currentTime = now(); |
| 3568 | bool gotOne = false; |
| 3569 | status_t status = OK; |
| 3570 | for (;;) { |
| 3571 | Result<InputPublisher::ConsumerResponse> result = |
| 3572 | connection->inputPublisher.receiveConsumerResponse(); |
| 3573 | if (!result.ok()) { |
| 3574 | status = result.error().code(); |
| 3575 | break; |
| 3576 | } |
| 3577 | |
| 3578 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3579 | const InputPublisher::Finished& finish = |
| 3580 | std::get<InputPublisher::Finished>(*result); |
| 3581 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3582 | finish.consumeTime); |
| 3583 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3584 | if (shouldReportMetricsForConnection(*connection)) { |
| 3585 | const InputPublisher::Timeline& timeline = |
| 3586 | std::get<InputPublisher::Timeline>(*result); |
| 3587 | mLatencyTracker |
| 3588 | .trackGraphicsLatency(timeline.inputEventId, |
| 3589 | connection->inputChannel->getConnectionToken(), |
| 3590 | std::move(timeline.graphicsTimeline)); |
| 3591 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3592 | } |
| 3593 | gotOne = true; |
| 3594 | } |
| 3595 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3596 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3597 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3598 | return 1; |
| 3599 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3600 | } |
| 3601 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3602 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3603 | if (notify) { |
| 3604 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3605 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3606 | status); |
| 3607 | } |
| 3608 | } else { |
| 3609 | // Monitor channels are never explicitly unregistered. |
| 3610 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3611 | const bool stillHaveWindowHandle = |
| 3612 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3613 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3614 | if (notify) { |
| 3615 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3616 | connection->getInputChannelName().c_str(), events); |
| 3617 | } |
| 3618 | } |
| 3619 | |
| 3620 | // Remove the channel. |
| 3621 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3622 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3623 | } |
| 3624 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3625 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3626 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3627 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3628 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3629 | } |
| 3630 | } |
| 3631 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3632 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3633 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3634 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3635 | for (const Monitor& monitor : monitors) { |
| 3636 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3637 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3638 | } |
| 3639 | } |
| 3640 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3641 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3642 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3643 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3644 | if (connection == nullptr) { |
| 3645 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3646 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3647 | |
| 3648 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3649 | } |
| 3650 | |
| 3651 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3652 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3653 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3654 | return; |
| 3655 | } |
| 3656 | |
| 3657 | nsecs_t currentTime = now(); |
| 3658 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3659 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3660 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3661 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3662 | if (cancelationEvents.empty()) { |
| 3663 | return; |
| 3664 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3665 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3666 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3667 | "with reality: %s, mode=%d.", |
| 3668 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3669 | options.mode); |
| 3670 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3671 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3672 | std::string reason = std::string("reason=").append(options.reason); |
| 3673 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3674 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3675 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3676 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3677 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3678 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3679 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3680 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3681 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3682 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3683 | } |
| 3684 | target.inputChannel = connection->inputChannel; |
| 3685 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3686 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3687 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3688 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3689 | switch (cancelationEventEntry->type) { |
| 3690 | case EventEntry::Type::KEY: { |
| 3691 | logOutboundKeyDetails("cancel - ", |
| 3692 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3693 | break; |
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 | case EventEntry::Type::MOTION: { |
| 3696 | logOutboundMotionDetails("cancel - ", |
| 3697 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3698 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3699 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3700 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3701 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3702 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3703 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3704 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3705 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3706 | break; |
| 3707 | } |
| 3708 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3709 | case EventEntry::Type::DEVICE_RESET: |
| 3710 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3711 | 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] | 3712 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3713 | break; |
| 3714 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3715 | } |
| 3716 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3717 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3718 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3719 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3720 | |
| 3721 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3722 | } |
| 3723 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3724 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3725 | const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3726 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3727 | return; |
| 3728 | } |
| 3729 | |
| 3730 | nsecs_t currentTime = now(); |
| 3731 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3732 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3733 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3734 | |
| 3735 | if (downEvents.empty()) { |
| 3736 | return; |
| 3737 | } |
| 3738 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3739 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3740 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3741 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3742 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3743 | |
| 3744 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3745 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3746 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3747 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3748 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3749 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3750 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3751 | } |
| 3752 | target.inputChannel = connection->inputChannel; |
| 3753 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3754 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3755 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3756 | switch (downEventEntry->type) { |
| 3757 | case EventEntry::Type::MOTION: { |
| 3758 | logOutboundMotionDetails("down - ", |
| 3759 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3760 | break; |
| 3761 | } |
| 3762 | |
| 3763 | case EventEntry::Type::KEY: |
| 3764 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3765 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3766 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3767 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3768 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3769 | case EventEntry::Type::SENSOR: |
| 3770 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3771 | 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] | 3772 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3773 | break; |
| 3774 | } |
| 3775 | } |
| 3776 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3777 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3778 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3779 | } |
| 3780 | |
| 3781 | startDispatchCycleLocked(currentTime, connection); |
| 3782 | } |
| 3783 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3784 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3785 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3786 | ALOG_ASSERT(pointerIds.value != 0); |
| 3787 | |
| 3788 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3789 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3790 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3791 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3792 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3793 | uint32_t splitPointerCount = 0; |
| 3794 | |
| 3795 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3796 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3797 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3798 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3799 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3800 | if (pointerIds.hasBit(pointerId)) { |
| 3801 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3802 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3803 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3804 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3805 | splitPointerCount += 1; |
| 3806 | } |
| 3807 | } |
| 3808 | |
| 3809 | if (splitPointerCount != pointerIds.count()) { |
| 3810 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3811 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3812 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3813 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3814 | // in this way. |
| 3815 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3816 | "we expected there to be %d pointers. This probably means we received " |
| 3817 | "a broken sequence of pointer ids from the input device.", |
| 3818 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3819 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3820 | } |
| 3821 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3822 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3823 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3824 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3825 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3826 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3827 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3828 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3829 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3830 | if (pointerIds.hasBit(pointerId)) { |
| 3831 | if (pointerIds.count() == 1) { |
| 3832 | // The first/last pointer went down/up. |
| 3833 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3834 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3835 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3836 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3837 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3838 | } else { |
| 3839 | // A secondary pointer went down/up. |
| 3840 | uint32_t splitPointerIndex = 0; |
| 3841 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3842 | splitPointerIndex += 1; |
| 3843 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3844 | action = maskedAction | |
| 3845 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3846 | } |
| 3847 | } else { |
| 3848 | // An unrelated pointer changed. |
| 3849 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3850 | } |
| 3851 | } |
| 3852 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3853 | int32_t newId = mIdGenerator.nextId(); |
| 3854 | if (ATRACE_ENABLED()) { |
| 3855 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3856 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3857 | originalMotionEntry.id, newId); |
| 3858 | ATRACE_NAME(message.c_str()); |
| 3859 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3860 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3861 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3862 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3863 | originalMotionEntry.displayId, |
| 3864 | originalMotionEntry.policyFlags, action, |
| 3865 | originalMotionEntry.actionButton, |
| 3866 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3867 | originalMotionEntry.buttonState, |
| 3868 | originalMotionEntry.classification, |
| 3869 | originalMotionEntry.edgeFlags, |
| 3870 | originalMotionEntry.xPrecision, |
| 3871 | originalMotionEntry.yPrecision, |
| 3872 | originalMotionEntry.xCursorPosition, |
| 3873 | originalMotionEntry.yCursorPosition, |
| 3874 | originalMotionEntry.downTime, splitPointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3875 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3876 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3877 | if (originalMotionEntry.injectionState) { |
| 3878 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3879 | splitMotionEntry->injectionState->refCount += 1; |
| 3880 | } |
| 3881 | |
| 3882 | return splitMotionEntry; |
| 3883 | } |
| 3884 | |
| 3885 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3886 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3887 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3888 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3889 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3890 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3891 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3892 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3893 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3894 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3895 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3896 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3897 | } // release lock |
| 3898 | |
| 3899 | if (needWake) { |
| 3900 | mLooper->wake(); |
| 3901 | } |
| 3902 | } |
| 3903 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3904 | /** |
| 3905 | * If one of the meta shortcuts is detected, process them here: |
| 3906 | * Meta + Backspace -> generate BACK |
| 3907 | * Meta + Enter -> generate HOME |
| 3908 | * This will potentially overwrite keyCode and metaState. |
| 3909 | */ |
| 3910 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3911 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3912 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3913 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3914 | if (keyCode == AKEYCODE_DEL) { |
| 3915 | newKeyCode = AKEYCODE_BACK; |
| 3916 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3917 | newKeyCode = AKEYCODE_HOME; |
| 3918 | } |
| 3919 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3920 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3921 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3922 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3923 | keyCode = newKeyCode; |
| 3924 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3925 | } |
| 3926 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3927 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3928 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3929 | // 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] | 3930 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3931 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3932 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3933 | if (replacementIt != mReplacedKeys.end()) { |
| 3934 | keyCode = replacementIt->second; |
| 3935 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3936 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3937 | } |
| 3938 | } |
| 3939 | } |
| 3940 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3941 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3942 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3943 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3944 | "policyFlags=0x%x, action=0x%x, " |
| 3945 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3946 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3947 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3948 | args->downTime); |
| 3949 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3950 | if (!validateKeyEvent(args->action)) { |
| 3951 | return; |
| 3952 | } |
| 3953 | |
| 3954 | uint32_t policyFlags = args->policyFlags; |
| 3955 | int32_t flags = args->flags; |
| 3956 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3957 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3958 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3959 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3960 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3961 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3962 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3963 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3964 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3965 | metaState |= AMETA_FUNCTION_ON; |
| 3966 | } |
| 3967 | |
| 3968 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3969 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3970 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3971 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3972 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3973 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3974 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3975 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3976 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3977 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3978 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3979 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3980 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3981 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3982 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3983 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3984 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3985 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3986 | { // acquire lock |
| 3987 | mLock.lock(); |
| 3988 | |
| 3989 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3990 | mLock.unlock(); |
| 3991 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3992 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3993 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3994 | return; // event was consumed by the filter |
| 3995 | } |
| 3996 | |
| 3997 | mLock.lock(); |
| 3998 | } |
| 3999 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4000 | std::unique_ptr<KeyEntry> newEntry = |
| 4001 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 4002 | args->displayId, policyFlags, args->action, flags, |
| 4003 | keyCode, args->scanCode, metaState, repeatCount, |
| 4004 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4005 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4006 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4007 | mLock.unlock(); |
| 4008 | } // release lock |
| 4009 | |
| 4010 | if (needWake) { |
| 4011 | mLooper->wake(); |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 4016 | return mInputFilterEnabled; |
| 4017 | } |
| 4018 | |
| 4019 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4020 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4021 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4022 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 4023 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 4024 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4025 | "yCursorPosition=%f, downTime=%" PRId64, |
| 4026 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 4027 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 4028 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 4029 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 4030 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 4031 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 4032 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 4033 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 4034 | "orientation=%f", |
| 4035 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 4036 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4037 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4038 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4039 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4040 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4041 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4042 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4043 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4044 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 4045 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4046 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4047 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 4048 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4049 | return; |
| 4050 | } |
| 4051 | |
| 4052 | uint32_t policyFlags = args->policyFlags; |
| 4053 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4054 | |
| 4055 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4056 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4057 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4058 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4059 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4060 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4061 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4062 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4063 | { // acquire lock |
| 4064 | mLock.lock(); |
| 4065 | |
| 4066 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4067 | ui::Transform displayTransform; |
| 4068 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4069 | displayTransform = it->second.transform; |
| 4070 | } |
| 4071 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4072 | mLock.unlock(); |
| 4073 | |
| 4074 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4075 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4076 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4077 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4078 | displayTransform, args->xPrecision, args->yPrecision, |
| 4079 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4080 | args->downTime, args->eventTime, args->pointerCount, |
| 4081 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4082 | |
| 4083 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4084 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4085 | return; // event was consumed by the filter |
| 4086 | } |
| 4087 | |
| 4088 | mLock.lock(); |
| 4089 | } |
| 4090 | |
| 4091 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4092 | std::unique_ptr<MotionEntry> newEntry = |
| 4093 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4094 | args->source, args->displayId, policyFlags, |
| 4095 | args->action, args->actionButton, args->flags, |
| 4096 | args->metaState, args->buttonState, |
| 4097 | args->classification, args->edgeFlags, |
| 4098 | args->xPrecision, args->yPrecision, |
| 4099 | args->xCursorPosition, args->yCursorPosition, |
| 4100 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4101 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4102 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4103 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4104 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4105 | !mInputFilterEnabled) { |
| 4106 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4107 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4108 | } |
| 4109 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4110 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4111 | mLock.unlock(); |
| 4112 | } // release lock |
| 4113 | |
| 4114 | if (needWake) { |
| 4115 | mLooper->wake(); |
| 4116 | } |
| 4117 | } |
| 4118 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4119 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4120 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4121 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4122 | " sensorType=%s", |
| 4123 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4124 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4125 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4126 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4127 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4128 | { // acquire lock |
| 4129 | mLock.lock(); |
| 4130 | |
| 4131 | // Just enqueue a new sensor event. |
| 4132 | std::unique_ptr<SensorEntry> newEntry = |
| 4133 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4134 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4135 | args->sensorType, args->accuracy, |
| 4136 | args->accuracyChanged, args->values); |
| 4137 | |
| 4138 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4139 | mLock.unlock(); |
| 4140 | } // release lock |
| 4141 | |
| 4142 | if (needWake) { |
| 4143 | mLooper->wake(); |
| 4144 | } |
| 4145 | } |
| 4146 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4147 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4148 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4149 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4150 | args->deviceId, args->isOn); |
| 4151 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4152 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4153 | } |
| 4154 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4155 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4156 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4157 | } |
| 4158 | |
| 4159 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4160 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4161 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4162 | "switchMask=0x%08x", |
| 4163 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4164 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4165 | |
| 4166 | uint32_t policyFlags = args->policyFlags; |
| 4167 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4168 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4169 | } |
| 4170 | |
| 4171 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4172 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4173 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4174 | args->deviceId); |
| 4175 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4176 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4177 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4178 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4179 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4180 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4181 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4182 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4183 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4184 | } // release lock |
| 4185 | |
| 4186 | if (needWake) { |
| 4187 | mLooper->wake(); |
| 4188 | } |
| 4189 | } |
| 4190 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4191 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4192 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4193 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4194 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4195 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4196 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4197 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4198 | { // acquire lock |
| 4199 | std::scoped_lock _l(mLock); |
| 4200 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4201 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4202 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4203 | } // release lock |
| 4204 | |
| 4205 | if (needWake) { |
| 4206 | mLooper->wake(); |
| 4207 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4208 | } |
| 4209 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 4210 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
| 4211 | std::optional<int32_t> targetUid, |
| 4212 | InputEventInjectionSync syncMode, |
| 4213 | std::chrono::milliseconds timeout, |
| 4214 | uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4215 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 4216 | ALOGD("injectInputEvent - eventType=%d, targetUid=%s, syncMode=%d, timeout=%lld, " |
| 4217 | "policyFlags=0x%08x", |
| 4218 | event->getType(), targetUid ? std::to_string(*targetUid).c_str() : "none", syncMode, |
| 4219 | timeout.count(), policyFlags); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4220 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4221 | 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] | 4222 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 4223 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4224 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4225 | // 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] | 4226 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4227 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4228 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4229 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4230 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4231 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4232 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4233 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4234 | } |
| 4235 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4236 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4237 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4238 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4239 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4240 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4241 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4242 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4243 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4244 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4245 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4246 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4247 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4248 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4249 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4250 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4251 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4252 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4253 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4254 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4255 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4256 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4257 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4258 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4259 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4260 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4261 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4262 | |
| 4263 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4264 | android::base::Timer t; |
| 4265 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4266 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4267 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4268 | std::to_string(t.duration().count()).c_str()); |
| 4269 | } |
| 4270 | } |
| 4271 | |
| 4272 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4273 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4274 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4275 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4276 | incomingKey.getDisplayId(), policyFlags, action, |
| 4277 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4278 | incomingKey.getRepeatCount(), |
| 4279 | incomingKey.getDownTime()); |
| 4280 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4281 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4282 | } |
| 4283 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4284 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4285 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4286 | const int32_t action = motionEvent.getAction(); |
| 4287 | const bool isPointerEvent = |
| 4288 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4289 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4290 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4291 | ? ADISPLAY_ID_DEFAULT |
| 4292 | : event->getDisplayId(); |
| 4293 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4294 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4295 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4296 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4297 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4298 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4299 | } |
| 4300 | |
| 4301 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4302 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4303 | android::base::Timer t; |
| 4304 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4305 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4306 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4307 | std::to_string(t.duration().count()).c_str()); |
| 4308 | } |
| 4309 | } |
| 4310 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4311 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4312 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4313 | } |
| 4314 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4315 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4316 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4317 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4318 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4319 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4320 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4321 | displayId, policyFlags, action, actionButton, |
| 4322 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4323 | motionEvent.getButtonState(), |
| 4324 | motionEvent.getClassification(), |
| 4325 | motionEvent.getEdgeFlags(), |
| 4326 | motionEvent.getXPrecision(), |
| 4327 | motionEvent.getYPrecision(), |
| 4328 | motionEvent.getRawXCursorPosition(), |
| 4329 | motionEvent.getRawYCursorPosition(), |
| 4330 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4331 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4332 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4333 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4334 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4335 | sampleEventTimes += 1; |
| 4336 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4337 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4338 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4339 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4340 | displayId, policyFlags, action, actionButton, |
| 4341 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4342 | motionEvent.getButtonState(), |
| 4343 | motionEvent.getClassification(), |
| 4344 | motionEvent.getEdgeFlags(), |
| 4345 | motionEvent.getXPrecision(), |
| 4346 | motionEvent.getYPrecision(), |
| 4347 | motionEvent.getRawXCursorPosition(), |
| 4348 | motionEvent.getRawYCursorPosition(), |
| 4349 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4350 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4351 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4352 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4353 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4354 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4355 | } |
| 4356 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4357 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4358 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4359 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4360 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4361 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4362 | } |
| 4363 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 4364 | InjectionState* injectionState = new InjectionState(targetUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4365 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4366 | injectionState->injectionIsAsync = true; |
| 4367 | } |
| 4368 | |
| 4369 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4370 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4371 | |
| 4372 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4373 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4374 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4375 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4376 | } |
| 4377 | |
| 4378 | mLock.unlock(); |
| 4379 | |
| 4380 | if (needWake) { |
| 4381 | mLooper->wake(); |
| 4382 | } |
| 4383 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4384 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4385 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4386 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4387 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4388 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4389 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4390 | } else { |
| 4391 | for (;;) { |
| 4392 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4393 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4394 | break; |
| 4395 | } |
| 4396 | |
| 4397 | nsecs_t remainingTimeout = endTime - now(); |
| 4398 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4399 | if (DEBUG_INJECTION) { |
| 4400 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4401 | "to become available."); |
| 4402 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4403 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4404 | break; |
| 4405 | } |
| 4406 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4407 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4408 | } |
| 4409 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4410 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4411 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4412 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4413 | if (DEBUG_INJECTION) { |
| 4414 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4415 | injectionState->pendingForegroundDispatches); |
| 4416 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4417 | nsecs_t remainingTimeout = endTime - now(); |
| 4418 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4419 | if (DEBUG_INJECTION) { |
| 4420 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4421 | "dispatches to finish."); |
| 4422 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4423 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4424 | break; |
| 4425 | } |
| 4426 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4427 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4428 | } |
| 4429 | } |
| 4430 | } |
| 4431 | |
| 4432 | injectionState->release(); |
| 4433 | } // release lock |
| 4434 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4435 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 4436 | ALOGD("injectInputEvent - Finished with result %d.", injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4437 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4438 | |
| 4439 | return injectionResult; |
| 4440 | } |
| 4441 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4442 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4443 | std::array<uint8_t, 32> calculatedHmac; |
| 4444 | std::unique_ptr<VerifiedInputEvent> result; |
| 4445 | switch (event.getType()) { |
| 4446 | case AINPUT_EVENT_TYPE_KEY: { |
| 4447 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4448 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4449 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4450 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4451 | break; |
| 4452 | } |
| 4453 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4454 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4455 | VerifiedMotionEvent verifiedMotionEvent = |
| 4456 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4457 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4458 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4459 | break; |
| 4460 | } |
| 4461 | default: { |
| 4462 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4463 | return nullptr; |
| 4464 | } |
| 4465 | } |
| 4466 | if (calculatedHmac == INVALID_HMAC) { |
| 4467 | return nullptr; |
| 4468 | } |
| 4469 | if (calculatedHmac != event.getHmac()) { |
| 4470 | return nullptr; |
| 4471 | } |
| 4472 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4473 | } |
| 4474 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4475 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4476 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4477 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4478 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4479 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 4480 | ALOGD("Setting input event injection result to %d.", injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4481 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4482 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4483 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4484 | // Log the outcome since the injector did not wait for the injection result. |
| 4485 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4486 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4487 | ALOGV("Asynchronous input event injection succeeded."); |
| 4488 | break; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame^] | 4489 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4490 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4491 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4492 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4493 | ALOGW("Asynchronous input event injection failed."); |
| 4494 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4495 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4496 | ALOGW("Asynchronous input event injection timed out."); |
| 4497 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4498 | case InputEventInjectionResult::PENDING: |
| 4499 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4500 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4501 | } |
| 4502 | } |
| 4503 | |
| 4504 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4505 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4506 | } |
| 4507 | } |
| 4508 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4509 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4510 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4511 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4512 | // display space, so we need to transform the injected events accordingly. |
| 4513 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4514 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4515 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4516 | |
| 4517 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4518 | entry.pointerCoords[i] = |
| 4519 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4520 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4521 | } |
| 4522 | } |
| 4523 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4524 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4525 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4526 | if (injectionState) { |
| 4527 | injectionState->pendingForegroundDispatches += 1; |
| 4528 | } |
| 4529 | } |
| 4530 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4531 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4532 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4533 | if (injectionState) { |
| 4534 | injectionState->pendingForegroundDispatches -= 1; |
| 4535 | |
| 4536 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4537 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4538 | } |
| 4539 | } |
| 4540 | } |
| 4541 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4542 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4543 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4544 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4545 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4546 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4547 | } |
| 4548 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4549 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4550 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4551 | if (windowHandleToken == nullptr) { |
| 4552 | return nullptr; |
| 4553 | } |
| 4554 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4555 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4556 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4557 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4558 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4559 | return windowHandle; |
| 4560 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4561 | } |
| 4562 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4563 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4564 | } |
| 4565 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4566 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4567 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4568 | if (windowHandleToken == nullptr) { |
| 4569 | return nullptr; |
| 4570 | } |
| 4571 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4572 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4573 | if (windowHandle->getToken() == windowHandleToken) { |
| 4574 | return windowHandle; |
| 4575 | } |
| 4576 | } |
| 4577 | return nullptr; |
| 4578 | } |
| 4579 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4580 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4581 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4582 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4583 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4584 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4585 | if (handle->getId() == windowHandle->getId() && |
| 4586 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4587 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4588 | ALOGE("Found window %s in display %" PRId32 |
| 4589 | ", but it should belong to display %" PRId32, |
| 4590 | windowHandle->getName().c_str(), it.first, |
| 4591 | windowHandle->getInfo()->displayId); |
| 4592 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4593 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4594 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4595 | } |
| 4596 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4597 | return nullptr; |
| 4598 | } |
| 4599 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4600 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4601 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4602 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4603 | } |
| 4604 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4605 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4606 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4607 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4608 | windowHandle.getInfo()->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4609 | if (connection != nullptr && noInputChannel) { |
| 4610 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4611 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4612 | return false; |
| 4613 | } |
| 4614 | |
| 4615 | if (connection == nullptr) { |
| 4616 | if (!noInputChannel) { |
| 4617 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4618 | } |
| 4619 | return false; |
| 4620 | } |
| 4621 | if (!connection->responsive) { |
| 4622 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4623 | return false; |
| 4624 | } |
| 4625 | return true; |
| 4626 | } |
| 4627 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4628 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4629 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4630 | auto connectionIt = mConnectionsByToken.find(token); |
| 4631 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4632 | return nullptr; |
| 4633 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4634 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4635 | } |
| 4636 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4637 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4638 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4639 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4640 | // Remove all handles on a display if there are no windows left. |
| 4641 | mWindowHandlesByDisplay.erase(displayId); |
| 4642 | return; |
| 4643 | } |
| 4644 | |
| 4645 | // Since we compare the pointer of input window handles across window updates, we need |
| 4646 | // 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] | 4647 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4648 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4649 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4650 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4651 | } |
| 4652 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4653 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4654 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4655 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4656 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4657 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4658 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4659 | const bool canReceiveInput = |
| 4660 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4661 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4662 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4663 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4664 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4665 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4666 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | if (info->displayId != displayId) { |
| 4670 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4671 | handle->getName().c_str(), displayId, info->displayId); |
| 4672 | continue; |
| 4673 | } |
| 4674 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4675 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4676 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4677 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4678 | oldHandle->updateFrom(handle); |
| 4679 | newHandles.push_back(oldHandle); |
| 4680 | } else { |
| 4681 | newHandles.push_back(handle); |
| 4682 | } |
| 4683 | } |
| 4684 | |
| 4685 | // Insert or replace |
| 4686 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4687 | } |
| 4688 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4689 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4690 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4691 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4692 | { // acquire lock |
| 4693 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4694 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4695 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4696 | } |
| 4697 | } |
| 4698 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4699 | mLooper->wake(); |
| 4700 | } |
| 4701 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4702 | /** |
| 4703 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4704 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4705 | * If set an empty list, remove all handles from the specific display. |
| 4706 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4707 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4708 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4709 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4710 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4711 | if (DEBUG_FOCUS) { |
| 4712 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4713 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4714 | windowList += iwh->getName() + " "; |
| 4715 | } |
| 4716 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4717 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4718 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4719 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4720 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4721 | const WindowInfo& info = *window->getInfo(); |
| 4722 | |
| 4723 | // 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] | 4724 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4725 | if (noInputWindow && window->getToken() != nullptr) { |
| 4726 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4727 | window->getName().c_str()); |
| 4728 | window->releaseChannel(); |
| 4729 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4730 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4731 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4732 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 4733 | !info.inputConfig.test( |
| 4734 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4735 | "%s has feature SPY, but is not a trusted overlay.", |
| 4736 | window->getName().c_str()); |
| 4737 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4738 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4739 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 4740 | !info.inputConfig.test( |
| 4741 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4742 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4743 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4744 | } |
| 4745 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4746 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4747 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4748 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4749 | // Save the old windows' orientation by ID before it gets updated. |
| 4750 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4751 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4752 | oldWindowOrientations.emplace(handle->getId(), |
| 4753 | handle->getInfo()->transform.getOrientation()); |
| 4754 | } |
| 4755 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4756 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4757 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4758 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4759 | if (mLastHoverWindowHandle && |
| 4760 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4761 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4762 | mLastHoverWindowHandle = nullptr; |
| 4763 | } |
| 4764 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4765 | std::optional<FocusResolver::FocusChanges> changes = |
| 4766 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4767 | if (changes) { |
| 4768 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4769 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4770 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4771 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4772 | mTouchStatesByDisplay.find(displayId); |
| 4773 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4774 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4775 | for (size_t i = 0; i < state.windows.size();) { |
| 4776 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4777 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4778 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4779 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4780 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4781 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4782 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4783 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4784 | if (touchedInputChannel != nullptr) { |
| 4785 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4786 | "touched window was removed"); |
| 4787 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4788 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4789 | // well. |
| 4790 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4791 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 4792 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4793 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4794 | if (wallpaper != nullptr) { |
| 4795 | sp<Connection> wallpaperConnection = |
| 4796 | getConnectionLocked(wallpaper->getToken()); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4797 | if (wallpaperConnection != nullptr) { |
| 4798 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4799 | options); |
| 4800 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4801 | } |
| 4802 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4803 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4804 | state.windows.erase(state.windows.begin() + i); |
| 4805 | } else { |
| 4806 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4807 | } |
| 4808 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4809 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4810 | // 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] | 4811 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4812 | if (mDragState && |
| 4813 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4814 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4815 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4816 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4817 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4818 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4819 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4820 | // pointer events if necessary. |
| 4821 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4822 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4823 | if (newWindowHandle != nullptr && |
| 4824 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4825 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4826 | std::shared_ptr<InputChannel> inputChannel = |
| 4827 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4828 | if (inputChannel != nullptr) { |
| 4829 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4830 | "touched window's orientation changed"); |
| 4831 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4832 | } |
| 4833 | } |
| 4834 | } |
| 4835 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4836 | // Release information for windows that are no longer present. |
| 4837 | // This ensures that unused input channels are released promptly. |
| 4838 | // Otherwise, they might stick around until the window handle is destroyed |
| 4839 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4840 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4841 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4842 | if (DEBUG_FOCUS) { |
| 4843 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4844 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4845 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4846 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4847 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4848 | } |
| 4849 | |
| 4850 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4851 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4852 | if (DEBUG_FOCUS) { |
| 4853 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4854 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4855 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4856 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4857 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4858 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4859 | } // release lock |
| 4860 | |
| 4861 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4862 | mLooper->wake(); |
| 4863 | } |
| 4864 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4865 | void InputDispatcher::setFocusedApplicationLocked( |
| 4866 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4867 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4868 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4869 | |
| 4870 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4871 | return; // This application is already focused. No need to wake up or change anything. |
| 4872 | } |
| 4873 | |
| 4874 | // Set the new application handle. |
| 4875 | if (inputApplicationHandle != nullptr) { |
| 4876 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4877 | } else { |
| 4878 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4879 | } |
| 4880 | |
| 4881 | // No matter what the old focused application was, stop waiting on it because it is |
| 4882 | // no longer focused. |
| 4883 | resetNoFocusedWindowTimeoutLocked(); |
| 4884 | } |
| 4885 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4886 | /** |
| 4887 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4888 | * the display not specified. |
| 4889 | * |
| 4890 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4891 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4892 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4893 | * display. The display-specified events won't be affected. |
| 4894 | */ |
| 4895 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4896 | if (DEBUG_FOCUS) { |
| 4897 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4898 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4899 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4900 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4901 | |
| 4902 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4903 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4904 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4905 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4906 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4907 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4908 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4909 | CancelationOptions |
| 4910 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4911 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4912 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4913 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4914 | } |
| 4915 | } |
| 4916 | mFocusedDisplayId = displayId; |
| 4917 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4918 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4919 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4920 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4921 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4922 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4923 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4924 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4925 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4926 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4927 | } |
| 4928 | } |
| 4929 | } |
| 4930 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4931 | if (DEBUG_FOCUS) { |
| 4932 | logDispatchStateLocked(); |
| 4933 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4934 | } // release lock |
| 4935 | |
| 4936 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4937 | mLooper->wake(); |
| 4938 | } |
| 4939 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4940 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4941 | if (DEBUG_FOCUS) { |
| 4942 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4943 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4944 | |
| 4945 | bool changed; |
| 4946 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4947 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4948 | |
| 4949 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4950 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4951 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | if (mDispatchEnabled && !enabled) { |
| 4955 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4956 | } |
| 4957 | |
| 4958 | mDispatchEnabled = enabled; |
| 4959 | mDispatchFrozen = frozen; |
| 4960 | changed = true; |
| 4961 | } else { |
| 4962 | changed = false; |
| 4963 | } |
| 4964 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4965 | if (DEBUG_FOCUS) { |
| 4966 | logDispatchStateLocked(); |
| 4967 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4968 | } // release lock |
| 4969 | |
| 4970 | if (changed) { |
| 4971 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4972 | mLooper->wake(); |
| 4973 | } |
| 4974 | } |
| 4975 | |
| 4976 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4977 | if (DEBUG_FOCUS) { |
| 4978 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4979 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4980 | |
| 4981 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4982 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4983 | |
| 4984 | if (mInputFilterEnabled == enabled) { |
| 4985 | return; |
| 4986 | } |
| 4987 | |
| 4988 | mInputFilterEnabled = enabled; |
| 4989 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4990 | } // release lock |
| 4991 | |
| 4992 | // Wake up poll loop since there might be work to do to drop everything. |
| 4993 | mLooper->wake(); |
| 4994 | } |
| 4995 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4996 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, |
| 4997 | bool hasPermission) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4998 | bool needWake = false; |
| 4999 | { |
| 5000 | std::scoped_lock lock(mLock); |
| 5001 | if (mInTouchMode == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5002 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5003 | } |
| 5004 | if (DEBUG_TOUCH_MODE) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5005 | ALOGD("Request to change touch mode from %s to %s (calling pid=%d, uid=%d, " |
| 5006 | "hasPermission=%s)", |
| 5007 | toString(mInTouchMode), toString(inTouchMode), pid, uid, toString(hasPermission)); |
| 5008 | } |
| 5009 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5010 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5011 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
| 5012 | ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " |
| 5013 | "window nor none of the previously interacted window", |
| 5014 | pid, uid); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5015 | return false; |
| 5016 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | // TODO(b/198499018): Store touch mode per display. |
| 5020 | mInTouchMode = inTouchMode; |
| 5021 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5022 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 5023 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5024 | } // release lock |
| 5025 | |
| 5026 | if (needWake) { |
| 5027 | mLooper->wake(); |
| 5028 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5029 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5030 | } |
| 5031 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5032 | bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { |
| 5033 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5034 | if (focusedToken == nullptr) { |
| 5035 | return false; |
| 5036 | } |
| 5037 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5038 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5039 | } |
| 5040 | |
| 5041 | bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { |
| 5042 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5043 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5044 | const sp<WindowInfoHandle> windowHandle = |
| 5045 | getWindowHandleLocked(connectionToken); |
| 5046 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5047 | }) != mInteractionConnectionTokens.end(); |
| 5048 | } |
| 5049 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5050 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5051 | if (opacity < 0 || opacity > 1) { |
| 5052 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5053 | return; |
| 5054 | } |
| 5055 | |
| 5056 | std::scoped_lock lock(mLock); |
| 5057 | mMaximumObscuringOpacityForTouch = opacity; |
| 5058 | } |
| 5059 | |
| 5060 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 5061 | std::scoped_lock lock(mLock); |
| 5062 | mBlockUntrustedTouchesMode = mode; |
| 5063 | } |
| 5064 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5065 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 5066 | const sp<IBinder>& token) { |
| 5067 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5068 | for (TouchedWindow& w : state.windows) { |
| 5069 | if (w.windowHandle->getToken() == token) { |
| 5070 | return std::make_pair(&state, &w); |
| 5071 | } |
| 5072 | } |
| 5073 | } |
| 5074 | return std::make_pair(nullptr, nullptr); |
| 5075 | } |
| 5076 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5077 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5078 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5079 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5080 | if (DEBUG_FOCUS) { |
| 5081 | ALOGD("Trivial transfer to same window."); |
| 5082 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5083 | return true; |
| 5084 | } |
| 5085 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5086 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5087 | std::scoped_lock _l(mLock); |
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 | // Find the target touch state and touched window by fromToken. |
| 5090 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 5091 | if (state == nullptr || touchedWindow == nullptr) { |
| 5092 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5093 | return false; |
| 5094 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5095 | |
| 5096 | const int32_t displayId = state->displayId; |
| 5097 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5098 | if (toWindowHandle == nullptr) { |
| 5099 | ALOGW("Cannot transfer focus because to window not found."); |
| 5100 | return false; |
| 5101 | } |
| 5102 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5103 | if (DEBUG_FOCUS) { |
| 5104 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5105 | touchedWindow->windowHandle->getName().c_str(), |
| 5106 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5107 | } |
| 5108 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5109 | // Erase old window. |
| 5110 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 5111 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 5112 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5113 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5114 | // Add new window. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5115 | int32_t newTargetFlags = |
| 5116 | oldTargetFlags & (InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5117 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
| 5118 | newTargetFlags |= InputTarget::FLAG_FOREGROUND; |
| 5119 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5120 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5121 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5122 | // Store the dragging window. |
| 5123 | if (isDragDrop) { |
| 5124 | mDragState = std::make_unique<DragState>(toWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5125 | } |
| 5126 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5127 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5128 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5129 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5130 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5131 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5132 | CancelationOptions |
| 5133 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5134 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5135 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5136 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5137 | } |
| 5138 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5139 | if (DEBUG_FOCUS) { |
| 5140 | logDispatchStateLocked(); |
| 5141 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5142 | } // release lock |
| 5143 | |
| 5144 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5145 | mLooper->wake(); |
| 5146 | return true; |
| 5147 | } |
| 5148 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5149 | /** |
| 5150 | * Get the touched foreground window on the given display. |
| 5151 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5152 | * window is being touched. |
| 5153 | */ |
| 5154 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5155 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5156 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5157 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5158 | return nullptr; |
| 5159 | } |
| 5160 | |
| 5161 | const TouchState& state = stateIt->second; |
| 5162 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5163 | // If multiple foreground windows are touched, return nullptr |
| 5164 | for (const TouchedWindow& window : state.windows) { |
| 5165 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 5166 | if (touchedForegroundWindow != nullptr) { |
| 5167 | ALOGI("Two or more foreground windows: %s and %s", |
| 5168 | touchedForegroundWindow->getName().c_str(), |
| 5169 | window.windowHandle->getName().c_str()); |
| 5170 | return nullptr; |
| 5171 | } |
| 5172 | touchedForegroundWindow = window.windowHandle; |
| 5173 | } |
| 5174 | } |
| 5175 | return touchedForegroundWindow; |
| 5176 | } |
| 5177 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5178 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5179 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5180 | sp<IBinder> fromToken; |
| 5181 | { // acquire lock |
| 5182 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5183 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5184 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5185 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5186 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5187 | return false; |
| 5188 | } |
| 5189 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5190 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5191 | if (from == nullptr) { |
| 5192 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5193 | return false; |
| 5194 | } |
| 5195 | |
| 5196 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5197 | } // release lock |
| 5198 | |
| 5199 | return transferTouchFocus(fromToken, destChannelToken); |
| 5200 | } |
| 5201 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5202 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5203 | if (DEBUG_FOCUS) { |
| 5204 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5205 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5206 | |
| 5207 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5208 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5209 | |
| 5210 | resetKeyRepeatLocked(); |
| 5211 | releasePendingEventLocked(); |
| 5212 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5213 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5214 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5215 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5216 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5217 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5218 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5219 | } |
| 5220 | |
| 5221 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5222 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5223 | dumpDispatchStateLocked(dump); |
| 5224 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5225 | std::istringstream stream(dump); |
| 5226 | std::string line; |
| 5227 | |
| 5228 | while (std::getline(stream, line, '\n')) { |
| 5229 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5230 | } |
| 5231 | } |
| 5232 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5233 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5234 | std::string dump; |
| 5235 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5236 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5237 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5238 | |
| 5239 | std::string windowName = "None"; |
| 5240 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5241 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5242 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5243 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5244 | : "token has capture without window"; |
| 5245 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5246 | 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] | 5247 | |
| 5248 | return dump; |
| 5249 | } |
| 5250 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5251 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5252 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5253 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5254 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5255 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5256 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5257 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5258 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5259 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5260 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5261 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5262 | const std::chrono::duration timeout = |
| 5263 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5264 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5265 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5266 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5267 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5268 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5269 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5270 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5271 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5272 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5273 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5274 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5275 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5276 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5277 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5278 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5279 | 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] | 5280 | state.displayId, toString(state.down), toString(state.split), |
| 5281 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5282 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5283 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5284 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5285 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5286 | dump += StringPrintf(INDENT4 |
| 5287 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5288 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5289 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5290 | } |
| 5291 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5292 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5293 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5294 | } |
| 5295 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5296 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5297 | } |
| 5298 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5299 | if (mDragState) { |
| 5300 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5301 | mDragState->dump(dump, INDENT2); |
| 5302 | } |
| 5303 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5304 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5305 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5306 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5307 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5308 | const auto& displayInfo = it->second; |
| 5309 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5310 | displayInfo.logicalHeight); |
| 5311 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5312 | } else { |
| 5313 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5314 | } |
| 5315 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5316 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5317 | dump += INDENT2 "Windows:\n"; |
| 5318 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5319 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5320 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5321 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5322 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5323 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5324 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5325 | "applicationInfo.name=%s, " |
| 5326 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5327 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5328 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5329 | windowInfo->displayId, |
| 5330 | windowInfo->inputConfig.string().c_str(), |
| 5331 | windowInfo->alpha, windowInfo->frameLeft, |
| 5332 | windowInfo->frameTop, windowInfo->frameRight, |
| 5333 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5334 | windowInfo->applicationInfo.name.c_str(), |
| 5335 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5336 | dump += dumpRegion(windowInfo->touchableRegion); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5337 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5338 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5339 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5340 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5341 | millis(windowInfo->dispatchingTimeout), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5342 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5343 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5344 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5345 | } |
| 5346 | } else { |
| 5347 | dump += INDENT2 "Windows: <none>\n"; |
| 5348 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5349 | } |
| 5350 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5351 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5352 | } |
| 5353 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5354 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5355 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5356 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5357 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5358 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5359 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5360 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5361 | } |
| 5362 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5363 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5364 | |
| 5365 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5366 | if (!mRecentQueue.empty()) { |
| 5367 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5368 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5369 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5370 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5371 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5372 | } |
| 5373 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5374 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5375 | } |
| 5376 | |
| 5377 | // Dump event currently being dispatched. |
| 5378 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5379 | dump += INDENT "PendingEvent:\n"; |
| 5380 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5381 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5382 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5383 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5384 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5385 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5389 | if (!mInboundQueue.empty()) { |
| 5390 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5391 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5392 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5393 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5394 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5395 | } |
| 5396 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5397 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5398 | } |
| 5399 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5400 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5401 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5402 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5403 | const KeyReplacement& replacement = pair.first; |
| 5404 | int32_t newKeyCode = pair.second; |
| 5405 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5406 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5407 | } |
| 5408 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5409 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5410 | } |
| 5411 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5412 | if (!mCommandQueue.empty()) { |
| 5413 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5414 | } else { |
| 5415 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5416 | } |
| 5417 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5418 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5419 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5420 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5421 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5422 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5423 | connection->inputChannel->getFd().get(), |
| 5424 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5425 | connection->getWindowName().c_str(), |
| 5426 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5427 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5428 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5429 | if (!connection->outboundQueue.empty()) { |
| 5430 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5431 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5432 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5433 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5434 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5435 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5436 | } |
| 5437 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5438 | if (!connection->waitQueue.empty()) { |
| 5439 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5440 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5441 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5442 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5443 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5444 | } |
| 5445 | } |
| 5446 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5447 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5448 | } |
| 5449 | |
| 5450 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5451 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5452 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5453 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5454 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5455 | } |
| 5456 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5457 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5458 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5459 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5460 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5461 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5462 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5463 | } |
| 5464 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5465 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5466 | const size_t numMonitors = monitors.size(); |
| 5467 | for (size_t i = 0; i < numMonitors; i++) { |
| 5468 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5469 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5470 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5471 | dump += "\n"; |
| 5472 | } |
| 5473 | } |
| 5474 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5475 | class LooperEventCallback : public LooperCallback { |
| 5476 | public: |
| 5477 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5478 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5479 | |
| 5480 | private: |
| 5481 | std::function<int(int events)> mCallback; |
| 5482 | }; |
| 5483 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5484 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5485 | if (DEBUG_CHANNEL_CREATION) { |
| 5486 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5487 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5488 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5489 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5490 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5491 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5492 | |
| 5493 | if (result) { |
| 5494 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5495 | } |
| 5496 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5497 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5498 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5499 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5500 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5501 | sp<Connection> connection = |
| 5502 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5503 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5504 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5505 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5506 | } |
| 5507 | mConnectionsByToken.emplace(token, connection); |
| 5508 | |
| 5509 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5510 | this, std::placeholders::_1, token); |
| 5511 | |
| 5512 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5513 | } // release lock |
| 5514 | |
| 5515 | // Wake the looper because some connections have changed. |
| 5516 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5517 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5518 | } |
| 5519 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5520 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5521 | const std::string& name, |
| 5522 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5523 | std::shared_ptr<InputChannel> serverChannel; |
| 5524 | std::unique_ptr<InputChannel> clientChannel; |
| 5525 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5526 | if (result) { |
| 5527 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5528 | } |
| 5529 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5530 | { // acquire lock |
| 5531 | std::scoped_lock _l(mLock); |
| 5532 | |
| 5533 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5534 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5535 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5538 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5539 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5540 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5541 | |
| 5542 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5543 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5544 | } |
| 5545 | mConnectionsByToken.emplace(token, connection); |
| 5546 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5547 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5548 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5549 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5550 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5551 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5552 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5553 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5554 | // Wake the looper because some connections have changed. |
| 5555 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5556 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5557 | } |
| 5558 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5559 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5560 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5561 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5562 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5563 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5564 | if (status) { |
| 5565 | return status; |
| 5566 | } |
| 5567 | } // release lock |
| 5568 | |
| 5569 | // Wake the poll loop because removing the connection may have changed the current |
| 5570 | // synchronization state. |
| 5571 | mLooper->wake(); |
| 5572 | return OK; |
| 5573 | } |
| 5574 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5575 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5576 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5577 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5578 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5579 | // 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] | 5580 | return BAD_VALUE; |
| 5581 | } |
| 5582 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5583 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5584 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5585 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5586 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5587 | } |
| 5588 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5589 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5590 | |
| 5591 | nsecs_t currentTime = now(); |
| 5592 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5593 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5594 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5595 | return OK; |
| 5596 | } |
| 5597 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5598 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5599 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5600 | auto& [displayId, monitors] = *it; |
| 5601 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5602 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5603 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5604 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5605 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5606 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5607 | } else { |
| 5608 | ++it; |
| 5609 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5610 | } |
| 5611 | } |
| 5612 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5613 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5614 | std::scoped_lock _l(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5615 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5616 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5617 | if (!requestingChannel) { |
| 5618 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5619 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5620 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5621 | |
| 5622 | auto [statePtr, windowPtr] = findTouchStateAndWindowLocked(token); |
| 5623 | if (statePtr == nullptr || windowPtr == nullptr || !statePtr->down) { |
| 5624 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5625 | " Ignoring."); |
| 5626 | return BAD_VALUE; |
| 5627 | } |
| 5628 | |
| 5629 | TouchState& state = *statePtr; |
| 5630 | |
| 5631 | // Send cancel events to all the input channels we're stealing from. |
| 5632 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5633 | "input channel stole pointer stream"); |
| 5634 | options.deviceId = state.deviceId; |
| 5635 | options.displayId = state.displayId; |
| 5636 | std::string canceledWindows; |
| 5637 | for (const TouchedWindow& window : state.windows) { |
| 5638 | const std::shared_ptr<InputChannel> channel = |
| 5639 | getInputChannelLocked(window.windowHandle->getToken()); |
| 5640 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5641 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5642 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5643 | canceledWindows += channel->getName(); |
| 5644 | } |
| 5645 | } |
| 5646 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5647 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5648 | canceledWindows.c_str()); |
| 5649 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5650 | // Prevent the gesture from being sent to any other windows. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5651 | state.filterWindowsExcept(token); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5652 | state.preventNewTargets = true; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5653 | return OK; |
| 5654 | } |
| 5655 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5656 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5657 | { // acquire lock |
| 5658 | std::scoped_lock _l(mLock); |
| 5659 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5660 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5661 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5662 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5663 | : "token without window"); |
| 5664 | } |
| 5665 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5666 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5667 | if (focusedToken != windowToken) { |
| 5668 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5669 | enabled ? "enable" : "disable"); |
| 5670 | return; |
| 5671 | } |
| 5672 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5673 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5674 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5675 | "window has %s requested pointer capture.", |
| 5676 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5677 | return; |
| 5678 | } |
| 5679 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5680 | if (enabled) { |
| 5681 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5682 | mIneligibleDisplaysForPointerCapture.end(), |
| 5683 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5684 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5685 | return; |
| 5686 | } |
| 5687 | } |
| 5688 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5689 | setPointerCaptureLocked(enabled); |
| 5690 | } // release lock |
| 5691 | |
| 5692 | // Wake the thread to process command entries. |
| 5693 | mLooper->wake(); |
| 5694 | } |
| 5695 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5696 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5697 | { // acquire lock |
| 5698 | std::scoped_lock _l(mLock); |
| 5699 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5700 | if (!isEligible) { |
| 5701 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5702 | } |
| 5703 | } // release lock |
| 5704 | } |
| 5705 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5706 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5707 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5708 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5709 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5710 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5711 | } |
| 5712 | } |
| 5713 | } |
| 5714 | return std::nullopt; |
| 5715 | } |
| 5716 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5717 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5718 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5719 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5720 | } |
| 5721 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5722 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5723 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5724 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5725 | } |
| 5726 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5727 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5728 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5729 | } |
| 5730 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5731 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5732 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5733 | if (connection == nullptr) { |
| 5734 | return "<nullptr>"; |
| 5735 | } |
| 5736 | return connection->getInputChannelName(); |
| 5737 | } |
| 5738 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5739 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5740 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5741 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5742 | } |
| 5743 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5744 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5745 | const sp<Connection>& connection, uint32_t seq, |
| 5746 | bool handled, nsecs_t consumeTime) { |
| 5747 | // Handle post-event policy actions. |
| 5748 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5749 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5750 | return; |
| 5751 | } |
| 5752 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5753 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5754 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5755 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5756 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5757 | } |
| 5758 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5759 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5760 | connection->inputChannel->getConnectionToken(), |
| 5761 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5762 | } |
| 5763 | |
| 5764 | bool restartEvent; |
| 5765 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5766 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5767 | restartEvent = |
| 5768 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5769 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5770 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5771 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5772 | handled); |
| 5773 | } else { |
| 5774 | restartEvent = false; |
| 5775 | } |
| 5776 | |
| 5777 | // Dequeue the event and start the next cycle. |
| 5778 | // Because the lock might have been released, it is possible that the |
| 5779 | // contents of the wait queue to have been drained, so we need to double-check |
| 5780 | // a few things. |
| 5781 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5782 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5783 | dispatchEntry = *dispatchEntryIt; |
| 5784 | connection->waitQueue.erase(dispatchEntryIt); |
| 5785 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5786 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5787 | if (!connection->responsive) { |
| 5788 | connection->responsive = isConnectionResponsive(*connection); |
| 5789 | if (connection->responsive) { |
| 5790 | // The connection was unresponsive, and now it's responsive. |
| 5791 | processConnectionResponsiveLocked(*connection); |
| 5792 | } |
| 5793 | } |
| 5794 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5795 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5796 | connection->outboundQueue.push_front(dispatchEntry); |
| 5797 | traceOutboundQueueLength(*connection); |
| 5798 | } else { |
| 5799 | releaseDispatchEntry(dispatchEntry); |
| 5800 | } |
| 5801 | } |
| 5802 | |
| 5803 | // Start the next dispatch cycle for this connection. |
| 5804 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5805 | } |
| 5806 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5807 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5808 | const sp<IBinder>& newToken) { |
| 5809 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5810 | scoped_unlock unlock(mLock); |
| 5811 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5812 | }; |
| 5813 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5814 | } |
| 5815 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5816 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5817 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5818 | scoped_unlock unlock(mLock); |
| 5819 | mPolicy->notifyDropWindow(token, x, y); |
| 5820 | }; |
| 5821 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5822 | } |
| 5823 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5824 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5825 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5826 | scoped_unlock unlock(mLock); |
| 5827 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5828 | }; |
| 5829 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5830 | } |
| 5831 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5832 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5833 | if (connection == nullptr) { |
| 5834 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5835 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5836 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5837 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5838 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5839 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5840 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5841 | return; |
| 5842 | } |
| 5843 | /** |
| 5844 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5845 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5846 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5847 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5848 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5849 | * most useful. |
| 5850 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5851 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5852 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5853 | std::string reason = |
| 5854 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5855 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5856 | ns2ms(currentWait), |
| 5857 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5858 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5859 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5860 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5861 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5862 | |
| 5863 | // Stop waking up for events on this connection, it is already unresponsive |
| 5864 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5865 | } |
| 5866 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5867 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5868 | std::string reason = |
| 5869 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5870 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5871 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5872 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5873 | scoped_unlock unlock(mLock); |
| 5874 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5875 | }; |
| 5876 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5877 | } |
| 5878 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5879 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5880 | const std::string& reason) { |
| 5881 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5882 | updateLastAnrStateLocked(windowLabel, reason); |
| 5883 | } |
| 5884 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5885 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5886 | const std::string& reason) { |
| 5887 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5888 | updateLastAnrStateLocked(windowLabel, reason); |
| 5889 | } |
| 5890 | |
| 5891 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5892 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5893 | // 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] | 5894 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5895 | struct tm tm; |
| 5896 | localtime_r(&t, &tm); |
| 5897 | char timestr[64]; |
| 5898 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5899 | mLastAnrState.clear(); |
| 5900 | mLastAnrState += INDENT "ANR:\n"; |
| 5901 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5902 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5903 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5904 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5905 | } |
| 5906 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5907 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5908 | KeyEntry& entry) { |
| 5909 | const KeyEvent event = createKeyEvent(entry); |
| 5910 | nsecs_t delay = 0; |
| 5911 | { // release lock |
| 5912 | scoped_unlock unlock(mLock); |
| 5913 | android::base::Timer t; |
| 5914 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5915 | entry.policyFlags); |
| 5916 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5917 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5918 | std::to_string(t.duration().count()).c_str()); |
| 5919 | } |
| 5920 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5921 | |
| 5922 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5923 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5924 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5925 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5926 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5927 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5928 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5929 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5930 | } |
| 5931 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5932 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5933 | std::optional<int32_t> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5934 | std::string reason) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5935 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5936 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5937 | mPolicy->notifyWindowUnresponsive(token, pid, reason); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5938 | }; |
| 5939 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5940 | } |
| 5941 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5942 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
| 5943 | std::optional<int32_t> pid) { |
| 5944 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5945 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5946 | mPolicy->notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5947 | }; |
| 5948 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5949 | } |
| 5950 | |
| 5951 | /** |
| 5952 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5953 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5954 | * command entry to the command queue. |
| 5955 | */ |
| 5956 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5957 | std::string reason) { |
| 5958 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5959 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5960 | if (connection.monitor) { |
| 5961 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5962 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5963 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 5964 | } else { |
| 5965 | // The connection is a window |
| 5966 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5967 | reason.c_str()); |
| 5968 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 5969 | if (handle != nullptr) { |
| 5970 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5971 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5972 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5973 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5974 | } |
| 5975 | |
| 5976 | /** |
| 5977 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5978 | */ |
| 5979 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5980 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5981 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5982 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5983 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 5984 | } else { |
| 5985 | // The connection is a window |
| 5986 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 5987 | if (handle != nullptr) { |
| 5988 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5989 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5990 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5991 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5992 | } |
| 5993 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5994 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5995 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5996 | KeyEntry& keyEntry, bool handled) { |
| 5997 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5998 | if (!handled) { |
| 5999 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6000 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6001 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6002 | return false; |
| 6003 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6004 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6005 | // Get the fallback key state. |
| 6006 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6007 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6008 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6009 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6010 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6011 | } |
| 6012 | |
| 6013 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6014 | // If the application handles the original key for which we previously |
| 6015 | // generated a fallback or if the window is not a foreground window, |
| 6016 | // then cancel the associated fallback key, if any. |
| 6017 | if (fallbackKeyCode != -1) { |
| 6018 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6019 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6020 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6021 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6022 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6023 | keyEntry.policyFlags); |
| 6024 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6025 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6026 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6027 | |
| 6028 | mLock.unlock(); |
| 6029 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6030 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6031 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6032 | |
| 6033 | mLock.lock(); |
| 6034 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6035 | // Cancel the fallback key. |
| 6036 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6037 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6038 | "application handled the original non-fallback key " |
| 6039 | "or is no longer a foreground target, " |
| 6040 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6041 | options.keyCode = fallbackKeyCode; |
| 6042 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6043 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6044 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6045 | } |
| 6046 | } else { |
| 6047 | // If the application did not handle a non-fallback key, first check |
| 6048 | // that we are in a good state to perform unhandled key event processing |
| 6049 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6050 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6051 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6052 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6053 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6054 | "since this is not an initial down. " |
| 6055 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6056 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6057 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6058 | return false; |
| 6059 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6060 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6061 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6062 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6063 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6064 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6065 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6066 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6067 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6068 | |
| 6069 | mLock.unlock(); |
| 6070 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6071 | bool fallback = |
| 6072 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6073 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6074 | |
| 6075 | mLock.lock(); |
| 6076 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6077 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6078 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6079 | return false; |
| 6080 | } |
| 6081 | |
| 6082 | // Latch the fallback keycode for this key on an initial down. |
| 6083 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6084 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6085 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6086 | fallbackKeyCode = event.getKeyCode(); |
| 6087 | } else { |
| 6088 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6089 | } |
| 6090 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6091 | } |
| 6092 | |
| 6093 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6094 | |
| 6095 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6096 | // We will continue to dispatch the key to the policy but we will no |
| 6097 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6098 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6099 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6100 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6101 | if (fallback) { |
| 6102 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6103 | "as a fallback for %d, but on the DOWN it had requested " |
| 6104 | "to send %d instead. Fallback canceled.", |
| 6105 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6106 | } else { |
| 6107 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6108 | "but on the DOWN it had requested to send %d. " |
| 6109 | "Fallback canceled.", |
| 6110 | originalKeyCode, fallbackKeyCode); |
| 6111 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6112 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6113 | |
| 6114 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6115 | "canceling fallback, policy no longer desires it"); |
| 6116 | options.keyCode = fallbackKeyCode; |
| 6117 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6118 | |
| 6119 | fallback = false; |
| 6120 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6121 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6122 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6123 | } |
| 6124 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6125 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6126 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6127 | { |
| 6128 | std::string msg; |
| 6129 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6130 | connection->inputState.getFallbackKeys(); |
| 6131 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6132 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6133 | } |
| 6134 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6135 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6136 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6137 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6138 | |
| 6139 | if (fallback) { |
| 6140 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6141 | keyEntry.eventTime = event.getEventTime(); |
| 6142 | keyEntry.deviceId = event.getDeviceId(); |
| 6143 | keyEntry.source = event.getSource(); |
| 6144 | keyEntry.displayId = event.getDisplayId(); |
| 6145 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6146 | keyEntry.keyCode = fallbackKeyCode; |
| 6147 | keyEntry.scanCode = event.getScanCode(); |
| 6148 | keyEntry.metaState = event.getMetaState(); |
| 6149 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6150 | keyEntry.downTime = event.getDownTime(); |
| 6151 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6152 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6153 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6154 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6155 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6156 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6157 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6158 | return true; // restart the event |
| 6159 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6160 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6161 | ALOGD("Unhandled key event: No fallback key."); |
| 6162 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6163 | |
| 6164 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6165 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6166 | } |
| 6167 | } |
| 6168 | return false; |
| 6169 | } |
| 6170 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6171 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6172 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6173 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6174 | return false; |
| 6175 | } |
| 6176 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6177 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6178 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6179 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6180 | } |
| 6181 | } |
| 6182 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6183 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6184 | if (ATRACE_ENABLED()) { |
| 6185 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6186 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6187 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6188 | } |
| 6189 | } |
| 6190 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6191 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6192 | if (ATRACE_ENABLED()) { |
| 6193 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6194 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6195 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6196 | } |
| 6197 | } |
| 6198 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6199 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6200 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6201 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6202 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6203 | dumpDispatchStateLocked(dump); |
| 6204 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6205 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6206 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6207 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6208 | } |
| 6209 | } |
| 6210 | |
| 6211 | void InputDispatcher::monitor() { |
| 6212 | // 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] | 6213 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6214 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6215 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6216 | } |
| 6217 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6218 | /** |
| 6219 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6220 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6221 | * this method can be safely called from any thread, as long as you've ensured that |
| 6222 | * the work you are interested in completing has already been queued. |
| 6223 | */ |
| 6224 | bool InputDispatcher::waitForIdle() { |
| 6225 | /** |
| 6226 | * Timeout should represent the longest possible time that a device might spend processing |
| 6227 | * events and commands. |
| 6228 | */ |
| 6229 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6230 | std::unique_lock lock(mLock); |
| 6231 | mLooper->wake(); |
| 6232 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6233 | return result == std::cv_status::no_timeout; |
| 6234 | } |
| 6235 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6236 | /** |
| 6237 | * Sets focus to the window identified by the token. This must be called |
| 6238 | * after updating any input window handles. |
| 6239 | * |
| 6240 | * Params: |
| 6241 | * request.token - input channel token used to identify the window that should gain focus. |
| 6242 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6243 | * specified token does not match the currently focused window, this request will be dropped. |
| 6244 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6245 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6246 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6247 | * when requesting the focus change. This determines which request gets |
| 6248 | * precedence if there is a focus change request from another source such as pointer down. |
| 6249 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6250 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6251 | { // acquire lock |
| 6252 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6253 | std::optional<FocusResolver::FocusChanges> changes = |
| 6254 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6255 | if (changes) { |
| 6256 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6257 | } |
| 6258 | } // release lock |
| 6259 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6260 | mLooper->wake(); |
| 6261 | } |
| 6262 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6263 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6264 | if (changes.oldFocus) { |
| 6265 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6266 | if (focusedInputChannel) { |
| 6267 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6268 | "focus left window"); |
| 6269 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6270 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6271 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6272 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6273 | if (changes.newFocus) { |
| 6274 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6275 | } |
| 6276 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6277 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6278 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6279 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6280 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6281 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6282 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6283 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6284 | // front. |
| 6285 | disablePointerCaptureForcedLocked(); |
| 6286 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6287 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6288 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6289 | } |
| 6290 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6291 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6292 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6293 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6294 | return; |
| 6295 | } |
| 6296 | |
| 6297 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6298 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6299 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6300 | setPointerCaptureLocked(false); |
| 6301 | } |
| 6302 | |
| 6303 | if (!mWindowTokenWithPointerCapture) { |
| 6304 | // No need to send capture changes because no window has capture. |
| 6305 | return; |
| 6306 | } |
| 6307 | |
| 6308 | if (mPendingEvent != nullptr) { |
| 6309 | // Move the pending event to the front of the queue. This will give the chance |
| 6310 | // for the pending event to be dropped if it is a captured event. |
| 6311 | mInboundQueue.push_front(mPendingEvent); |
| 6312 | mPendingEvent = nullptr; |
| 6313 | } |
| 6314 | |
| 6315 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6316 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6317 | mInboundQueue.push_front(std::move(entry)); |
| 6318 | } |
| 6319 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6320 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6321 | mCurrentPointerCaptureRequest.enable = enable; |
| 6322 | mCurrentPointerCaptureRequest.seq++; |
| 6323 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6324 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6325 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6326 | }; |
| 6327 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6328 | } |
| 6329 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6330 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6331 | { // acquire lock |
| 6332 | std::scoped_lock _l(mLock); |
| 6333 | // Set an empty list to remove all handles from the specific display. |
| 6334 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6335 | setFocusedApplicationLocked(displayId, nullptr); |
| 6336 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6337 | // have been removed for the removed display. |
| 6338 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6339 | // Reset pointer capture eligibility, regardless of previous state. |
| 6340 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6341 | } // release lock |
| 6342 | |
| 6343 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6344 | mLooper->wake(); |
| 6345 | } |
| 6346 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6347 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6348 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6349 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6350 | // more convenient parsing. |
| 6351 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6352 | for (const auto& info : windowInfos) { |
| 6353 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6354 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6355 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6356 | |
| 6357 | { // acquire lock |
| 6358 | std::scoped_lock _l(mLock); |
| 6359 | mDisplayInfos.clear(); |
| 6360 | for (const auto& displayInfo : displayInfos) { |
| 6361 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6362 | } |
| 6363 | |
| 6364 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6365 | setInputWindowsLocked(handles, displayId); |
| 6366 | } |
| 6367 | } |
| 6368 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6369 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6370 | } |
| 6371 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6372 | bool InputDispatcher::shouldDropInput( |
| 6373 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6374 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6375 | (windowHandle->getInfo()->inputConfig.test( |
| 6376 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6377 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6378 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6379 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6380 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6381 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6382 | windowHandle->getInfo()->displayId); |
| 6383 | return true; |
| 6384 | } |
| 6385 | return false; |
| 6386 | } |
| 6387 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6388 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6389 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6390 | const std::vector<DisplayInfo>& displayInfos) { |
| 6391 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6392 | } |
| 6393 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6394 | void InputDispatcher::cancelCurrentTouch() { |
| 6395 | { |
| 6396 | std::scoped_lock _l(mLock); |
| 6397 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
| 6398 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 6399 | "cancel current touch"); |
| 6400 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6401 | |
| 6402 | mTouchStatesByDisplay.clear(); |
| 6403 | mLastHoverWindowHandle.clear(); |
| 6404 | } |
| 6405 | // Wake up poll loop since there might be work to do. |
| 6406 | mLooper->wake(); |
| 6407 | } |
| 6408 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6409 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6410 | std::scoped_lock _l(mLock); |
| 6411 | mMonitorDispatchingTimeout = timeout; |
| 6412 | } |
| 6413 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6414 | } // namespace android::inputdispatcher |