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 | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 581 | } // namespace |
| 582 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 583 | // --- InputDispatcher --- |
| 584 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 585 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 586 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} |
| 587 | |
| 588 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy, |
| 589 | std::chrono::nanoseconds staleEventTimeout) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 590 | : mPolicy(policy), |
| 591 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 592 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 593 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 594 | mAppSwitchSawKeyDown(false), |
| 595 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 596 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 597 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 598 | mDispatchEnabled(false), |
| 599 | mDispatchFrozen(false), |
| 600 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 601 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 602 | // To avoid leaking stack in case that call never comes, and for tests, |
| 603 | // initialize it here anyways. |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 604 | mInTouchMode(kDefaultInTouchMode), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 605 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 606 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 607 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 608 | mStaleEventTimeout(staleEventTimeout), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 609 | mLatencyAggregator(), |
Siarhei Vishniakou | bd25272 | 2022-01-06 03:49:35 -0800 | [diff] [blame] | 610 | mLatencyTracker(&mLatencyAggregator) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 611 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 612 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 613 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 614 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 615 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 616 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 617 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 618 | |
| 619 | policy->getDispatcherConfiguration(&mConfig); |
| 620 | } |
| 621 | |
| 622 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 623 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 624 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 625 | resetKeyRepeatLocked(); |
| 626 | releasePendingEventLocked(); |
| 627 | drainInboundQueueLocked(); |
| 628 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 629 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 630 | while (!mConnectionsByToken.empty()) { |
| 631 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 632 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 633 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 637 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 638 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 639 | return ALREADY_EXISTS; |
| 640 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 641 | mThread = std::make_unique<InputThread>( |
| 642 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 643 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 647 | if (mThread && mThread->isCallingThread()) { |
| 648 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 649 | return INVALID_OPERATION; |
| 650 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 651 | mThread.reset(); |
| 652 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 655 | void InputDispatcher::dispatchOnce() { |
| 656 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 657 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 658 | std::scoped_lock _l(mLock); |
| 659 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 660 | |
| 661 | // Run a dispatch loop if there are no pending commands. |
| 662 | // The dispatch loop might enqueue commands to run afterwards. |
| 663 | if (!haveCommandsLocked()) { |
| 664 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 665 | } |
| 666 | |
| 667 | // Run all pending commands if there are any. |
| 668 | // 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] | 669 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 670 | nextWakeupTime = LONG_LONG_MIN; |
| 671 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 672 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 673 | // If we are still waiting for ack on some events, |
| 674 | // we might have to wake up earlier to check if an app is anr'ing. |
| 675 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 676 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 677 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 678 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 679 | // pending or queued events |
| 680 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 681 | mDispatcherEnteredIdle.notify_all(); |
| 682 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 683 | } // release lock |
| 684 | |
| 685 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 686 | nsecs_t currentTime = now(); |
| 687 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 688 | mLooper->pollOnce(timeoutMillis); |
| 689 | } |
| 690 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 691 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 692 | * Raise ANR if there is no focused window. |
| 693 | * Before the ANR is raised, do a final state check: |
| 694 | * 1. The currently focused application must be the same one we are waiting for. |
| 695 | * 2. Ensure we still don't have a focused window. |
| 696 | */ |
| 697 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 698 | // Check if the application that we are waiting for is still focused. |
| 699 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 700 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 701 | if (focusedApplication == nullptr || |
| 702 | focusedApplication->getApplicationToken() != |
| 703 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 704 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 705 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 706 | focusedApplication->getName().c_str()); |
| 707 | return; // The focused application has changed. |
| 708 | } |
| 709 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 710 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 711 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 712 | if (focusedWindowHandle != nullptr) { |
| 713 | return; // We now have a focused window. No need for ANR. |
| 714 | } |
| 715 | onAnrLocked(mAwaitedFocusedApplication); |
| 716 | } |
| 717 | |
| 718 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 719 | * Check if any of the connections' wait queues have events that are too old. |
| 720 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 721 | * Return the time at which we should wake up next. |
| 722 | */ |
| 723 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 724 | const nsecs_t currentTime = now(); |
| 725 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 726 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 727 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 728 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 729 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 730 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 731 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 732 | return LONG_LONG_MIN; |
| 733 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 734 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 735 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | // Check if any connection ANRs are due |
| 740 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 741 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 742 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 743 | } |
| 744 | |
| 745 | // If we reached here, we have an unresponsive connection. |
| 746 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 747 | if (connection == nullptr) { |
| 748 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 749 | return nextAnrCheck; |
| 750 | } |
| 751 | connection->responsive = false; |
| 752 | // Stop waking up for this unresponsive connection |
| 753 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 754 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 755 | return LONG_LONG_MIN; |
| 756 | } |
| 757 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 758 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
| 759 | const sp<Connection>& connection) { |
| 760 | if (connection->monitor) { |
| 761 | return mMonitorDispatchingTimeout; |
| 762 | } |
| 763 | const sp<WindowInfoHandle> window = |
| 764 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 765 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 766 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 767 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 768 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 771 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 772 | nsecs_t currentTime = now(); |
| 773 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 774 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 775 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 776 | // repeat if the device is just coming out of sleep. |
| 777 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 778 | resetKeyRepeatLocked(); |
| 779 | } |
| 780 | |
| 781 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 782 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 783 | if (DEBUG_FOCUS) { |
| 784 | ALOGD("Dispatch frozen. Waiting some more."); |
| 785 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 786 | return; |
| 787 | } |
| 788 | |
| 789 | // Optimize latency of app switches. |
| 790 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 791 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 792 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 793 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 794 | *nextWakeupTime = mAppSwitchDueTime; |
| 795 | } |
| 796 | |
| 797 | // Ready to start a new event. |
| 798 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 799 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 800 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 801 | if (isAppSwitchDue) { |
| 802 | // The inbound queue is empty so the app switch key we were waiting |
| 803 | // for will never arrive. Stop waiting for it. |
| 804 | resetPendingAppSwitchLocked(false); |
| 805 | isAppSwitchDue = false; |
| 806 | } |
| 807 | |
| 808 | // Synthesize a key repeat if appropriate. |
| 809 | if (mKeyRepeatState.lastKeyEntry) { |
| 810 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 811 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 812 | } else { |
| 813 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 814 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | // Nothing to do if there is no pending event. |
| 820 | if (!mPendingEvent) { |
| 821 | return; |
| 822 | } |
| 823 | } else { |
| 824 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 825 | mPendingEvent = mInboundQueue.front(); |
| 826 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 827 | traceInboundQueueLengthLocked(); |
| 828 | } |
| 829 | |
| 830 | // Poke user activity for this event. |
| 831 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 832 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 833 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | // Now we have an event to dispatch. |
| 837 | // 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] | 838 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 839 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 840 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 841 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 842 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 843 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 844 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 848 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 852 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 853 | const ConfigurationChangedEntry& typedEntry = |
| 854 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 855 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 856 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 857 | break; |
| 858 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 859 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 860 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 861 | const DeviceResetEntry& typedEntry = |
| 862 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 863 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 864 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 865 | break; |
| 866 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 867 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 868 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 869 | std::shared_ptr<FocusEntry> typedEntry = |
| 870 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 871 | dispatchFocusLocked(currentTime, typedEntry); |
| 872 | done = true; |
| 873 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 874 | break; |
| 875 | } |
| 876 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 877 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 878 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 879 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 880 | done = true; |
| 881 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 882 | break; |
| 883 | } |
| 884 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 885 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 886 | const auto typedEntry = |
| 887 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 888 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 889 | done = true; |
| 890 | break; |
| 891 | } |
| 892 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 893 | case EventEntry::Type::DRAG: { |
| 894 | std::shared_ptr<DragEntry> typedEntry = |
| 895 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 896 | dispatchDragLocked(currentTime, typedEntry); |
| 897 | done = true; |
| 898 | break; |
| 899 | } |
| 900 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 901 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 902 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 903 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 904 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 905 | resetPendingAppSwitchLocked(true); |
| 906 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 907 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 908 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 909 | } |
| 910 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 911 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 912 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 913 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 914 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 915 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 916 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 917 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 918 | break; |
| 919 | } |
| 920 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 921 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 922 | std::shared_ptr<MotionEntry> motionEntry = |
| 923 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 924 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 925 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 926 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 927 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 928 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 929 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 930 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 931 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 932 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 933 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 934 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 935 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 936 | |
| 937 | case EventEntry::Type::SENSOR: { |
| 938 | std::shared_ptr<SensorEntry> sensorEntry = |
| 939 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 940 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 941 | dropReason = DropReason::APP_SWITCH; |
| 942 | } |
| 943 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 944 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 945 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 946 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 947 | dropReason = DropReason::STALE; |
| 948 | } |
| 949 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 950 | done = true; |
| 951 | break; |
| 952 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 956 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 957 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 958 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 959 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 960 | |
| 961 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 962 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 966 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 967 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 968 | } |
| 969 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 970 | /** |
| 971 | * Return true if the events preceding this incoming motion event should be dropped |
| 972 | * Return false otherwise (the default behaviour) |
| 973 | */ |
| 974 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 975 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 976 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 977 | |
| 978 | // Optimize case where the current application is unresponsive and the user |
| 979 | // decides to touch a window in a different application. |
| 980 | // If the application takes too long to catch up then we drop all events preceding |
| 981 | // the touch into the other window. |
| 982 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 983 | int32_t displayId = motionEntry.displayId; |
| 984 | int32_t x = static_cast<int32_t>( |
| 985 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 986 | int32_t y = static_cast<int32_t>( |
| 987 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 988 | |
| 989 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 990 | sp<WindowInfoHandle> touchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 991 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 992 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 993 | touchedWindowHandle->getApplicationToken() != |
| 994 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 995 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 996 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 997 | "for %s", |
| 998 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 999 | return true; |
| 1000 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1001 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1002 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1003 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1004 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1005 | for (const auto& windowHandle : touchedSpies) { |
| 1006 | const sp<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1007 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1008 | // This spy window could take more input. Drop all events preceding this |
| 1009 | // 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] | 1010 | 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] | 1011 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1012 | mAwaitedFocusedApplication->getName().c_str()); |
| 1013 | return true; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 1019 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 1020 | // events to be processed before dispatching the key event. This is because these motion events |
| 1021 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 1022 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 1023 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1024 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1025 | "just send the pending key event to the focused window."); |
| 1026 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1027 | } |
| 1028 | return false; |
| 1029 | } |
| 1030 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1031 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1032 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1033 | mInboundQueue.push_back(std::move(newEntry)); |
| 1034 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1035 | traceInboundQueueLengthLocked(); |
| 1036 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1037 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1038 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1039 | // Optimize app switch latency. |
| 1040 | // If the application takes too long to catch up then we drop all events preceding |
| 1041 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1042 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1043 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1044 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1045 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1046 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1047 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1048 | if (DEBUG_APP_SWITCH) { |
| 1049 | ALOGD("App switch is pending!"); |
| 1050 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1051 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1052 | mAppSwitchSawKeyDown = false; |
| 1053 | needWake = true; |
| 1054 | } |
| 1055 | } |
| 1056 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1057 | |
| 1058 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1059 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1060 | // time for it may have been handled in the policy and could be dropped. |
| 1061 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1062 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1063 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1064 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1065 | pendingKey.interceptKeyResult == |
| 1066 | KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1067 | pendingKey.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1068 | pendingKey.interceptKeyWakeupTime = 0; |
| 1069 | needWake = true; |
| 1070 | } |
| 1071 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1072 | break; |
| 1073 | } |
| 1074 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1075 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1076 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1077 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1078 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1079 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1080 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1081 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1082 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1083 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1084 | break; |
| 1085 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1086 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1087 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1088 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1089 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1090 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1091 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1092 | // nothing to do |
| 1093 | break; |
| 1094 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | return needWake; |
| 1098 | } |
| 1099 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1100 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1101 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1102 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1103 | mRecentQueue.push_back(entry); |
| 1104 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1105 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1106 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1110 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1111 | int32_t y, TouchState* touchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1112 | bool isStylus, |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1113 | bool addOutsideTargets, |
| 1114 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1115 | if (addOutsideTargets && touchState == nullptr) { |
| 1116 | 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] | 1117 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1118 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1119 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1120 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1121 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1122 | continue; |
| 1123 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1124 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1125 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1126 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1127 | return windowHandle; |
| 1128 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1129 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1130 | if (addOutsideTargets && |
| 1131 | info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1132 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1133 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1134 | } |
| 1135 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1136 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1139 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1140 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1141 | // Traverse windows from front to back and gather the touched spy windows. |
| 1142 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1143 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1144 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1145 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1146 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1147 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1148 | continue; |
| 1149 | } |
| 1150 | if (!info.isSpy()) { |
| 1151 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1152 | return spyWindows; |
| 1153 | } |
| 1154 | spyWindows.push_back(windowHandle); |
| 1155 | } |
| 1156 | return spyWindows; |
| 1157 | } |
| 1158 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1159 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1160 | const char* reason; |
| 1161 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1162 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1163 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1164 | ALOGD("Dropped event because policy consumed it."); |
| 1165 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1166 | reason = "inbound event was dropped because the policy consumed it"; |
| 1167 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1168 | case DropReason::DISABLED: |
| 1169 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1170 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1171 | } |
| 1172 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1173 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1174 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1175 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1176 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1177 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1178 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1179 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1180 | "has started interacting with a different application."); |
| 1181 | reason = "inbound event was dropped because the current application is not responding " |
| 1182 | "and the user has started interacting with a different application"; |
| 1183 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1184 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1185 | ALOGI("Dropped event because it is stale."); |
| 1186 | reason = "inbound event was dropped because it is stale"; |
| 1187 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1188 | case DropReason::NO_POINTER_CAPTURE: |
| 1189 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1190 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1191 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1192 | case DropReason::NOT_DROPPED: { |
| 1193 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1194 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1195 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1198 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1199 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1201 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1202 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1203 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1204 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1205 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1206 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1207 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1208 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1209 | } else { |
| 1210 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1211 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1212 | } |
| 1213 | break; |
| 1214 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1215 | case EventEntry::Type::SENSOR: { |
| 1216 | break; |
| 1217 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1218 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1219 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1220 | break; |
| 1221 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1222 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1223 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1224 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1225 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1226 | 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] | 1227 | break; |
| 1228 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1232 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1233 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1234 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1237 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1238 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1239 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1240 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1244 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1245 | } |
| 1246 | |
| 1247 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1248 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1249 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1250 | if (DEBUG_APP_SWITCH) { |
| 1251 | if (handled) { |
| 1252 | ALOGD("App switch has arrived."); |
| 1253 | } else { |
| 1254 | ALOGD("App switch was abandoned."); |
| 1255 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1256 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1257 | } |
| 1258 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1259 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1260 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1263 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1264 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1265 | return false; |
| 1266 | } |
| 1267 | |
| 1268 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1269 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1270 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1271 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1272 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1273 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1274 | return true; |
| 1275 | } |
| 1276 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1277 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1278 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1282 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1283 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1284 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1285 | releaseInboundEventLocked(entry); |
| 1286 | } |
| 1287 | traceInboundQueueLengthLocked(); |
| 1288 | } |
| 1289 | |
| 1290 | void InputDispatcher::releasePendingEventLocked() { |
| 1291 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1292 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1293 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1297 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1298 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1299 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1300 | if (DEBUG_DISPATCH_CYCLE) { |
| 1301 | ALOGD("Injected inbound event was dropped."); |
| 1302 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1303 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1304 | } |
| 1305 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1306 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1307 | } |
| 1308 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1312 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1313 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1314 | } |
| 1315 | } |
| 1316 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1317 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1318 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1319 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1320 | uint32_t policyFlags = entry->policyFlags & |
| 1321 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1322 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1323 | std::shared_ptr<KeyEntry> newEntry = |
| 1324 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1325 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1326 | entry->flags, entry->keyCode, entry->scanCode, |
| 1327 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1328 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1329 | newEntry->syntheticRepeat = true; |
| 1330 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1331 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1332 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1335 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1336 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1337 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1338 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1339 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1340 | |
| 1341 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1342 | resetKeyRepeatLocked(); |
| 1343 | |
| 1344 | // 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] | 1345 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1346 | scoped_unlock unlock(mLock); |
| 1347 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1348 | }; |
| 1349 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1350 | return true; |
| 1351 | } |
| 1352 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1353 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1354 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1355 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1356 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1357 | entry.deviceId); |
| 1358 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1359 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1360 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1361 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1362 | resetKeyRepeatLocked(); |
| 1363 | } |
| 1364 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1365 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1366 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1367 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1368 | return true; |
| 1369 | } |
| 1370 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1371 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1372 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1373 | if (mPendingEvent != nullptr) { |
| 1374 | // Move the pending event to the front of the queue. This will give the chance |
| 1375 | // for the pending event to get dispatched to the newly focused window |
| 1376 | mInboundQueue.push_front(mPendingEvent); |
| 1377 | mPendingEvent = nullptr; |
| 1378 | } |
| 1379 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1380 | std::unique_ptr<FocusEntry> focusEntry = |
| 1381 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1382 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1383 | |
| 1384 | // This event should go to the front of the queue, but behind all other focus events |
| 1385 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1386 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1387 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1388 | [](const std::shared_ptr<EventEntry>& event) { |
| 1389 | return event->type == EventEntry::Type::FOCUS; |
| 1390 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1391 | |
| 1392 | // 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] | 1393 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1394 | } |
| 1395 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1396 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1397 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1398 | if (channel == nullptr) { |
| 1399 | return; // Window has gone away |
| 1400 | } |
| 1401 | InputTarget target; |
| 1402 | target.inputChannel = channel; |
| 1403 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1404 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1405 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1406 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1407 | std::string reason = std::string("reason=").append(entry->reason); |
| 1408 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1409 | dispatchEventLocked(currentTime, entry, {target}); |
| 1410 | } |
| 1411 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1412 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1413 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1414 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1415 | dropReason = DropReason::NOT_DROPPED; |
| 1416 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1417 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1418 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1419 | |
| 1420 | if (entry->pointerCaptureRequest.enable) { |
| 1421 | // Enable Pointer Capture. |
| 1422 | if (haveWindowWithPointerCapture && |
| 1423 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame^] | 1424 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1425 | // app of the state change, so there is no need to notify the app. |
| 1426 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1427 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1428 | } |
| 1429 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1430 | // This can happen if a window requests capture and immediately releases capture. |
| 1431 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1432 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1433 | return; |
| 1434 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1435 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1436 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1437 | return; |
| 1438 | } |
| 1439 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1440 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1441 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1442 | mWindowTokenWithPointerCapture = token; |
| 1443 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1444 | // Disable Pointer Capture. |
| 1445 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1446 | // for two reasons: |
| 1447 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1448 | // to disable capture with the same sequence number: one generated by |
| 1449 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1450 | // Capture being disabled in InputReader. |
| 1451 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1452 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1453 | // in InputReader. |
| 1454 | if (!haveWindowWithPointerCapture) { |
| 1455 | // Pointer capture was already forcefully disabled because of focus change. |
| 1456 | dropReason = DropReason::NOT_DROPPED; |
| 1457 | return; |
| 1458 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1459 | token = mWindowTokenWithPointerCapture; |
| 1460 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1461 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1462 | setPointerCaptureLocked(false); |
| 1463 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | auto channel = getInputChannelLocked(token); |
| 1467 | if (channel == nullptr) { |
| 1468 | // Window has gone away, clean up Pointer Capture state. |
| 1469 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1470 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1471 | setPointerCaptureLocked(false); |
| 1472 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1473 | return; |
| 1474 | } |
| 1475 | InputTarget target; |
| 1476 | target.inputChannel = channel; |
| 1477 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1478 | entry->dispatchInProgress = true; |
| 1479 | dispatchEventLocked(currentTime, entry, {target}); |
| 1480 | |
| 1481 | dropReason = DropReason::NOT_DROPPED; |
| 1482 | } |
| 1483 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1484 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1485 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1486 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1487 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1488 | if (windowHandles.empty()) { |
| 1489 | return; |
| 1490 | } |
| 1491 | const std::vector<InputTarget> inputTargets = |
| 1492 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1493 | if (inputTargets.empty()) { |
| 1494 | return; |
| 1495 | } |
| 1496 | entry->dispatchInProgress = true; |
| 1497 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1498 | } |
| 1499 | |
| 1500 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1501 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1502 | std::vector<InputTarget> inputTargets; |
| 1503 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1504 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1505 | const sp<IBinder>& token = handle->getToken(); |
| 1506 | if (token == nullptr) { |
| 1507 | continue; |
| 1508 | } |
| 1509 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1510 | if (channel == nullptr) { |
| 1511 | continue; // Window has gone away |
| 1512 | } |
| 1513 | InputTarget target; |
| 1514 | target.inputChannel = channel; |
| 1515 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1516 | inputTargets.push_back(target); |
| 1517 | } |
| 1518 | return inputTargets; |
| 1519 | } |
| 1520 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1521 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1522 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1523 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1524 | if (!entry->dispatchInProgress) { |
| 1525 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1526 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1527 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1528 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1529 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1530 | // We have seen two identical key downs in a row which indicates that the device |
| 1531 | // driver is automatically generating key repeats itself. We take note of the |
| 1532 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1533 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1534 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1535 | // Make sure we don't get key down from a different device. If a different |
| 1536 | // device Id has same key pressed down, the new device Id will replace the |
| 1537 | // current one to hold the key repeat with repeat count reset. |
| 1538 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1539 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1540 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1541 | resetKeyRepeatLocked(); |
| 1542 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1543 | } else { |
| 1544 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1545 | resetKeyRepeatLocked(); |
| 1546 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1547 | } |
| 1548 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1549 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1550 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1551 | // 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] | 1552 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1553 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1554 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1555 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1556 | resetKeyRepeatLocked(); |
| 1557 | } |
| 1558 | |
| 1559 | if (entry->repeatCount == 1) { |
| 1560 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1561 | } else { |
| 1562 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1563 | } |
| 1564 | |
| 1565 | entry->dispatchInProgress = true; |
| 1566 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1567 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | // Handle case where the policy asked us to try again later last time. |
| 1571 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1572 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1573 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1574 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1575 | } |
| 1576 | return false; // wait until next wakeup |
| 1577 | } |
| 1578 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1579 | entry->interceptKeyWakeupTime = 0; |
| 1580 | } |
| 1581 | |
| 1582 | // Give the policy a chance to intercept the key. |
| 1583 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1584 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1585 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1586 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1587 | |
| 1588 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1589 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1590 | }; |
| 1591 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1592 | return false; // wait for the command to run |
| 1593 | } else { |
| 1594 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1595 | } |
| 1596 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1597 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1598 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1603 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1604 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1605 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1606 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1607 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1608 | return true; |
| 1609 | } |
| 1610 | |
| 1611 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1612 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1613 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1614 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1615 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1616 | return false; |
| 1617 | } |
| 1618 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1619 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1620 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1621 | return true; |
| 1622 | } |
| 1623 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1624 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1625 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1626 | |
| 1627 | // Dispatch the key. |
| 1628 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1629 | return true; |
| 1630 | } |
| 1631 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1632 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1633 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1634 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1635 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1636 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1637 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1638 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1639 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1640 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1641 | } |
| 1642 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1643 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1644 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1645 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1646 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1647 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1648 | "source=0x%x, sensorType=%s", |
| 1649 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1650 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1651 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1652 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1653 | scoped_unlock unlock(mLock); |
| 1654 | |
| 1655 | if (entry->accuracyChanged) { |
| 1656 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1657 | } |
| 1658 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1659 | entry->hwTimestamp, entry->values); |
| 1660 | }; |
| 1661 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1665 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1666 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1667 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1668 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1669 | { // acquire lock |
| 1670 | std::scoped_lock _l(mLock); |
| 1671 | |
| 1672 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1673 | std::shared_ptr<EventEntry> entry = *it; |
| 1674 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1675 | it = mInboundQueue.erase(it); |
| 1676 | releaseInboundEventLocked(entry); |
| 1677 | } |
| 1678 | } |
| 1679 | } |
| 1680 | return true; |
| 1681 | } |
| 1682 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1683 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1684 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1685 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1686 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1687 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1688 | entry->dispatchInProgress = true; |
| 1689 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1690 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1694 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1695 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1696 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1697 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1698 | return true; |
| 1699 | } |
| 1700 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1701 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1702 | |
| 1703 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1704 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1705 | |
| 1706 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1707 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1708 | if (isPointerEvent) { |
| 1709 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1710 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1711 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1712 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1713 | } else { |
| 1714 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1715 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1716 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1717 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1718 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1719 | return false; |
| 1720 | } |
| 1721 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1722 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 1723 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
| 1724 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1725 | return true; |
| 1726 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1727 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1728 | CancelationOptions::Mode mode(isPointerEvent |
| 1729 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1730 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1731 | CancelationOptions options(mode, "input event injection failed"); |
| 1732 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1733 | return true; |
| 1734 | } |
| 1735 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1736 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1737 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1738 | |
| 1739 | // Dispatch the motion. |
| 1740 | if (conflictingPointerActions) { |
| 1741 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1742 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1743 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1744 | } |
| 1745 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1746 | return true; |
| 1747 | } |
| 1748 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1749 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1750 | bool isExiting, const MotionEntry& motionEntry) { |
| 1751 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1752 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1753 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1754 | PointerCoords pointerCoords; |
| 1755 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1756 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1757 | |
| 1758 | std::unique_ptr<DragEntry> dragEntry = |
| 1759 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1760 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1761 | pointerCoords.getY()); |
| 1762 | |
| 1763 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1764 | } |
| 1765 | |
| 1766 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1767 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1768 | if (channel == nullptr) { |
| 1769 | return; // Window has gone away |
| 1770 | } |
| 1771 | InputTarget target; |
| 1772 | target.inputChannel = channel; |
| 1773 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1774 | entry->dispatchInProgress = true; |
| 1775 | dispatchEventLocked(currentTime, entry, {target}); |
| 1776 | } |
| 1777 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1778 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1779 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1780 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1781 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1782 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1783 | "metaState=0x%x, buttonState=0x%x," |
| 1784 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1785 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1786 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1787 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1788 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1789 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1790 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1791 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1792 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1793 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1794 | "orientation=%f", |
| 1795 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1796 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1797 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1798 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1799 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1800 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1801 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1802 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1803 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1804 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1805 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1806 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1807 | } |
| 1808 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1809 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1810 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1811 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1812 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1813 | if (DEBUG_DISPATCH_CYCLE) { |
| 1814 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1815 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1816 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1817 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1818 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1819 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1820 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1821 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1822 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1823 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1824 | sp<Connection> connection = |
| 1825 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1826 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1827 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1828 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1829 | if (DEBUG_FOCUS) { |
| 1830 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1831 | "is no longer registered with the input dispatcher.", |
| 1832 | inputTarget.inputChannel->getName().c_str()); |
| 1833 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1834 | } |
| 1835 | } |
| 1836 | } |
| 1837 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1838 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1839 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1840 | // If the policy decides to close the app, we will get a channel removal event via |
| 1841 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1842 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1843 | // pile up. |
| 1844 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1845 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1846 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1847 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1848 | "application not responding"); |
| 1849 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1853 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1854 | if (DEBUG_FOCUS) { |
| 1855 | ALOGD("Resetting ANR timeouts."); |
| 1856 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1857 | |
| 1858 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1859 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1860 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1861 | } |
| 1862 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1863 | /** |
| 1864 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1865 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1866 | * Focused display is the display that the user most recently interacted with. |
| 1867 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1868 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1869 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1870 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1871 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1872 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1873 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1874 | break; |
| 1875 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1876 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1877 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1878 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1879 | break; |
| 1880 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1881 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1882 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1883 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1884 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1885 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1886 | case EventEntry::Type::SENSOR: |
| 1887 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1888 | 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] | 1889 | return ADISPLAY_ID_NONE; |
| 1890 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1891 | } |
| 1892 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1893 | } |
| 1894 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1895 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1896 | const char* focusedWindowName) { |
| 1897 | if (mAnrTracker.empty()) { |
| 1898 | // already processed all events that we waited for |
| 1899 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1900 | return false; |
| 1901 | } |
| 1902 | |
| 1903 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1904 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1905 | // 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] | 1906 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1907 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1908 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1909 | return true; |
| 1910 | } |
| 1911 | |
| 1912 | // We still have pending events, and already started the timer |
| 1913 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1914 | return true; // Still waiting |
| 1915 | } |
| 1916 | |
| 1917 | // Waited too long, and some connection still hasn't processed all motions |
| 1918 | // Just send the key to the focused window |
| 1919 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1920 | focusedWindowName); |
| 1921 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1922 | return false; |
| 1923 | } |
| 1924 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1925 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1926 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1927 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1928 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1929 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1930 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1931 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1932 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1933 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1934 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1935 | // If there is no currently focused window and no focused application |
| 1936 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1937 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1938 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1939 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1940 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1941 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1942 | } |
| 1943 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1944 | // Drop key events if requested by input feature |
| 1945 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1946 | return InputEventInjectionResult::FAILED; |
| 1947 | } |
| 1948 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1949 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1950 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1951 | // start interacting with another application via touch (app switch). This code can be removed |
| 1952 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1953 | // an app is expected to have a focused window. |
| 1954 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1955 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1956 | // 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] | 1957 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1958 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1959 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1960 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1961 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1962 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1963 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1964 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1965 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1966 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1967 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1968 | // Already raised ANR. Drop the event |
| 1969 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1970 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1971 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1972 | } else { |
| 1973 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1974 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | // we have a valid, non-null focused window |
| 1979 | resetNoFocusedWindowTimeoutLocked(); |
| 1980 | |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 1981 | // Check permissions. |
| 1982 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
| 1983 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1984 | } |
| 1985 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1986 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 1987 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1988 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1989 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | // If the event is a key event, then we must wait for all previous events to |
| 1993 | // complete before delivering it because previous events may have the |
| 1994 | // side-effect of transferring focus to a different window and we want to |
| 1995 | // ensure that the following keys are sent to the new window. |
| 1996 | // |
| 1997 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1998 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1999 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2000 | // often anticipate pending UI changes when typing on a keyboard. |
| 2001 | // To obtain this behavior, we must serialize key events with respect to all |
| 2002 | // prior input events. |
| 2003 | if (entry.type == EventEntry::Type::KEY) { |
| 2004 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 2005 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2006 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2007 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2011 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2012 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2013 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2014 | |
| 2015 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2016 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2017 | } |
| 2018 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2019 | /** |
| 2020 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2021 | * that are currently unresponsive. |
| 2022 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2023 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2024 | const std::vector<Monitor>& monitors) const { |
| 2025 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2026 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2027 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 2028 | sp<Connection> connection = |
| 2029 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2030 | if (connection == nullptr) { |
| 2031 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2032 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2033 | return false; |
| 2034 | } |
| 2035 | if (!connection->responsive) { |
| 2036 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2037 | connection->inputChannel->getName().c_str()); |
| 2038 | return false; |
| 2039 | } |
| 2040 | return true; |
| 2041 | }); |
| 2042 | return responsiveMonitors; |
| 2043 | } |
| 2044 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2045 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 2046 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 2047 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2048 | ATRACE_CALL(); |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 2049 | enum InjectionPermission { |
| 2050 | INJECTION_PERMISSION_UNKNOWN, |
| 2051 | INJECTION_PERMISSION_GRANTED, |
| 2052 | INJECTION_PERMISSION_DENIED |
| 2053 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2054 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2055 | // For security reasons, we defer updating the touch state until we are sure that |
| 2056 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2057 | const int32_t displayId = entry.displayId; |
| 2058 | const int32_t action = entry.action; |
| 2059 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2060 | |
| 2061 | // 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] | 2062 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 2063 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2064 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 2065 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2066 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2067 | // Copy current touch state into tempTouchState. |
| 2068 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2069 | // 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] | 2070 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2071 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2072 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2073 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2074 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2075 | } |
| 2076 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2077 | bool isSplit = tempTouchState.split; |
| 2078 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 2079 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 2080 | tempTouchState.displayId != displayId); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2081 | |
| 2082 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2083 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2084 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2085 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2086 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2087 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2088 | bool wrongDevice = false; |
| 2089 | if (newGesture) { |
| 2090 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2091 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2092 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2093 | "in display %" PRId32, |
| 2094 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2095 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2096 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2097 | switchedDevice = false; |
| 2098 | wrongDevice = true; |
| 2099 | goto Failed; |
| 2100 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2101 | tempTouchState.reset(); |
| 2102 | tempTouchState.down = down; |
| 2103 | tempTouchState.deviceId = entry.deviceId; |
| 2104 | tempTouchState.source = entry.source; |
| 2105 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2106 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2107 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2108 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2109 | "in display %" PRId32, |
| 2110 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2111 | // TODO: test multiple simultaneous input streams. |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 2112 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2113 | switchedDevice = false; |
| 2114 | wrongDevice = true; |
| 2115 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2119 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2120 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2121 | int32_t x; |
| 2122 | int32_t y; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2123 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2124 | // Always dispatch mouse events to cursor position. |
| 2125 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2126 | x = int32_t(entry.xCursorPosition); |
| 2127 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2128 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2129 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2130 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2131 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2132 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2133 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2134 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2135 | isStylus, isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2136 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2137 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2138 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2139 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2140 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2141 | // 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] | 2142 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2145 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2146 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2147 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2148 | // New window supports splitting, but we should never split mouse events. |
| 2149 | isSplit = !isFromMouse; |
| 2150 | } else if (isSplit) { |
| 2151 | // New window does not support splitting but we have already split events. |
| 2152 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2153 | newTouchedWindowHandle = nullptr; |
| 2154 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2155 | } else { |
| 2156 | // 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] | 2157 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2158 | // should never be split. |
| 2159 | tempTouchState.split = isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2160 | } |
| 2161 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2162 | // Update hover state. |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2163 | if (newTouchedWindowHandle != nullptr) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2164 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2165 | newHoverWindowHandle = nullptr; |
| 2166 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2167 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2168 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2169 | } |
| 2170 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2171 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2172 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2173 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2174 | // Process the foreground window first so that it is the first to receive the event. |
| 2175 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2176 | } |
| 2177 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2178 | if (newTouchedWindows.empty()) { |
| 2179 | ALOGI("Dropping event because there is no touchable window at (%d, %d) on display %d.", |
| 2180 | x, y, displayId); |
| 2181 | injectionResult = InputEventInjectionResult::FAILED; |
| 2182 | goto Failed; |
| 2183 | } |
| 2184 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2185 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
| 2186 | const WindowInfo& info = *windowHandle->getInfo(); |
| 2187 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2188 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2189 | ALOGI("Not sending touch event to %s because it is paused", |
| 2190 | windowHandle->getName().c_str()); |
| 2191 | continue; |
| 2192 | } |
| 2193 | |
| 2194 | // Ensure the window has a connection and the connection is responsive |
| 2195 | const bool isResponsive = hasResponsiveConnectionLocked(*windowHandle); |
| 2196 | if (!isResponsive) { |
| 2197 | ALOGW("Not sending touch gesture to %s because it is not responsive", |
| 2198 | windowHandle->getName().c_str()); |
| 2199 | continue; |
| 2200 | } |
| 2201 | |
| 2202 | // Drop events that can't be trusted due to occlusion |
| 2203 | if (mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2204 | TouchOcclusionInfo occlusionInfo = |
| 2205 | computeTouchOcclusionInfoLocked(windowHandle, x, y); |
| 2206 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 2207 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2208 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2209 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2210 | ALOGD("%s", log.c_str()); |
| 2211 | } |
| 2212 | } |
| 2213 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
| 2214 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2215 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2216 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2217 | continue; |
| 2218 | } |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | // Drop touch events if requested by input feature |
| 2223 | if (shouldDropInput(entry, windowHandle)) { |
| 2224 | continue; |
| 2225 | } |
| 2226 | |
| 2227 | // Set target flags. |
| 2228 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2229 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2230 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2231 | // 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] | 2232 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2233 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2234 | |
| 2235 | if (isSplit) { |
| 2236 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2237 | } |
| 2238 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
| 2239 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2240 | } else if (isWindowObscuredLocked(windowHandle)) { |
| 2241 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2242 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2243 | |
| 2244 | // Update the temporary touch state. |
| 2245 | BitSet32 pointerIds; |
| 2246 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2247 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2248 | pointerIds.markBit(pointerId); |
| 2249 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2250 | |
| 2251 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2252 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2253 | } else { |
| 2254 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2255 | |
| 2256 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2257 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2258 | if (DEBUG_FOCUS) { |
| 2259 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2260 | "dropped the pointer down event in display %" PRId32, |
| 2261 | displayId); |
| 2262 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2263 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2264 | goto Failed; |
| 2265 | } |
| 2266 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2267 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2268 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2269 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2270 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2271 | tempTouchState.isSlippery()) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2272 | const int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2273 | 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] | 2274 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2275 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2276 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2277 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2278 | newTouchedWindowHandle = |
| 2279 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2280 | |
| 2281 | // Drop touch events if requested by input feature |
| 2282 | if (newTouchedWindowHandle != nullptr && |
| 2283 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2284 | newTouchedWindowHandle = nullptr; |
| 2285 | } |
| 2286 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2287 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2288 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2289 | if (DEBUG_FOCUS) { |
| 2290 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2291 | oldTouchedWindowHandle->getName().c_str(), |
| 2292 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2293 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2294 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2295 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2296 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2297 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2298 | |
| 2299 | // Make a slippery entrance into the new window. |
| 2300 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2301 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2302 | } |
| 2303 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2304 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
| 2305 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
| 2306 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2307 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2308 | if (isSplit) { |
| 2309 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2310 | } |
| 2311 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2312 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2313 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2314 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | BitSet32 pointerIds; |
| 2318 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2319 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2320 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2321 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2322 | } |
| 2323 | } |
| 2324 | } |
| 2325 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2326 | // Update dispatching for hover enter and exit. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2327 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2328 | // Let the previous window know that the hover sequence is over, unless we already did |
| 2329 | // it when dispatching it as is to newTouchedWindowHandle. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2330 | if (mLastHoverWindowHandle != nullptr && |
| 2331 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2332 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2333 | if (DEBUG_HOVER) { |
| 2334 | ALOGD("Sending hover exit event to window %s.", |
| 2335 | mLastHoverWindowHandle->getName().c_str()); |
| 2336 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2337 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2338 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2339 | } |
| 2340 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2341 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2342 | // when dispatching it as is to newTouchedWindowHandle. |
| 2343 | if (newHoverWindowHandle != nullptr && |
| 2344 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2345 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2346 | if (DEBUG_HOVER) { |
| 2347 | ALOGD("Sending hover enter event to window %s.", |
| 2348 | newHoverWindowHandle->getName().c_str()); |
| 2349 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2350 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2351 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2352 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2353 | } |
| 2354 | } |
| 2355 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2356 | // Ensure that we have at least one foreground window or at least one window that cannot be a |
| 2357 | // foreground target. If we only have windows that are not receiving foreground touches (e.g. we |
| 2358 | // only have windows getting ACTION_OUTSIDE), then drop the event, because there is no window |
| 2359 | // that is actually receiving the entire gesture. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2360 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2361 | [](const TouchedWindow& touchedWindow) { |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2362 | return !canReceiveForegroundTouches( |
| 2363 | *touchedWindow.windowHandle->getInfo()) || |
| 2364 | (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2365 | })) { |
Siarhei Vishniakou | 1fb1891 | 2022-03-08 10:31:39 -0800 | [diff] [blame] | 2366 | ALOGI("Dropping event because there is no touched window on display %d to receive it: %s", |
| 2367 | displayId, entry.getDescription().c_str()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2368 | injectionResult = InputEventInjectionResult::FAILED; |
| 2369 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2370 | } |
| 2371 | |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 2372 | // Check permission to inject into all touched foreground windows. |
| 2373 | if (std::any_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2374 | [this, &entry](const TouchedWindow& touchedWindow) { |
| 2375 | return (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0 && |
| 2376 | !checkInjectionPermission(touchedWindow.windowHandle, |
| 2377 | entry.injectionState); |
| 2378 | })) { |
| 2379 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
| 2380 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2381 | goto Failed; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2382 | } |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 2383 | // Permission granted to inject into all touched foreground windows. |
| 2384 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2385 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2386 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2387 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2388 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2389 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2390 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2391 | if (foregroundWindowHandle) { |
| 2392 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2393 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2394 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2395 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2396 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2397 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2398 | InputTarget::FLAG_ZERO_COORDS, |
| 2399 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2400 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | } |
| 2404 | } |
| 2405 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2406 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2407 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2408 | // of the touch gesture. |
| 2409 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2410 | // engine only supports touch events. We would need to add a mechanism similar |
| 2411 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2412 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2413 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2414 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2415 | if (foregroundWindowHandle && |
| 2416 | foregroundWindowHandle->getInfo()->inputConfig.test( |
| 2417 | WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2418 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2419 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2420 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2421 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2422 | if (info->displayId == displayId && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2423 | windowHandle->getInfo()->inputConfig.test( |
| 2424 | WindowInfo::InputConfig::IS_WALLPAPER)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2425 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2426 | .addOrUpdateWindow(windowHandle, |
| 2427 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2428 | InputTarget:: |
| 2429 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2430 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2431 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2432 | } |
| 2433 | } |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2438 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2439 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2440 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2441 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2442 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | // Drop the outside or hover touch windows since we will not care about them |
| 2446 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2447 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2448 | |
| 2449 | Failed: |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 2450 | // Check injection permission once and for all. |
| 2451 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
| 2452 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
| 2453 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2454 | } else { |
| 2455 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2460 | return injectionResult; |
| 2461 | } |
| 2462 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2463 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2464 | if (!wrongDevice) { |
| 2465 | if (switchedDevice) { |
| 2466 | if (DEBUG_FOCUS) { |
| 2467 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2468 | } |
| 2469 | *outConflictingPointerActions = true; |
| 2470 | } |
| 2471 | |
| 2472 | if (isHoverAction) { |
| 2473 | // Started hovering, therefore no longer down. |
| 2474 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2475 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2476 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2477 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2478 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2479 | *outConflictingPointerActions = true; |
| 2480 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2481 | tempTouchState.reset(); |
| 2482 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2483 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2484 | tempTouchState.deviceId = entry.deviceId; |
| 2485 | tempTouchState.source = entry.source; |
| 2486 | tempTouchState.displayId = displayId; |
| 2487 | } |
| 2488 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2489 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2490 | // All pointers up or canceled. |
| 2491 | tempTouchState.reset(); |
| 2492 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2493 | // First pointer went down. |
| 2494 | if (oldState && oldState->down) { |
| 2495 | if (DEBUG_FOCUS) { |
| 2496 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2497 | } |
| 2498 | *outConflictingPointerActions = true; |
| 2499 | } |
| 2500 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2501 | // One pointer went up. |
| 2502 | if (isSplit) { |
| 2503 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2504 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2505 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2506 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2507 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2508 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2509 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2510 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2511 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2512 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2513 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2514 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2515 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2516 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2517 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2518 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2519 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2520 | // Save changes unless the action was scroll in which case the temporary touch |
| 2521 | // state was only valid for this one action. |
| 2522 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2523 | if (tempTouchState.displayId >= 0) { |
| 2524 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2525 | } else { |
| 2526 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2527 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2528 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2529 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2530 | // Update hover state. |
| 2531 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2532 | } |
| 2533 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2534 | return injectionResult; |
| 2535 | } |
| 2536 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2537 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2538 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2539 | // have an explicit reason to support it. |
| 2540 | constexpr bool isStylus = false; |
| 2541 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2542 | const sp<WindowInfoHandle> dropWindow = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2543 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2544 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2545 | if (dropWindow) { |
| 2546 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2547 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2548 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2549 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2550 | } |
| 2551 | mDragState.reset(); |
| 2552 | } |
| 2553 | |
| 2554 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2555 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2556 | return; |
| 2557 | } |
| 2558 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2559 | if (!mDragState->isStartDrag) { |
| 2560 | mDragState->isStartDrag = true; |
| 2561 | mDragState->isStylusButtonDownAtStart = |
| 2562 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2563 | } |
| 2564 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2565 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2566 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2567 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2568 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2569 | // Handle the special case : stylus button no longer pressed. |
| 2570 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2571 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2572 | finishDragAndDrop(entry.displayId, x, y); |
| 2573 | return; |
| 2574 | } |
| 2575 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2576 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until |
| 2577 | // we have an explicit reason to support it. |
| 2578 | constexpr bool isStylus = false; |
| 2579 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2580 | const sp<WindowInfoHandle> hoverWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2581 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2582 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2583 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2584 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2585 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2586 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2587 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2588 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2589 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2590 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2591 | } |
| 2592 | // enqueue drag location if needed. |
| 2593 | if (hoverWindowHandle != nullptr) { |
| 2594 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2595 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2596 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2597 | finishDragAndDrop(entry.displayId, x, y); |
| 2598 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2599 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2600 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2601 | } |
| 2602 | } |
| 2603 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2604 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2605 | int32_t targetFlags, BitSet32 pointerIds, |
| 2606 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2607 | std::vector<InputTarget>::iterator it = |
| 2608 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2609 | [&windowHandle](const InputTarget& inputTarget) { |
| 2610 | return inputTarget.inputChannel->getConnectionToken() == |
| 2611 | windowHandle->getToken(); |
| 2612 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2613 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2614 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2615 | |
| 2616 | if (it == inputTargets.end()) { |
| 2617 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2618 | std::shared_ptr<InputChannel> inputChannel = |
| 2619 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2620 | if (inputChannel == nullptr) { |
| 2621 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2622 | return; |
| 2623 | } |
| 2624 | inputTarget.inputChannel = inputChannel; |
| 2625 | inputTarget.flags = targetFlags; |
| 2626 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2627 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2628 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2629 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2630 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2631 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2632 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2633 | inputTargets.push_back(inputTarget); |
| 2634 | it = inputTargets.end() - 1; |
| 2635 | } |
| 2636 | |
| 2637 | ALOG_ASSERT(it->flags == targetFlags); |
| 2638 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2639 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2640 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2641 | } |
| 2642 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2643 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2644 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2645 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2646 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2647 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2648 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2649 | InputTarget target; |
| 2650 | target.inputChannel = monitor.inputChannel; |
| 2651 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2652 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2653 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2654 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2655 | target.setDefaultPointerTransform(target.displayTransform); |
| 2656 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2657 | } |
| 2658 | } |
| 2659 | |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 2660 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
| 2661 | const InjectionState* injectionState) { |
| 2662 | if (injectionState && |
| 2663 | (windowHandle == nullptr || |
| 2664 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2665 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
| 2666 | if (windowHandle != nullptr) { |
| 2667 | ALOGW("Permission denied: injecting event from pid %d uid %d to window %s " |
| 2668 | "owned by uid %d", |
| 2669 | injectionState->injectorPid, injectionState->injectorUid, |
| 2670 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
| 2671 | } else { |
| 2672 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
| 2673 | injectionState->injectorPid, injectionState->injectorUid); |
| 2674 | } |
| 2675 | return false; |
| 2676 | } |
| 2677 | return true; |
| 2678 | } |
| 2679 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2680 | /** |
| 2681 | * Indicate whether one window handle should be considered as obscuring |
| 2682 | * another window handle. We only check a few preconditions. Actually |
| 2683 | * checking the bounds is left to the caller. |
| 2684 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2685 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2686 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2687 | // Compare by token so cloned layers aren't counted |
| 2688 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2689 | return false; |
| 2690 | } |
| 2691 | auto info = windowHandle->getInfo(); |
| 2692 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2693 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2694 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2695 | } else if (otherInfo->alpha == 0 && |
| 2696 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2697 | // Those act as if they were invisible, so we don't need to flag them. |
| 2698 | // We do want to potentially flag touchable windows even if they have 0 |
| 2699 | // opacity, since they can consume touches and alter the effects of the |
| 2700 | // user interaction (eg. apps that rely on |
| 2701 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2702 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2703 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2704 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2705 | // If ownerUid is the same we don't generate occlusion events as there |
| 2706 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2707 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2708 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2709 | return false; |
| 2710 | } else if (otherInfo->displayId != info->displayId) { |
| 2711 | return false; |
| 2712 | } |
| 2713 | return true; |
| 2714 | } |
| 2715 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2716 | /** |
| 2717 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2718 | * untrusted, one should check: |
| 2719 | * |
| 2720 | * 1. If result.hasBlockingOcclusion is true. |
| 2721 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2722 | * BLOCK_UNTRUSTED. |
| 2723 | * |
| 2724 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2725 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2726 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2727 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2728 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2729 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2730 | * |
| 2731 | * If neither of those is true, then it means the touch can be allowed. |
| 2732 | */ |
| 2733 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2734 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2735 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2736 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2737 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2738 | TouchOcclusionInfo info; |
| 2739 | info.hasBlockingOcclusion = false; |
| 2740 | info.obscuringOpacity = 0; |
| 2741 | info.obscuringUid = -1; |
| 2742 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2743 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2744 | if (windowHandle == otherHandle) { |
| 2745 | break; // All future windows are below us. Exit early. |
| 2746 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2747 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2748 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2749 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2750 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2751 | info.debugInfo.push_back( |
| 2752 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2753 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2754 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2755 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2756 | // window's touch occlusion mode |
| 2757 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2758 | info.hasBlockingOcclusion = true; |
| 2759 | info.obscuringUid = otherInfo->ownerUid; |
| 2760 | info.obscuringPackage = otherInfo->packageName; |
| 2761 | break; |
| 2762 | } |
| 2763 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2764 | uint32_t uid = otherInfo->ownerUid; |
| 2765 | float opacity = |
| 2766 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2767 | // Given windows A and B: |
| 2768 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2769 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2770 | opacityByUid[uid] = opacity; |
| 2771 | if (opacity > info.obscuringOpacity) { |
| 2772 | info.obscuringOpacity = opacity; |
| 2773 | info.obscuringUid = uid; |
| 2774 | info.obscuringPackage = otherInfo->packageName; |
| 2775 | } |
| 2776 | } |
| 2777 | } |
| 2778 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2779 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2780 | info.debugInfo.push_back( |
| 2781 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2782 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2783 | return info; |
| 2784 | } |
| 2785 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2786 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2787 | bool isTouchedWindow) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2788 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2789 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2790 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 2791 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2792 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 2793 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 2794 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 2795 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 2796 | info->name.c_str(), info->inputConfig.string().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2797 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2798 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2801 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2802 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2803 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2804 | occlusionInfo.obscuringUid); |
| 2805 | return false; |
| 2806 | } |
| 2807 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2808 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2809 | "%.2f, maximum allowed = %.2f)", |
| 2810 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2811 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2812 | return false; |
| 2813 | } |
| 2814 | return true; |
| 2815 | } |
| 2816 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2817 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2818 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2819 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2820 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2821 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2822 | if (windowHandle == otherHandle) { |
| 2823 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2824 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2825 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2826 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2827 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2828 | return true; |
| 2829 | } |
| 2830 | } |
| 2831 | return false; |
| 2832 | } |
| 2833 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2834 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2835 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2836 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2837 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2838 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2839 | if (windowHandle == otherHandle) { |
| 2840 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2841 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2842 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2843 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2844 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2845 | return true; |
| 2846 | } |
| 2847 | } |
| 2848 | return false; |
| 2849 | } |
| 2850 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2851 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2852 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2853 | if (applicationHandle != nullptr) { |
| 2854 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2855 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2856 | } else { |
| 2857 | return applicationHandle->getName(); |
| 2858 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2859 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2860 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2861 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2862 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2863 | } |
| 2864 | } |
| 2865 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2866 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2867 | if (!isUserActivityEvent(eventEntry)) { |
| 2868 | // 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] | 2869 | return; |
| 2870 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2871 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2872 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2873 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2874 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2875 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2876 | if (DEBUG_DISPATCH_CYCLE) { |
| 2877 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2878 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2879 | return; |
| 2880 | } |
| 2881 | } |
| 2882 | |
| 2883 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2884 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2885 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2886 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2887 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2888 | return; |
| 2889 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2890 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2891 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2892 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2893 | } |
| 2894 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2895 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2896 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2897 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2898 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2899 | return; |
| 2900 | } |
| 2901 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2902 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2903 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2904 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2905 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2906 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2907 | break; |
| 2908 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2909 | } |
| 2910 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2911 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2912 | REQUIRES(mLock) { |
| 2913 | scoped_unlock unlock(mLock); |
| 2914 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2915 | }; |
| 2916 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2920 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2921 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2922 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2923 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2924 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2925 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2926 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2927 | ATRACE_NAME(message.c_str()); |
| 2928 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2929 | if (DEBUG_DISPATCH_CYCLE) { |
| 2930 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2931 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2932 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2933 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2934 | inputTarget.getPointerInfoString().c_str()); |
| 2935 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2936 | |
| 2937 | // Skip this event if the connection status is not normal. |
| 2938 | // 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] | 2939 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2940 | if (DEBUG_DISPATCH_CYCLE) { |
| 2941 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2942 | connection->getInputChannelName().c_str(), |
| 2943 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2944 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2945 | return; |
| 2946 | } |
| 2947 | |
| 2948 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2949 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2950 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2951 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2952 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2953 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2954 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2955 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2956 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2957 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2958 | if (!splitMotionEntry) { |
| 2959 | return; // split event was dropped |
| 2960 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2961 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2962 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2963 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2964 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2965 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2966 | if (DEBUG_FOCUS) { |
| 2967 | ALOGD("channel '%s' ~ Split motion event.", |
| 2968 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2969 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2970 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2971 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2972 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2973 | return; |
| 2974 | } |
| 2975 | } |
| 2976 | |
| 2977 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2978 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2979 | } |
| 2980 | |
| 2981 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2982 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2983 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2984 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2985 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2986 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2987 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2988 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2989 | ATRACE_NAME(message.c_str()); |
| 2990 | } |
| 2991 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2992 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2993 | |
| 2994 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2995 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2996 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2997 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2998 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2999 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3000 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3001 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3002 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3003 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3004 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3005 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3006 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3007 | |
| 3008 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3009 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3010 | startDispatchCycleLocked(currentTime, connection); |
| 3011 | } |
| 3012 | } |
| 3013 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3014 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3015 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3016 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3017 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3018 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3019 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 3020 | connection->getInputChannelName().c_str(), |
| 3021 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3022 | ATRACE_NAME(message.c_str()); |
| 3023 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3024 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3025 | if (!(inputTargetFlags & dispatchMode)) { |
| 3026 | return; |
| 3027 | } |
| 3028 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 3029 | |
| 3030 | // This is a new event. |
| 3031 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3032 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3033 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3034 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3035 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3036 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3037 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3038 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3039 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3040 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3041 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3042 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3043 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3044 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3045 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3046 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3047 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3048 | if (DEBUG_DISPATCH_CYCLE) { |
| 3049 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3050 | "event", |
| 3051 | connection->getInputChannelName().c_str()); |
| 3052 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3053 | return; // skip the inconsistent event |
| 3054 | } |
| 3055 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3056 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3057 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3058 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3059 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3060 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3061 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3062 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3063 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3064 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3065 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3066 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 3067 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 3068 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3069 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 3070 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3071 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 3072 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 3073 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 3074 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3075 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3076 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3077 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3078 | } |
| 3079 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3080 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3081 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3082 | if (DEBUG_DISPATCH_CYCLE) { |
| 3083 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3084 | "enter event", |
| 3085 | connection->getInputChannelName().c_str()); |
| 3086 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3087 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3088 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3089 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3090 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3091 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3092 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3093 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3094 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3095 | } |
| 3096 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3097 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3098 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3099 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3100 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3101 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3102 | if (DEBUG_DISPATCH_CYCLE) { |
| 3103 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3104 | "event", |
| 3105 | connection->getInputChannelName().c_str()); |
| 3106 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3107 | return; // skip the inconsistent event |
| 3108 | } |
| 3109 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3110 | dispatchEntry->resolvedEventId = |
| 3111 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3112 | ? mIdGenerator.nextId() |
| 3113 | : motionEntry.id; |
| 3114 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3115 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3116 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3117 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3118 | ATRACE_NAME(message.c_str()); |
| 3119 | } |
| 3120 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3121 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3122 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3123 | // Skip reporting pointer down outside focus to the policy. |
| 3124 | break; |
| 3125 | } |
| 3126 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3127 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3128 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3129 | |
| 3130 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3131 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3132 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3133 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3134 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3135 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3136 | break; |
| 3137 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3138 | case EventEntry::Type::SENSOR: { |
| 3139 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3140 | break; |
| 3141 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3142 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3143 | case EventEntry::Type::DEVICE_RESET: { |
| 3144 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3145 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3146 | break; |
| 3147 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | // Remember that we are waiting for this dispatch to complete. |
| 3151 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3152 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3156 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3157 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3158 | } |
| 3159 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3160 | /** |
| 3161 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3162 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3163 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3164 | * We will see both launcher and wallpaper in that list. |
| 3165 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3166 | * until the set of interacted connections changes. |
| 3167 | * |
| 3168 | * The following items are skipped, to reduce the logspam: |
| 3169 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3170 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3171 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3172 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3173 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3174 | */ |
| 3175 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3176 | const std::vector<InputTarget>& targets) { |
| 3177 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3178 | if (entry.type == EventEntry::Type::KEY) { |
| 3179 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3180 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3181 | return; |
| 3182 | } |
| 3183 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3184 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3185 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3186 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3187 | return; |
| 3188 | } |
| 3189 | } else { |
| 3190 | return; // Not a key or a motion |
| 3191 | } |
| 3192 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3193 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3194 | std::vector<sp<Connection>> newConnections; |
| 3195 | for (const InputTarget& target : targets) { |
| 3196 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3197 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3198 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3199 | } |
| 3200 | |
| 3201 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3202 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3203 | if (connection == nullptr) { |
| 3204 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3205 | } |
| 3206 | newConnectionTokens.insert(std::move(token)); |
| 3207 | newConnections.emplace_back(connection); |
| 3208 | } |
| 3209 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3210 | return; // no change |
| 3211 | } |
| 3212 | mInteractionConnectionTokens = newConnectionTokens; |
| 3213 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3214 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3215 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3216 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3217 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3218 | std::string message = "Interaction with: " + targetList; |
| 3219 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3220 | message += "<none>"; |
| 3221 | } |
| 3222 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3223 | } |
| 3224 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3225 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3226 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3227 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3228 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3229 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3230 | return; |
| 3231 | } |
| 3232 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3233 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3234 | if (focusedToken == token) { |
| 3235 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3236 | return; |
| 3237 | } |
| 3238 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3239 | auto command = [this, token]() REQUIRES(mLock) { |
| 3240 | scoped_unlock unlock(mLock); |
| 3241 | mPolicy->onPointerDownOutsideFocus(token); |
| 3242 | }; |
| 3243 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3247 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3248 | if (ATRACE_ENABLED()) { |
| 3249 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3250 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3251 | ATRACE_NAME(message.c_str()); |
| 3252 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3253 | if (DEBUG_DISPATCH_CYCLE) { |
| 3254 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3255 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3256 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3257 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3258 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3259 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3260 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3261 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3262 | |
| 3263 | // Publish the event. |
| 3264 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3265 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3266 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3267 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3268 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3269 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3270 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3271 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3272 | status = connection->inputPublisher |
| 3273 | .publishKeyEvent(dispatchEntry->seq, |
| 3274 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3275 | keyEntry.source, keyEntry.displayId, |
| 3276 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3277 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3278 | keyEntry.scanCode, keyEntry.metaState, |
| 3279 | keyEntry.repeatCount, keyEntry.downTime, |
| 3280 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3281 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3282 | } |
| 3283 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3284 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3285 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3286 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3287 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3288 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3289 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3290 | // 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] | 3291 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3292 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3293 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3294 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3295 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3296 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3297 | // Don't apply window scale here since we don't want scale to affect raw |
| 3298 | // coordinates. The scale will be sent back to the client and applied |
| 3299 | // later when requesting relative coordinates. |
| 3300 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3301 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3302 | } |
| 3303 | usingCoords = scaledCoords; |
| 3304 | } |
| 3305 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3306 | // We don't want the dispatch target to know. |
| 3307 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3308 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3309 | scaledCoords[i].clear(); |
| 3310 | } |
| 3311 | usingCoords = scaledCoords; |
| 3312 | } |
| 3313 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3314 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3315 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3316 | |
| 3317 | // Publish the motion event. |
| 3318 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3319 | .publishMotionEvent(dispatchEntry->seq, |
| 3320 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3321 | motionEntry.deviceId, motionEntry.source, |
| 3322 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3323 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3324 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3325 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3326 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3327 | motionEntry.buttonState, |
| 3328 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3329 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3330 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3331 | motionEntry.xCursorPosition, |
| 3332 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3333 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3334 | motionEntry.downTime, motionEntry.eventTime, |
| 3335 | motionEntry.pointerCount, |
| 3336 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3337 | break; |
| 3338 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3339 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3340 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3341 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3342 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3343 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3344 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3345 | break; |
| 3346 | } |
| 3347 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3348 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3349 | const TouchModeEntry& touchModeEntry = |
| 3350 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3351 | status = connection->inputPublisher |
| 3352 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3353 | touchModeEntry.inTouchMode); |
| 3354 | |
| 3355 | break; |
| 3356 | } |
| 3357 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3358 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3359 | const auto& captureEntry = |
| 3360 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3361 | status = connection->inputPublisher |
| 3362 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3363 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3364 | break; |
| 3365 | } |
| 3366 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3367 | case EventEntry::Type::DRAG: { |
| 3368 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3369 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3370 | dragEntry.id, dragEntry.x, |
| 3371 | dragEntry.y, |
| 3372 | dragEntry.isExiting); |
| 3373 | break; |
| 3374 | } |
| 3375 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3376 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3377 | case EventEntry::Type::DEVICE_RESET: |
| 3378 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3379 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3380 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3381 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3382 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3383 | } |
| 3384 | |
| 3385 | // Check the result. |
| 3386 | if (status) { |
| 3387 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3388 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3389 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3390 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3391 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3392 | "event to it, status=%s(%d)", |
| 3393 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3394 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3395 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3396 | } else { |
| 3397 | // Pipe is full and we are waiting for the app to finish process some events |
| 3398 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3399 | if (DEBUG_DISPATCH_CYCLE) { |
| 3400 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3401 | "waiting for the application to catch up", |
| 3402 | connection->getInputChannelName().c_str()); |
| 3403 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3404 | } |
| 3405 | } else { |
| 3406 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3407 | "status=%s(%d)", |
| 3408 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3409 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3410 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3411 | } |
| 3412 | return; |
| 3413 | } |
| 3414 | |
| 3415 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3416 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3417 | connection->outboundQueue.end(), |
| 3418 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3419 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3420 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3421 | if (connection->responsive) { |
| 3422 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3423 | connection->inputChannel->getConnectionToken()); |
| 3424 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3425 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3426 | } |
| 3427 | } |
| 3428 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3429 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3430 | size_t size; |
| 3431 | switch (event.type) { |
| 3432 | case VerifiedInputEvent::Type::KEY: { |
| 3433 | size = sizeof(VerifiedKeyEvent); |
| 3434 | break; |
| 3435 | } |
| 3436 | case VerifiedInputEvent::Type::MOTION: { |
| 3437 | size = sizeof(VerifiedMotionEvent); |
| 3438 | break; |
| 3439 | } |
| 3440 | } |
| 3441 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3442 | return mHmacKeyManager.sign(start, size); |
| 3443 | } |
| 3444 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3445 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3446 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3447 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3448 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3449 | // Only sign events up and down events as the purely move events |
| 3450 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3451 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3452 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3453 | |
| 3454 | VerifiedMotionEvent verifiedEvent = |
| 3455 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3456 | verifiedEvent.actionMasked = actionMasked; |
| 3457 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3458 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3459 | } |
| 3460 | |
| 3461 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3462 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3463 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3464 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3465 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3466 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3467 | } |
| 3468 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3469 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3470 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3471 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3472 | if (DEBUG_DISPATCH_CYCLE) { |
| 3473 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3474 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3475 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3476 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3477 | if (connection->status == Connection::Status::BROKEN || |
| 3478 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3479 | return; |
| 3480 | } |
| 3481 | |
| 3482 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3483 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3484 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3485 | }; |
| 3486 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3487 | } |
| 3488 | |
| 3489 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3490 | const sp<Connection>& connection, |
| 3491 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3492 | if (DEBUG_DISPATCH_CYCLE) { |
| 3493 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3494 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3495 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3496 | |
| 3497 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3498 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3499 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3500 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3501 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3502 | |
| 3503 | // The connection appears to be unrecoverably broken. |
| 3504 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3505 | if (connection->status == Connection::Status::NORMAL) { |
| 3506 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3507 | |
| 3508 | if (notify) { |
| 3509 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3510 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3511 | connection->getInputChannelName().c_str()); |
| 3512 | |
| 3513 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3514 | scoped_unlock unlock(mLock); |
| 3515 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3516 | }; |
| 3517 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3518 | } |
| 3519 | } |
| 3520 | } |
| 3521 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3522 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3523 | while (!queue.empty()) { |
| 3524 | DispatchEntry* dispatchEntry = queue.front(); |
| 3525 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3526 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3527 | } |
| 3528 | } |
| 3529 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3530 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3531 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3532 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3533 | } |
| 3534 | delete dispatchEntry; |
| 3535 | } |
| 3536 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3537 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3538 | std::scoped_lock _l(mLock); |
| 3539 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3540 | if (connection == nullptr) { |
| 3541 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3542 | connectionToken.get(), events); |
| 3543 | return 0; // remove the callback |
| 3544 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3545 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3546 | bool notify; |
| 3547 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3548 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3549 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3550 | "events=0x%x", |
| 3551 | connection->getInputChannelName().c_str(), events); |
| 3552 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3553 | } |
| 3554 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3555 | nsecs_t currentTime = now(); |
| 3556 | bool gotOne = false; |
| 3557 | status_t status = OK; |
| 3558 | for (;;) { |
| 3559 | Result<InputPublisher::ConsumerResponse> result = |
| 3560 | connection->inputPublisher.receiveConsumerResponse(); |
| 3561 | if (!result.ok()) { |
| 3562 | status = result.error().code(); |
| 3563 | break; |
| 3564 | } |
| 3565 | |
| 3566 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3567 | const InputPublisher::Finished& finish = |
| 3568 | std::get<InputPublisher::Finished>(*result); |
| 3569 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3570 | finish.consumeTime); |
| 3571 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3572 | if (shouldReportMetricsForConnection(*connection)) { |
| 3573 | const InputPublisher::Timeline& timeline = |
| 3574 | std::get<InputPublisher::Timeline>(*result); |
| 3575 | mLatencyTracker |
| 3576 | .trackGraphicsLatency(timeline.inputEventId, |
| 3577 | connection->inputChannel->getConnectionToken(), |
| 3578 | std::move(timeline.graphicsTimeline)); |
| 3579 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3580 | } |
| 3581 | gotOne = true; |
| 3582 | } |
| 3583 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3584 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3585 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3586 | return 1; |
| 3587 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3588 | } |
| 3589 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3590 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3591 | if (notify) { |
| 3592 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3593 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3594 | status); |
| 3595 | } |
| 3596 | } else { |
| 3597 | // Monitor channels are never explicitly unregistered. |
| 3598 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3599 | const bool stillHaveWindowHandle = |
| 3600 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3601 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3602 | if (notify) { |
| 3603 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3604 | connection->getInputChannelName().c_str(), events); |
| 3605 | } |
| 3606 | } |
| 3607 | |
| 3608 | // Remove the channel. |
| 3609 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3610 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3611 | } |
| 3612 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3613 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3614 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3615 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3616 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3617 | } |
| 3618 | } |
| 3619 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3620 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3621 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3622 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3623 | for (const Monitor& monitor : monitors) { |
| 3624 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3625 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3626 | } |
| 3627 | } |
| 3628 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3629 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3630 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3631 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3632 | if (connection == nullptr) { |
| 3633 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3634 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3635 | |
| 3636 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3640 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3641 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3642 | return; |
| 3643 | } |
| 3644 | |
| 3645 | nsecs_t currentTime = now(); |
| 3646 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3647 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3648 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3649 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3650 | if (cancelationEvents.empty()) { |
| 3651 | return; |
| 3652 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3653 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3654 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3655 | "with reality: %s, mode=%d.", |
| 3656 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3657 | options.mode); |
| 3658 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3659 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3660 | std::string reason = std::string("reason=").append(options.reason); |
| 3661 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3662 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3663 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3664 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3665 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3666 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3667 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3668 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3669 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3670 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3671 | } |
| 3672 | target.inputChannel = connection->inputChannel; |
| 3673 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3674 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3675 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3676 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3677 | switch (cancelationEventEntry->type) { |
| 3678 | case EventEntry::Type::KEY: { |
| 3679 | logOutboundKeyDetails("cancel - ", |
| 3680 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3681 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3682 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3683 | case EventEntry::Type::MOTION: { |
| 3684 | logOutboundMotionDetails("cancel - ", |
| 3685 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3686 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3687 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3688 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3689 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3690 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3691 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3692 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3693 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3694 | break; |
| 3695 | } |
| 3696 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3697 | case EventEntry::Type::DEVICE_RESET: |
| 3698 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3699 | 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] | 3700 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3701 | break; |
| 3702 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3703 | } |
| 3704 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3705 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3706 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3707 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3708 | |
| 3709 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3710 | } |
| 3711 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3712 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3713 | const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3714 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3715 | return; |
| 3716 | } |
| 3717 | |
| 3718 | nsecs_t currentTime = now(); |
| 3719 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3720 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3721 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3722 | |
| 3723 | if (downEvents.empty()) { |
| 3724 | return; |
| 3725 | } |
| 3726 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3727 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3728 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3729 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3730 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3731 | |
| 3732 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3733 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3734 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3735 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3736 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3737 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3738 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3739 | } |
| 3740 | target.inputChannel = connection->inputChannel; |
| 3741 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3742 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3743 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3744 | switch (downEventEntry->type) { |
| 3745 | case EventEntry::Type::MOTION: { |
| 3746 | logOutboundMotionDetails("down - ", |
| 3747 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3748 | break; |
| 3749 | } |
| 3750 | |
| 3751 | case EventEntry::Type::KEY: |
| 3752 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3753 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3754 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3755 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3756 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3757 | case EventEntry::Type::SENSOR: |
| 3758 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3759 | 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] | 3760 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3761 | break; |
| 3762 | } |
| 3763 | } |
| 3764 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3765 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3766 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | startDispatchCycleLocked(currentTime, connection); |
| 3770 | } |
| 3771 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3772 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3773 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3774 | ALOG_ASSERT(pointerIds.value != 0); |
| 3775 | |
| 3776 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3777 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3778 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3779 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3780 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3781 | uint32_t splitPointerCount = 0; |
| 3782 | |
| 3783 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3784 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3785 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3786 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3787 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3788 | if (pointerIds.hasBit(pointerId)) { |
| 3789 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3790 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3791 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3792 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3793 | splitPointerCount += 1; |
| 3794 | } |
| 3795 | } |
| 3796 | |
| 3797 | if (splitPointerCount != pointerIds.count()) { |
| 3798 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3799 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3800 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3801 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3802 | // in this way. |
| 3803 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3804 | "we expected there to be %d pointers. This probably means we received " |
| 3805 | "a broken sequence of pointer ids from the input device.", |
| 3806 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3807 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3808 | } |
| 3809 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3810 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3811 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3812 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3813 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3814 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3815 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3816 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3817 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3818 | if (pointerIds.hasBit(pointerId)) { |
| 3819 | if (pointerIds.count() == 1) { |
| 3820 | // The first/last pointer went down/up. |
| 3821 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3822 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3823 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3824 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3825 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3826 | } else { |
| 3827 | // A secondary pointer went down/up. |
| 3828 | uint32_t splitPointerIndex = 0; |
| 3829 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3830 | splitPointerIndex += 1; |
| 3831 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3832 | action = maskedAction | |
| 3833 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3834 | } |
| 3835 | } else { |
| 3836 | // An unrelated pointer changed. |
| 3837 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3838 | } |
| 3839 | } |
| 3840 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3841 | int32_t newId = mIdGenerator.nextId(); |
| 3842 | if (ATRACE_ENABLED()) { |
| 3843 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3844 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3845 | originalMotionEntry.id, newId); |
| 3846 | ATRACE_NAME(message.c_str()); |
| 3847 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3848 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3849 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3850 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3851 | originalMotionEntry.displayId, |
| 3852 | originalMotionEntry.policyFlags, action, |
| 3853 | originalMotionEntry.actionButton, |
| 3854 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3855 | originalMotionEntry.buttonState, |
| 3856 | originalMotionEntry.classification, |
| 3857 | originalMotionEntry.edgeFlags, |
| 3858 | originalMotionEntry.xPrecision, |
| 3859 | originalMotionEntry.yPrecision, |
| 3860 | originalMotionEntry.xCursorPosition, |
| 3861 | originalMotionEntry.yCursorPosition, |
| 3862 | originalMotionEntry.downTime, splitPointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3863 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3864 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3865 | if (originalMotionEntry.injectionState) { |
| 3866 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3867 | splitMotionEntry->injectionState->refCount += 1; |
| 3868 | } |
| 3869 | |
| 3870 | return splitMotionEntry; |
| 3871 | } |
| 3872 | |
| 3873 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3874 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3875 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3876 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3877 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3878 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3879 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3880 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3881 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3882 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3883 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3884 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3885 | } // release lock |
| 3886 | |
| 3887 | if (needWake) { |
| 3888 | mLooper->wake(); |
| 3889 | } |
| 3890 | } |
| 3891 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3892 | /** |
| 3893 | * If one of the meta shortcuts is detected, process them here: |
| 3894 | * Meta + Backspace -> generate BACK |
| 3895 | * Meta + Enter -> generate HOME |
| 3896 | * This will potentially overwrite keyCode and metaState. |
| 3897 | */ |
| 3898 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3899 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3900 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3901 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3902 | if (keyCode == AKEYCODE_DEL) { |
| 3903 | newKeyCode = AKEYCODE_BACK; |
| 3904 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3905 | newKeyCode = AKEYCODE_HOME; |
| 3906 | } |
| 3907 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3908 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3909 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3910 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3911 | keyCode = newKeyCode; |
| 3912 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3913 | } |
| 3914 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3915 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3916 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3917 | // 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] | 3918 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3919 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3920 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3921 | if (replacementIt != mReplacedKeys.end()) { |
| 3922 | keyCode = replacementIt->second; |
| 3923 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3924 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3925 | } |
| 3926 | } |
| 3927 | } |
| 3928 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3929 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3930 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3931 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3932 | "policyFlags=0x%x, action=0x%x, " |
| 3933 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3934 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3935 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3936 | args->downTime); |
| 3937 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3938 | if (!validateKeyEvent(args->action)) { |
| 3939 | return; |
| 3940 | } |
| 3941 | |
| 3942 | uint32_t policyFlags = args->policyFlags; |
| 3943 | int32_t flags = args->flags; |
| 3944 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3945 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3946 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3947 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3948 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3949 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3950 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3951 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3952 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3953 | metaState |= AMETA_FUNCTION_ON; |
| 3954 | } |
| 3955 | |
| 3956 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3957 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3958 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3959 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3960 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3961 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3962 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3963 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3964 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3965 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3966 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3967 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3968 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3969 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3970 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3971 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3972 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3973 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3974 | { // acquire lock |
| 3975 | mLock.lock(); |
| 3976 | |
| 3977 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3978 | mLock.unlock(); |
| 3979 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3980 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3981 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3982 | return; // event was consumed by the filter |
| 3983 | } |
| 3984 | |
| 3985 | mLock.lock(); |
| 3986 | } |
| 3987 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3988 | std::unique_ptr<KeyEntry> newEntry = |
| 3989 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3990 | args->displayId, policyFlags, args->action, flags, |
| 3991 | keyCode, args->scanCode, metaState, repeatCount, |
| 3992 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3993 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3994 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3995 | mLock.unlock(); |
| 3996 | } // release lock |
| 3997 | |
| 3998 | if (needWake) { |
| 3999 | mLooper->wake(); |
| 4000 | } |
| 4001 | } |
| 4002 | |
| 4003 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 4004 | return mInputFilterEnabled; |
| 4005 | } |
| 4006 | |
| 4007 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4008 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4009 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4010 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 4011 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 4012 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4013 | "yCursorPosition=%f, downTime=%" PRId64, |
| 4014 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 4015 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 4016 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 4017 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 4018 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 4019 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 4020 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 4021 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 4022 | "orientation=%f", |
| 4023 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 4024 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4025 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4026 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4027 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4028 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4029 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4030 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4031 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4032 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 4033 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4034 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4035 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 4036 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4037 | return; |
| 4038 | } |
| 4039 | |
| 4040 | uint32_t policyFlags = args->policyFlags; |
| 4041 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4042 | |
| 4043 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4044 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4045 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4046 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4047 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4048 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4049 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4050 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4051 | { // acquire lock |
| 4052 | mLock.lock(); |
| 4053 | |
| 4054 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4055 | ui::Transform displayTransform; |
| 4056 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4057 | displayTransform = it->second.transform; |
| 4058 | } |
| 4059 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4060 | mLock.unlock(); |
| 4061 | |
| 4062 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4063 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4064 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4065 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4066 | displayTransform, args->xPrecision, args->yPrecision, |
| 4067 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4068 | args->downTime, args->eventTime, args->pointerCount, |
| 4069 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4070 | |
| 4071 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4072 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4073 | return; // event was consumed by the filter |
| 4074 | } |
| 4075 | |
| 4076 | mLock.lock(); |
| 4077 | } |
| 4078 | |
| 4079 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4080 | std::unique_ptr<MotionEntry> newEntry = |
| 4081 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4082 | args->source, args->displayId, policyFlags, |
| 4083 | args->action, args->actionButton, args->flags, |
| 4084 | args->metaState, args->buttonState, |
| 4085 | args->classification, args->edgeFlags, |
| 4086 | args->xPrecision, args->yPrecision, |
| 4087 | args->xCursorPosition, args->yCursorPosition, |
| 4088 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4089 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4090 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4091 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4092 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4093 | !mInputFilterEnabled) { |
| 4094 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4095 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4096 | } |
| 4097 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4098 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4099 | mLock.unlock(); |
| 4100 | } // release lock |
| 4101 | |
| 4102 | if (needWake) { |
| 4103 | mLooper->wake(); |
| 4104 | } |
| 4105 | } |
| 4106 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4107 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4108 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4109 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4110 | " sensorType=%s", |
| 4111 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4112 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4113 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4114 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4115 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4116 | { // acquire lock |
| 4117 | mLock.lock(); |
| 4118 | |
| 4119 | // Just enqueue a new sensor event. |
| 4120 | std::unique_ptr<SensorEntry> newEntry = |
| 4121 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4122 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4123 | args->sensorType, args->accuracy, |
| 4124 | args->accuracyChanged, args->values); |
| 4125 | |
| 4126 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4127 | mLock.unlock(); |
| 4128 | } // release lock |
| 4129 | |
| 4130 | if (needWake) { |
| 4131 | mLooper->wake(); |
| 4132 | } |
| 4133 | } |
| 4134 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4135 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4136 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4137 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4138 | args->deviceId, args->isOn); |
| 4139 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4140 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4141 | } |
| 4142 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4143 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4144 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4148 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4149 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4150 | "switchMask=0x%08x", |
| 4151 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4152 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4153 | |
| 4154 | uint32_t policyFlags = args->policyFlags; |
| 4155 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4156 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4157 | } |
| 4158 | |
| 4159 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4160 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4161 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4162 | args->deviceId); |
| 4163 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4164 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4165 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4166 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4167 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4168 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4169 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4170 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4171 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4172 | } // release lock |
| 4173 | |
| 4174 | if (needWake) { |
| 4175 | mLooper->wake(); |
| 4176 | } |
| 4177 | } |
| 4178 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4179 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4180 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4181 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4182 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4183 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4184 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4185 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4186 | { // acquire lock |
| 4187 | std::scoped_lock _l(mLock); |
| 4188 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4189 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4190 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4191 | } // release lock |
| 4192 | |
| 4193 | if (needWake) { |
| 4194 | mLooper->wake(); |
| 4195 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4196 | } |
| 4197 | |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 4198 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4199 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4200 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4201 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 4202 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 4203 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4204 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4205 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4206 | 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] | 4207 | |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 4208 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4209 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4210 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4211 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4212 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4213 | // 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] | 4214 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4215 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4216 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4217 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4218 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4219 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4220 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4221 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4224 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4225 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4226 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4227 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4228 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4229 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4230 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4231 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4232 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4233 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4234 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4235 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4236 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4237 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4238 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4239 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4240 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4241 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4242 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4243 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4244 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4245 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4246 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4247 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4248 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4249 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4250 | |
| 4251 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4252 | android::base::Timer t; |
| 4253 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4254 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4255 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4256 | std::to_string(t.duration().count()).c_str()); |
| 4257 | } |
| 4258 | } |
| 4259 | |
| 4260 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4261 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4262 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4263 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4264 | incomingKey.getDisplayId(), policyFlags, action, |
| 4265 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4266 | incomingKey.getRepeatCount(), |
| 4267 | incomingKey.getDownTime()); |
| 4268 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4269 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4270 | } |
| 4271 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4272 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4273 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4274 | const int32_t action = motionEvent.getAction(); |
| 4275 | const bool isPointerEvent = |
| 4276 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4277 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4278 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4279 | ? ADISPLAY_ID_DEFAULT |
| 4280 | : event->getDisplayId(); |
| 4281 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4282 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4283 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4284 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4285 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4286 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
| 4289 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4290 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4291 | android::base::Timer t; |
| 4292 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4293 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4294 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4295 | std::to_string(t.duration().count()).c_str()); |
| 4296 | } |
| 4297 | } |
| 4298 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4299 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4300 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4301 | } |
| 4302 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4303 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4304 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4305 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4306 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4307 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4308 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4309 | displayId, policyFlags, action, actionButton, |
| 4310 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4311 | motionEvent.getButtonState(), |
| 4312 | motionEvent.getClassification(), |
| 4313 | motionEvent.getEdgeFlags(), |
| 4314 | motionEvent.getXPrecision(), |
| 4315 | motionEvent.getYPrecision(), |
| 4316 | motionEvent.getRawXCursorPosition(), |
| 4317 | motionEvent.getRawYCursorPosition(), |
| 4318 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4319 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4320 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4321 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4322 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4323 | sampleEventTimes += 1; |
| 4324 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4325 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4326 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4327 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4328 | displayId, policyFlags, action, actionButton, |
| 4329 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4330 | motionEvent.getButtonState(), |
| 4331 | motionEvent.getClassification(), |
| 4332 | motionEvent.getEdgeFlags(), |
| 4333 | motionEvent.getXPrecision(), |
| 4334 | motionEvent.getYPrecision(), |
| 4335 | motionEvent.getRawXCursorPosition(), |
| 4336 | motionEvent.getRawYCursorPosition(), |
| 4337 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4338 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4339 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4340 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4341 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4342 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4343 | } |
| 4344 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4345 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4346 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4347 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4348 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4349 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4350 | } |
| 4351 | |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 4352 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4353 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4354 | injectionState->injectionIsAsync = true; |
| 4355 | } |
| 4356 | |
| 4357 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4358 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4359 | |
| 4360 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4361 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4362 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4363 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | mLock.unlock(); |
| 4367 | |
| 4368 | if (needWake) { |
| 4369 | mLooper->wake(); |
| 4370 | } |
| 4371 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4372 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4373 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4374 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4375 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4376 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4377 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4378 | } else { |
| 4379 | for (;;) { |
| 4380 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4381 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4382 | break; |
| 4383 | } |
| 4384 | |
| 4385 | nsecs_t remainingTimeout = endTime - now(); |
| 4386 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4387 | if (DEBUG_INJECTION) { |
| 4388 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4389 | "to become available."); |
| 4390 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4391 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4392 | break; |
| 4393 | } |
| 4394 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4395 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4396 | } |
| 4397 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4398 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4399 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4400 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4401 | if (DEBUG_INJECTION) { |
| 4402 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4403 | injectionState->pendingForegroundDispatches); |
| 4404 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4405 | nsecs_t remainingTimeout = endTime - now(); |
| 4406 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4407 | if (DEBUG_INJECTION) { |
| 4408 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4409 | "dispatches to finish."); |
| 4410 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4411 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4412 | break; |
| 4413 | } |
| 4414 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4415 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4416 | } |
| 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | injectionState->release(); |
| 4421 | } // release lock |
| 4422 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4423 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 4424 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
| 4425 | injectionResult, injectorPid, injectorUid); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4426 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4427 | |
| 4428 | return injectionResult; |
| 4429 | } |
| 4430 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4431 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4432 | std::array<uint8_t, 32> calculatedHmac; |
| 4433 | std::unique_ptr<VerifiedInputEvent> result; |
| 4434 | switch (event.getType()) { |
| 4435 | case AINPUT_EVENT_TYPE_KEY: { |
| 4436 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4437 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4438 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4439 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4440 | break; |
| 4441 | } |
| 4442 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4443 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4444 | VerifiedMotionEvent verifiedMotionEvent = |
| 4445 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4446 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4447 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4448 | break; |
| 4449 | } |
| 4450 | default: { |
| 4451 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4452 | return nullptr; |
| 4453 | } |
| 4454 | } |
| 4455 | if (calculatedHmac == INVALID_HMAC) { |
| 4456 | return nullptr; |
| 4457 | } |
| 4458 | if (calculatedHmac != event.getHmac()) { |
| 4459 | return nullptr; |
| 4460 | } |
| 4461 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4462 | } |
| 4463 | |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 4464 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
| 4465 | return injectorUid == 0 || |
| 4466 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
| 4467 | } |
| 4468 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4469 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4470 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4471 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4472 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4473 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 4474 | ALOGD("Setting input event injection result to %d. " |
| 4475 | "injectorPid=%d, injectorUid=%d", |
| 4476 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4477 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4478 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4479 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4480 | // Log the outcome since the injector did not wait for the injection result. |
| 4481 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4482 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4483 | ALOGV("Asynchronous input event injection succeeded."); |
| 4484 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4485 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4486 | ALOGW("Asynchronous input event injection failed."); |
| 4487 | break; |
Prabir Pradhan | 4df80f5 | 2022-04-05 18:33:16 +0000 | [diff] [blame] | 4488 | case InputEventInjectionResult::PERMISSION_DENIED: |
| 4489 | ALOGW("Asynchronous input event injection permission denied."); |
| 4490 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4491 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4492 | ALOGW("Asynchronous input event injection timed out."); |
| 4493 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4494 | case InputEventInjectionResult::PENDING: |
| 4495 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4496 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4497 | } |
| 4498 | } |
| 4499 | |
| 4500 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4501 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4502 | } |
| 4503 | } |
| 4504 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4505 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4506 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4507 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4508 | // display space, so we need to transform the injected events accordingly. |
| 4509 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4510 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4511 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4512 | |
| 4513 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4514 | entry.pointerCoords[i] = |
| 4515 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4516 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4517 | } |
| 4518 | } |
| 4519 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4520 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4521 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4522 | if (injectionState) { |
| 4523 | injectionState->pendingForegroundDispatches += 1; |
| 4524 | } |
| 4525 | } |
| 4526 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4527 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4528 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4529 | if (injectionState) { |
| 4530 | injectionState->pendingForegroundDispatches -= 1; |
| 4531 | |
| 4532 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4533 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4534 | } |
| 4535 | } |
| 4536 | } |
| 4537 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4538 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4539 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4540 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4541 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4542 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4543 | } |
| 4544 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4545 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4546 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4547 | if (windowHandleToken == nullptr) { |
| 4548 | return nullptr; |
| 4549 | } |
| 4550 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4551 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4552 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4553 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4554 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4555 | return windowHandle; |
| 4556 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4557 | } |
| 4558 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4559 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4560 | } |
| 4561 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4562 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4563 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4564 | if (windowHandleToken == nullptr) { |
| 4565 | return nullptr; |
| 4566 | } |
| 4567 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4568 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4569 | if (windowHandle->getToken() == windowHandleToken) { |
| 4570 | return windowHandle; |
| 4571 | } |
| 4572 | } |
| 4573 | return nullptr; |
| 4574 | } |
| 4575 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4576 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4577 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4578 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4579 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4580 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4581 | if (handle->getId() == windowHandle->getId() && |
| 4582 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4583 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4584 | ALOGE("Found window %s in display %" PRId32 |
| 4585 | ", but it should belong to display %" PRId32, |
| 4586 | windowHandle->getName().c_str(), it.first, |
| 4587 | windowHandle->getInfo()->displayId); |
| 4588 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4589 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4590 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4591 | } |
| 4592 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4593 | return nullptr; |
| 4594 | } |
| 4595 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4596 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4597 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4598 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4599 | } |
| 4600 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4601 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4602 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4603 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4604 | windowHandle.getInfo()->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4605 | if (connection != nullptr && noInputChannel) { |
| 4606 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4607 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4608 | return false; |
| 4609 | } |
| 4610 | |
| 4611 | if (connection == nullptr) { |
| 4612 | if (!noInputChannel) { |
| 4613 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4614 | } |
| 4615 | return false; |
| 4616 | } |
| 4617 | if (!connection->responsive) { |
| 4618 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4619 | return false; |
| 4620 | } |
| 4621 | return true; |
| 4622 | } |
| 4623 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4624 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4625 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4626 | auto connectionIt = mConnectionsByToken.find(token); |
| 4627 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4628 | return nullptr; |
| 4629 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4630 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4631 | } |
| 4632 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4633 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4634 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4635 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4636 | // Remove all handles on a display if there are no windows left. |
| 4637 | mWindowHandlesByDisplay.erase(displayId); |
| 4638 | return; |
| 4639 | } |
| 4640 | |
| 4641 | // Since we compare the pointer of input window handles across window updates, we need |
| 4642 | // 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] | 4643 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4644 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4645 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4646 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4647 | } |
| 4648 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4649 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4650 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4651 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4652 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4653 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4654 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4655 | const bool canReceiveInput = |
| 4656 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4657 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4658 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4659 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4660 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4661 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4662 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | if (info->displayId != displayId) { |
| 4666 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4667 | handle->getName().c_str(), displayId, info->displayId); |
| 4668 | continue; |
| 4669 | } |
| 4670 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4671 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4672 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4673 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4674 | oldHandle->updateFrom(handle); |
| 4675 | newHandles.push_back(oldHandle); |
| 4676 | } else { |
| 4677 | newHandles.push_back(handle); |
| 4678 | } |
| 4679 | } |
| 4680 | |
| 4681 | // Insert or replace |
| 4682 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4683 | } |
| 4684 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4685 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4686 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4687 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4688 | { // acquire lock |
| 4689 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4690 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4691 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4692 | } |
| 4693 | } |
| 4694 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4695 | mLooper->wake(); |
| 4696 | } |
| 4697 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4698 | /** |
| 4699 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4700 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4701 | * If set an empty list, remove all handles from the specific display. |
| 4702 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4703 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4704 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4705 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4706 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4707 | if (DEBUG_FOCUS) { |
| 4708 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4709 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4710 | windowList += iwh->getName() + " "; |
| 4711 | } |
| 4712 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4713 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4714 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4715 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4716 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4717 | const WindowInfo& info = *window->getInfo(); |
| 4718 | |
| 4719 | // 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] | 4720 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4721 | if (noInputWindow && window->getToken() != nullptr) { |
| 4722 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4723 | window->getName().c_str()); |
| 4724 | window->releaseChannel(); |
| 4725 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4726 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4727 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4728 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 4729 | !info.inputConfig.test( |
| 4730 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4731 | "%s has feature SPY, but is not a trusted overlay.", |
| 4732 | window->getName().c_str()); |
| 4733 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4734 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4735 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 4736 | !info.inputConfig.test( |
| 4737 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4738 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4739 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4740 | } |
| 4741 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4742 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4743 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4744 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4745 | // Save the old windows' orientation by ID before it gets updated. |
| 4746 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4747 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4748 | oldWindowOrientations.emplace(handle->getId(), |
| 4749 | handle->getInfo()->transform.getOrientation()); |
| 4750 | } |
| 4751 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4752 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4753 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4754 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4755 | if (mLastHoverWindowHandle && |
| 4756 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4757 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4758 | mLastHoverWindowHandle = nullptr; |
| 4759 | } |
| 4760 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4761 | std::optional<FocusResolver::FocusChanges> changes = |
| 4762 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4763 | if (changes) { |
| 4764 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4765 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4766 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4767 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4768 | mTouchStatesByDisplay.find(displayId); |
| 4769 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4770 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4771 | for (size_t i = 0; i < state.windows.size();) { |
| 4772 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4773 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4774 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4775 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4776 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4777 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4778 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4779 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4780 | if (touchedInputChannel != nullptr) { |
| 4781 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4782 | "touched window was removed"); |
| 4783 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4784 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4785 | // well. |
| 4786 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4787 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 4788 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4789 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4790 | if (wallpaper != nullptr) { |
| 4791 | sp<Connection> wallpaperConnection = |
| 4792 | getConnectionLocked(wallpaper->getToken()); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4793 | if (wallpaperConnection != nullptr) { |
| 4794 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4795 | options); |
| 4796 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4797 | } |
| 4798 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4799 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4800 | state.windows.erase(state.windows.begin() + i); |
| 4801 | } else { |
| 4802 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4803 | } |
| 4804 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4805 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4806 | // 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] | 4807 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4808 | if (mDragState && |
| 4809 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4810 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4811 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4812 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4813 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4814 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4815 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4816 | // pointer events if necessary. |
| 4817 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4818 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4819 | if (newWindowHandle != nullptr && |
| 4820 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4821 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4822 | std::shared_ptr<InputChannel> inputChannel = |
| 4823 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4824 | if (inputChannel != nullptr) { |
| 4825 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4826 | "touched window's orientation changed"); |
| 4827 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4828 | } |
| 4829 | } |
| 4830 | } |
| 4831 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4832 | // Release information for windows that are no longer present. |
| 4833 | // This ensures that unused input channels are released promptly. |
| 4834 | // Otherwise, they might stick around until the window handle is destroyed |
| 4835 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4836 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4837 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4838 | if (DEBUG_FOCUS) { |
| 4839 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4840 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4841 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4842 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4843 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4844 | } |
| 4845 | |
| 4846 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4847 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4848 | if (DEBUG_FOCUS) { |
| 4849 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4850 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4851 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4852 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4853 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4854 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4855 | } // release lock |
| 4856 | |
| 4857 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4858 | mLooper->wake(); |
| 4859 | } |
| 4860 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4861 | void InputDispatcher::setFocusedApplicationLocked( |
| 4862 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4863 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4864 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4865 | |
| 4866 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4867 | return; // This application is already focused. No need to wake up or change anything. |
| 4868 | } |
| 4869 | |
| 4870 | // Set the new application handle. |
| 4871 | if (inputApplicationHandle != nullptr) { |
| 4872 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4873 | } else { |
| 4874 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4875 | } |
| 4876 | |
| 4877 | // No matter what the old focused application was, stop waiting on it because it is |
| 4878 | // no longer focused. |
| 4879 | resetNoFocusedWindowTimeoutLocked(); |
| 4880 | } |
| 4881 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4882 | /** |
| 4883 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4884 | * the display not specified. |
| 4885 | * |
| 4886 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4887 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4888 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4889 | * display. The display-specified events won't be affected. |
| 4890 | */ |
| 4891 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4892 | if (DEBUG_FOCUS) { |
| 4893 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4894 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4895 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4896 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4897 | |
| 4898 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4899 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4900 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4901 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4902 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4903 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4904 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4905 | CancelationOptions |
| 4906 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4907 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4908 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4909 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4910 | } |
| 4911 | } |
| 4912 | mFocusedDisplayId = displayId; |
| 4913 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4914 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4915 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4916 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4917 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4918 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4919 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4920 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4921 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4922 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4923 | } |
| 4924 | } |
| 4925 | } |
| 4926 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4927 | if (DEBUG_FOCUS) { |
| 4928 | logDispatchStateLocked(); |
| 4929 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4930 | } // release lock |
| 4931 | |
| 4932 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4933 | mLooper->wake(); |
| 4934 | } |
| 4935 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4936 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4937 | if (DEBUG_FOCUS) { |
| 4938 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4939 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4940 | |
| 4941 | bool changed; |
| 4942 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4943 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4944 | |
| 4945 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4946 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4947 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4948 | } |
| 4949 | |
| 4950 | if (mDispatchEnabled && !enabled) { |
| 4951 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4952 | } |
| 4953 | |
| 4954 | mDispatchEnabled = enabled; |
| 4955 | mDispatchFrozen = frozen; |
| 4956 | changed = true; |
| 4957 | } else { |
| 4958 | changed = false; |
| 4959 | } |
| 4960 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4961 | if (DEBUG_FOCUS) { |
| 4962 | logDispatchStateLocked(); |
| 4963 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4964 | } // release lock |
| 4965 | |
| 4966 | if (changed) { |
| 4967 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4968 | mLooper->wake(); |
| 4969 | } |
| 4970 | } |
| 4971 | |
| 4972 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4973 | if (DEBUG_FOCUS) { |
| 4974 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4975 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4976 | |
| 4977 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4978 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4979 | |
| 4980 | if (mInputFilterEnabled == enabled) { |
| 4981 | return; |
| 4982 | } |
| 4983 | |
| 4984 | mInputFilterEnabled = enabled; |
| 4985 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4986 | } // release lock |
| 4987 | |
| 4988 | // Wake up poll loop since there might be work to do to drop everything. |
| 4989 | mLooper->wake(); |
| 4990 | } |
| 4991 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4992 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, |
| 4993 | bool hasPermission) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4994 | bool needWake = false; |
| 4995 | { |
| 4996 | std::scoped_lock lock(mLock); |
| 4997 | if (mInTouchMode == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4998 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4999 | } |
| 5000 | if (DEBUG_TOUCH_MODE) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5001 | ALOGD("Request to change touch mode from %s to %s (calling pid=%d, uid=%d, " |
| 5002 | "hasPermission=%s)", |
| 5003 | toString(mInTouchMode), toString(inTouchMode), pid, uid, toString(hasPermission)); |
| 5004 | } |
| 5005 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5006 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5007 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
| 5008 | ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " |
| 5009 | "window nor none of the previously interacted window", |
| 5010 | pid, uid); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5011 | return false; |
| 5012 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
| 5015 | // TODO(b/198499018): Store touch mode per display. |
| 5016 | mInTouchMode = inTouchMode; |
| 5017 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5018 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); |
| 5019 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5020 | } // release lock |
| 5021 | |
| 5022 | if (needWake) { |
| 5023 | mLooper->wake(); |
| 5024 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5025 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5026 | } |
| 5027 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5028 | bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { |
| 5029 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5030 | if (focusedToken == nullptr) { |
| 5031 | return false; |
| 5032 | } |
| 5033 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5034 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5035 | } |
| 5036 | |
| 5037 | bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { |
| 5038 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5039 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5040 | const sp<WindowInfoHandle> windowHandle = |
| 5041 | getWindowHandleLocked(connectionToken); |
| 5042 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5043 | }) != mInteractionConnectionTokens.end(); |
| 5044 | } |
| 5045 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5046 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5047 | if (opacity < 0 || opacity > 1) { |
| 5048 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5049 | return; |
| 5050 | } |
| 5051 | |
| 5052 | std::scoped_lock lock(mLock); |
| 5053 | mMaximumObscuringOpacityForTouch = opacity; |
| 5054 | } |
| 5055 | |
| 5056 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 5057 | std::scoped_lock lock(mLock); |
| 5058 | mBlockUntrustedTouchesMode = mode; |
| 5059 | } |
| 5060 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5061 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 5062 | const sp<IBinder>& token) { |
| 5063 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5064 | for (TouchedWindow& w : state.windows) { |
| 5065 | if (w.windowHandle->getToken() == token) { |
| 5066 | return std::make_pair(&state, &w); |
| 5067 | } |
| 5068 | } |
| 5069 | } |
| 5070 | return std::make_pair(nullptr, nullptr); |
| 5071 | } |
| 5072 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5073 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5074 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5075 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5076 | if (DEBUG_FOCUS) { |
| 5077 | ALOGD("Trivial transfer to same window."); |
| 5078 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5079 | return true; |
| 5080 | } |
| 5081 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5082 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5083 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5084 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5085 | // Find the target touch state and touched window by fromToken. |
| 5086 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 5087 | if (state == nullptr || touchedWindow == nullptr) { |
| 5088 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5089 | return false; |
| 5090 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5091 | |
| 5092 | const int32_t displayId = state->displayId; |
| 5093 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5094 | if (toWindowHandle == nullptr) { |
| 5095 | ALOGW("Cannot transfer focus because to window not found."); |
| 5096 | return false; |
| 5097 | } |
| 5098 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5099 | if (DEBUG_FOCUS) { |
| 5100 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5101 | touchedWindow->windowHandle->getName().c_str(), |
| 5102 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5103 | } |
| 5104 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5105 | // Erase old window. |
| 5106 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 5107 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 5108 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5109 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5110 | // Add new window. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5111 | int32_t newTargetFlags = |
| 5112 | oldTargetFlags & (InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5113 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
| 5114 | newTargetFlags |= InputTarget::FLAG_FOREGROUND; |
| 5115 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5116 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5117 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5118 | // Store the dragging window. |
| 5119 | if (isDragDrop) { |
| 5120 | mDragState = std::make_unique<DragState>(toWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5121 | } |
| 5122 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5123 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5124 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5125 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5126 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5127 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5128 | CancelationOptions |
| 5129 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5130 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5131 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5132 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5133 | } |
| 5134 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5135 | if (DEBUG_FOCUS) { |
| 5136 | logDispatchStateLocked(); |
| 5137 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5138 | } // release lock |
| 5139 | |
| 5140 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5141 | mLooper->wake(); |
| 5142 | return true; |
| 5143 | } |
| 5144 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5145 | /** |
| 5146 | * Get the touched foreground window on the given display. |
| 5147 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5148 | * window is being touched. |
| 5149 | */ |
| 5150 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5151 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5152 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5153 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5154 | return nullptr; |
| 5155 | } |
| 5156 | |
| 5157 | const TouchState& state = stateIt->second; |
| 5158 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5159 | // If multiple foreground windows are touched, return nullptr |
| 5160 | for (const TouchedWindow& window : state.windows) { |
| 5161 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 5162 | if (touchedForegroundWindow != nullptr) { |
| 5163 | ALOGI("Two or more foreground windows: %s and %s", |
| 5164 | touchedForegroundWindow->getName().c_str(), |
| 5165 | window.windowHandle->getName().c_str()); |
| 5166 | return nullptr; |
| 5167 | } |
| 5168 | touchedForegroundWindow = window.windowHandle; |
| 5169 | } |
| 5170 | } |
| 5171 | return touchedForegroundWindow; |
| 5172 | } |
| 5173 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5174 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5175 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5176 | sp<IBinder> fromToken; |
| 5177 | { // acquire lock |
| 5178 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5179 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5180 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5181 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5182 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5183 | return false; |
| 5184 | } |
| 5185 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5186 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5187 | if (from == nullptr) { |
| 5188 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5189 | return false; |
| 5190 | } |
| 5191 | |
| 5192 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5193 | } // release lock |
| 5194 | |
| 5195 | return transferTouchFocus(fromToken, destChannelToken); |
| 5196 | } |
| 5197 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5198 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5199 | if (DEBUG_FOCUS) { |
| 5200 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5201 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5202 | |
| 5203 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5204 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5205 | |
| 5206 | resetKeyRepeatLocked(); |
| 5207 | releasePendingEventLocked(); |
| 5208 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5209 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5210 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5211 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5212 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5213 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5214 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5215 | } |
| 5216 | |
| 5217 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5218 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5219 | dumpDispatchStateLocked(dump); |
| 5220 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5221 | std::istringstream stream(dump); |
| 5222 | std::string line; |
| 5223 | |
| 5224 | while (std::getline(stream, line, '\n')) { |
| 5225 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5226 | } |
| 5227 | } |
| 5228 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5229 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5230 | std::string dump; |
| 5231 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5232 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5233 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5234 | |
| 5235 | std::string windowName = "None"; |
| 5236 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5237 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5238 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5239 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5240 | : "token has capture without window"; |
| 5241 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5242 | 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] | 5243 | |
| 5244 | return dump; |
| 5245 | } |
| 5246 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5247 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5248 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5249 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5250 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5251 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5252 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5253 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5254 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5255 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5256 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5257 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5258 | const std::chrono::duration timeout = |
| 5259 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5260 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5261 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5262 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5263 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5264 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5265 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5266 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5267 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5268 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5269 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5270 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5271 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5272 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5273 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5274 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5275 | 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] | 5276 | state.displayId, toString(state.down), toString(state.split), |
| 5277 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5278 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5279 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5280 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5281 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5282 | dump += StringPrintf(INDENT4 |
| 5283 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5284 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5285 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5286 | } |
| 5287 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5288 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5289 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5290 | } |
| 5291 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5292 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5293 | } |
| 5294 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5295 | if (mDragState) { |
| 5296 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5297 | mDragState->dump(dump, INDENT2); |
| 5298 | } |
| 5299 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5300 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5301 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5302 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5303 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5304 | const auto& displayInfo = it->second; |
| 5305 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5306 | displayInfo.logicalHeight); |
| 5307 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5308 | } else { |
| 5309 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5310 | } |
| 5311 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5312 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5313 | dump += INDENT2 "Windows:\n"; |
| 5314 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5315 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5316 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5317 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5318 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5319 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5320 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5321 | "applicationInfo.name=%s, " |
| 5322 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5323 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5324 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5325 | windowInfo->displayId, |
| 5326 | windowInfo->inputConfig.string().c_str(), |
| 5327 | windowInfo->alpha, windowInfo->frameLeft, |
| 5328 | windowInfo->frameTop, windowInfo->frameRight, |
| 5329 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5330 | windowInfo->applicationInfo.name.c_str(), |
| 5331 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5332 | dump += dumpRegion(windowInfo->touchableRegion); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5333 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5334 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5335 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5336 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5337 | millis(windowInfo->dispatchingTimeout), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5338 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5339 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5340 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5341 | } |
| 5342 | } else { |
| 5343 | dump += INDENT2 "Windows: <none>\n"; |
| 5344 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5345 | } |
| 5346 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5347 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5348 | } |
| 5349 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5350 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5351 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5352 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5353 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5354 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5355 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5356 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5357 | } |
| 5358 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5359 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5360 | |
| 5361 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5362 | if (!mRecentQueue.empty()) { |
| 5363 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5364 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5365 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5366 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5367 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5368 | } |
| 5369 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5370 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5371 | } |
| 5372 | |
| 5373 | // Dump event currently being dispatched. |
| 5374 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5375 | dump += INDENT "PendingEvent:\n"; |
| 5376 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5377 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5378 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5379 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5380 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5381 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5382 | } |
| 5383 | |
| 5384 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5385 | if (!mInboundQueue.empty()) { |
| 5386 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5387 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5388 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5389 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5390 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5391 | } |
| 5392 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5393 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5394 | } |
| 5395 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5396 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5397 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5398 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5399 | const KeyReplacement& replacement = pair.first; |
| 5400 | int32_t newKeyCode = pair.second; |
| 5401 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5402 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5403 | } |
| 5404 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5405 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5406 | } |
| 5407 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5408 | if (!mCommandQueue.empty()) { |
| 5409 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5410 | } else { |
| 5411 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5412 | } |
| 5413 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5414 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5415 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5416 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5417 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5418 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5419 | connection->inputChannel->getFd().get(), |
| 5420 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5421 | connection->getWindowName().c_str(), |
| 5422 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5423 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5424 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5425 | if (!connection->outboundQueue.empty()) { |
| 5426 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5427 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5428 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5429 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5430 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5431 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5432 | } |
| 5433 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5434 | if (!connection->waitQueue.empty()) { |
| 5435 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5436 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5437 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5438 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5439 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5440 | } |
| 5441 | } |
| 5442 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5443 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5444 | } |
| 5445 | |
| 5446 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5447 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5448 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5449 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5450 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5451 | } |
| 5452 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5453 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5454 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5455 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5456 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5457 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5458 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5459 | } |
| 5460 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5461 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5462 | const size_t numMonitors = monitors.size(); |
| 5463 | for (size_t i = 0; i < numMonitors; i++) { |
| 5464 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5465 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5466 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5467 | dump += "\n"; |
| 5468 | } |
| 5469 | } |
| 5470 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5471 | class LooperEventCallback : public LooperCallback { |
| 5472 | public: |
| 5473 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5474 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5475 | |
| 5476 | private: |
| 5477 | std::function<int(int events)> mCallback; |
| 5478 | }; |
| 5479 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5480 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5481 | if (DEBUG_CHANNEL_CREATION) { |
| 5482 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5483 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5484 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5485 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5486 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5487 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5488 | |
| 5489 | if (result) { |
| 5490 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5491 | } |
| 5492 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5493 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5494 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5495 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5496 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5497 | sp<Connection> connection = |
| 5498 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5499 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5500 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5501 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5502 | } |
| 5503 | mConnectionsByToken.emplace(token, connection); |
| 5504 | |
| 5505 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5506 | this, std::placeholders::_1, token); |
| 5507 | |
| 5508 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5509 | } // release lock |
| 5510 | |
| 5511 | // Wake the looper because some connections have changed. |
| 5512 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5513 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5514 | } |
| 5515 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5516 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5517 | const std::string& name, |
| 5518 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5519 | std::shared_ptr<InputChannel> serverChannel; |
| 5520 | std::unique_ptr<InputChannel> clientChannel; |
| 5521 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5522 | if (result) { |
| 5523 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5524 | } |
| 5525 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5526 | { // acquire lock |
| 5527 | std::scoped_lock _l(mLock); |
| 5528 | |
| 5529 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5530 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5531 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5532 | } |
| 5533 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5534 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5535 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5536 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5537 | |
| 5538 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5539 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5540 | } |
| 5541 | mConnectionsByToken.emplace(token, connection); |
| 5542 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5543 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5544 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5545 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5546 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5547 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5548 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5549 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5550 | // Wake the looper because some connections have changed. |
| 5551 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5552 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5553 | } |
| 5554 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5555 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5556 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5557 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5558 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5559 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5560 | if (status) { |
| 5561 | return status; |
| 5562 | } |
| 5563 | } // release lock |
| 5564 | |
| 5565 | // Wake the poll loop because removing the connection may have changed the current |
| 5566 | // synchronization state. |
| 5567 | mLooper->wake(); |
| 5568 | return OK; |
| 5569 | } |
| 5570 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5571 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5572 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5573 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5574 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5575 | // 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] | 5576 | return BAD_VALUE; |
| 5577 | } |
| 5578 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5579 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5580 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5581 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5582 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5583 | } |
| 5584 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5585 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5586 | |
| 5587 | nsecs_t currentTime = now(); |
| 5588 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5589 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5590 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5591 | return OK; |
| 5592 | } |
| 5593 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5594 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5595 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5596 | auto& [displayId, monitors] = *it; |
| 5597 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5598 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5599 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5600 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5601 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5602 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5603 | } else { |
| 5604 | ++it; |
| 5605 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5606 | } |
| 5607 | } |
| 5608 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5609 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5610 | std::scoped_lock _l(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5611 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5612 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5613 | if (!requestingChannel) { |
| 5614 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5615 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5616 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5617 | |
| 5618 | auto [statePtr, windowPtr] = findTouchStateAndWindowLocked(token); |
| 5619 | if (statePtr == nullptr || windowPtr == nullptr || !statePtr->down) { |
| 5620 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5621 | " Ignoring."); |
| 5622 | return BAD_VALUE; |
| 5623 | } |
| 5624 | |
| 5625 | TouchState& state = *statePtr; |
| 5626 | |
| 5627 | // Send cancel events to all the input channels we're stealing from. |
| 5628 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5629 | "input channel stole pointer stream"); |
| 5630 | options.deviceId = state.deviceId; |
| 5631 | options.displayId = state.displayId; |
| 5632 | std::string canceledWindows; |
| 5633 | for (const TouchedWindow& window : state.windows) { |
| 5634 | const std::shared_ptr<InputChannel> channel = |
| 5635 | getInputChannelLocked(window.windowHandle->getToken()); |
| 5636 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5637 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5638 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5639 | canceledWindows += channel->getName(); |
| 5640 | } |
| 5641 | } |
| 5642 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5643 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5644 | canceledWindows.c_str()); |
| 5645 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5646 | // Prevent the gesture from being sent to any other windows. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5647 | state.filterWindowsExcept(token); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5648 | state.preventNewTargets = true; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5649 | return OK; |
| 5650 | } |
| 5651 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5652 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5653 | { // acquire lock |
| 5654 | std::scoped_lock _l(mLock); |
| 5655 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5656 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5657 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5658 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5659 | : "token without window"); |
| 5660 | } |
| 5661 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5662 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5663 | if (focusedToken != windowToken) { |
| 5664 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5665 | enabled ? "enable" : "disable"); |
| 5666 | return; |
| 5667 | } |
| 5668 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5669 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5670 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5671 | "window has %s requested pointer capture.", |
| 5672 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5673 | return; |
| 5674 | } |
| 5675 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5676 | if (enabled) { |
| 5677 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5678 | mIneligibleDisplaysForPointerCapture.end(), |
| 5679 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5680 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5681 | return; |
| 5682 | } |
| 5683 | } |
| 5684 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5685 | setPointerCaptureLocked(enabled); |
| 5686 | } // release lock |
| 5687 | |
| 5688 | // Wake the thread to process command entries. |
| 5689 | mLooper->wake(); |
| 5690 | } |
| 5691 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5692 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5693 | { // acquire lock |
| 5694 | std::scoped_lock _l(mLock); |
| 5695 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5696 | if (!isEligible) { |
| 5697 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5698 | } |
| 5699 | } // release lock |
| 5700 | } |
| 5701 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5702 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5703 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5704 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5705 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5706 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5707 | } |
| 5708 | } |
| 5709 | } |
| 5710 | return std::nullopt; |
| 5711 | } |
| 5712 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5713 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5714 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5715 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5716 | } |
| 5717 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5718 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5719 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5720 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5721 | } |
| 5722 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5723 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5724 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5725 | } |
| 5726 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5727 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5728 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5729 | if (connection == nullptr) { |
| 5730 | return "<nullptr>"; |
| 5731 | } |
| 5732 | return connection->getInputChannelName(); |
| 5733 | } |
| 5734 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5735 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5736 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5737 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5738 | } |
| 5739 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5740 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5741 | const sp<Connection>& connection, uint32_t seq, |
| 5742 | bool handled, nsecs_t consumeTime) { |
| 5743 | // Handle post-event policy actions. |
| 5744 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5745 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5746 | return; |
| 5747 | } |
| 5748 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5749 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5750 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5751 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5752 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5753 | } |
| 5754 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5755 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5756 | connection->inputChannel->getConnectionToken(), |
| 5757 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5758 | } |
| 5759 | |
| 5760 | bool restartEvent; |
| 5761 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5762 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5763 | restartEvent = |
| 5764 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5765 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5766 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5767 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5768 | handled); |
| 5769 | } else { |
| 5770 | restartEvent = false; |
| 5771 | } |
| 5772 | |
| 5773 | // Dequeue the event and start the next cycle. |
| 5774 | // Because the lock might have been released, it is possible that the |
| 5775 | // contents of the wait queue to have been drained, so we need to double-check |
| 5776 | // a few things. |
| 5777 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5778 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5779 | dispatchEntry = *dispatchEntryIt; |
| 5780 | connection->waitQueue.erase(dispatchEntryIt); |
| 5781 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5782 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5783 | if (!connection->responsive) { |
| 5784 | connection->responsive = isConnectionResponsive(*connection); |
| 5785 | if (connection->responsive) { |
| 5786 | // The connection was unresponsive, and now it's responsive. |
| 5787 | processConnectionResponsiveLocked(*connection); |
| 5788 | } |
| 5789 | } |
| 5790 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5791 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5792 | connection->outboundQueue.push_front(dispatchEntry); |
| 5793 | traceOutboundQueueLength(*connection); |
| 5794 | } else { |
| 5795 | releaseDispatchEntry(dispatchEntry); |
| 5796 | } |
| 5797 | } |
| 5798 | |
| 5799 | // Start the next dispatch cycle for this connection. |
| 5800 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5801 | } |
| 5802 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5803 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5804 | const sp<IBinder>& newToken) { |
| 5805 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5806 | scoped_unlock unlock(mLock); |
| 5807 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5808 | }; |
| 5809 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5810 | } |
| 5811 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5812 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5813 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5814 | scoped_unlock unlock(mLock); |
| 5815 | mPolicy->notifyDropWindow(token, x, y); |
| 5816 | }; |
| 5817 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5818 | } |
| 5819 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5820 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5821 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5822 | scoped_unlock unlock(mLock); |
| 5823 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5824 | }; |
| 5825 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5826 | } |
| 5827 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5828 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5829 | if (connection == nullptr) { |
| 5830 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5831 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5832 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5833 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5834 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5835 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5836 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5837 | return; |
| 5838 | } |
| 5839 | /** |
| 5840 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5841 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5842 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5843 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5844 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5845 | * most useful. |
| 5846 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5847 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5848 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5849 | std::string reason = |
| 5850 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5851 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5852 | ns2ms(currentWait), |
| 5853 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5854 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5855 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5856 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5857 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5858 | |
| 5859 | // Stop waking up for events on this connection, it is already unresponsive |
| 5860 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5861 | } |
| 5862 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5863 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5864 | std::string reason = |
| 5865 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5866 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5867 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5868 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5869 | scoped_unlock unlock(mLock); |
| 5870 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5871 | }; |
| 5872 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5873 | } |
| 5874 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5875 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5876 | const std::string& reason) { |
| 5877 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5878 | updateLastAnrStateLocked(windowLabel, reason); |
| 5879 | } |
| 5880 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5881 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5882 | const std::string& reason) { |
| 5883 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5884 | updateLastAnrStateLocked(windowLabel, reason); |
| 5885 | } |
| 5886 | |
| 5887 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5888 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5889 | // 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] | 5890 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5891 | struct tm tm; |
| 5892 | localtime_r(&t, &tm); |
| 5893 | char timestr[64]; |
| 5894 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5895 | mLastAnrState.clear(); |
| 5896 | mLastAnrState += INDENT "ANR:\n"; |
| 5897 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5898 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5899 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5900 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5901 | } |
| 5902 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5903 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5904 | KeyEntry& entry) { |
| 5905 | const KeyEvent event = createKeyEvent(entry); |
| 5906 | nsecs_t delay = 0; |
| 5907 | { // release lock |
| 5908 | scoped_unlock unlock(mLock); |
| 5909 | android::base::Timer t; |
| 5910 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5911 | entry.policyFlags); |
| 5912 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5913 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5914 | std::to_string(t.duration().count()).c_str()); |
| 5915 | } |
| 5916 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5917 | |
| 5918 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5919 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5920 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5921 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5922 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5923 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5924 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5925 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5926 | } |
| 5927 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5928 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5929 | std::optional<int32_t> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5930 | std::string reason) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5931 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5932 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5933 | mPolicy->notifyWindowUnresponsive(token, pid, reason); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5934 | }; |
| 5935 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5936 | } |
| 5937 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5938 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
| 5939 | std::optional<int32_t> pid) { |
| 5940 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5941 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5942 | mPolicy->notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5943 | }; |
| 5944 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5945 | } |
| 5946 | |
| 5947 | /** |
| 5948 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5949 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5950 | * command entry to the command queue. |
| 5951 | */ |
| 5952 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5953 | std::string reason) { |
| 5954 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5955 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5956 | if (connection.monitor) { |
| 5957 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5958 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5959 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 5960 | } else { |
| 5961 | // The connection is a window |
| 5962 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5963 | reason.c_str()); |
| 5964 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 5965 | if (handle != nullptr) { |
| 5966 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5967 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5968 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5969 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5970 | } |
| 5971 | |
| 5972 | /** |
| 5973 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5974 | */ |
| 5975 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5976 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5977 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5978 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5979 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 5980 | } else { |
| 5981 | // The connection is a window |
| 5982 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 5983 | if (handle != nullptr) { |
| 5984 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5985 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5986 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5987 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5988 | } |
| 5989 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5990 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5991 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5992 | KeyEntry& keyEntry, bool handled) { |
| 5993 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5994 | if (!handled) { |
| 5995 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5996 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5997 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5998 | return false; |
| 5999 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6000 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6001 | // Get the fallback key state. |
| 6002 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6003 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6004 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6005 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6006 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6007 | } |
| 6008 | |
| 6009 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6010 | // If the application handles the original key for which we previously |
| 6011 | // generated a fallback or if the window is not a foreground window, |
| 6012 | // then cancel the associated fallback key, if any. |
| 6013 | if (fallbackKeyCode != -1) { |
| 6014 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6015 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6016 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6017 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6018 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6019 | keyEntry.policyFlags); |
| 6020 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6021 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6022 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6023 | |
| 6024 | mLock.unlock(); |
| 6025 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6026 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6027 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6028 | |
| 6029 | mLock.lock(); |
| 6030 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6031 | // Cancel the fallback key. |
| 6032 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6033 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6034 | "application handled the original non-fallback key " |
| 6035 | "or is no longer a foreground target, " |
| 6036 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6037 | options.keyCode = fallbackKeyCode; |
| 6038 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6039 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6040 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6041 | } |
| 6042 | } else { |
| 6043 | // If the application did not handle a non-fallback key, first check |
| 6044 | // that we are in a good state to perform unhandled key event processing |
| 6045 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6046 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6047 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6048 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6049 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6050 | "since this is not an initial down. " |
| 6051 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6052 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6053 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6054 | return false; |
| 6055 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6056 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6057 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6058 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6059 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6060 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6061 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6062 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6063 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6064 | |
| 6065 | mLock.unlock(); |
| 6066 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6067 | bool fallback = |
| 6068 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6069 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6070 | |
| 6071 | mLock.lock(); |
| 6072 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6073 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6074 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6075 | return false; |
| 6076 | } |
| 6077 | |
| 6078 | // Latch the fallback keycode for this key on an initial down. |
| 6079 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6080 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6081 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6082 | fallbackKeyCode = event.getKeyCode(); |
| 6083 | } else { |
| 6084 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6085 | } |
| 6086 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6087 | } |
| 6088 | |
| 6089 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6090 | |
| 6091 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6092 | // We will continue to dispatch the key to the policy but we will no |
| 6093 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6094 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6095 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6096 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6097 | if (fallback) { |
| 6098 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6099 | "as a fallback for %d, but on the DOWN it had requested " |
| 6100 | "to send %d instead. Fallback canceled.", |
| 6101 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6102 | } else { |
| 6103 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6104 | "but on the DOWN it had requested to send %d. " |
| 6105 | "Fallback canceled.", |
| 6106 | originalKeyCode, fallbackKeyCode); |
| 6107 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6108 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6109 | |
| 6110 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6111 | "canceling fallback, policy no longer desires it"); |
| 6112 | options.keyCode = fallbackKeyCode; |
| 6113 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6114 | |
| 6115 | fallback = false; |
| 6116 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6117 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6118 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6119 | } |
| 6120 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6121 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6122 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6123 | { |
| 6124 | std::string msg; |
| 6125 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6126 | connection->inputState.getFallbackKeys(); |
| 6127 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6128 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6129 | } |
| 6130 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6131 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6132 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6133 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6134 | |
| 6135 | if (fallback) { |
| 6136 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6137 | keyEntry.eventTime = event.getEventTime(); |
| 6138 | keyEntry.deviceId = event.getDeviceId(); |
| 6139 | keyEntry.source = event.getSource(); |
| 6140 | keyEntry.displayId = event.getDisplayId(); |
| 6141 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6142 | keyEntry.keyCode = fallbackKeyCode; |
| 6143 | keyEntry.scanCode = event.getScanCode(); |
| 6144 | keyEntry.metaState = event.getMetaState(); |
| 6145 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6146 | keyEntry.downTime = event.getDownTime(); |
| 6147 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6148 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6149 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6150 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6151 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6152 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6153 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6154 | return true; // restart the event |
| 6155 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6156 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6157 | ALOGD("Unhandled key event: No fallback key."); |
| 6158 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6159 | |
| 6160 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6161 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6162 | } |
| 6163 | } |
| 6164 | return false; |
| 6165 | } |
| 6166 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6167 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6168 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6169 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6170 | return false; |
| 6171 | } |
| 6172 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6173 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6174 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6175 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6176 | } |
| 6177 | } |
| 6178 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6179 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6180 | if (ATRACE_ENABLED()) { |
| 6181 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6182 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6183 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6184 | } |
| 6185 | } |
| 6186 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6187 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6188 | if (ATRACE_ENABLED()) { |
| 6189 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6190 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6191 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6192 | } |
| 6193 | } |
| 6194 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6195 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6196 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6197 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6198 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6199 | dumpDispatchStateLocked(dump); |
| 6200 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6201 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6202 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6203 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6204 | } |
| 6205 | } |
| 6206 | |
| 6207 | void InputDispatcher::monitor() { |
| 6208 | // 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] | 6209 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6210 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6211 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6212 | } |
| 6213 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6214 | /** |
| 6215 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6216 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6217 | * this method can be safely called from any thread, as long as you've ensured that |
| 6218 | * the work you are interested in completing has already been queued. |
| 6219 | */ |
| 6220 | bool InputDispatcher::waitForIdle() { |
| 6221 | /** |
| 6222 | * Timeout should represent the longest possible time that a device might spend processing |
| 6223 | * events and commands. |
| 6224 | */ |
| 6225 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6226 | std::unique_lock lock(mLock); |
| 6227 | mLooper->wake(); |
| 6228 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6229 | return result == std::cv_status::no_timeout; |
| 6230 | } |
| 6231 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6232 | /** |
| 6233 | * Sets focus to the window identified by the token. This must be called |
| 6234 | * after updating any input window handles. |
| 6235 | * |
| 6236 | * Params: |
| 6237 | * request.token - input channel token used to identify the window that should gain focus. |
| 6238 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6239 | * specified token does not match the currently focused window, this request will be dropped. |
| 6240 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6241 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6242 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6243 | * when requesting the focus change. This determines which request gets |
| 6244 | * precedence if there is a focus change request from another source such as pointer down. |
| 6245 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6246 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6247 | { // acquire lock |
| 6248 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6249 | std::optional<FocusResolver::FocusChanges> changes = |
| 6250 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6251 | if (changes) { |
| 6252 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6253 | } |
| 6254 | } // release lock |
| 6255 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6256 | mLooper->wake(); |
| 6257 | } |
| 6258 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6259 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6260 | if (changes.oldFocus) { |
| 6261 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6262 | if (focusedInputChannel) { |
| 6263 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6264 | "focus left window"); |
| 6265 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6266 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6267 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6268 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6269 | if (changes.newFocus) { |
| 6270 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6271 | } |
| 6272 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6273 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6274 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6275 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6276 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6277 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6278 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6279 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6280 | // front. |
| 6281 | disablePointerCaptureForcedLocked(); |
| 6282 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6283 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6284 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6285 | } |
| 6286 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6287 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6288 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6289 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6290 | return; |
| 6291 | } |
| 6292 | |
| 6293 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6294 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6295 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6296 | setPointerCaptureLocked(false); |
| 6297 | } |
| 6298 | |
| 6299 | if (!mWindowTokenWithPointerCapture) { |
| 6300 | // No need to send capture changes because no window has capture. |
| 6301 | return; |
| 6302 | } |
| 6303 | |
| 6304 | if (mPendingEvent != nullptr) { |
| 6305 | // Move the pending event to the front of the queue. This will give the chance |
| 6306 | // for the pending event to be dropped if it is a captured event. |
| 6307 | mInboundQueue.push_front(mPendingEvent); |
| 6308 | mPendingEvent = nullptr; |
| 6309 | } |
| 6310 | |
| 6311 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6312 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6313 | mInboundQueue.push_front(std::move(entry)); |
| 6314 | } |
| 6315 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6316 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6317 | mCurrentPointerCaptureRequest.enable = enable; |
| 6318 | mCurrentPointerCaptureRequest.seq++; |
| 6319 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6320 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6321 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6322 | }; |
| 6323 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6324 | } |
| 6325 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6326 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6327 | { // acquire lock |
| 6328 | std::scoped_lock _l(mLock); |
| 6329 | // Set an empty list to remove all handles from the specific display. |
| 6330 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6331 | setFocusedApplicationLocked(displayId, nullptr); |
| 6332 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6333 | // have been removed for the removed display. |
| 6334 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6335 | // Reset pointer capture eligibility, regardless of previous state. |
| 6336 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6337 | } // release lock |
| 6338 | |
| 6339 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6340 | mLooper->wake(); |
| 6341 | } |
| 6342 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6343 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6344 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6345 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6346 | // more convenient parsing. |
| 6347 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6348 | for (const auto& info : windowInfos) { |
| 6349 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6350 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6351 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6352 | |
| 6353 | { // acquire lock |
| 6354 | std::scoped_lock _l(mLock); |
| 6355 | mDisplayInfos.clear(); |
| 6356 | for (const auto& displayInfo : displayInfos) { |
| 6357 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6358 | } |
| 6359 | |
| 6360 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6361 | setInputWindowsLocked(handles, displayId); |
| 6362 | } |
| 6363 | } |
| 6364 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6365 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6366 | } |
| 6367 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6368 | bool InputDispatcher::shouldDropInput( |
| 6369 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6370 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6371 | (windowHandle->getInfo()->inputConfig.test( |
| 6372 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6373 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6374 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6375 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6376 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6377 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6378 | windowHandle->getInfo()->displayId); |
| 6379 | return true; |
| 6380 | } |
| 6381 | return false; |
| 6382 | } |
| 6383 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6384 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6385 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6386 | const std::vector<DisplayInfo>& displayInfos) { |
| 6387 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6388 | } |
| 6389 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6390 | void InputDispatcher::cancelCurrentTouch() { |
| 6391 | { |
| 6392 | std::scoped_lock _l(mLock); |
| 6393 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
| 6394 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 6395 | "cancel current touch"); |
| 6396 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6397 | |
| 6398 | mTouchStatesByDisplay.clear(); |
| 6399 | mLastHoverWindowHandle.clear(); |
| 6400 | } |
| 6401 | // Wake up poll loop since there might be work to do. |
| 6402 | mLooper->wake(); |
| 6403 | } |
| 6404 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6405 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6406 | std::scoped_lock _l(mLock); |
| 6407 | mMonitorDispatchingTimeout = timeout; |
| 6408 | } |
| 6409 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6410 | } // namespace android::inputdispatcher |