| 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 |  | 
| Prabir Pradhan | d2c9e8e | 2021-05-24 15:00:12 -0700 | [diff] [blame] | 22 | #include <InputFlingerProperties.sysprop.h> | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 23 | #include <android-base/chrono_utils.h> | 
| Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 24 | #include <android-base/properties.h> | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 26 | #include <android/os/IInputConstants.h> | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 27 | #include <binder/Binder.h> | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> | 
|  | 29 | #include <com/android/internal/compat/IPlatformCompatNative.h> | 
| chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 30 | #include <gui/SurfaceComposerClient.h> | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 31 | #include <input/InputDevice.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 32 | #include <log/log.h> | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 33 | #include <log/log_event_list.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 34 | #include <powermanager/PowerManager.h> | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 35 | #include <unistd.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 36 | #include <utils/Trace.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 |  | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 38 | #include <cerrno> | 
|  | 39 | #include <cinttypes> | 
|  | 40 | #include <climits> | 
|  | 41 | #include <cstddef> | 
|  | 42 | #include <ctime> | 
|  | 43 | #include <queue> | 
|  | 44 | #include <sstream> | 
|  | 45 |  | 
|  | 46 | #include "Connection.h" | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 47 | #include "InputDispatcher.h" | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 48 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 49 | #define INDENT "  " | 
|  | 50 | #define INDENT2 "    " | 
|  | 51 | #define INDENT3 "      " | 
|  | 52 | #define INDENT4 "        " | 
|  | 53 |  | 
| Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 54 | using android::base::HwTimeoutMultiplier; | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 55 | using android::base::Result; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 56 | using android::base::StringPrintf; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 57 | using android::gui::FocusRequest; | 
|  | 58 | using android::gui::TouchOcclusionMode; | 
|  | 59 | using android::gui::WindowInfo; | 
|  | 60 | using android::gui::WindowInfoHandle; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 61 | using android::os::BlockUntrustedTouchesMode; | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 62 | using android::os::IInputConstants; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 63 | using android::os::InputEventInjectionResult; | 
|  | 64 | using android::os::InputEventInjectionSync; | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 65 | using com::android::internal::compat::IPlatformCompatNative; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 66 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 67 | namespace android::inputdispatcher { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 68 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 69 | namespace { | 
|  | 70 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 71 | // Log detailed debug messages about each inbound event notification to the dispatcher. | 
|  | 72 | constexpr bool DEBUG_INBOUND_EVENT_DETAILS = false; | 
|  | 73 |  | 
|  | 74 | // Log detailed debug messages about each outbound event processed by the dispatcher. | 
|  | 75 | constexpr bool DEBUG_OUTBOUND_EVENT_DETAILS = false; | 
|  | 76 |  | 
|  | 77 | // Log debug messages about the dispatch cycle. | 
|  | 78 | constexpr bool DEBUG_DISPATCH_CYCLE = false; | 
|  | 79 |  | 
|  | 80 | // Log debug messages about channel creation | 
|  | 81 | constexpr bool DEBUG_CHANNEL_CREATION = false; | 
|  | 82 |  | 
|  | 83 | // Log debug messages about input event injection. | 
|  | 84 | constexpr bool DEBUG_INJECTION = false; | 
|  | 85 |  | 
|  | 86 | // Log debug messages about input focus tracking. | 
|  | 87 | constexpr bool DEBUG_FOCUS = false; | 
|  | 88 |  | 
|  | 89 | // Log debug messages about touch occlusion | 
|  | 90 | // STOPSHIP(b/169067926): Set to false | 
|  | 91 | constexpr bool DEBUG_TOUCH_OCCLUSION = true; | 
|  | 92 |  | 
|  | 93 | // Log debug messages about the app switch latency optimization. | 
|  | 94 | constexpr bool DEBUG_APP_SWITCH = false; | 
|  | 95 |  | 
|  | 96 | // Log debug messages about hover events. | 
|  | 97 | constexpr bool DEBUG_HOVER = false; | 
|  | 98 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 99 | // Temporarily releases a held mutex for the lifetime of the instance. | 
|  | 100 | // Named to match std::scoped_lock | 
|  | 101 | class scoped_unlock { | 
|  | 102 | public: | 
|  | 103 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } | 
|  | 104 | ~scoped_unlock() { mMutex.lock(); } | 
|  | 105 |  | 
|  | 106 | private: | 
|  | 107 | std::mutex& mMutex; | 
|  | 108 | }; | 
|  | 109 |  | 
| Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 110 | // When per-window-input-rotation is enabled, InputFlinger works in the un-rotated display | 
|  | 111 | // coordinates and SurfaceFlinger includes the display rotation in the input window transforms. | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 112 | bool isPerWindowInputRotationEnabled() { | 
| Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 113 | static const bool PER_WINDOW_INPUT_ROTATION = | 
| Prabir Pradhan | d2c9e8e | 2021-05-24 15:00:12 -0700 | [diff] [blame] | 114 | sysprop::InputFlingerProperties::per_window_input_rotation().value_or(false); | 
|  | 115 |  | 
| Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 116 | return PER_WINDOW_INPUT_ROTATION; | 
|  | 117 | } | 
|  | 118 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 119 | // Default input dispatching timeout if there is no focused application or paused window | 
|  | 120 | // from which to determine an appropriate dispatching timeout. | 
| Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 121 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( | 
|  | 122 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * | 
|  | 123 | HwTimeoutMultiplier()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 124 |  | 
|  | 125 | // Amount of time to allow for all pending events to be processed when an app switch | 
|  | 126 | // key is on the way.  This is used to preempt input dispatch and drop input events | 
|  | 127 | // 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] | 128 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 129 |  | 
|  | 130 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) | 
|  | 131 | // before considering it stale and dropping it. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 132 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 133 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 134 | // 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] | 135 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec | 
|  | 136 |  | 
|  | 137 | // Log a warning when an interception call takes longer than this to process. | 
|  | 138 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 139 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 140 | // Additional key latency in case a connection is still processing some motion events. | 
|  | 141 | // This will help with the case when a user touched a button that opens a new window, | 
|  | 142 | // and gives us the chance to dispatch the key to this new window. | 
|  | 143 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; | 
|  | 144 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 145 | // Number of recent events to keep for debugging purposes. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 146 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; | 
|  | 147 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 148 | // Event log tags. See EventLogTags.logtags for reference | 
|  | 149 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; | 
|  | 150 | constexpr int LOGTAG_INPUT_FOCUS = 62001; | 
|  | 151 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 152 | inline nsecs_t now() { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 153 | return systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 154 | } | 
|  | 155 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 156 | inline const char* toString(bool value) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 157 | return value ? "true" : "false"; | 
|  | 158 | } | 
|  | 159 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 160 | inline const std::string toString(const sp<IBinder>& binder) { | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 161 | if (binder == nullptr) { | 
|  | 162 | return "<null>"; | 
|  | 163 | } | 
|  | 164 | return StringPrintf("%p", binder.get()); | 
|  | 165 | } | 
|  | 166 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 167 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 168 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> | 
|  | 169 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 172 | bool isValidKeyAction(int32_t action) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 173 | switch (action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 174 | case AKEY_EVENT_ACTION_DOWN: | 
|  | 175 | case AKEY_EVENT_ACTION_UP: | 
|  | 176 | return true; | 
|  | 177 | default: | 
|  | 178 | return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 179 | } | 
|  | 180 | } | 
|  | 181 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 182 | bool validateKeyEvent(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 183 | if (!isValidKeyAction(action)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 184 | ALOGE("Key event has invalid action code 0x%x", action); | 
|  | 185 | return false; | 
|  | 186 | } | 
|  | 187 | return true; | 
|  | 188 | } | 
|  | 189 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 190 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 191 | switch (action & AMOTION_EVENT_ACTION_MASK) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 192 | case AMOTION_EVENT_ACTION_DOWN: | 
|  | 193 | case AMOTION_EVENT_ACTION_UP: | 
|  | 194 | case AMOTION_EVENT_ACTION_CANCEL: | 
|  | 195 | case AMOTION_EVENT_ACTION_MOVE: | 
|  | 196 | case AMOTION_EVENT_ACTION_OUTSIDE: | 
|  | 197 | case AMOTION_EVENT_ACTION_HOVER_ENTER: | 
|  | 198 | case AMOTION_EVENT_ACTION_HOVER_MOVE: | 
|  | 199 | case AMOTION_EVENT_ACTION_HOVER_EXIT: | 
|  | 200 | case AMOTION_EVENT_ACTION_SCROLL: | 
|  | 201 | return true; | 
|  | 202 | case AMOTION_EVENT_ACTION_POINTER_DOWN: | 
|  | 203 | case AMOTION_EVENT_ACTION_POINTER_UP: { | 
|  | 204 | int32_t index = getMotionEventActionPointerIndex(action); | 
|  | 205 | return index >= 0 && index < pointerCount; | 
|  | 206 | } | 
|  | 207 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: | 
|  | 208 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: | 
|  | 209 | return actionButton != 0; | 
|  | 210 | default: | 
|  | 211 | return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 212 | } | 
|  | 213 | } | 
|  | 214 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 215 | int64_t millis(std::chrono::nanoseconds t) { | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 216 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); | 
|  | 217 | } | 
|  | 218 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 219 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, | 
|  | 220 | const PointerProperties* pointerProperties) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 221 | if (!isValidMotionAction(action, actionButton, pointerCount)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 222 | ALOGE("Motion event has invalid action code 0x%x", action); | 
|  | 223 | return false; | 
|  | 224 | } | 
|  | 225 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { | 
| Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 226 | ALOGE("Motion event has invalid pointer count %zu; value must be between 1 and %d.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 227 | pointerCount, MAX_POINTERS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 228 | return false; | 
|  | 229 | } | 
|  | 230 | BitSet32 pointerIdBits; | 
|  | 231 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 232 | int32_t id = pointerProperties[i].id; | 
|  | 233 | if (id < 0 || id > MAX_POINTER_ID) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 234 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, | 
|  | 235 | MAX_POINTER_ID); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 236 | return false; | 
|  | 237 | } | 
|  | 238 | if (pointerIdBits.hasBit(id)) { | 
|  | 239 | ALOGE("Motion event has duplicate pointer id %d", id); | 
|  | 240 | return false; | 
|  | 241 | } | 
|  | 242 | pointerIdBits.markBit(id); | 
|  | 243 | } | 
|  | 244 | return true; | 
|  | 245 | } | 
|  | 246 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 247 | std::string dumpRegion(const Region& region) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 248 | if (region.isEmpty()) { | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 249 | return "<empty>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 250 | } | 
|  | 251 |  | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 252 | std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 253 | bool first = true; | 
|  | 254 | Region::const_iterator cur = region.begin(); | 
|  | 255 | Region::const_iterator const tail = region.end(); | 
|  | 256 | while (cur != tail) { | 
|  | 257 | if (first) { | 
|  | 258 | first = false; | 
|  | 259 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 260 | dump += "|"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 261 | } | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 262 | 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] | 263 | cur++; | 
|  | 264 | } | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 265 | return dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 266 | } | 
|  | 267 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 268 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 269 | constexpr size_t maxEntries = 50; // max events to print | 
|  | 270 | constexpr size_t skipBegin = maxEntries / 2; | 
|  | 271 | const size_t skipEnd = queue.size() - maxEntries / 2; | 
|  | 272 | // skip from maxEntries / 2 ... size() - maxEntries/2 | 
|  | 273 | // only print from 0 .. skipBegin and then from skipEnd .. size() | 
|  | 274 |  | 
|  | 275 | std::string dump; | 
|  | 276 | for (size_t i = 0; i < queue.size(); i++) { | 
|  | 277 | const DispatchEntry& entry = *queue[i]; | 
|  | 278 | if (i >= skipBegin && i < skipEnd) { | 
|  | 279 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); | 
|  | 280 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' | 
|  | 281 | continue; | 
|  | 282 | } | 
|  | 283 | dump.append(INDENT4); | 
|  | 284 | dump += entry.eventEntry->getDescription(); | 
|  | 285 | dump += StringPrintf(", seq=%" PRIu32 | 
|  | 286 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", | 
|  | 287 | entry.seq, entry.targetFlags, entry.resolvedAction, | 
|  | 288 | ns2ms(currentTime - entry.eventEntry->eventTime)); | 
|  | 289 | if (entry.deliveryTime != 0) { | 
|  | 290 | // This entry was delivered, so add information on how long we've been waiting | 
|  | 291 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); | 
|  | 292 | } | 
|  | 293 | dump.append("\n"); | 
|  | 294 | } | 
|  | 295 | return dump; | 
|  | 296 | } | 
|  | 297 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 298 | /** | 
|  | 299 | * Find the entry in std::unordered_map by key, and return it. | 
|  | 300 | * If the entry is not found, return a default constructed entry. | 
|  | 301 | * | 
|  | 302 | * Useful when the entries are vectors, since an empty vector will be returned | 
|  | 303 | * if the entry is not found. | 
|  | 304 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. | 
|  | 305 | */ | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 306 | template <typename K, typename V> | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 307 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 308 | auto it = map.find(key); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 309 | return it != map.end() ? it->second : V{}; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 310 | } | 
|  | 311 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 312 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 313 | if (first == second) { | 
|  | 314 | return true; | 
|  | 315 | } | 
|  | 316 |  | 
|  | 317 | if (first == nullptr || second == nullptr) { | 
|  | 318 | return false; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | return first->getToken() == second->getToken(); | 
|  | 322 | } | 
|  | 323 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 324 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { | 
| Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 325 | if (first == nullptr || second == nullptr) { | 
|  | 326 | return false; | 
|  | 327 | } | 
|  | 328 | return first->applicationInfo.token != nullptr && | 
|  | 329 | first->applicationInfo.token == second->applicationInfo.token; | 
|  | 330 | } | 
|  | 331 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 332 | bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { | 
| Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 333 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; | 
|  | 334 | } | 
|  | 335 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 336 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, | 
|  | 337 | std::shared_ptr<EventEntry> eventEntry, | 
|  | 338 | int32_t inputTargetFlags) { | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 339 | if (eventEntry->type == EventEntry::Type::MOTION) { | 
|  | 340 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
| Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 341 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_JOYSTICK) || | 
|  | 342 | (motionEntry.source & AINPUT_SOURCE_CLASS_POSITION)) { | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 343 | const ui::Transform identityTransform; | 
| Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 344 | // Use identity transform for joystick and position-based (touchpad) events because they | 
|  | 345 | // don't depend on the window transform. | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 346 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, identityTransform, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 347 | 1.0f /*globalScaleFactor*/, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 348 | inputTarget.displayOrientation, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 349 | inputTarget.displaySize); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 350 | } | 
|  | 351 | } | 
|  | 352 |  | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 353 | if (inputTarget.useDefaultPointerTransform()) { | 
|  | 354 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 355 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 356 | inputTarget.globalScaleFactor, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 357 | inputTarget.displayOrientation, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 358 | inputTarget.displaySize); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 359 | } | 
|  | 360 |  | 
|  | 361 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); | 
|  | 362 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
|  | 363 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 364 | std::vector<PointerCoords> pointerCoords; | 
|  | 365 | pointerCoords.resize(motionEntry.pointerCount); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 366 |  | 
|  | 367 | // Use the first pointer information to normalize all other pointers. This could be any pointer | 
|  | 368 | // 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] | 369 | // uses the transform for the normalized pointer. | 
|  | 370 | const ui::Transform& firstPointerTransform = | 
|  | 371 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; | 
|  | 372 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 373 |  | 
|  | 374 | // Iterate through all pointers in the event to normalize against the first. | 
|  | 375 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { | 
|  | 376 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; | 
|  | 377 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 378 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 379 |  | 
|  | 380 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 381 | // First, apply the current pointer's transform to update the coordinates into | 
|  | 382 | // window space. | 
|  | 383 | pointerCoords[pointerIndex].transform(currTransform); | 
|  | 384 | // Next, apply the inverse transform of the normalized coordinates so the | 
|  | 385 | // current coordinates are transformed into the normalized coordinate space. | 
|  | 386 | pointerCoords[pointerIndex].transform(inverseFirstTransform); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 387 | } | 
|  | 388 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 389 | std::unique_ptr<MotionEntry> combinedMotionEntry = | 
|  | 390 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, | 
|  | 391 | motionEntry.deviceId, motionEntry.source, | 
|  | 392 | motionEntry.displayId, motionEntry.policyFlags, | 
|  | 393 | motionEntry.action, motionEntry.actionButton, | 
|  | 394 | motionEntry.flags, motionEntry.metaState, | 
|  | 395 | motionEntry.buttonState, motionEntry.classification, | 
|  | 396 | motionEntry.edgeFlags, motionEntry.xPrecision, | 
|  | 397 | motionEntry.yPrecision, motionEntry.xCursorPosition, | 
|  | 398 | motionEntry.yCursorPosition, motionEntry.downTime, | 
|  | 399 | motionEntry.pointerCount, motionEntry.pointerProperties, | 
|  | 400 | pointerCoords.data(), 0 /* xOffset */, 0 /* yOffset */); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 401 |  | 
|  | 402 | if (motionEntry.injectionState) { | 
|  | 403 | combinedMotionEntry->injectionState = motionEntry.injectionState; | 
|  | 404 | combinedMotionEntry->injectionState->refCount += 1; | 
|  | 405 | } | 
|  | 406 |  | 
|  | 407 | std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 408 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 409 | firstPointerTransform, inputTarget.globalScaleFactor, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 410 | inputTarget.displayOrientation, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 411 | inputTarget.displaySize); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 412 | return dispatchEntry; | 
|  | 413 | } | 
|  | 414 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 415 | void addGestureMonitors(const std::vector<Monitor>& monitors, | 
|  | 416 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, | 
|  | 417 | float yOffset = 0) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 418 | if (monitors.empty()) { | 
|  | 419 | return; | 
|  | 420 | } | 
|  | 421 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); | 
|  | 422 | for (const Monitor& monitor : monitors) { | 
|  | 423 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); | 
|  | 424 | } | 
|  | 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 | sp<IPlatformCompatNative> getCompatService() { | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 448 | sp<IBinder> service(defaultServiceManager()->getService(String16("platform_compat_native"))); | 
|  | 449 | if (service == nullptr) { | 
|  | 450 | ALOGE("Failed to link to compat service"); | 
|  | 451 | return nullptr; | 
|  | 452 | } | 
|  | 453 | return interface_cast<IPlatformCompatNative>(service); | 
|  | 454 | } | 
|  | 455 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 456 | KeyEvent createKeyEvent(const KeyEntry& entry) { | 
| Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 457 | KeyEvent event; | 
|  | 458 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, | 
|  | 459 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
|  | 460 | entry.repeatCount, entry.downTime, entry.eventTime); | 
|  | 461 | return event; | 
|  | 462 | } | 
|  | 463 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 464 | std::optional<int32_t> findMonitorPidByToken( | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 465 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, | 
|  | 466 | const sp<IBinder>& token) { | 
|  | 467 | for (const auto& it : monitorsByDisplay) { | 
|  | 468 | const std::vector<Monitor>& monitors = it.second; | 
|  | 469 | for (const Monitor& monitor : monitors) { | 
|  | 470 | if (monitor.inputChannel->getConnectionToken() == token) { | 
|  | 471 | return monitor.pid; | 
|  | 472 | } | 
|  | 473 | } | 
|  | 474 | } | 
|  | 475 | return std::nullopt; | 
|  | 476 | } | 
|  | 477 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 478 | bool shouldReportMetricsForConnection(const Connection& connection) { | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 479 | // Do not keep track of gesture monitors. They receive every event and would disproportionately | 
|  | 480 | // affect the statistics. | 
|  | 481 | if (connection.monitor) { | 
|  | 482 | return false; | 
|  | 483 | } | 
|  | 484 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics | 
|  | 485 | if (!connection.responsive) { | 
|  | 486 | return false; | 
|  | 487 | } | 
|  | 488 | return true; | 
|  | 489 | } | 
|  | 490 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 491 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 492 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; | 
|  | 493 | const int32_t& inputEventId = eventEntry.id; | 
|  | 494 | if (inputEventId != dispatchEntry.resolvedEventId) { | 
|  | 495 | // Event was transmuted | 
|  | 496 | return false; | 
|  | 497 | } | 
|  | 498 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { | 
|  | 499 | return false; | 
|  | 500 | } | 
|  | 501 | // Only track latency for events that originated from hardware | 
|  | 502 | if (eventEntry.isSynthesized()) { | 
|  | 503 | return false; | 
|  | 504 | } | 
|  | 505 | const EventEntry::Type& inputEventEntryType = eventEntry.type; | 
|  | 506 | if (inputEventEntryType == EventEntry::Type::KEY) { | 
|  | 507 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
|  | 508 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { | 
|  | 509 | return false; | 
|  | 510 | } | 
|  | 511 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { | 
|  | 512 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
|  | 513 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || | 
|  | 514 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { | 
|  | 515 | return false; | 
|  | 516 | } | 
|  | 517 | } else { | 
|  | 518 | // Not a key or a motion | 
|  | 519 | return false; | 
|  | 520 | } | 
|  | 521 | if (!shouldReportMetricsForConnection(connection)) { | 
|  | 522 | return false; | 
|  | 523 | } | 
|  | 524 | return true; | 
|  | 525 | } | 
|  | 526 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 527 | /** | 
|  | 528 | * Connection is responsive if it has no events in the waitQueue that are older than the | 
|  | 529 | * current time. | 
|  | 530 | */ | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 531 | bool isConnectionResponsive(const Connection& connection) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 532 | const nsecs_t currentTime = now(); | 
|  | 533 | for (const DispatchEntry* entry : connection.waitQueue) { | 
|  | 534 | if (entry->timeoutTime < currentTime) { | 
|  | 535 | return false; | 
|  | 536 | } | 
|  | 537 | } | 
|  | 538 | return true; | 
|  | 539 | } | 
|  | 540 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 541 | } // namespace | 
|  | 542 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 543 | // --- InputDispatcher --- | 
|  | 544 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 545 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) | 
|  | 546 | : mPolicy(policy), | 
|  | 547 | mPendingEvent(nullptr), | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 548 | mLastDropReason(DropReason::NOT_DROPPED), | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 549 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 550 | mAppSwitchSawKeyDown(false), | 
|  | 551 | mAppSwitchDueTime(LONG_LONG_MAX), | 
|  | 552 | mNextUnblockedEvent(nullptr), | 
|  | 553 | mDispatchEnabled(false), | 
|  | 554 | mDispatchFrozen(false), | 
|  | 555 | mInputFilterEnabled(false), | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 556 | // mInTouchMode will be initialized by the WindowManager to the default device config. | 
|  | 557 | // To avoid leaking stack in case that call never comes, and for tests, | 
|  | 558 | // initialize it here anyways. | 
|  | 559 | mInTouchMode(true), | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 560 | mMaximumObscuringOpacityForTouch(1.0f), | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 561 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 562 | mFocusedWindowRequestedPointerCapture(false), | 
|  | 563 | mWindowTokenWithPointerCapture(nullptr), | 
| Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 564 | mLatencyAggregator(), | 
|  | 565 | mLatencyTracker(&mLatencyAggregator), | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 566 | mCompatService(getCompatService()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 567 | mLooper = new Looper(false); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 568 | mReporter = createInputReporter(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 569 |  | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 570 | mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 571 |  | 
|  | 572 | policy->getDispatcherConfiguration(&mConfig); | 
|  | 573 | } | 
|  | 574 |  | 
|  | 575 | InputDispatcher::~InputDispatcher() { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 576 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 577 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 578 | resetKeyRepeatLocked(); | 
|  | 579 | releasePendingEventLocked(); | 
|  | 580 | drainInboundQueueLocked(); | 
|  | 581 | mCommandQueue.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 582 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 583 | while (!mConnectionsByToken.empty()) { | 
|  | 584 | sp<Connection> connection = mConnectionsByToken.begin()->second; | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 585 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), | 
|  | 586 | false /* notify */); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 587 | } | 
|  | 588 | } | 
|  | 589 |  | 
| chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 590 | void InputDispatcher::onFirstRef() { | 
|  | 591 | SurfaceComposerClient::getDefault()->addWindowInfosListener(this); | 
|  | 592 | } | 
|  | 593 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 594 | status_t InputDispatcher::start() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 595 | if (mThread) { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 596 | return ALREADY_EXISTS; | 
|  | 597 | } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 598 | mThread = std::make_unique<InputThread>( | 
|  | 599 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); | 
|  | 600 | return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 601 | } | 
|  | 602 |  | 
|  | 603 | status_t InputDispatcher::stop() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 604 | if (mThread && mThread->isCallingThread()) { | 
|  | 605 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 606 | return INVALID_OPERATION; | 
|  | 607 | } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 608 | mThread.reset(); | 
|  | 609 | return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 610 | } | 
|  | 611 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 612 | void InputDispatcher::dispatchOnce() { | 
|  | 613 | nsecs_t nextWakeupTime = LONG_LONG_MAX; | 
|  | 614 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 615 | std::scoped_lock _l(mLock); | 
|  | 616 | mDispatcherIsAlive.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 617 |  | 
|  | 618 | // Run a dispatch loop if there are no pending commands. | 
|  | 619 | // The dispatch loop might enqueue commands to run afterwards. | 
|  | 620 | if (!haveCommandsLocked()) { | 
|  | 621 | dispatchOnceInnerLocked(&nextWakeupTime); | 
|  | 622 | } | 
|  | 623 |  | 
|  | 624 | // Run all pending commands if there are any. | 
|  | 625 | // 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] | 626 | if (runCommandsLockedInterruptable()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 627 | nextWakeupTime = LONG_LONG_MIN; | 
|  | 628 | } | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 629 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 630 | // If we are still waiting for ack on some events, | 
|  | 631 | // we might have to wake up earlier to check if an app is anr'ing. | 
|  | 632 | const nsecs_t nextAnrCheck = processAnrsLocked(); | 
|  | 633 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); | 
|  | 634 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 635 | // We are about to enter an infinitely long sleep, because we have no commands or | 
|  | 636 | // pending or queued events | 
|  | 637 | if (nextWakeupTime == LONG_LONG_MAX) { | 
|  | 638 | mDispatcherEnteredIdle.notify_all(); | 
|  | 639 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 640 | } // release lock | 
|  | 641 |  | 
|  | 642 | // Wait for callback or timeout or wake.  (make sure we round up, not down) | 
|  | 643 | nsecs_t currentTime = now(); | 
|  | 644 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); | 
|  | 645 | mLooper->pollOnce(timeoutMillis); | 
|  | 646 | } | 
|  | 647 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 648 | /** | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 649 | * Raise ANR if there is no focused window. | 
|  | 650 | * Before the ANR is raised, do a final state check: | 
|  | 651 | * 1. The currently focused application must be the same one we are waiting for. | 
|  | 652 | * 2. Ensure we still don't have a focused window. | 
|  | 653 | */ | 
|  | 654 | void InputDispatcher::processNoFocusedWindowAnrLocked() { | 
|  | 655 | // Check if the application that we are waiting for is still focused. | 
|  | 656 | std::shared_ptr<InputApplicationHandle> focusedApplication = | 
|  | 657 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); | 
|  | 658 | if (focusedApplication == nullptr || | 
|  | 659 | focusedApplication->getApplicationToken() != | 
|  | 660 | mAwaitedFocusedApplication->getApplicationToken()) { | 
|  | 661 | // Unexpected because we should have reset the ANR timer when focused application changed | 
|  | 662 | ALOGE("Waited for a focused window, but focused application has already changed to %s", | 
|  | 663 | focusedApplication->getName().c_str()); | 
|  | 664 | return; // The focused application has changed. | 
|  | 665 | } | 
|  | 666 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 667 | const sp<WindowInfoHandle>& focusedWindowHandle = | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 668 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); | 
|  | 669 | if (focusedWindowHandle != nullptr) { | 
|  | 670 | return; // We now have a focused window. No need for ANR. | 
|  | 671 | } | 
|  | 672 | onAnrLocked(mAwaitedFocusedApplication); | 
|  | 673 | } | 
|  | 674 |  | 
|  | 675 | /** | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 676 | * Check if any of the connections' wait queues have events that are too old. | 
|  | 677 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. | 
|  | 678 | * Return the time at which we should wake up next. | 
|  | 679 | */ | 
|  | 680 | nsecs_t InputDispatcher::processAnrsLocked() { | 
|  | 681 | const nsecs_t currentTime = now(); | 
|  | 682 | nsecs_t nextAnrCheck = LONG_LONG_MAX; | 
|  | 683 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long | 
|  | 684 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { | 
|  | 685 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 686 | processNoFocusedWindowAnrLocked(); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 687 | mAwaitedFocusedApplication.reset(); | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 688 | mNoFocusedWindowTimeoutTime = std::nullopt; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 689 | return LONG_LONG_MIN; | 
|  | 690 | } else { | 
| Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 691 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 692 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; | 
|  | 693 | } | 
|  | 694 | } | 
|  | 695 |  | 
|  | 696 | // Check if any connection ANRs are due | 
|  | 697 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); | 
|  | 698 | if (currentTime < nextAnrCheck) { // most likely scenario | 
|  | 699 | return nextAnrCheck;          // everything is normal. Let's check again at nextAnrCheck | 
|  | 700 | } | 
|  | 701 |  | 
|  | 702 | // If we reached here, we have an unresponsive connection. | 
|  | 703 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); | 
|  | 704 | if (connection == nullptr) { | 
|  | 705 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); | 
|  | 706 | return nextAnrCheck; | 
|  | 707 | } | 
|  | 708 | connection->responsive = false; | 
|  | 709 | // Stop waking up for this unresponsive connection | 
|  | 710 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 711 | onAnrLocked(connection); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 712 | return LONG_LONG_MIN; | 
|  | 713 | } | 
|  | 714 |  | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 715 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 716 | sp<WindowInfoHandle> window = getWindowHandleLocked(token); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 717 | if (window != nullptr) { | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 718 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 719 | } | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 720 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 721 | } | 
|  | 722 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 723 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { | 
|  | 724 | nsecs_t currentTime = now(); | 
|  | 725 |  | 
| Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 726 | // Reset the key repeat timer whenever normal dispatch is suspended while the | 
|  | 727 | // device is in a non-interactive state.  This is to ensure that we abort a key | 
|  | 728 | // repeat if the device is just coming out of sleep. | 
|  | 729 | if (!mDispatchEnabled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 730 | resetKeyRepeatLocked(); | 
|  | 731 | } | 
|  | 732 |  | 
|  | 733 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. | 
|  | 734 | if (mDispatchFrozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 735 | if (DEBUG_FOCUS) { | 
|  | 736 | ALOGD("Dispatch frozen.  Waiting some more."); | 
|  | 737 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 738 | return; | 
|  | 739 | } | 
|  | 740 |  | 
|  | 741 | // Optimize latency of app switches. | 
|  | 742 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has | 
|  | 743 | // been pressed.  When it expires, we preempt dispatch and drop all other pending events. | 
|  | 744 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; | 
|  | 745 | if (mAppSwitchDueTime < *nextWakeupTime) { | 
|  | 746 | *nextWakeupTime = mAppSwitchDueTime; | 
|  | 747 | } | 
|  | 748 |  | 
|  | 749 | // Ready to start a new event. | 
|  | 750 | // If we don't already have a pending event, go grab one. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 751 | if (!mPendingEvent) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 752 | if (mInboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 753 | if (isAppSwitchDue) { | 
|  | 754 | // The inbound queue is empty so the app switch key we were waiting | 
|  | 755 | // for will never arrive.  Stop waiting for it. | 
|  | 756 | resetPendingAppSwitchLocked(false); | 
|  | 757 | isAppSwitchDue = false; | 
|  | 758 | } | 
|  | 759 |  | 
|  | 760 | // Synthesize a key repeat if appropriate. | 
|  | 761 | if (mKeyRepeatState.lastKeyEntry) { | 
|  | 762 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { | 
|  | 763 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); | 
|  | 764 | } else { | 
|  | 765 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { | 
|  | 766 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; | 
|  | 767 | } | 
|  | 768 | } | 
|  | 769 | } | 
|  | 770 |  | 
|  | 771 | // Nothing to do if there is no pending event. | 
|  | 772 | if (!mPendingEvent) { | 
|  | 773 | return; | 
|  | 774 | } | 
|  | 775 | } else { | 
|  | 776 | // Inbound queue has at least one entry. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 777 | mPendingEvent = mInboundQueue.front(); | 
|  | 778 | mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 779 | traceInboundQueueLengthLocked(); | 
|  | 780 | } | 
|  | 781 |  | 
|  | 782 | // Poke user activity for this event. | 
|  | 783 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 784 | pokeUserActivityLocked(*mPendingEvent); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 785 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 786 | } | 
|  | 787 |  | 
|  | 788 | // Now we have an event to dispatch. | 
|  | 789 | // 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] | 790 | ALOG_ASSERT(mPendingEvent != nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 791 | bool done = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 792 | DropReason dropReason = DropReason::NOT_DROPPED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 793 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 794 | dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 795 | } else if (!mDispatchEnabled) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 796 | dropReason = DropReason::DISABLED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 797 | } | 
|  | 798 |  | 
|  | 799 | if (mNextUnblockedEvent == mPendingEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 800 | mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 801 | } | 
|  | 802 |  | 
|  | 803 | switch (mPendingEvent->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 804 | case EventEntry::Type::CONFIGURATION_CHANGED: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 805 | const ConfigurationChangedEntry& typedEntry = | 
|  | 806 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 807 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 808 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 809 | break; | 
|  | 810 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 811 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 812 | case EventEntry::Type::DEVICE_RESET: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 813 | const DeviceResetEntry& typedEntry = | 
|  | 814 | static_cast<const DeviceResetEntry&>(*mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 815 | done = dispatchDeviceResetLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 816 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 817 | break; | 
|  | 818 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 819 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 820 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 821 | std::shared_ptr<FocusEntry> typedEntry = | 
|  | 822 | std::static_pointer_cast<FocusEntry>(mPendingEvent); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 823 | dispatchFocusLocked(currentTime, typedEntry); | 
|  | 824 | done = true; | 
|  | 825 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped | 
|  | 826 | break; | 
|  | 827 | } | 
|  | 828 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 829 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
|  | 830 | const auto typedEntry = | 
|  | 831 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); | 
|  | 832 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); | 
|  | 833 | done = true; | 
|  | 834 | break; | 
|  | 835 | } | 
|  | 836 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 837 | case EventEntry::Type::DRAG: { | 
|  | 838 | std::shared_ptr<DragEntry> typedEntry = | 
|  | 839 | std::static_pointer_cast<DragEntry>(mPendingEvent); | 
|  | 840 | dispatchDragLocked(currentTime, typedEntry); | 
|  | 841 | done = true; | 
|  | 842 | break; | 
|  | 843 | } | 
|  | 844 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 845 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 846 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 847 | if (isAppSwitchDue) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 848 | if (isAppSwitchKeyEvent(*keyEntry)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 849 | resetPendingAppSwitchLocked(true); | 
|  | 850 | isAppSwitchDue = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 851 | } else if (dropReason == DropReason::NOT_DROPPED) { | 
|  | 852 | dropReason = DropReason::APP_SWITCH; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 853 | } | 
|  | 854 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 855 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 856 | dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 857 | } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 858 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
|  | 859 | dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 860 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 861 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 862 | break; | 
|  | 863 | } | 
|  | 864 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 865 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 866 | std::shared_ptr<MotionEntry> motionEntry = | 
|  | 867 | std::static_pointer_cast<MotionEntry>(mPendingEvent); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 868 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { | 
|  | 869 | dropReason = DropReason::APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 870 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 871 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 872 | dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 873 | } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 874 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
|  | 875 | dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 876 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 877 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 878 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 879 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 880 |  | 
|  | 881 | case EventEntry::Type::SENSOR: { | 
|  | 882 | std::shared_ptr<SensorEntry> sensorEntry = | 
|  | 883 | std::static_pointer_cast<SensorEntry>(mPendingEvent); | 
|  | 884 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { | 
|  | 885 | dropReason = DropReason::APP_SWITCH; | 
|  | 886 | } | 
|  | 887 | //  Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use | 
|  | 888 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. | 
|  | 889 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); | 
|  | 890 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { | 
|  | 891 | dropReason = DropReason::STALE; | 
|  | 892 | } | 
|  | 893 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); | 
|  | 894 | done = true; | 
|  | 895 | break; | 
|  | 896 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 897 | } | 
|  | 898 |  | 
|  | 899 | if (done) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 900 | if (dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 901 | dropInboundEventLocked(*mPendingEvent, dropReason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 902 | } | 
| Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 903 | mLastDropReason = dropReason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 904 |  | 
|  | 905 | releasePendingEventLocked(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 906 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 907 | } | 
|  | 908 | } | 
|  | 909 |  | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 910 | /** | 
|  | 911 | * Return true if the events preceding this incoming motion event should be dropped | 
|  | 912 | * Return false otherwise (the default behaviour) | 
|  | 913 | */ | 
|  | 914 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 915 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 916 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 917 |  | 
|  | 918 | // Optimize case where the current application is unresponsive and the user | 
|  | 919 | // decides to touch a window in a different application. | 
|  | 920 | // If the application takes too long to catch up then we drop all events preceding | 
|  | 921 | // the touch into the other window. | 
|  | 922 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 923 | int32_t displayId = motionEntry.displayId; | 
|  | 924 | int32_t x = static_cast<int32_t>( | 
|  | 925 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 926 | int32_t y = static_cast<int32_t>( | 
|  | 927 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 928 | sp<WindowInfoHandle> touchedWindowHandle = | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 929 | findTouchedWindowAtLocked(displayId, x, y, nullptr); | 
|  | 930 | if (touchedWindowHandle != nullptr && | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 931 | touchedWindowHandle->getApplicationToken() != | 
|  | 932 | mAwaitedFocusedApplication->getApplicationToken()) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 933 | // User touched a different application than the one we are waiting on. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 934 | ALOGI("Pruning input queue because user touched a different application while waiting " | 
|  | 935 | "for %s", | 
|  | 936 | mAwaitedFocusedApplication->getName().c_str()); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 937 | return true; | 
|  | 938 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 939 |  | 
|  | 940 | // Alternatively, maybe there's a gesture monitor that could handle this event | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 941 | std::vector<TouchedMonitor> gestureMonitors = findTouchedGestureMonitorsLocked(displayId); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 942 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { | 
|  | 943 | sp<Connection> connection = | 
|  | 944 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 945 | if (connection != nullptr && connection->responsive) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 946 | // This monitor could take more input. Drop all events preceding this | 
|  | 947 | // event, so that gesture monitor could get a chance to receive the stream | 
|  | 948 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " | 
|  | 949 | "responsive gesture monitor that may handle the event", | 
|  | 950 | mAwaitedFocusedApplication->getName().c_str()); | 
|  | 951 | return true; | 
|  | 952 | } | 
|  | 953 | } | 
|  | 954 | } | 
|  | 955 |  | 
|  | 956 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not | 
|  | 957 | // yet been processed by some connections, the dispatcher will wait for these motion | 
|  | 958 | // events to be processed before dispatching the key event. This is because these motion events | 
|  | 959 | // may cause a new window to be launched, which the user might expect to receive focus. | 
|  | 960 | // To prevent waiting forever for such events, just send the key to the currently focused window | 
|  | 961 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { | 
|  | 962 | ALOGD("Received a new pointer down event, stop waiting for events to process and " | 
|  | 963 | "just send the pending key event to the focused window."); | 
|  | 964 | mKeyIsWaitingForEventsTimeout = now(); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 965 | } | 
|  | 966 | return false; | 
|  | 967 | } | 
|  | 968 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 969 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 970 | bool needWake = mInboundQueue.empty(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 971 | mInboundQueue.push_back(std::move(newEntry)); | 
|  | 972 | EventEntry& entry = *(mInboundQueue.back()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 973 | traceInboundQueueLengthLocked(); | 
|  | 974 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 975 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 976 | case EventEntry::Type::KEY: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 977 | // Optimize app switch latency. | 
|  | 978 | // If the application takes too long to catch up then we drop all events preceding | 
|  | 979 | // the app switch key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 980 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 981 | if (isAppSwitchKeyEvent(keyEntry)) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 982 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 983 | mAppSwitchSawKeyDown = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 984 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 985 | if (mAppSwitchSawKeyDown) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 986 | if (DEBUG_APP_SWITCH) { | 
|  | 987 | ALOGD("App switch is pending!"); | 
|  | 988 | } | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 989 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 990 | mAppSwitchSawKeyDown = false; | 
|  | 991 | needWake = true; | 
|  | 992 | } | 
|  | 993 | } | 
|  | 994 | } | 
|  | 995 | break; | 
|  | 996 | } | 
|  | 997 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 998 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 999 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { | 
|  | 1000 | mNextUnblockedEvent = mInboundQueue.back(); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1001 | needWake = true; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1002 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1003 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1004 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1005 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1006 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); | 
|  | 1007 | break; | 
|  | 1008 | } | 
|  | 1009 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1010 | case EventEntry::Type::DEVICE_RESET: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1011 | case EventEntry::Type::SENSOR: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1012 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 1013 | case EventEntry::Type::DRAG: { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1014 | // nothing to do | 
|  | 1015 | break; | 
|  | 1016 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1017 | } | 
|  | 1018 |  | 
|  | 1019 | return needWake; | 
|  | 1020 | } | 
|  | 1021 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1022 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1023 | // Do not store sensor event in recent queue to avoid flooding the queue. | 
|  | 1024 | if (entry->type != EventEntry::Type::SENSOR) { | 
|  | 1025 | mRecentQueue.push_back(entry); | 
|  | 1026 | } | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1027 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1028 | mRecentQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1029 | } | 
|  | 1030 | } | 
|  | 1031 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1032 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, | 
|  | 1033 | int32_t y, TouchState* touchState, | 
|  | 1034 | bool addOutsideTargets, | 
|  | 1035 | bool ignoreDragWindow) { | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1036 | if (addOutsideTargets && touchState == nullptr) { | 
|  | 1037 | LOG_ALWAYS_FATAL("Must provide a valid touch state if adding outside targets"); | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1038 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1039 | // Traverse windows from front to back to find touched window. | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1040 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
|  | 1041 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1042 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1043 | continue; | 
|  | 1044 | } | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1045 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1046 | if (windowInfo->displayId == displayId) { | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1047 | auto flags = windowInfo->flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1048 |  | 
|  | 1049 | if (windowInfo->visible) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1050 | if (!flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { | 
|  | 1051 | bool isTouchModal = !flags.test(WindowInfo::Flag::NOT_FOCUSABLE) && | 
|  | 1052 | !flags.test(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1053 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { | 
|  | 1054 | // Found window. | 
|  | 1055 | return windowHandle; | 
|  | 1056 | } | 
|  | 1057 | } | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1058 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1059 | if (addOutsideTargets && flags.test(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1060 | touchState->addOrUpdateWindow(windowHandle, | 
|  | 1061 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, | 
|  | 1062 | BitSet32(0)); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1063 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1064 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1065 | } | 
|  | 1066 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1067 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1068 | } | 
|  | 1069 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1070 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1071 | int32_t displayId) const { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1072 | std::vector<TouchedMonitor> touchedMonitors; | 
|  | 1073 |  | 
|  | 1074 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); | 
|  | 1075 | addGestureMonitors(monitors, touchedMonitors); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1076 | return touchedMonitors; | 
|  | 1077 | } | 
|  | 1078 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1079 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1080 | const char* reason; | 
|  | 1081 | switch (dropReason) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1082 | case DropReason::POLICY: | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1083 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 1084 | ALOGD("Dropped event because policy consumed it."); | 
|  | 1085 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1086 | reason = "inbound event was dropped because the policy consumed it"; | 
|  | 1087 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1088 | case DropReason::DISABLED: | 
|  | 1089 | if (mLastDropReason != DropReason::DISABLED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1090 | ALOGI("Dropped event because input dispatch is disabled."); | 
|  | 1091 | } | 
|  | 1092 | reason = "inbound event was dropped because input dispatch is disabled"; | 
|  | 1093 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1094 | case DropReason::APP_SWITCH: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1095 | ALOGI("Dropped event because of pending overdue app switch."); | 
|  | 1096 | reason = "inbound event was dropped because of pending overdue app switch"; | 
|  | 1097 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1098 | case DropReason::BLOCKED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1099 | ALOGI("Dropped event because the current application is not responding and the user " | 
|  | 1100 | "has started interacting with a different application."); | 
|  | 1101 | reason = "inbound event was dropped because the current application is not responding " | 
|  | 1102 | "and the user has started interacting with a different application"; | 
|  | 1103 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1104 | case DropReason::STALE: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1105 | ALOGI("Dropped event because it is stale."); | 
|  | 1106 | reason = "inbound event was dropped because it is stale"; | 
|  | 1107 | break; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1108 | case DropReason::NO_POINTER_CAPTURE: | 
|  | 1109 | ALOGI("Dropped event because there is no window with Pointer Capture."); | 
|  | 1110 | reason = "inbound event was dropped because there is no window with Pointer Capture"; | 
|  | 1111 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1112 | case DropReason::NOT_DROPPED: { | 
|  | 1113 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1114 | return; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1115 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1116 | } | 
|  | 1117 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1118 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1119 | case EventEntry::Type::KEY: { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1120 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
|  | 1121 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1122 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1123 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1124 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1125 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 1126 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1127 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); | 
|  | 1128 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1129 | } else { | 
|  | 1130 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
|  | 1131 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1132 | } | 
|  | 1133 | break; | 
|  | 1134 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1135 | case EventEntry::Type::SENSOR: { | 
|  | 1136 | break; | 
|  | 1137 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1138 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 1139 | case EventEntry::Type::DRAG: { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1140 | break; | 
|  | 1141 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1142 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1143 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 1144 | case EventEntry::Type::DEVICE_RESET: { | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1145 | LOG_ALWAYS_FATAL("Should not drop %s events", NamedEnum::string(entry.type).c_str()); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1146 | break; | 
|  | 1147 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1148 | } | 
|  | 1149 | } | 
|  | 1150 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1151 | static bool isAppSwitchKeyCode(int32_t keyCode) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1152 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || | 
|  | 1153 | keyCode == AKEYCODE_APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1154 | } | 
|  | 1155 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1156 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { | 
|  | 1157 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && | 
|  | 1158 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && | 
|  | 1159 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1160 | } | 
|  | 1161 |  | 
|  | 1162 | bool InputDispatcher::isAppSwitchPendingLocked() { | 
|  | 1163 | return mAppSwitchDueTime != LONG_LONG_MAX; | 
|  | 1164 | } | 
|  | 1165 |  | 
|  | 1166 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { | 
|  | 1167 | mAppSwitchDueTime = LONG_LONG_MAX; | 
|  | 1168 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1169 | if (DEBUG_APP_SWITCH) { | 
|  | 1170 | if (handled) { | 
|  | 1171 | ALOGD("App switch has arrived."); | 
|  | 1172 | } else { | 
|  | 1173 | ALOGD("App switch was abandoned."); | 
|  | 1174 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1175 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1176 | } | 
|  | 1177 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1178 | bool InputDispatcher::haveCommandsLocked() const { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1179 | return !mCommandQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1180 | } | 
|  | 1181 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1182 | bool InputDispatcher::runCommandsLockedInterruptable() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1183 | if (mCommandQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1184 | return false; | 
|  | 1185 | } | 
|  | 1186 |  | 
|  | 1187 | do { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1188 | auto command = std::move(mCommandQueue.front()); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1189 | mCommandQueue.pop_front(); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1190 | // Commands are run with the lock held, but may release and re-acquire the lock from within. | 
|  | 1191 | command(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1192 | } while (!mCommandQueue.empty()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1193 | return true; | 
|  | 1194 | } | 
|  | 1195 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1196 | void InputDispatcher::postCommandLocked(Command&& command) { | 
|  | 1197 | mCommandQueue.push_back(command); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1198 | } | 
|  | 1199 |  | 
|  | 1200 | void InputDispatcher::drainInboundQueueLocked() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1201 | while (!mInboundQueue.empty()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1202 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1203 | mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1204 | releaseInboundEventLocked(entry); | 
|  | 1205 | } | 
|  | 1206 | traceInboundQueueLengthLocked(); | 
|  | 1207 | } | 
|  | 1208 |  | 
|  | 1209 | void InputDispatcher::releasePendingEventLocked() { | 
|  | 1210 | if (mPendingEvent) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | releaseInboundEventLocked(mPendingEvent); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1212 | mPendingEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1213 | } | 
|  | 1214 | } | 
|  | 1215 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1216 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1217 | InjectionState* injectionState = entry->injectionState; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1218 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1219 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 1220 | ALOGD("Injected inbound event was dropped."); | 
|  | 1221 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1222 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1223 | } | 
|  | 1224 | if (entry == mNextUnblockedEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1225 | mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1226 | } | 
|  | 1227 | addRecentEventLocked(entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1228 | } | 
|  | 1229 |  | 
|  | 1230 | void InputDispatcher::resetKeyRepeatLocked() { | 
|  | 1231 | if (mKeyRepeatState.lastKeyEntry) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1232 | mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1233 | } | 
|  | 1234 | } | 
|  | 1235 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1236 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { | 
|  | 1237 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1238 |  | 
| Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1239 | uint32_t policyFlags = entry->policyFlags & | 
|  | 1240 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1241 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1242 | std::shared_ptr<KeyEntry> newEntry = | 
|  | 1243 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, | 
|  | 1244 | entry->source, entry->displayId, policyFlags, entry->action, | 
|  | 1245 | entry->flags, entry->keyCode, entry->scanCode, | 
|  | 1246 | entry->metaState, entry->repeatCount + 1, entry->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1247 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1248 | newEntry->syntheticRepeat = true; | 
|  | 1249 | mKeyRepeatState.lastKeyEntry = newEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1250 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1251 | return newEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1252 | } | 
|  | 1253 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1254 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1255 | const ConfigurationChangedEntry& entry) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1256 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 1257 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); | 
|  | 1258 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1259 |  | 
|  | 1260 | // Reset key repeating in case a keyboard device was added or removed or something. | 
|  | 1261 | resetKeyRepeatLocked(); | 
|  | 1262 |  | 
|  | 1263 | // 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] | 1264 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { | 
|  | 1265 | scoped_unlock unlock(mLock); | 
|  | 1266 | mPolicy->notifyConfigurationChanged(eventTime); | 
|  | 1267 | }; | 
|  | 1268 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1269 | return true; | 
|  | 1270 | } | 
|  | 1271 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1272 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, | 
|  | 1273 | const DeviceResetEntry& entry) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1274 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 1275 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, | 
|  | 1276 | entry.deviceId); | 
|  | 1277 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1278 |  | 
| liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1279 | // Reset key repeating in case a keyboard device was disabled or enabled. | 
|  | 1280 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { | 
|  | 1281 | resetKeyRepeatLocked(); | 
|  | 1282 | } | 
|  | 1283 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1284 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1285 | options.deviceId = entry.deviceId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1286 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1287 | return true; | 
|  | 1288 | } | 
|  | 1289 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1290 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1291 | const std::string& reason) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1292 | if (mPendingEvent != nullptr) { | 
|  | 1293 | // Move the pending event to the front of the queue. This will give the chance | 
|  | 1294 | // for the pending event to get dispatched to the newly focused window | 
|  | 1295 | mInboundQueue.push_front(mPendingEvent); | 
|  | 1296 | mPendingEvent = nullptr; | 
|  | 1297 | } | 
|  | 1298 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1299 | std::unique_ptr<FocusEntry> focusEntry = | 
|  | 1300 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, | 
|  | 1301 | reason); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1302 |  | 
|  | 1303 | // This event should go to the front of the queue, but behind all other focus events | 
|  | 1304 | // Find the last focus event, and insert right after it | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1305 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1306 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1307 | [](const std::shared_ptr<EventEntry>& event) { | 
|  | 1308 | return event->type == EventEntry::Type::FOCUS; | 
|  | 1309 | }); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1310 |  | 
|  | 1311 | // 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] | 1312 | mInboundQueue.insert(it.base(), std::move(focusEntry)); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1313 | } | 
|  | 1314 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1315 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1316 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1317 | if (channel == nullptr) { | 
|  | 1318 | return; // Window has gone away | 
|  | 1319 | } | 
|  | 1320 | InputTarget target; | 
|  | 1321 | target.inputChannel = channel; | 
|  | 1322 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1323 | entry->dispatchInProgress = true; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1324 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + | 
|  | 1325 | channel->getName(); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1326 | std::string reason = std::string("reason=").append(entry->reason); | 
|  | 1327 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1328 | dispatchEventLocked(currentTime, entry, {target}); | 
|  | 1329 | } | 
|  | 1330 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1331 | void InputDispatcher::dispatchPointerCaptureChangedLocked( | 
|  | 1332 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, | 
|  | 1333 | DropReason& dropReason) { | 
|  | 1334 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 1335 | if (entry->pointerCaptureEnabled && haveWindowWithPointerCapture) { | 
|  | 1336 | LOG_ALWAYS_FATAL("Pointer Capture has already been enabled for the window."); | 
|  | 1337 | } | 
|  | 1338 | if (!entry->pointerCaptureEnabled && !haveWindowWithPointerCapture) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1339 | // Pointer capture was already forcefully disabled because of focus change. | 
|  | 1340 | dropReason = DropReason::NOT_DROPPED; | 
|  | 1341 | return; | 
|  | 1342 | } | 
|  | 1343 |  | 
|  | 1344 | // Set drop reason for early returns | 
|  | 1345 | dropReason = DropReason::NO_POINTER_CAPTURE; | 
|  | 1346 |  | 
|  | 1347 | sp<IBinder> token; | 
|  | 1348 | if (entry->pointerCaptureEnabled) { | 
|  | 1349 | // Enable Pointer Capture | 
|  | 1350 | if (!mFocusedWindowRequestedPointerCapture) { | 
|  | 1351 | // This can happen if a window requests capture and immediately releases capture. | 
|  | 1352 | ALOGW("No window requested Pointer Capture."); | 
|  | 1353 | return; | 
|  | 1354 | } | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1355 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1356 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); | 
|  | 1357 | mWindowTokenWithPointerCapture = token; | 
|  | 1358 | } else { | 
|  | 1359 | // Disable Pointer Capture | 
|  | 1360 | token = mWindowTokenWithPointerCapture; | 
|  | 1361 | mWindowTokenWithPointerCapture = nullptr; | 
| Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1362 | if (mFocusedWindowRequestedPointerCapture) { | 
|  | 1363 | mFocusedWindowRequestedPointerCapture = false; | 
|  | 1364 | setPointerCaptureLocked(false); | 
|  | 1365 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1366 | } | 
|  | 1367 |  | 
|  | 1368 | auto channel = getInputChannelLocked(token); | 
|  | 1369 | if (channel == nullptr) { | 
|  | 1370 | // Window has gone away, clean up Pointer Capture state. | 
|  | 1371 | mWindowTokenWithPointerCapture = nullptr; | 
| Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1372 | if (mFocusedWindowRequestedPointerCapture) { | 
|  | 1373 | mFocusedWindowRequestedPointerCapture = false; | 
|  | 1374 | setPointerCaptureLocked(false); | 
|  | 1375 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1376 | return; | 
|  | 1377 | } | 
|  | 1378 | InputTarget target; | 
|  | 1379 | target.inputChannel = channel; | 
|  | 1380 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1381 | entry->dispatchInProgress = true; | 
|  | 1382 | dispatchEventLocked(currentTime, entry, {target}); | 
|  | 1383 |  | 
|  | 1384 | dropReason = DropReason::NOT_DROPPED; | 
|  | 1385 | } | 
|  | 1386 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1387 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1388 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1389 | // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1390 | if (!entry->dispatchInProgress) { | 
|  | 1391 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && | 
|  | 1392 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && | 
|  | 1393 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { | 
|  | 1394 | if (mKeyRepeatState.lastKeyEntry && | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1395 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1396 | // We have seen two identical key downs in a row which indicates that the device | 
|  | 1397 | // driver is automatically generating key repeats itself.  We take note of the | 
|  | 1398 | // repeat here, but we disable our own next key repeat timer since it is clear that | 
|  | 1399 | // we will not need to synthesize key repeats ourselves. | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1400 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { | 
|  | 1401 | // Make sure we don't get key down from a different device. If a different | 
|  | 1402 | // device Id has same key pressed down, the new device Id will replace the | 
|  | 1403 | // current one to hold the key repeat with repeat count reset. | 
|  | 1404 | // In the future when got a KEY_UP on the device id, drop it and do not | 
|  | 1405 | // stop the key repeat on current device. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1406 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; | 
|  | 1407 | resetKeyRepeatLocked(); | 
|  | 1408 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves | 
|  | 1409 | } else { | 
|  | 1410 | // Not a repeat.  Save key down state in case we do see a repeat later. | 
|  | 1411 | resetKeyRepeatLocked(); | 
|  | 1412 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; | 
|  | 1413 | } | 
|  | 1414 | mKeyRepeatState.lastKeyEntry = entry; | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1415 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && | 
|  | 1416 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1417 | // 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] | 1418 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 1419 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); | 
|  | 1420 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1421 | } else if (!entry->syntheticRepeat) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1422 | resetKeyRepeatLocked(); | 
|  | 1423 | } | 
|  | 1424 |  | 
|  | 1425 | if (entry->repeatCount == 1) { | 
|  | 1426 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; | 
|  | 1427 | } else { | 
|  | 1428 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; | 
|  | 1429 | } | 
|  | 1430 |  | 
|  | 1431 | entry->dispatchInProgress = true; | 
|  | 1432 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1433 | logOutboundKeyDetails("dispatchKey - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1434 | } | 
|  | 1435 |  | 
|  | 1436 | // Handle case where the policy asked us to try again later last time. | 
|  | 1437 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { | 
|  | 1438 | if (currentTime < entry->interceptKeyWakeupTime) { | 
|  | 1439 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { | 
|  | 1440 | *nextWakeupTime = entry->interceptKeyWakeupTime; | 
|  | 1441 | } | 
|  | 1442 | return false; // wait until next wakeup | 
|  | 1443 | } | 
|  | 1444 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; | 
|  | 1445 | entry->interceptKeyWakeupTime = 0; | 
|  | 1446 | } | 
|  | 1447 |  | 
|  | 1448 | // Give the policy a chance to intercept the key. | 
|  | 1449 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { | 
|  | 1450 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1451 | sp<IBinder> focusedWindowToken = | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1452 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1453 |  | 
|  | 1454 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { | 
|  | 1455 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); | 
|  | 1456 | }; | 
|  | 1457 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1458 | return false; // wait for the command to run | 
|  | 1459 | } else { | 
|  | 1460 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
|  | 1461 | } | 
|  | 1462 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1463 | if (*dropReason == DropReason::NOT_DROPPED) { | 
|  | 1464 | *dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1465 | } | 
|  | 1466 | } | 
|  | 1467 |  | 
|  | 1468 | // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1469 | if (*dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1470 | setInjectionResult(*entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1471 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED | 
|  | 1472 | : InputEventInjectionResult::FAILED); | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1473 | mReporter->reportDroppedKey(entry->id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1474 | return true; | 
|  | 1475 | } | 
|  | 1476 |  | 
|  | 1477 | // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1478 | std::vector<InputTarget> inputTargets; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1479 | InputEventInjectionResult injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1480 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1481 | if (injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1482 | return false; | 
|  | 1483 | } | 
|  | 1484 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1485 | setInjectionResult(*entry, injectionResult); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1486 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1487 | return true; | 
|  | 1488 | } | 
|  | 1489 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1490 | // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1491 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1492 |  | 
|  | 1493 | // Dispatch the key. | 
|  | 1494 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1495 | return true; | 
|  | 1496 | } | 
|  | 1497 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1498 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1499 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 1500 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " | 
|  | 1501 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " | 
|  | 1502 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, | 
|  | 1503 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, | 
|  | 1504 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, | 
|  | 1505 | entry.metaState, entry.repeatCount, entry.downTime); | 
|  | 1506 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1507 | } | 
|  | 1508 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1509 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, | 
|  | 1510 | const std::shared_ptr<SensorEntry>& entry, | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1511 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1512 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 1513 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " | 
|  | 1514 | "source=0x%x, sensorType=%s", | 
|  | 1515 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, | 
|  | 1516 | NamedEnum::string(entry->sensorType).c_str()); | 
|  | 1517 | } | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1518 | auto command = [this, entry]() REQUIRES(mLock) { | 
|  | 1519 | scoped_unlock unlock(mLock); | 
|  | 1520 |  | 
|  | 1521 | if (entry->accuracyChanged) { | 
|  | 1522 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); | 
|  | 1523 | } | 
|  | 1524 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, | 
|  | 1525 | entry->hwTimestamp, entry->values); | 
|  | 1526 | }; | 
|  | 1527 | postCommandLocked(std::move(command)); | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1528 | } | 
|  | 1529 |  | 
|  | 1530 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1531 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 1532 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, | 
|  | 1533 | NamedEnum::string(sensorType).c_str()); | 
|  | 1534 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1535 | { // acquire lock | 
|  | 1536 | std::scoped_lock _l(mLock); | 
|  | 1537 |  | 
|  | 1538 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { | 
|  | 1539 | std::shared_ptr<EventEntry> entry = *it; | 
|  | 1540 | if (entry->type == EventEntry::Type::SENSOR) { | 
|  | 1541 | it = mInboundQueue.erase(it); | 
|  | 1542 | releaseInboundEventLocked(entry); | 
|  | 1543 | } | 
|  | 1544 | } | 
|  | 1545 | } | 
|  | 1546 | return true; | 
|  | 1547 | } | 
|  | 1548 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1549 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1550 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1551 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1552 | // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1553 | if (!entry->dispatchInProgress) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1554 | entry->dispatchInProgress = true; | 
|  | 1555 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1556 | logOutboundMotionDetails("dispatchMotion - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1557 | } | 
|  | 1558 |  | 
|  | 1559 | // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1560 | if (*dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1561 | setInjectionResult(*entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1562 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED | 
|  | 1563 | : InputEventInjectionResult::FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1564 | return true; | 
|  | 1565 | } | 
|  | 1566 |  | 
|  | 1567 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; | 
|  | 1568 |  | 
|  | 1569 | // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1570 | std::vector<InputTarget> inputTargets; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1571 |  | 
|  | 1572 | bool conflictingPointerActions = false; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1573 | InputEventInjectionResult injectionResult; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1574 | if (isPointerEvent) { | 
|  | 1575 | // Pointer event.  (eg. touchscreen) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1576 | injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1577 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1578 | &conflictingPointerActions); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1579 | } else { | 
|  | 1580 | // Non touch event.  (eg. trackball) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1581 | injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1582 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1583 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1584 | if (injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1585 | return false; | 
|  | 1586 | } | 
|  | 1587 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1588 | setInjectionResult(*entry, injectionResult); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1589 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1590 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); | 
|  | 1591 | return true; | 
|  | 1592 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1593 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1594 | CancelationOptions::Mode mode(isPointerEvent | 
|  | 1595 | ? CancelationOptions::CANCEL_POINTER_EVENTS | 
|  | 1596 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); | 
|  | 1597 | CancelationOptions options(mode, "input event injection failed"); | 
|  | 1598 | synthesizeCancelationEventsForMonitorsLocked(options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1599 | return true; | 
|  | 1600 | } | 
|  | 1601 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1602 | // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1603 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1604 |  | 
|  | 1605 | // Dispatch the motion. | 
|  | 1606 | if (conflictingPointerActions) { | 
|  | 1607 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1608 | "conflicting pointer actions"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1609 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1610 | } | 
|  | 1611 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1612 | return true; | 
|  | 1613 | } | 
|  | 1614 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1615 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1616 | bool isExiting, const MotionEntry& motionEntry) { | 
|  | 1617 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should | 
|  | 1618 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. | 
|  | 1619 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); | 
|  | 1620 | PointerCoords pointerCoords; | 
|  | 1621 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); | 
|  | 1622 | pointerCoords.transform(windowHandle->getInfo()->transform); | 
|  | 1623 |  | 
|  | 1624 | std::unique_ptr<DragEntry> dragEntry = | 
|  | 1625 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, | 
|  | 1626 | windowHandle->getToken(), isExiting, pointerCoords.getX(), | 
|  | 1627 | pointerCoords.getY()); | 
|  | 1628 |  | 
|  | 1629 | enqueueInboundEventLocked(std::move(dragEntry)); | 
|  | 1630 | } | 
|  | 1631 |  | 
|  | 1632 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { | 
|  | 1633 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); | 
|  | 1634 | if (channel == nullptr) { | 
|  | 1635 | return; // Window has gone away | 
|  | 1636 | } | 
|  | 1637 | InputTarget target; | 
|  | 1638 | target.inputChannel = channel; | 
|  | 1639 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1640 | entry->dispatchInProgress = true; | 
|  | 1641 | dispatchEventLocked(currentTime, entry, {target}); | 
|  | 1642 | } | 
|  | 1643 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1644 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1645 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 1646 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
|  | 1647 | ", policyFlags=0x%x, " | 
|  | 1648 | "action=0x%x, actionButton=0x%x, flags=0x%x, " | 
|  | 1649 | "metaState=0x%x, buttonState=0x%x," | 
|  | 1650 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, | 
|  | 1651 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, | 
|  | 1652 | entry.policyFlags, entry.action, entry.actionButton, entry.flags, entry.metaState, | 
|  | 1653 | entry.buttonState, entry.edgeFlags, entry.xPrecision, entry.yPrecision, | 
|  | 1654 | entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1655 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1656 | for (uint32_t i = 0; i < entry.pointerCount; i++) { | 
|  | 1657 | ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
|  | 1658 | "x=%f, y=%f, pressure=%f, size=%f, " | 
|  | 1659 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
|  | 1660 | "orientation=%f", | 
|  | 1661 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, | 
|  | 1662 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 1663 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 1664 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 1665 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
|  | 1666 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 1667 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 1668 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 1669 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 1670 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
|  | 1671 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1672 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1673 | } | 
|  | 1674 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1675 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, | 
|  | 1676 | std::shared_ptr<EventEntry> eventEntry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1677 | const std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1678 | ATRACE_CALL(); | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1679 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 1680 | ALOGD("dispatchEventToCurrentInputTargets"); | 
|  | 1681 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1682 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1683 | updateInteractionTokensLocked(*eventEntry, inputTargets); | 
|  | 1684 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1685 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true | 
|  | 1686 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1687 | pokeUserActivityLocked(*eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1688 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1689 | for (const InputTarget& inputTarget : inputTargets) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1690 | sp<Connection> connection = | 
|  | 1691 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1692 | if (connection != nullptr) { | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1693 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1694 | } else { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1695 | if (DEBUG_FOCUS) { | 
|  | 1696 | ALOGD("Dropping event delivery to target with channel '%s' because it " | 
|  | 1697 | "is no longer registered with the input dispatcher.", | 
|  | 1698 | inputTarget.inputChannel->getName().c_str()); | 
|  | 1699 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1700 | } | 
|  | 1701 | } | 
|  | 1702 | } | 
|  | 1703 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1704 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { | 
|  | 1705 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. | 
|  | 1706 | // If the policy decides to close the app, we will get a channel removal event via | 
|  | 1707 | // unregisterInputChannel, and will clean up the connection that way. We are already not | 
|  | 1708 | // sending new pointers to the connection when it blocked, but focused events will continue to | 
|  | 1709 | // pile up. | 
|  | 1710 | ALOGW("Canceling events for %s because it is unresponsive", | 
|  | 1711 | connection->inputChannel->getName().c_str()); | 
|  | 1712 | if (connection->status == Connection::STATUS_NORMAL) { | 
|  | 1713 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, | 
|  | 1714 | "application not responding"); | 
|  | 1715 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1716 | } | 
|  | 1717 | } | 
|  | 1718 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1719 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1720 | if (DEBUG_FOCUS) { | 
|  | 1721 | ALOGD("Resetting ANR timeouts."); | 
|  | 1722 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1723 |  | 
|  | 1724 | // Reset input target wait timeout. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1725 | mNoFocusedWindowTimeoutTime = std::nullopt; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1726 | mAwaitedFocusedApplication.reset(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1727 | } | 
|  | 1728 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1729 | /** | 
|  | 1730 | * Get the display id that the given event should go to. If this event specifies a valid display id, | 
|  | 1731 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. | 
|  | 1732 | * Focused display is the display that the user most recently interacted with. | 
|  | 1733 | */ | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1734 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1735 | int32_t displayId; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1736 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1737 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1738 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
|  | 1739 | displayId = keyEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1740 | break; | 
|  | 1741 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1742 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1743 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 1744 | displayId = motionEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1745 | break; | 
|  | 1746 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1747 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1748 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1749 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1750 | case EventEntry::Type::DEVICE_RESET: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1751 | case EventEntry::Type::SENSOR: | 
|  | 1752 | case EventEntry::Type::DRAG: { | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1753 | ALOGE("%s events do not have a target display", NamedEnum::string(entry.type).c_str()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1754 | return ADISPLAY_ID_NONE; | 
|  | 1755 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1756 | } | 
|  | 1757 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; | 
|  | 1758 | } | 
|  | 1759 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1760 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, | 
|  | 1761 | const char* focusedWindowName) { | 
|  | 1762 | if (mAnrTracker.empty()) { | 
|  | 1763 | // already processed all events that we waited for | 
|  | 1764 | mKeyIsWaitingForEventsTimeout = std::nullopt; | 
|  | 1765 | return false; | 
|  | 1766 | } | 
|  | 1767 |  | 
|  | 1768 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { | 
|  | 1769 | // Start the timer | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1770 | // 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] | 1771 | mKeyIsWaitingForEventsTimeout = currentTime + | 
|  | 1772 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) | 
|  | 1773 | .count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1774 | return true; | 
|  | 1775 | } | 
|  | 1776 |  | 
|  | 1777 | // We still have pending events, and already started the timer | 
|  | 1778 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { | 
|  | 1779 | return true; // Still waiting | 
|  | 1780 | } | 
|  | 1781 |  | 
|  | 1782 | // Waited too long, and some connection still hasn't processed all motions | 
|  | 1783 | // Just send the key to the focused window | 
|  | 1784 | ALOGW("Dispatching key to %s even though there are other unprocessed events", | 
|  | 1785 | focusedWindowName); | 
|  | 1786 | mKeyIsWaitingForEventsTimeout = std::nullopt; | 
|  | 1787 | return false; | 
|  | 1788 | } | 
|  | 1789 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1790 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( | 
|  | 1791 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, | 
|  | 1792 | nsecs_t* nextWakeupTime) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1793 | std::string reason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1794 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1795 | int32_t displayId = getTargetDisplayId(entry); | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1796 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1797 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1798 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
|  | 1799 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1800 | // If there is no currently focused window and no focused application | 
|  | 1801 | // then drop the event. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1802 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { | 
|  | 1803 | ALOGI("Dropping %s event because there is no focused window or focused application in " | 
|  | 1804 | "display %" PRId32 ".", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1805 | NamedEnum::string(entry.type).c_str(), displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1806 | return InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1807 | } | 
|  | 1808 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1809 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. | 
|  | 1810 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we | 
|  | 1811 | // start interacting with another application via touch (app switch). This code can be removed | 
|  | 1812 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether | 
|  | 1813 | // an app is expected to have a focused window. | 
|  | 1814 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { | 
|  | 1815 | if (!mNoFocusedWindowTimeoutTime.has_value()) { | 
|  | 1816 | // 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] | 1817 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( | 
|  | 1818 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
|  | 1819 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1820 | mAwaitedFocusedApplication = focusedApplicationHandle; | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1821 | mAwaitedApplicationDisplayId = displayId; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1822 | ALOGW("Waiting because no window has focus but %s may eventually add a " | 
|  | 1823 | "window when it finishes starting up. Will wait for %" PRId64 "ms", | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1824 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1825 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1826 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1827 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { | 
|  | 1828 | // Already raised ANR. Drop the event | 
|  | 1829 | ALOGE("Dropping %s event because there is no focused window", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1830 | NamedEnum::string(entry.type).c_str()); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1831 | return InputEventInjectionResult::FAILED; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1832 | } else { | 
|  | 1833 | // Still waiting for the focused window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1834 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1835 | } | 
|  | 1836 | } | 
|  | 1837 |  | 
|  | 1838 | // we have a valid, non-null focused window | 
|  | 1839 | resetNoFocusedWindowTimeoutLocked(); | 
|  | 1840 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1841 | // Check permissions. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1842 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1843 | return InputEventInjectionResult::PERMISSION_DENIED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1844 | } | 
|  | 1845 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1846 | if (focusedWindowHandle->getInfo()->paused) { | 
|  | 1847 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1848 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1849 | } | 
|  | 1850 |  | 
|  | 1851 | // If the event is a key event, then we must wait for all previous events to | 
|  | 1852 | // complete before delivering it because previous events may have the | 
|  | 1853 | // side-effect of transferring focus to a different window and we want to | 
|  | 1854 | // ensure that the following keys are sent to the new window. | 
|  | 1855 | // | 
|  | 1856 | // Suppose the user touches a button in a window then immediately presses "A". | 
|  | 1857 | // If the button causes a pop-up window to appear then we want to ensure that | 
|  | 1858 | // the "A" key is delivered to the new pop-up window.  This is because users | 
|  | 1859 | // often anticipate pending UI changes when typing on a keyboard. | 
|  | 1860 | // To obtain this behavior, we must serialize key events with respect to all | 
|  | 1861 | // prior input events. | 
|  | 1862 | if (entry.type == EventEntry::Type::KEY) { | 
|  | 1863 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { | 
|  | 1864 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1865 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1866 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1867 | } | 
|  | 1868 |  | 
|  | 1869 | // Success!  Output targets. | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1870 | addWindowTargetLocked(focusedWindowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1871 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, | 
|  | 1872 | BitSet32(0), inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1873 |  | 
|  | 1874 | // Done. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1875 | return InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1876 | } | 
|  | 1877 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1878 | /** | 
|  | 1879 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones | 
|  | 1880 | * that are currently unresponsive. | 
|  | 1881 | */ | 
|  | 1882 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( | 
|  | 1883 | const std::vector<TouchedMonitor>& monitors) const { | 
|  | 1884 | std::vector<TouchedMonitor> responsiveMonitors; | 
|  | 1885 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), | 
|  | 1886 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { | 
|  | 1887 | sp<Connection> connection = getConnectionLocked( | 
|  | 1888 | monitor.monitor.inputChannel->getConnectionToken()); | 
|  | 1889 | if (connection == nullptr) { | 
|  | 1890 | ALOGE("Could not find connection for monitor %s", | 
|  | 1891 | monitor.monitor.inputChannel->getName().c_str()); | 
|  | 1892 | return false; | 
|  | 1893 | } | 
|  | 1894 | if (!connection->responsive) { | 
|  | 1895 | ALOGW("Unresponsive monitor %s will not get the new gesture", | 
|  | 1896 | connection->inputChannel->getName().c_str()); | 
|  | 1897 | return false; | 
|  | 1898 | } | 
|  | 1899 | return true; | 
|  | 1900 | }); | 
|  | 1901 | return responsiveMonitors; | 
|  | 1902 | } | 
|  | 1903 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1904 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( | 
|  | 1905 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, | 
|  | 1906 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1907 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1908 | enum InjectionPermission { | 
|  | 1909 | INJECTION_PERMISSION_UNKNOWN, | 
|  | 1910 | INJECTION_PERMISSION_GRANTED, | 
|  | 1911 | INJECTION_PERMISSION_DENIED | 
|  | 1912 | }; | 
|  | 1913 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1914 | // For security reasons, we defer updating the touch state until we are sure that | 
|  | 1915 | // event injection will be allowed. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1916 | int32_t displayId = entry.displayId; | 
|  | 1917 | int32_t action = entry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1918 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
|  | 1919 |  | 
|  | 1920 | // 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] | 1921 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1922 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1923 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); | 
|  | 1924 | sp<WindowInfoHandle> newTouchedWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1925 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1926 | // Copy current touch state into tempTouchState. | 
|  | 1927 | // This state will be used to update mTouchStatesByDisplay at the end of this function. | 
|  | 1928 | // 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] | 1929 | const TouchState* oldState = nullptr; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1930 | TouchState tempTouchState; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1931 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = | 
|  | 1932 | mTouchStatesByDisplay.find(displayId); | 
|  | 1933 | if (oldStateIt != mTouchStatesByDisplay.end()) { | 
|  | 1934 | oldState = &(oldStateIt->second); | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1935 | tempTouchState.copyFrom(*oldState); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1936 | } | 
|  | 1937 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1938 | bool isSplit = tempTouchState.split; | 
|  | 1939 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && | 
|  | 1940 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || | 
|  | 1941 | tempTouchState.displayId != displayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1942 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || | 
|  | 1943 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 1944 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); | 
|  | 1945 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || | 
|  | 1946 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1947 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1948 | bool wrongDevice = false; | 
|  | 1949 | if (newGesture) { | 
|  | 1950 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1951 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1952 | ALOGI("Dropping event because a pointer for a different device is already down " | 
|  | 1953 | "in display %" PRId32, | 
|  | 1954 | displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1955 | // TODO: test multiple simultaneous input streams. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1956 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1957 | switchedDevice = false; | 
|  | 1958 | wrongDevice = true; | 
|  | 1959 | goto Failed; | 
|  | 1960 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1961 | tempTouchState.reset(); | 
|  | 1962 | tempTouchState.down = down; | 
|  | 1963 | tempTouchState.deviceId = entry.deviceId; | 
|  | 1964 | tempTouchState.source = entry.source; | 
|  | 1965 | tempTouchState.displayId = displayId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1966 | isSplit = false; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1967 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1968 | ALOGI("Dropping move event because a pointer for a different device is already active " | 
|  | 1969 | "in display %" PRId32, | 
|  | 1970 | displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1971 | // TODO: test multiple simultaneous input streams. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1972 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1973 | switchedDevice = false; | 
|  | 1974 | wrongDevice = true; | 
|  | 1975 | goto Failed; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1976 | } | 
|  | 1977 |  | 
|  | 1978 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { | 
|  | 1979 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ | 
|  | 1980 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1981 | int32_t x; | 
|  | 1982 | int32_t y; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1983 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1984 | // Always dispatch mouse events to cursor position. | 
|  | 1985 | if (isFromMouse) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1986 | x = int32_t(entry.xCursorPosition); | 
|  | 1987 | y = int32_t(entry.yCursorPosition); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1988 | } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1989 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 1990 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1991 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1992 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1993 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, | 
|  | 1994 | isDown /*addOutsideTargets*/); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1995 |  | 
|  | 1996 | std::vector<TouchedMonitor> newGestureMonitors = isDown | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1997 | ? findTouchedGestureMonitorsLocked(displayId) | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1998 | : std::vector<TouchedMonitor>{}; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1999 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2000 | // Figure out whether splitting will be allowed for this window. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2001 | if (newTouchedWindowHandle != nullptr && | 
|  | 2002 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
| Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 2003 | // New window supports splitting, but we should never split mouse events. | 
|  | 2004 | isSplit = !isFromMouse; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2005 | } else if (isSplit) { | 
|  | 2006 | // New window does not support splitting but we have already split events. | 
|  | 2007 | // Ignore the new window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2008 | newTouchedWindowHandle = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2009 | } | 
|  | 2010 |  | 
|  | 2011 | // Handle the case where we did not find a window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2012 | if (newTouchedWindowHandle == nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2013 | // 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] | 2014 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2015 | } | 
|  | 2016 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2017 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { | 
|  | 2018 | ALOGI("Not sending touch event to %s because it is paused", | 
|  | 2019 | newTouchedWindowHandle->getName().c_str()); | 
|  | 2020 | newTouchedWindowHandle = nullptr; | 
|  | 2021 | } | 
|  | 2022 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2023 | // Ensure the window has a connection and the connection is responsive | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2024 | if (newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2025 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); | 
|  | 2026 | if (!isResponsive) { | 
|  | 2027 | ALOGW("%s will not receive the new gesture at %" PRIu64, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2028 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); | 
|  | 2029 | newTouchedWindowHandle = nullptr; | 
|  | 2030 | } | 
|  | 2031 | } | 
|  | 2032 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2033 | // Drop events that can't be trusted due to occlusion | 
|  | 2034 | if (newTouchedWindowHandle != nullptr && | 
|  | 2035 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { | 
|  | 2036 | TouchOcclusionInfo occlusionInfo = | 
|  | 2037 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2038 | if (!isTouchTrustedLocked(occlusionInfo)) { | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2039 | if (DEBUG_TOUCH_OCCLUSION) { | 
|  | 2040 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); | 
|  | 2041 | for (const auto& log : occlusionInfo.debugInfo) { | 
|  | 2042 | ALOGD("%s", log.c_str()); | 
|  | 2043 | } | 
|  | 2044 | } | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2045 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2046 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { | 
|  | 2047 | ALOGW("Dropping untrusted touch event due to %s/%d", | 
|  | 2048 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); | 
|  | 2049 | newTouchedWindowHandle = nullptr; | 
|  | 2050 | } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2051 | } | 
|  | 2052 | } | 
|  | 2053 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2054 | // Also don't send the new touch event to unresponsive gesture monitors | 
|  | 2055 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); | 
|  | 2056 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2057 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { | 
|  | 2058 | ALOGI("Dropping event because there is no touchable window or gesture monitor at " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2059 | "(%d, %d) in display %" PRId32 ".", | 
|  | 2060 | x, y, displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2061 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2062 | goto Failed; | 
|  | 2063 | } | 
|  | 2064 |  | 
|  | 2065 | if (newTouchedWindowHandle != nullptr) { | 
|  | 2066 | // Set target flags. | 
|  | 2067 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 2068 | if (isSplit) { | 
|  | 2069 | targetFlags |= InputTarget::FLAG_SPLIT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2070 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2071 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
|  | 2072 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
|  | 2073 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { | 
|  | 2074 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 2075 | } | 
|  | 2076 |  | 
|  | 2077 | // Update hover state. | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2078 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { | 
|  | 2079 | newHoverWindowHandle = nullptr; | 
|  | 2080 | } else if (isHoverAction) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2081 | newHoverWindowHandle = newTouchedWindowHandle; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2082 | } | 
|  | 2083 |  | 
|  | 2084 | // Update the temporary touch state. | 
|  | 2085 | BitSet32 pointerIds; | 
|  | 2086 | if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2087 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2088 | pointerIds.markBit(pointerId); | 
|  | 2089 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2090 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2091 | } | 
|  | 2092 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2093 | tempTouchState.addGestureMonitors(newGestureMonitors); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2094 | } else { | 
|  | 2095 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ | 
|  | 2096 |  | 
|  | 2097 | // If the pointer is not currently down, then ignore the event. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2098 | if (!tempTouchState.down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2099 | if (DEBUG_FOCUS) { | 
|  | 2100 | ALOGD("Dropping event because the pointer is not down or we previously " | 
|  | 2101 | "dropped the pointer down event in display %" PRId32, | 
|  | 2102 | displayId); | 
|  | 2103 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2104 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2105 | goto Failed; | 
|  | 2106 | } | 
|  | 2107 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2108 | addDragEventLocked(entry); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2109 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2110 | // Check whether touches should slip outside of the current foreground window. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2111 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2112 | tempTouchState.isSlippery()) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2113 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 2114 | 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] | 2115 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2116 | sp<WindowInfoHandle> oldTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2117 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2118 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2119 | if (oldTouchedWindowHandle != newTouchedWindowHandle && | 
|  | 2120 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2121 | if (DEBUG_FOCUS) { | 
|  | 2122 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, | 
|  | 2123 | oldTouchedWindowHandle->getName().c_str(), | 
|  | 2124 | newTouchedWindowHandle->getName().c_str(), displayId); | 
|  | 2125 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2126 | // Make a slippery exit from the old window. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2127 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, | 
|  | 2128 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, | 
|  | 2129 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2130 |  | 
|  | 2131 | // Make a slippery entrance into the new window. | 
|  | 2132 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
|  | 2133 | isSplit = true; | 
|  | 2134 | } | 
|  | 2135 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2136 | int32_t targetFlags = | 
|  | 2137 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2138 | if (isSplit) { | 
|  | 2139 | targetFlags |= InputTarget::FLAG_SPLIT; | 
|  | 2140 | } | 
|  | 2141 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
|  | 2142 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2143 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { | 
|  | 2144 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2145 | } | 
|  | 2146 |  | 
|  | 2147 | BitSet32 pointerIds; | 
|  | 2148 | if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2149 | pointerIds.markBit(entry.pointerProperties[0].id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2150 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2151 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2152 | } | 
|  | 2153 | } | 
|  | 2154 | } | 
|  | 2155 |  | 
|  | 2156 | if (newHoverWindowHandle != mLastHoverWindowHandle) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2157 | // Let the previous window know that the hover sequence is over, unless we already did it | 
|  | 2158 | // when dispatching it as is to newTouchedWindowHandle. | 
|  | 2159 | if (mLastHoverWindowHandle != nullptr && | 
|  | 2160 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || | 
|  | 2161 | mLastHoverWindowHandle != newTouchedWindowHandle)) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2162 | if (DEBUG_HOVER) { | 
|  | 2163 | ALOGD("Sending hover exit event to window %s.", | 
|  | 2164 | mLastHoverWindowHandle->getName().c_str()); | 
|  | 2165 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2166 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, | 
|  | 2167 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2168 | } | 
|  | 2169 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2170 | // Let the new window know that the hover sequence is starting, unless we already did it | 
|  | 2171 | // when dispatching it as is to newTouchedWindowHandle. | 
|  | 2172 | if (newHoverWindowHandle != nullptr && | 
|  | 2173 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 2174 | newHoverWindowHandle != newTouchedWindowHandle)) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2175 | if (DEBUG_HOVER) { | 
|  | 2176 | ALOGD("Sending hover enter event to window %s.", | 
|  | 2177 | newHoverWindowHandle->getName().c_str()); | 
|  | 2178 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2179 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, | 
|  | 2180 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, | 
|  | 2181 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2182 | } | 
|  | 2183 | } | 
|  | 2184 |  | 
|  | 2185 | // Check permission to inject into all touched foreground windows and ensure there | 
|  | 2186 | // is at least one touched foreground window. | 
|  | 2187 | { | 
|  | 2188 | bool haveForegroundWindow = false; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2189 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2190 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { | 
|  | 2191 | haveForegroundWindow = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2192 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2193 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2194 | injectionPermission = INJECTION_PERMISSION_DENIED; | 
|  | 2195 | goto Failed; | 
|  | 2196 | } | 
|  | 2197 | } | 
|  | 2198 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2199 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2200 | if (!haveForegroundWindow && !hasGestureMonitor) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2201 | ALOGI("Dropping event because there is no touched foreground window in display " | 
|  | 2202 | "%" PRId32 " or gesture monitor to receive it.", | 
|  | 2203 | displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2204 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2205 | goto Failed; | 
|  | 2206 | } | 
|  | 2207 |  | 
|  | 2208 | // Permission granted to injection into all touched foreground windows. | 
|  | 2209 | injectionPermission = INJECTION_PERMISSION_GRANTED; | 
|  | 2210 | } | 
|  | 2211 |  | 
|  | 2212 | // Check whether windows listening for outside touches are owned by the same UID. If it is | 
|  | 2213 | // set the policy flag that we will not reveal coordinate information to this window. | 
|  | 2214 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2215 | sp<WindowInfoHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2216 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2217 | if (foregroundWindowHandle) { | 
|  | 2218 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2219 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2220 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2221 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; | 
|  | 2222 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { | 
|  | 2223 | tempTouchState.addOrUpdateWindow(windowInfoHandle, | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2224 | InputTarget::FLAG_ZERO_COORDS, | 
|  | 2225 | BitSet32(0)); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2226 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2227 | } | 
|  | 2228 | } | 
|  | 2229 | } | 
|  | 2230 | } | 
|  | 2231 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2232 | // If this is the first pointer going down and the touched window has a wallpaper | 
|  | 2233 | // then also add the touched wallpaper windows so they are locked in for the duration | 
|  | 2234 | // of the touch gesture. | 
|  | 2235 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper | 
|  | 2236 | // engine only supports touch events.  We would need to add a mechanism similar | 
|  | 2237 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. | 
|  | 2238 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2239 | sp<WindowInfoHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2240 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2241 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2242 | const std::vector<sp<WindowInfoHandle>>& windowHandles = | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2243 | getWindowHandlesLocked(displayId); | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2244 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { | 
|  | 2245 | const WindowInfo* info = windowHandle->getInfo(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2246 | if (info->displayId == displayId && | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2247 | windowHandle->getInfo()->type == WindowInfo::Type::WALLPAPER) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2248 | tempTouchState | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2249 | .addOrUpdateWindow(windowHandle, | 
|  | 2250 | InputTarget::FLAG_WINDOW_IS_OBSCURED | | 
|  | 2251 | InputTarget:: | 
|  | 2252 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | | 
|  | 2253 | InputTarget::FLAG_DISPATCH_AS_IS, | 
|  | 2254 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2255 | } | 
|  | 2256 | } | 
|  | 2257 | } | 
|  | 2258 | } | 
|  | 2259 |  | 
|  | 2260 | // Success!  Output targets. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2261 | injectionResult = InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2262 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2263 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2264 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2265 | touchedWindow.pointerIds, inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2266 | } | 
|  | 2267 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2268 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2269 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2270 | touchedMonitor.yOffset, inputTargets); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2271 | } | 
|  | 2272 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2273 | // Drop the outside or hover touch windows since we will not care about them | 
|  | 2274 | // in the next iteration. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2275 | tempTouchState.filterNonAsIsTouchWindows(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2276 |  | 
|  | 2277 | Failed: | 
|  | 2278 | // Check injection permission once and for all. | 
|  | 2279 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2280 | if (checkInjectionPermission(nullptr, entry.injectionState)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2281 | injectionPermission = INJECTION_PERMISSION_GRANTED; | 
|  | 2282 | } else { | 
|  | 2283 | injectionPermission = INJECTION_PERMISSION_DENIED; | 
|  | 2284 | } | 
|  | 2285 | } | 
|  | 2286 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2287 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { | 
|  | 2288 | return injectionResult; | 
|  | 2289 | } | 
|  | 2290 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2291 | // Update final pieces of touch state if the injector had permission. | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2292 | if (!wrongDevice) { | 
|  | 2293 | if (switchedDevice) { | 
|  | 2294 | if (DEBUG_FOCUS) { | 
|  | 2295 | ALOGD("Conflicting pointer actions: Switched to a different device."); | 
|  | 2296 | } | 
|  | 2297 | *outConflictingPointerActions = true; | 
|  | 2298 | } | 
|  | 2299 |  | 
|  | 2300 | if (isHoverAction) { | 
|  | 2301 | // Started hovering, therefore no longer down. | 
|  | 2302 | if (oldState && oldState->down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2303 | if (DEBUG_FOCUS) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2304 | ALOGD("Conflicting pointer actions: Hover received while pointer was " | 
|  | 2305 | "down."); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2306 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2307 | *outConflictingPointerActions = true; | 
|  | 2308 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2309 | tempTouchState.reset(); | 
|  | 2310 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 2311 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { | 
|  | 2312 | tempTouchState.deviceId = entry.deviceId; | 
|  | 2313 | tempTouchState.source = entry.source; | 
|  | 2314 | tempTouchState.displayId = displayId; | 
|  | 2315 | } | 
|  | 2316 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || | 
|  | 2317 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 2318 | // All pointers up or canceled. | 
|  | 2319 | tempTouchState.reset(); | 
|  | 2320 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 2321 | // First pointer went down. | 
|  | 2322 | if (oldState && oldState->down) { | 
|  | 2323 | if (DEBUG_FOCUS) { | 
|  | 2324 | ALOGD("Conflicting pointer actions: Down received while already down."); | 
|  | 2325 | } | 
|  | 2326 | *outConflictingPointerActions = true; | 
|  | 2327 | } | 
|  | 2328 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
|  | 2329 | // One pointer went up. | 
|  | 2330 | if (isSplit) { | 
|  | 2331 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 2332 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2333 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2334 | for (size_t i = 0; i < tempTouchState.windows.size();) { | 
|  | 2335 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; | 
|  | 2336 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { | 
|  | 2337 | touchedWindow.pointerIds.clearBit(pointerId); | 
|  | 2338 | if (touchedWindow.pointerIds.isEmpty()) { | 
|  | 2339 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); | 
|  | 2340 | continue; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2341 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2342 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2343 | i += 1; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2344 | } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2345 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2346 | } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2347 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2348 | // Save changes unless the action was scroll in which case the temporary touch | 
|  | 2349 | // state was only valid for this one action. | 
|  | 2350 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { | 
|  | 2351 | if (tempTouchState.displayId >= 0) { | 
|  | 2352 | mTouchStatesByDisplay[displayId] = tempTouchState; | 
|  | 2353 | } else { | 
|  | 2354 | mTouchStatesByDisplay.erase(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2355 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2356 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2357 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2358 | // Update hover state. | 
|  | 2359 | mLastHoverWindowHandle = newHoverWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2360 | } | 
|  | 2361 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2362 | return injectionResult; | 
|  | 2363 | } | 
|  | 2364 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2365 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2366 | const sp<WindowInfoHandle> dropWindow = | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2367 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2368 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2369 | if (dropWindow) { | 
|  | 2370 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2371 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2372 | } else { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2373 | sendDropWindowCommandLocked(nullptr, 0, 0); | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2374 | } | 
|  | 2375 | mDragState.reset(); | 
|  | 2376 | } | 
|  | 2377 |  | 
|  | 2378 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { | 
|  | 2379 | if (entry.pointerCount != 1 || !mDragState) { | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2380 | return; | 
|  | 2381 | } | 
|  | 2382 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2383 | if (!mDragState->isStartDrag) { | 
|  | 2384 | mDragState->isStartDrag = true; | 
|  | 2385 | mDragState->isStylusButtonDownAtStart = | 
|  | 2386 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; | 
|  | 2387 | } | 
|  | 2388 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2389 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; | 
|  | 2390 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 2391 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
|  | 2392 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2393 | // Handle the special case : stylus button no longer pressed. | 
|  | 2394 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; | 
|  | 2395 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { | 
|  | 2396 | finishDragAndDrop(entry.displayId, x, y); | 
|  | 2397 | return; | 
|  | 2398 | } | 
|  | 2399 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2400 | const sp<WindowInfoHandle> hoverWindowHandle = | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2401 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2402 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2403 | // enqueue drag exit if needed. | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2404 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && | 
|  | 2405 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { | 
|  | 2406 | if (mDragState->dragHoverWindowHandle != nullptr) { | 
|  | 2407 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, | 
|  | 2408 | entry); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2409 | } | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2410 | mDragState->dragHoverWindowHandle = hoverWindowHandle; | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2411 | } | 
|  | 2412 | // enqueue drag location if needed. | 
|  | 2413 | if (hoverWindowHandle != nullptr) { | 
|  | 2414 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); | 
|  | 2415 | } | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2416 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { | 
|  | 2417 | finishDragAndDrop(entry.displayId, x, y); | 
|  | 2418 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2419 | sendDropWindowCommandLocked(nullptr, 0, 0); | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2420 | mDragState.reset(); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2421 | } | 
|  | 2422 | } | 
|  | 2423 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2424 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2425 | int32_t targetFlags, BitSet32 pointerIds, | 
|  | 2426 | std::vector<InputTarget>& inputTargets) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2427 | std::vector<InputTarget>::iterator it = | 
|  | 2428 | std::find_if(inputTargets.begin(), inputTargets.end(), | 
|  | 2429 | [&windowHandle](const InputTarget& inputTarget) { | 
|  | 2430 | return inputTarget.inputChannel->getConnectionToken() == | 
|  | 2431 | windowHandle->getToken(); | 
|  | 2432 | }); | 
| Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2433 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2434 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2435 |  | 
|  | 2436 | if (it == inputTargets.end()) { | 
|  | 2437 | InputTarget inputTarget; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2438 | std::shared_ptr<InputChannel> inputChannel = | 
|  | 2439 | getInputChannelLocked(windowHandle->getToken()); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2440 | if (inputChannel == nullptr) { | 
|  | 2441 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); | 
|  | 2442 | return; | 
|  | 2443 | } | 
|  | 2444 | inputTarget.inputChannel = inputChannel; | 
|  | 2445 | inputTarget.flags = targetFlags; | 
|  | 2446 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 2447 | inputTarget.displayOrientation = windowInfo->displayOrientation; | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2448 | inputTarget.displaySize = | 
| Evan Rosky | 44edce9 | 2021-05-14 18:09:55 -0700 | [diff] [blame] | 2449 | int2(windowHandle->getInfo()->displayWidth, windowHandle->getInfo()->displayHeight); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2450 | inputTargets.push_back(inputTarget); | 
|  | 2451 | it = inputTargets.end() - 1; | 
|  | 2452 | } | 
|  | 2453 |  | 
|  | 2454 | ALOG_ASSERT(it->flags == targetFlags); | 
|  | 2455 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); | 
|  | 2456 |  | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2457 | it->addPointers(pointerIds, windowInfo->transform); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2458 | } | 
|  | 2459 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2460 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2461 | int32_t displayId, float xOffset, | 
|  | 2462 | float yOffset) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2463 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = | 
|  | 2464 | mGlobalMonitorsByDisplay.find(displayId); | 
|  | 2465 |  | 
|  | 2466 | if (it != mGlobalMonitorsByDisplay.end()) { | 
|  | 2467 | const std::vector<Monitor>& monitors = it->second; | 
|  | 2468 | for (const Monitor& monitor : monitors) { | 
|  | 2469 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2470 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2471 | } | 
|  | 2472 | } | 
|  | 2473 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2474 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, | 
|  | 2475 | float yOffset, | 
|  | 2476 | std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2477 | InputTarget target; | 
|  | 2478 | target.inputChannel = monitor.inputChannel; | 
|  | 2479 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2480 | ui::Transform t; | 
|  | 2481 | t.set(xOffset, yOffset); | 
|  | 2482 | target.setDefaultPointerTransform(t); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2483 | inputTargets.push_back(target); | 
|  | 2484 | } | 
|  | 2485 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2486 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2487 | const InjectionState* injectionState) { | 
|  | 2488 | if (injectionState && | 
|  | 2489 | (windowHandle == nullptr || | 
|  | 2490 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && | 
|  | 2491 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2492 | if (windowHandle != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2493 | ALOGW("Permission denied: injecting event from pid %d uid %d to window %s " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2494 | "owned by uid %d", | 
|  | 2495 | injectionState->injectorPid, injectionState->injectorUid, | 
|  | 2496 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2497 | } else { | 
|  | 2498 | ALOGW("Permission denied: injecting event from pid %d uid %d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2499 | injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2500 | } | 
|  | 2501 | return false; | 
|  | 2502 | } | 
|  | 2503 | return true; | 
|  | 2504 | } | 
|  | 2505 |  | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2506 | /** | 
|  | 2507 | * Indicate whether one window handle should be considered as obscuring | 
|  | 2508 | * another window handle. We only check a few preconditions. Actually | 
|  | 2509 | * checking the bounds is left to the caller. | 
|  | 2510 | */ | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2511 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, | 
|  | 2512 | const sp<WindowInfoHandle>& otherHandle) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2513 | // Compare by token so cloned layers aren't counted | 
|  | 2514 | if (haveSameToken(windowHandle, otherHandle)) { | 
|  | 2515 | return false; | 
|  | 2516 | } | 
|  | 2517 | auto info = windowHandle->getInfo(); | 
|  | 2518 | auto otherInfo = otherHandle->getInfo(); | 
|  | 2519 | if (!otherInfo->visible) { | 
|  | 2520 | return false; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2521 | } else if (otherInfo->alpha == 0 && otherInfo->flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { | 
| Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2522 | // Those act as if they were invisible, so we don't need to flag them. | 
|  | 2523 | // We do want to potentially flag touchable windows even if they have 0 | 
|  | 2524 | // opacity, since they can consume touches and alter the effects of the | 
|  | 2525 | // user interaction (eg. apps that rely on | 
|  | 2526 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those | 
|  | 2527 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. | 
|  | 2528 | return false; | 
| Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2529 | } else if (info->ownerUid == otherInfo->ownerUid) { | 
|  | 2530 | // If ownerUid is the same we don't generate occlusion events as there | 
|  | 2531 | // is no security boundary within an uid. | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2532 | return false; | 
| Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2533 | } else if (otherInfo->trustedOverlay) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2534 | return false; | 
|  | 2535 | } else if (otherInfo->displayId != info->displayId) { | 
|  | 2536 | return false; | 
|  | 2537 | } | 
|  | 2538 | return true; | 
|  | 2539 | } | 
|  | 2540 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2541 | /** | 
|  | 2542 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is | 
|  | 2543 | * untrusted, one should check: | 
|  | 2544 | * | 
|  | 2545 | * 1. If result.hasBlockingOcclusion is true. | 
|  | 2546 | *    If it's, it means the touch should be blocked due to a window with occlusion mode of | 
|  | 2547 | *    BLOCK_UNTRUSTED. | 
|  | 2548 | * | 
|  | 2549 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. | 
|  | 2550 | *    If it is (and 1 is false), then the touch should be blocked because a stack of windows | 
|  | 2551 | *    (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed | 
|  | 2552 | *    obscuring opacity above the threshold. Note that if there was no window of occlusion mode | 
|  | 2553 | *    USE_OPACITY, result.obscuringOpacity would've been 0 and since | 
|  | 2554 | *    mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. | 
|  | 2555 | * | 
|  | 2556 | * If neither of those is true, then it means the touch can be allowed. | 
|  | 2557 | */ | 
|  | 2558 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2559 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { | 
|  | 2560 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2561 | int32_t displayId = windowInfo->displayId; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2562 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2563 | TouchOcclusionInfo info; | 
|  | 2564 | info.hasBlockingOcclusion = false; | 
|  | 2565 | info.obscuringOpacity = 0; | 
|  | 2566 | info.obscuringUid = -1; | 
|  | 2567 | std::map<int32_t, float> opacityByUid; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2568 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2569 | if (windowHandle == otherHandle) { | 
|  | 2570 | break; // All future windows are below us. Exit early. | 
|  | 2571 | } | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2572 | const WindowInfo* otherInfo = otherHandle->getInfo(); | 
| Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2573 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && | 
|  | 2574 | !haveSameApplicationToken(windowInfo, otherInfo)) { | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2575 | if (DEBUG_TOUCH_OCCLUSION) { | 
|  | 2576 | info.debugInfo.push_back( | 
|  | 2577 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); | 
|  | 2578 | } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2579 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so | 
|  | 2580 | // we perform the checks below to see if the touch can be propagated or not based on the | 
|  | 2581 | // window's touch occlusion mode | 
|  | 2582 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { | 
|  | 2583 | info.hasBlockingOcclusion = true; | 
|  | 2584 | info.obscuringUid = otherInfo->ownerUid; | 
|  | 2585 | info.obscuringPackage = otherInfo->packageName; | 
|  | 2586 | break; | 
|  | 2587 | } | 
|  | 2588 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { | 
|  | 2589 | uint32_t uid = otherInfo->ownerUid; | 
|  | 2590 | float opacity = | 
|  | 2591 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; | 
|  | 2592 | // Given windows A and B: | 
|  | 2593 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] | 
|  | 2594 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); | 
|  | 2595 | opacityByUid[uid] = opacity; | 
|  | 2596 | if (opacity > info.obscuringOpacity) { | 
|  | 2597 | info.obscuringOpacity = opacity; | 
|  | 2598 | info.obscuringUid = uid; | 
|  | 2599 | info.obscuringPackage = otherInfo->packageName; | 
|  | 2600 | } | 
|  | 2601 | } | 
|  | 2602 | } | 
|  | 2603 | } | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2604 | if (DEBUG_TOUCH_OCCLUSION) { | 
|  | 2605 | info.debugInfo.push_back( | 
|  | 2606 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); | 
|  | 2607 | } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2608 | return info; | 
|  | 2609 | } | 
|  | 2610 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2611 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2612 | bool isTouchedWindow) const { | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2613 | return StringPrintf(INDENT2 | 
|  | 2614 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " | 
|  | 2615 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 | 
|  | 2616 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " | 
|  | 2617 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2618 | (isTouchedWindow) ? "[TOUCHED] " : "", | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2619 | NamedEnum::string(info->type, "%" PRId32).c_str(), | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2620 | info->packageName.c_str(), info->ownerUid, info->id, | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2621 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, | 
|  | 2622 | info->frameTop, info->frameRight, info->frameBottom, | 
|  | 2623 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2624 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), | 
|  | 2625 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), | 
|  | 2626 | toString(info->applicationInfo.token).c_str()); | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2627 | } | 
|  | 2628 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2629 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { | 
|  | 2630 | if (occlusionInfo.hasBlockingOcclusion) { | 
|  | 2631 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), | 
|  | 2632 | occlusionInfo.obscuringUid); | 
|  | 2633 | return false; | 
|  | 2634 | } | 
|  | 2635 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { | 
|  | 2636 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " | 
|  | 2637 | "%.2f, maximum allowed = %.2f)", | 
|  | 2638 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, | 
|  | 2639 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); | 
|  | 2640 | return false; | 
|  | 2641 | } | 
|  | 2642 | return true; | 
|  | 2643 | } | 
|  | 2644 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2645 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2646 | int32_t x, int32_t y) const { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2647 | int32_t displayId = windowHandle->getInfo()->displayId; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2648 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
|  | 2649 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2650 | if (windowHandle == otherHandle) { | 
|  | 2651 | break; // All future windows are below us. Exit early. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2652 | } | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2653 | const WindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2654 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2655 | otherInfo->frameContainsPoint(x, y)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2656 | return true; | 
|  | 2657 | } | 
|  | 2658 | } | 
|  | 2659 | return false; | 
|  | 2660 | } | 
|  | 2661 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2662 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2663 | int32_t displayId = windowHandle->getInfo()->displayId; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2664 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
|  | 2665 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
|  | 2666 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2667 | if (windowHandle == otherHandle) { | 
|  | 2668 | break; // All future windows are below us. Exit early. | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2669 | } | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2670 | const WindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2671 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2672 | otherInfo->overlaps(windowInfo)) { | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2673 | return true; | 
|  | 2674 | } | 
|  | 2675 | } | 
|  | 2676 | return false; | 
|  | 2677 | } | 
|  | 2678 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2679 | std::string InputDispatcher::getApplicationWindowLabel( | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2680 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2681 | if (applicationHandle != nullptr) { | 
|  | 2682 | if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2683 | return applicationHandle->getName() + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2684 | } else { | 
|  | 2685 | return applicationHandle->getName(); | 
|  | 2686 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2687 | } else if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2688 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2689 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2690 | return "<unknown application or window>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2691 | } | 
|  | 2692 | } | 
|  | 2693 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2694 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2695 | if (eventEntry.type == EventEntry::Type::FOCUS || | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2696 | eventEntry.type == EventEntry::Type::POINTER_CAPTURE_CHANGED || | 
|  | 2697 | eventEntry.type == EventEntry::Type::DRAG) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2698 | // Focus or pointer capture changed events are passed to apps, but do not represent user | 
|  | 2699 | // activity. | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2700 | return; | 
|  | 2701 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2702 | int32_t displayId = getTargetDisplayId(eventEntry); | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2703 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2704 | if (focusedWindowHandle != nullptr) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2705 | const WindowInfo* info = focusedWindowHandle->getInfo(); | 
|  | 2706 | if (info->inputFeatures.test(WindowInfo::Feature::DISABLE_USER_ACTIVITY)) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2707 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 2708 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); | 
|  | 2709 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2710 | return; | 
|  | 2711 | } | 
|  | 2712 | } | 
|  | 2713 |  | 
|  | 2714 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2715 | switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2716 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2717 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
|  | 2718 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2719 | return; | 
|  | 2720 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2721 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2722 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2723 | eventType = USER_ACTIVITY_EVENT_TOUCH; | 
|  | 2724 | } | 
|  | 2725 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2726 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2727 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2728 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
|  | 2729 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2730 | return; | 
|  | 2731 | } | 
|  | 2732 | eventType = USER_ACTIVITY_EVENT_BUTTON; | 
|  | 2733 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2734 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2735 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2736 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2737 | case EventEntry::Type::DEVICE_RESET: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2738 | case EventEntry::Type::SENSOR: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2739 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 2740 | case EventEntry::Type::DRAG: { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2741 | LOG_ALWAYS_FATAL("%s events are not user activity", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2742 | NamedEnum::string(eventEntry.type).c_str()); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2743 | break; | 
|  | 2744 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2745 | } | 
|  | 2746 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2747 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() | 
|  | 2748 | REQUIRES(mLock) { | 
|  | 2749 | scoped_unlock unlock(mLock); | 
|  | 2750 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); | 
|  | 2751 | }; | 
|  | 2752 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2753 | } | 
|  | 2754 |  | 
|  | 2755 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2756 | const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2757 | std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2758 | const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2759 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2760 | std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2761 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2762 | connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2763 | ATRACE_NAME(message.c_str()); | 
|  | 2764 | } | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2765 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 2766 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " | 
|  | 2767 | "globalScaleFactor=%f, pointerIds=0x%x %s", | 
|  | 2768 | connection->getInputChannelName().c_str(), inputTarget.flags, | 
|  | 2769 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, | 
|  | 2770 | inputTarget.getPointerInfoString().c_str()); | 
|  | 2771 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2772 |  | 
|  | 2773 | // Skip this event if the connection status is not normal. | 
|  | 2774 | // We don't want to enqueue additional outbound events if the connection is broken. | 
|  | 2775 | if (connection->status != Connection::STATUS_NORMAL) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2776 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 2777 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", | 
|  | 2778 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); | 
|  | 2779 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2780 | return; | 
|  | 2781 | } | 
|  | 2782 |  | 
|  | 2783 | // Split a motion event if needed. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2784 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { | 
|  | 2785 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, | 
|  | 2786 | "Entry type %s should not have FLAG_SPLIT", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2787 | NamedEnum::string(eventEntry->type).c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2788 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2789 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2790 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2791 | std::unique_ptr<MotionEntry> splitMotionEntry = | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2792 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2793 | if (!splitMotionEntry) { | 
|  | 2794 | return; // split event was dropped | 
|  | 2795 | } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2796 | if (DEBUG_FOCUS) { | 
|  | 2797 | ALOGD("channel '%s' ~ Split motion event.", | 
|  | 2798 | connection->getInputChannelName().c_str()); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2799 | logOutboundMotionDetails("  ", *splitMotionEntry); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2800 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2801 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), | 
|  | 2802 | inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2803 | return; | 
|  | 2804 | } | 
|  | 2805 | } | 
|  | 2806 |  | 
|  | 2807 | // Not splitting.  Enqueue dispatch entries for the event as is. | 
|  | 2808 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); | 
|  | 2809 | } | 
|  | 2810 |  | 
|  | 2811 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2812 | const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2813 | std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2814 | const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2815 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2816 | std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2817 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2818 | connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2819 | ATRACE_NAME(message.c_str()); | 
|  | 2820 | } | 
|  | 2821 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2822 | bool wasEmpty = connection->outboundQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2823 |  | 
|  | 2824 | // Enqueue dispatch entries for the requested modes. | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2825 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2826 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2827 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2828 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2829 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2830 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2831 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2832 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2833 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2834 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2835 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2836 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2837 |  | 
|  | 2838 | // If the outbound queue was previously empty, start the dispatch cycle going. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2839 | if (wasEmpty && !connection->outboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2840 | startDispatchCycleLocked(currentTime, connection); | 
|  | 2841 | } | 
|  | 2842 | } | 
|  | 2843 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2844 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2845 | std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2846 | const InputTarget& inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2847 | int32_t dispatchMode) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2848 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2849 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", | 
|  | 2850 | connection->getInputChannelName().c_str(), | 
|  | 2851 | dispatchModeToString(dispatchMode).c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2852 | ATRACE_NAME(message.c_str()); | 
|  | 2853 | } | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2854 | int32_t inputTargetFlags = inputTarget.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2855 | if (!(inputTargetFlags & dispatchMode)) { | 
|  | 2856 | return; | 
|  | 2857 | } | 
|  | 2858 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; | 
|  | 2859 |  | 
|  | 2860 | // This is a new event. | 
|  | 2861 | // Enqueue a new dispatch entry onto the outbound queue for this connection. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2862 | std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2863 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2864 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2865 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a | 
|  | 2866 | // different EventEntry than what was passed in. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2867 | EventEntry& newEntry = *(dispatchEntry->eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2868 | // Apply target flags and update the connection's input state. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2869 | switch (newEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2870 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2871 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2872 | dispatchEntry->resolvedEventId = keyEntry.id; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2873 | dispatchEntry->resolvedAction = keyEntry.action; | 
|  | 2874 | dispatchEntry->resolvedFlags = keyEntry.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2875 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2876 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, | 
|  | 2877 | dispatchEntry->resolvedFlags)) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2878 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 2879 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " | 
|  | 2880 | "event", | 
|  | 2881 | connection->getInputChannelName().c_str()); | 
|  | 2882 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2883 | return; // skip the inconsistent event | 
|  | 2884 | } | 
|  | 2885 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2886 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2887 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2888 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2889 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2890 | // Assign a default value to dispatchEntry that will never be generated by InputReader, | 
|  | 2891 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. | 
|  | 2892 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = | 
|  | 2893 | static_cast<int32_t>(IdGenerator::Source::OTHER); | 
|  | 2894 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2895 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 2896 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; | 
|  | 2897 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { | 
|  | 2898 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; | 
|  | 2899 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { | 
|  | 2900 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
|  | 2901 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { | 
|  | 2902 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; | 
|  | 2903 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { | 
|  | 2904 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; | 
|  | 2905 | } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2906 | dispatchEntry->resolvedAction = motionEntry.action; | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2907 | dispatchEntry->resolvedEventId = motionEntry.id; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2908 | } | 
|  | 2909 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2910 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, | 
|  | 2911 | motionEntry.displayId)) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2912 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 2913 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " | 
|  | 2914 | "enter event", | 
|  | 2915 | connection->getInputChannelName().c_str()); | 
|  | 2916 | } | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 2917 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because | 
|  | 2918 | // this is a one-to-one event conversion. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2919 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
|  | 2920 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2921 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2922 | dispatchEntry->resolvedFlags = motionEntry.flags; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2923 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { | 
|  | 2924 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; | 
|  | 2925 | } | 
|  | 2926 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { | 
|  | 2927 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 2928 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2929 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2930 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, | 
|  | 2931 | dispatchEntry->resolvedFlags)) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2932 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 2933 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " | 
|  | 2934 | "event", | 
|  | 2935 | connection->getInputChannelName().c_str()); | 
|  | 2936 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2937 | return; // skip the inconsistent event | 
|  | 2938 | } | 
|  | 2939 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2940 | dispatchEntry->resolvedEventId = | 
|  | 2941 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID | 
|  | 2942 | ? mIdGenerator.nextId() | 
|  | 2943 | : motionEntry.id; | 
|  | 2944 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { | 
|  | 2945 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 | 
|  | 2946 | ") to MotionEvent(id=0x%" PRIx32 ").", | 
|  | 2947 | motionEntry.id, dispatchEntry->resolvedEventId); | 
|  | 2948 | ATRACE_NAME(message.c_str()); | 
|  | 2949 | } | 
|  | 2950 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 2951 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && | 
|  | 2952 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { | 
|  | 2953 | // Skip reporting pointer down outside focus to the policy. | 
|  | 2954 | break; | 
|  | 2955 | } | 
|  | 2956 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2957 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2958 | inputTarget.inputChannel->getConnectionToken()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2959 |  | 
|  | 2960 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2961 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2962 | case EventEntry::Type::FOCUS: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2963 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 2964 | case EventEntry::Type::DRAG: { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2965 | break; | 
|  | 2966 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2967 | case EventEntry::Type::SENSOR: { | 
|  | 2968 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); | 
|  | 2969 | break; | 
|  | 2970 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2971 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2972 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2973 | LOG_ALWAYS_FATAL("%s events should not go to apps", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2974 | NamedEnum::string(newEntry.type).c_str()); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2975 | break; | 
|  | 2976 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2977 | } | 
|  | 2978 |  | 
|  | 2979 | // Remember that we are waiting for this dispatch to complete. | 
|  | 2980 | if (dispatchEntry->hasForegroundTarget()) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2981 | incrementPendingForegroundDispatches(newEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2982 | } | 
|  | 2983 |  | 
|  | 2984 | // Enqueue the dispatch entry. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2985 | connection->outboundQueue.push_back(dispatchEntry.release()); | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 2986 | traceOutboundQueueLength(*connection); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2987 | } | 
|  | 2988 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2989 | /** | 
|  | 2990 | * This function is purely for debugging. It helps us understand where the user interaction | 
|  | 2991 | * was taking place. For example, if user is touching launcher, we will see a log that user | 
|  | 2992 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. | 
|  | 2993 | * We will see both launcher and wallpaper in that list. | 
|  | 2994 | * Once the interaction with a particular set of connections starts, no new logs will be printed | 
|  | 2995 | * until the set of interacted connections changes. | 
|  | 2996 | * | 
|  | 2997 | * The following items are skipped, to reduce the logspam: | 
|  | 2998 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged | 
|  | 2999 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). | 
|  | 3000 | * This includes situations like the soft BACK button key. When the user releases (lifts up the | 
|  | 3001 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. | 
|  | 3002 | * Both of those ACTION_UP events would not be logged | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3003 | */ | 
|  | 3004 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, | 
|  | 3005 | const std::vector<InputTarget>& targets) { | 
|  | 3006 | // Skip ACTION_UP events, and all events other than keys and motions | 
|  | 3007 | if (entry.type == EventEntry::Type::KEY) { | 
|  | 3008 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
|  | 3009 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
|  | 3010 | return; | 
|  | 3011 | } | 
|  | 3012 | } else if (entry.type == EventEntry::Type::MOTION) { | 
|  | 3013 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 3014 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || | 
|  | 3015 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 3016 | return; | 
|  | 3017 | } | 
|  | 3018 | } else { | 
|  | 3019 | return; // Not a key or a motion | 
|  | 3020 | } | 
|  | 3021 |  | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3022 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3023 | std::vector<sp<Connection>> newConnections; | 
|  | 3024 | for (const InputTarget& target : targets) { | 
|  | 3025 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == | 
|  | 3026 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 3027 | continue; // Skip windows that receive ACTION_OUTSIDE | 
|  | 3028 | } | 
|  | 3029 |  | 
|  | 3030 | sp<IBinder> token = target.inputChannel->getConnectionToken(); | 
|  | 3031 | sp<Connection> connection = getConnectionLocked(token); | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3032 | if (connection == nullptr) { | 
|  | 3033 | continue; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3034 | } | 
|  | 3035 | newConnectionTokens.insert(std::move(token)); | 
|  | 3036 | newConnections.emplace_back(connection); | 
|  | 3037 | } | 
|  | 3038 | if (newConnectionTokens == mInteractionConnectionTokens) { | 
|  | 3039 | return; // no change | 
|  | 3040 | } | 
|  | 3041 | mInteractionConnectionTokens = newConnectionTokens; | 
|  | 3042 |  | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3043 | std::string targetList; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3044 | for (const sp<Connection>& connection : newConnections) { | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3045 | targetList += connection->getWindowName() + ", "; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3046 | } | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3047 | std::string message = "Interaction with: " + targetList; | 
|  | 3048 | if (targetList.empty()) { | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3049 | message += "<none>"; | 
|  | 3050 | } | 
|  | 3051 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; | 
|  | 3052 | } | 
|  | 3053 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3054 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3055 | const sp<IBinder>& token) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3056 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3057 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; | 
|  | 3058 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3059 | return; | 
|  | 3060 | } | 
|  | 3061 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3062 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3063 | if (focusedToken == token) { | 
|  | 3064 | // ignore since token is focused | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3065 | return; | 
|  | 3066 | } | 
|  | 3067 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3068 | auto command = [this, token]() REQUIRES(mLock) { | 
|  | 3069 | scoped_unlock unlock(mLock); | 
|  | 3070 | mPolicy->onPointerDownOutsideFocus(token); | 
|  | 3071 | }; | 
|  | 3072 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3073 | } | 
|  | 3074 |  | 
|  | 3075 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3076 | const sp<Connection>& connection) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3077 | if (ATRACE_ENABLED()) { | 
|  | 3078 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3079 | connection->getInputChannelName().c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3080 | ATRACE_NAME(message.c_str()); | 
|  | 3081 | } | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3082 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 3083 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); | 
|  | 3084 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3085 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3086 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { | 
|  | 3087 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3088 | dispatchEntry->deliveryTime = currentTime; | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3089 | const std::chrono::nanoseconds timeout = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3090 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3091 | dispatchEntry->timeoutTime = currentTime + timeout.count(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3092 |  | 
|  | 3093 | // Publish the event. | 
|  | 3094 | status_t status; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3095 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); | 
|  | 3096 | switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3097 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3098 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
|  | 3099 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3100 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3101 | // Publish the key event. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3102 | status = connection->inputPublisher | 
|  | 3103 | .publishKeyEvent(dispatchEntry->seq, | 
|  | 3104 | dispatchEntry->resolvedEventId, keyEntry.deviceId, | 
|  | 3105 | keyEntry.source, keyEntry.displayId, | 
|  | 3106 | std::move(hmac), dispatchEntry->resolvedAction, | 
|  | 3107 | dispatchEntry->resolvedFlags, keyEntry.keyCode, | 
|  | 3108 | keyEntry.scanCode, keyEntry.metaState, | 
|  | 3109 | keyEntry.repeatCount, keyEntry.downTime, | 
|  | 3110 | keyEntry.eventTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3111 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3112 | } | 
|  | 3113 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3114 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3115 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3116 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3117 | PointerCoords scaledCoords[MAX_POINTERS]; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3118 | const PointerCoords* usingCoords = motionEntry.pointerCoords; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3119 |  | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3120 | // 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] | 3121 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3122 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { | 
|  | 3123 | float globalScaleFactor = dispatchEntry->globalScaleFactor; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3124 | if (globalScaleFactor != 1.0f) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3125 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { | 
|  | 3126 | scaledCoords[i] = motionEntry.pointerCoords[i]; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3127 | // Don't apply window scale here since we don't want scale to affect raw | 
|  | 3128 | // coordinates. The scale will be sent back to the client and applied | 
|  | 3129 | // later when requesting relative coordinates. | 
|  | 3130 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, | 
|  | 3131 | 1 /* windowYScale */); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3132 | } | 
|  | 3133 | usingCoords = scaledCoords; | 
|  | 3134 | } | 
|  | 3135 | } else { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3136 | // We don't want the dispatch target to know. | 
|  | 3137 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3138 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3139 | scaledCoords[i].clear(); | 
|  | 3140 | } | 
|  | 3141 | usingCoords = scaledCoords; | 
|  | 3142 | } | 
|  | 3143 | } | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3144 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3145 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3146 |  | 
|  | 3147 | // Publish the motion event. | 
|  | 3148 | status = connection->inputPublisher | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3149 | .publishMotionEvent(dispatchEntry->seq, | 
|  | 3150 | dispatchEntry->resolvedEventId, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3151 | motionEntry.deviceId, motionEntry.source, | 
|  | 3152 | motionEntry.displayId, std::move(hmac), | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3153 | dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3154 | motionEntry.actionButton, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3155 | dispatchEntry->resolvedFlags, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3156 | motionEntry.edgeFlags, motionEntry.metaState, | 
|  | 3157 | motionEntry.buttonState, | 
|  | 3158 | motionEntry.classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3159 | dispatchEntry->transform, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3160 | motionEntry.xPrecision, motionEntry.yPrecision, | 
|  | 3161 | motionEntry.xCursorPosition, | 
|  | 3162 | motionEntry.yCursorPosition, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 3163 | dispatchEntry->displayOrientation, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3164 | dispatchEntry->displaySize.x, | 
|  | 3165 | dispatchEntry->displaySize.y, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3166 | motionEntry.downTime, motionEntry.eventTime, | 
|  | 3167 | motionEntry.pointerCount, | 
|  | 3168 | motionEntry.pointerProperties, usingCoords); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3169 | break; | 
|  | 3170 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3171 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3172 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3173 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3174 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3175 | focusEntry.id, | 
|  | 3176 | focusEntry.hasFocus, | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3177 | mInTouchMode); | 
|  | 3178 | break; | 
|  | 3179 | } | 
|  | 3180 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3181 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
|  | 3182 | const auto& captureEntry = | 
|  | 3183 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); | 
|  | 3184 | status = connection->inputPublisher | 
|  | 3185 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, | 
|  | 3186 | captureEntry.pointerCaptureEnabled); | 
|  | 3187 | break; | 
|  | 3188 | } | 
|  | 3189 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3190 | case EventEntry::Type::DRAG: { | 
|  | 3191 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); | 
|  | 3192 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, | 
|  | 3193 | dragEntry.id, dragEntry.x, | 
|  | 3194 | dragEntry.y, | 
|  | 3195 | dragEntry.isExiting); | 
|  | 3196 | break; | 
|  | 3197 | } | 
|  | 3198 |  | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3199 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3200 | case EventEntry::Type::DEVICE_RESET: | 
|  | 3201 | case EventEntry::Type::SENSOR: { | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3202 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3203 | NamedEnum::string(eventEntry.type).c_str()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3204 | return; | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3205 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3206 | } | 
|  | 3207 |  | 
|  | 3208 | // Check the result. | 
|  | 3209 | if (status) { | 
|  | 3210 | if (status == WOULD_BLOCK) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3211 | if (connection->waitQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3212 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3213 | "This is unexpected because the wait queue is empty, so the pipe " | 
|  | 3214 | "should be empty and we shouldn't have any problems writing an " | 
| Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3215 | "event to it, status=%s(%d)", | 
|  | 3216 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), | 
|  | 3217 | status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3218 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
|  | 3219 | } else { | 
|  | 3220 | // Pipe is full and we are waiting for the app to finish process some events | 
|  | 3221 | // before sending more events to it. | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3222 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 3223 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " | 
|  | 3224 | "waiting for the application to catch up", | 
|  | 3225 | connection->getInputChannelName().c_str()); | 
|  | 3226 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3227 | } | 
|  | 3228 | } else { | 
|  | 3229 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " | 
| Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3230 | "status=%s(%d)", | 
|  | 3231 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), | 
|  | 3232 | status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3233 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
|  | 3234 | } | 
|  | 3235 | return; | 
|  | 3236 | } | 
|  | 3237 |  | 
|  | 3238 | // Re-enqueue the event on the wait queue. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3239 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), | 
|  | 3240 | connection->outboundQueue.end(), | 
|  | 3241 | dispatchEntry)); | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3242 | traceOutboundQueueLength(*connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3243 | connection->waitQueue.push_back(dispatchEntry); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3244 | if (connection->responsive) { | 
|  | 3245 | mAnrTracker.insert(dispatchEntry->timeoutTime, | 
|  | 3246 | connection->inputChannel->getConnectionToken()); | 
|  | 3247 | } | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3248 | traceWaitQueueLength(*connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3249 | } | 
|  | 3250 | } | 
|  | 3251 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3252 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { | 
|  | 3253 | size_t size; | 
|  | 3254 | switch (event.type) { | 
|  | 3255 | case VerifiedInputEvent::Type::KEY: { | 
|  | 3256 | size = sizeof(VerifiedKeyEvent); | 
|  | 3257 | break; | 
|  | 3258 | } | 
|  | 3259 | case VerifiedInputEvent::Type::MOTION: { | 
|  | 3260 | size = sizeof(VerifiedMotionEvent); | 
|  | 3261 | break; | 
|  | 3262 | } | 
|  | 3263 | } | 
|  | 3264 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); | 
|  | 3265 | return mHmacKeyManager.sign(start, size); | 
|  | 3266 | } | 
|  | 3267 |  | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3268 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
|  | 3269 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { | 
|  | 3270 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; | 
|  | 3271 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { | 
|  | 3272 | // Only sign events up and down events as the purely move events | 
|  | 3273 | // are tied to their up/down counterparts so signing would be redundant. | 
|  | 3274 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); | 
|  | 3275 | verifiedEvent.actionMasked = actionMasked; | 
|  | 3276 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3277 | return sign(verifiedEvent); | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3278 | } | 
|  | 3279 | return INVALID_HMAC; | 
|  | 3280 | } | 
|  | 3281 |  | 
|  | 3282 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
|  | 3283 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { | 
|  | 3284 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); | 
|  | 3285 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; | 
|  | 3286 | verifiedEvent.action = dispatchEntry.resolvedAction; | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3287 | return sign(verifiedEvent); | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3288 | } | 
|  | 3289 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3290 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3291 | const sp<Connection>& connection, uint32_t seq, | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3292 | bool handled, nsecs_t consumeTime) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3293 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 3294 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", | 
|  | 3295 | connection->getInputChannelName().c_str(), seq, toString(handled)); | 
|  | 3296 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3297 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3298 | if (connection->status == Connection::STATUS_BROKEN || | 
|  | 3299 | connection->status == Connection::STATUS_ZOMBIE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3300 | return; | 
|  | 3301 | } | 
|  | 3302 |  | 
|  | 3303 | // Notify other system components and prepare to start the next dispatch cycle. | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3304 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { | 
|  | 3305 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); | 
|  | 3306 | }; | 
|  | 3307 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3308 | } | 
|  | 3309 |  | 
|  | 3310 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3311 | const sp<Connection>& connection, | 
|  | 3312 | bool notify) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3313 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 3314 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", | 
|  | 3315 | connection->getInputChannelName().c_str(), toString(notify)); | 
|  | 3316 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3317 |  | 
|  | 3318 | // Clear the dispatch queues. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3319 | drainDispatchQueue(connection->outboundQueue); | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3320 | traceOutboundQueueLength(*connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3321 | drainDispatchQueue(connection->waitQueue); | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3322 | traceWaitQueueLength(*connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3323 |  | 
|  | 3324 | // The connection appears to be unrecoverably broken. | 
|  | 3325 | // Ignore already broken or zombie connections. | 
|  | 3326 | if (connection->status == Connection::STATUS_NORMAL) { | 
|  | 3327 | connection->status = Connection::STATUS_BROKEN; | 
|  | 3328 |  | 
|  | 3329 | if (notify) { | 
|  | 3330 | // Notify other system components. | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3331 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", | 
|  | 3332 | connection->getInputChannelName().c_str()); | 
|  | 3333 |  | 
|  | 3334 | auto command = [this, connection]() REQUIRES(mLock) { | 
|  | 3335 | if (connection->status == Connection::STATUS_ZOMBIE) return; | 
|  | 3336 | scoped_unlock unlock(mLock); | 
|  | 3337 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); | 
|  | 3338 | }; | 
|  | 3339 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3340 | } | 
|  | 3341 | } | 
|  | 3342 | } | 
|  | 3343 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3344 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { | 
|  | 3345 | while (!queue.empty()) { | 
|  | 3346 | DispatchEntry* dispatchEntry = queue.front(); | 
|  | 3347 | queue.pop_front(); | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3348 | releaseDispatchEntry(dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3349 | } | 
|  | 3350 | } | 
|  | 3351 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3352 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3353 | if (dispatchEntry->hasForegroundTarget()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3354 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3355 | } | 
|  | 3356 | delete dispatchEntry; | 
|  | 3357 | } | 
|  | 3358 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3359 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { | 
|  | 3360 | std::scoped_lock _l(mLock); | 
|  | 3361 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
|  | 3362 | if (connection == nullptr) { | 
|  | 3363 | ALOGW("Received looper callback for unknown input channel token %p.  events=0x%x", | 
|  | 3364 | connectionToken.get(), events); | 
|  | 3365 | return 0; // remove the callback | 
|  | 3366 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3367 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3368 | bool notify; | 
|  | 3369 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { | 
|  | 3370 | if (!(events & ALOOPER_EVENT_INPUT)) { | 
|  | 3371 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event.  " | 
|  | 3372 | "events=0x%x", | 
|  | 3373 | connection->getInputChannelName().c_str(), events); | 
|  | 3374 | return 1; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3375 | } | 
|  | 3376 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3377 | nsecs_t currentTime = now(); | 
|  | 3378 | bool gotOne = false; | 
|  | 3379 | status_t status = OK; | 
|  | 3380 | for (;;) { | 
|  | 3381 | Result<InputPublisher::ConsumerResponse> result = | 
|  | 3382 | connection->inputPublisher.receiveConsumerResponse(); | 
|  | 3383 | if (!result.ok()) { | 
|  | 3384 | status = result.error().code(); | 
|  | 3385 | break; | 
|  | 3386 | } | 
|  | 3387 |  | 
|  | 3388 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { | 
|  | 3389 | const InputPublisher::Finished& finish = | 
|  | 3390 | std::get<InputPublisher::Finished>(*result); | 
|  | 3391 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, | 
|  | 3392 | finish.consumeTime); | 
|  | 3393 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3394 | if (shouldReportMetricsForConnection(*connection)) { | 
|  | 3395 | const InputPublisher::Timeline& timeline = | 
|  | 3396 | std::get<InputPublisher::Timeline>(*result); | 
|  | 3397 | mLatencyTracker | 
|  | 3398 | .trackGraphicsLatency(timeline.inputEventId, | 
|  | 3399 | connection->inputChannel->getConnectionToken(), | 
|  | 3400 | std::move(timeline.graphicsTimeline)); | 
|  | 3401 | } | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3402 | } | 
|  | 3403 | gotOne = true; | 
|  | 3404 | } | 
|  | 3405 | if (gotOne) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3406 | runCommandsLockedInterruptable(); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3407 | if (status == WOULD_BLOCK) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3408 | return 1; | 
|  | 3409 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3410 | } | 
|  | 3411 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3412 | notify = status != DEAD_OBJECT || !connection->monitor; | 
|  | 3413 | if (notify) { | 
|  | 3414 | ALOGE("channel '%s' ~ Failed to receive finished signal.  status=%s(%d)", | 
|  | 3415 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), | 
|  | 3416 | status); | 
|  | 3417 | } | 
|  | 3418 | } else { | 
|  | 3419 | // Monitor channels are never explicitly unregistered. | 
|  | 3420 | // We do it automatically when the remote endpoint is closed so don't warn about them. | 
|  | 3421 | const bool stillHaveWindowHandle = | 
|  | 3422 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; | 
|  | 3423 | notify = !connection->monitor && stillHaveWindowHandle; | 
|  | 3424 | if (notify) { | 
|  | 3425 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred.  events=0x%x", | 
|  | 3426 | connection->getInputChannelName().c_str(), events); | 
|  | 3427 | } | 
|  | 3428 | } | 
|  | 3429 |  | 
|  | 3430 | // Remove the channel. | 
|  | 3431 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); | 
|  | 3432 | return 0; // remove the callback | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3433 | } | 
|  | 3434 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3435 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3436 | const CancelationOptions& options) { | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3437 | for (const auto& [token, connection] : mConnectionsByToken) { | 
| Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3438 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3439 | } | 
|  | 3440 | } | 
|  | 3441 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3442 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3443 | const CancelationOptions& options) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3444 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); | 
|  | 3445 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); | 
|  | 3446 | } | 
|  | 3447 |  | 
|  | 3448 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
|  | 3449 | const CancelationOptions& options, | 
|  | 3450 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
|  | 3451 | for (const auto& it : monitorsByDisplay) { | 
|  | 3452 | const std::vector<Monitor>& monitors = it.second; | 
|  | 3453 | for (const Monitor& monitor : monitors) { | 
|  | 3454 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3455 | } | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3456 | } | 
|  | 3457 | } | 
|  | 3458 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3459 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3460 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3461 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3462 | if (connection == nullptr) { | 
|  | 3463 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3464 | } | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3465 |  | 
|  | 3466 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3467 | } | 
|  | 3468 |  | 
|  | 3469 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( | 
|  | 3470 | const sp<Connection>& connection, const CancelationOptions& options) { | 
|  | 3471 | if (connection->status == Connection::STATUS_BROKEN) { | 
|  | 3472 | return; | 
|  | 3473 | } | 
|  | 3474 |  | 
|  | 3475 | nsecs_t currentTime = now(); | 
|  | 3476 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3477 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = | 
| Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3478 | connection->inputState.synthesizeCancelationEvents(currentTime, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3479 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3480 | if (cancelationEvents.empty()) { | 
|  | 3481 | return; | 
|  | 3482 | } | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3483 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 3484 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " | 
|  | 3485 | "with reality: %s, mode=%d.", | 
|  | 3486 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, | 
|  | 3487 | options.mode); | 
|  | 3488 | } | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3489 |  | 
|  | 3490 | InputTarget target; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3491 | sp<WindowInfoHandle> windowHandle = | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3492 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
|  | 3493 | if (windowHandle != nullptr) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3494 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3495 | target.setDefaultPointerTransform(windowInfo->transform); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3496 | target.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 3497 | } | 
|  | 3498 | target.inputChannel = connection->inputChannel; | 
|  | 3499 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 3500 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3501 | for (size_t i = 0; i < cancelationEvents.size(); i++) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3502 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3503 | switch (cancelationEventEntry->type) { | 
|  | 3504 | case EventEntry::Type::KEY: { | 
|  | 3505 | logOutboundKeyDetails("cancel - ", | 
|  | 3506 | static_cast<const KeyEntry&>(*cancelationEventEntry)); | 
|  | 3507 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3508 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3509 | case EventEntry::Type::MOTION: { | 
|  | 3510 | logOutboundMotionDetails("cancel - ", | 
|  | 3511 | static_cast<const MotionEntry&>(*cancelationEventEntry)); | 
|  | 3512 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3513 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3514 | case EventEntry::Type::FOCUS: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3515 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 3516 | case EventEntry::Type::DRAG: { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3517 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3518 | NamedEnum::string(cancelationEventEntry->type).c_str()); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3519 | break; | 
|  | 3520 | } | 
|  | 3521 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3522 | case EventEntry::Type::DEVICE_RESET: | 
|  | 3523 | case EventEntry::Type::SENSOR: { | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3524 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3525 | NamedEnum::string(cancelationEventEntry->type).c_str()); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3526 | break; | 
|  | 3527 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3528 | } | 
|  | 3529 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3530 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, | 
|  | 3531 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3532 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3533 |  | 
|  | 3534 | startDispatchCycleLocked(currentTime, connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3535 | } | 
|  | 3536 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3537 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( | 
|  | 3538 | const sp<Connection>& connection) { | 
|  | 3539 | if (connection->status == Connection::STATUS_BROKEN) { | 
|  | 3540 | return; | 
|  | 3541 | } | 
|  | 3542 |  | 
|  | 3543 | nsecs_t currentTime = now(); | 
|  | 3544 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3545 | std::vector<std::unique_ptr<EventEntry>> downEvents = | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3546 | connection->inputState.synthesizePointerDownEvents(currentTime); | 
|  | 3547 |  | 
|  | 3548 | if (downEvents.empty()) { | 
|  | 3549 | return; | 
|  | 3550 | } | 
|  | 3551 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3552 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3553 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", | 
|  | 3554 | connection->getInputChannelName().c_str(), downEvents.size()); | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3555 | } | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3556 |  | 
|  | 3557 | InputTarget target; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3558 | sp<WindowInfoHandle> windowHandle = | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3559 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
|  | 3560 | if (windowHandle != nullptr) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3561 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3562 | target.setDefaultPointerTransform(windowInfo->transform); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3563 | target.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 3564 | } | 
|  | 3565 | target.inputChannel = connection->inputChannel; | 
|  | 3566 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 3567 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3568 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3569 | switch (downEventEntry->type) { | 
|  | 3570 | case EventEntry::Type::MOTION: { | 
|  | 3571 | logOutboundMotionDetails("down - ", | 
|  | 3572 | static_cast<const MotionEntry&>(*downEventEntry)); | 
|  | 3573 | break; | 
|  | 3574 | } | 
|  | 3575 |  | 
|  | 3576 | case EventEntry::Type::KEY: | 
|  | 3577 | case EventEntry::Type::FOCUS: | 
|  | 3578 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3579 | case EventEntry::Type::DEVICE_RESET: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3580 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3581 | case EventEntry::Type::SENSOR: | 
|  | 3582 | case EventEntry::Type::DRAG: { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3583 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3584 | NamedEnum::string(downEventEntry->type).c_str()); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3585 | break; | 
|  | 3586 | } | 
|  | 3587 | } | 
|  | 3588 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3589 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, | 
|  | 3590 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3591 | } | 
|  | 3592 |  | 
|  | 3593 | startDispatchCycleLocked(currentTime, connection); | 
|  | 3594 | } | 
|  | 3595 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3596 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( | 
|  | 3597 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3598 | ALOG_ASSERT(pointerIds.value != 0); | 
|  | 3599 |  | 
|  | 3600 | uint32_t splitPointerIndexMap[MAX_POINTERS]; | 
|  | 3601 | PointerProperties splitPointerProperties[MAX_POINTERS]; | 
|  | 3602 | PointerCoords splitPointerCoords[MAX_POINTERS]; | 
|  | 3603 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3604 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3605 | uint32_t splitPointerCount = 0; | 
|  | 3606 |  | 
|  | 3607 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3608 | originalPointerIndex++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3609 | const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3610 | originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3611 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 3612 | if (pointerIds.hasBit(pointerId)) { | 
|  | 3613 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; | 
|  | 3614 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); | 
|  | 3615 | splitPointerCoords[splitPointerCount].copyFrom( | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3616 | originalMotionEntry.pointerCoords[originalPointerIndex]); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3617 | splitPointerCount += 1; | 
|  | 3618 | } | 
|  | 3619 | } | 
|  | 3620 |  | 
|  | 3621 | if (splitPointerCount != pointerIds.count()) { | 
|  | 3622 | // This is bad.  We are missing some of the pointers that we expected to deliver. | 
|  | 3623 | // Most likely this indicates that we received an ACTION_MOVE events that has | 
|  | 3624 | // different pointer ids than we expected based on the previous ACTION_DOWN | 
|  | 3625 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers | 
|  | 3626 | // in this way. | 
|  | 3627 | ALOGW("Dropping split motion event because the pointer count is %d but " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3628 | "we expected there to be %d pointers.  This probably means we received " | 
|  | 3629 | "a broken sequence of pointer ids from the input device.", | 
|  | 3630 | splitPointerCount, pointerIds.count()); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3631 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3632 | } | 
|  | 3633 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3634 | int32_t action = originalMotionEntry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3635 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3636 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || | 
|  | 3637 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3638 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 3639 | const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3640 | originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3641 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 3642 | if (pointerIds.hasBit(pointerId)) { | 
|  | 3643 | if (pointerIds.count() == 1) { | 
|  | 3644 | // The first/last pointer went down/up. | 
|  | 3645 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3646 | ? AMOTION_EVENT_ACTION_DOWN | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3647 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 | 
|  | 3648 | ? AMOTION_EVENT_ACTION_CANCEL | 
|  | 3649 | : AMOTION_EVENT_ACTION_UP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3650 | } else { | 
|  | 3651 | // A secondary pointer went down/up. | 
|  | 3652 | uint32_t splitPointerIndex = 0; | 
|  | 3653 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { | 
|  | 3654 | splitPointerIndex += 1; | 
|  | 3655 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3656 | action = maskedAction | | 
|  | 3657 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3658 | } | 
|  | 3659 | } else { | 
|  | 3660 | // An unrelated pointer changed. | 
|  | 3661 | action = AMOTION_EVENT_ACTION_MOVE; | 
|  | 3662 | } | 
|  | 3663 | } | 
|  | 3664 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3665 | int32_t newId = mIdGenerator.nextId(); | 
|  | 3666 | if (ATRACE_ENABLED()) { | 
|  | 3667 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 | 
|  | 3668 | ") to MotionEvent(id=0x%" PRIx32 ").", | 
|  | 3669 | originalMotionEntry.id, newId); | 
|  | 3670 | ATRACE_NAME(message.c_str()); | 
|  | 3671 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3672 | std::unique_ptr<MotionEntry> splitMotionEntry = | 
|  | 3673 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, | 
|  | 3674 | originalMotionEntry.deviceId, originalMotionEntry.source, | 
|  | 3675 | originalMotionEntry.displayId, | 
|  | 3676 | originalMotionEntry.policyFlags, action, | 
|  | 3677 | originalMotionEntry.actionButton, | 
|  | 3678 | originalMotionEntry.flags, originalMotionEntry.metaState, | 
|  | 3679 | originalMotionEntry.buttonState, | 
|  | 3680 | originalMotionEntry.classification, | 
|  | 3681 | originalMotionEntry.edgeFlags, | 
|  | 3682 | originalMotionEntry.xPrecision, | 
|  | 3683 | originalMotionEntry.yPrecision, | 
|  | 3684 | originalMotionEntry.xCursorPosition, | 
|  | 3685 | originalMotionEntry.yCursorPosition, | 
|  | 3686 | originalMotionEntry.downTime, splitPointerCount, | 
|  | 3687 | splitPointerProperties, splitPointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3688 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3689 | if (originalMotionEntry.injectionState) { | 
|  | 3690 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3691 | splitMotionEntry->injectionState->refCount += 1; | 
|  | 3692 | } | 
|  | 3693 |  | 
|  | 3694 | return splitMotionEntry; | 
|  | 3695 | } | 
|  | 3696 |  | 
|  | 3697 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3698 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 3699 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); | 
|  | 3700 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3701 |  | 
|  | 3702 | bool needWake; | 
|  | 3703 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3704 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3705 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3706 | std::unique_ptr<ConfigurationChangedEntry> newEntry = | 
|  | 3707 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); | 
|  | 3708 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3709 | } // release lock | 
|  | 3710 |  | 
|  | 3711 | if (needWake) { | 
|  | 3712 | mLooper->wake(); | 
|  | 3713 | } | 
|  | 3714 | } | 
|  | 3715 |  | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3716 | /** | 
|  | 3717 | * If one of the meta shortcuts is detected, process them here: | 
|  | 3718 | *     Meta + Backspace -> generate BACK | 
|  | 3719 | *     Meta + Enter -> generate HOME | 
|  | 3720 | * This will potentially overwrite keyCode and metaState. | 
|  | 3721 | */ | 
|  | 3722 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3723 | int32_t& keyCode, int32_t& metaState) { | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3724 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { | 
|  | 3725 | int32_t newKeyCode = AKEYCODE_UNKNOWN; | 
|  | 3726 | if (keyCode == AKEYCODE_DEL) { | 
|  | 3727 | newKeyCode = AKEYCODE_BACK; | 
|  | 3728 | } else if (keyCode == AKEYCODE_ENTER) { | 
|  | 3729 | newKeyCode = AKEYCODE_HOME; | 
|  | 3730 | } | 
|  | 3731 | if (newKeyCode != AKEYCODE_UNKNOWN) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3732 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3733 | struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3734 | mReplacedKeys[replacement] = newKeyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3735 | keyCode = newKeyCode; | 
|  | 3736 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
|  | 3737 | } | 
|  | 3738 | } else if (action == AKEY_EVENT_ACTION_UP) { | 
|  | 3739 | // In order to maintain a consistent stream of up and down events, check to see if the key | 
|  | 3740 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, | 
|  | 3741 | // 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] | 3742 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3743 | struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3744 | auto replacementIt = mReplacedKeys.find(replacement); | 
|  | 3745 | if (replacementIt != mReplacedKeys.end()) { | 
|  | 3746 | keyCode = replacementIt->second; | 
|  | 3747 | mReplacedKeys.erase(replacementIt); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3748 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
|  | 3749 | } | 
|  | 3750 | } | 
|  | 3751 | } | 
|  | 3752 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3753 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3754 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 3755 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
|  | 3756 | "policyFlags=0x%x, action=0x%x, " | 
|  | 3757 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, | 
|  | 3758 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, | 
|  | 3759 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, | 
|  | 3760 | args->downTime); | 
|  | 3761 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3762 | if (!validateKeyEvent(args->action)) { | 
|  | 3763 | return; | 
|  | 3764 | } | 
|  | 3765 |  | 
|  | 3766 | uint32_t policyFlags = args->policyFlags; | 
|  | 3767 | int32_t flags = args->flags; | 
|  | 3768 | int32_t metaState = args->metaState; | 
| Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3769 | // InputDispatcher tracks and generates key repeats on behalf of | 
|  | 3770 | // whatever notifies it, so repeatCount should always be set to 0 | 
|  | 3771 | constexpr int32_t repeatCount = 0; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3772 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { | 
|  | 3773 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
|  | 3774 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; | 
|  | 3775 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3776 | if (policyFlags & POLICY_FLAG_FUNCTION) { | 
|  | 3777 | metaState |= AMETA_FUNCTION_ON; | 
|  | 3778 | } | 
|  | 3779 |  | 
|  | 3780 | policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 3781 |  | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3782 | int32_t keyCode = args->keyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3783 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3784 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3785 | KeyEvent event; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3786 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3787 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, | 
|  | 3788 | args->downTime, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3789 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3790 | android::base::Timer t; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3791 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3792 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3793 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3794 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3795 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3796 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3797 | bool needWake; | 
|  | 3798 | { // acquire lock | 
|  | 3799 | mLock.lock(); | 
|  | 3800 |  | 
|  | 3801 | if (shouldSendKeyToInputFilterLocked(args)) { | 
|  | 3802 | mLock.unlock(); | 
|  | 3803 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3804 | policyFlags |= POLICY_FLAG_FILTERED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3805 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
|  | 3806 | return; // event was consumed by the filter | 
|  | 3807 | } | 
|  | 3808 |  | 
|  | 3809 | mLock.lock(); | 
|  | 3810 | } | 
|  | 3811 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3812 | std::unique_ptr<KeyEntry> newEntry = | 
|  | 3813 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, | 
|  | 3814 | args->displayId, policyFlags, args->action, flags, | 
|  | 3815 | keyCode, args->scanCode, metaState, repeatCount, | 
|  | 3816 | args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3817 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3818 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3819 | mLock.unlock(); | 
|  | 3820 | } // release lock | 
|  | 3821 |  | 
|  | 3822 | if (needWake) { | 
|  | 3823 | mLooper->wake(); | 
|  | 3824 | } | 
|  | 3825 | } | 
|  | 3826 |  | 
|  | 3827 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { | 
|  | 3828 | return mInputFilterEnabled; | 
|  | 3829 | } | 
|  | 3830 |  | 
|  | 3831 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3832 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 3833 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " | 
|  | 3834 | "displayId=%" PRId32 ", policyFlags=0x%x, " | 
|  | 3835 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " | 
|  | 3836 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " | 
|  | 3837 | "yCursorPosition=%f, downTime=%" PRId64, | 
|  | 3838 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, | 
|  | 3839 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, | 
|  | 3840 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, | 
|  | 3841 | args->xCursorPosition, args->yCursorPosition, args->downTime); | 
|  | 3842 | for (uint32_t i = 0; i < args->pointerCount; i++) { | 
|  | 3843 | ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
|  | 3844 | "x=%f, y=%f, pressure=%f, size=%f, " | 
|  | 3845 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
|  | 3846 | "orientation=%f", | 
|  | 3847 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, | 
|  | 3848 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 3849 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 3850 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 3851 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
|  | 3852 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 3853 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 3854 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 3855 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 3856 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
|  | 3857 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3858 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3859 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, | 
|  | 3860 | args->pointerProperties)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3861 | return; | 
|  | 3862 | } | 
|  | 3863 |  | 
|  | 3864 | uint32_t policyFlags = args->policyFlags; | 
|  | 3865 | policyFlags |= POLICY_FLAG_TRUSTED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3866 |  | 
|  | 3867 | android::base::Timer t; | 
| Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3868 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3869 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3870 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3871 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3872 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3873 |  | 
|  | 3874 | bool needWake; | 
|  | 3875 | { // acquire lock | 
|  | 3876 | mLock.lock(); | 
|  | 3877 |  | 
|  | 3878 | if (shouldSendMotionToInputFilterLocked(args)) { | 
|  | 3879 | mLock.unlock(); | 
|  | 3880 |  | 
|  | 3881 | MotionEvent event; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3882 | ui::Transform transform; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3883 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
|  | 3884 | args->action, args->actionButton, args->flags, args->edgeFlags, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3885 | args->metaState, args->buttonState, args->classification, transform, | 
|  | 3886 | args->xPrecision, args->yPrecision, args->xCursorPosition, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 3887 | args->yCursorPosition, ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, | 
|  | 3888 | INVALID_DISPLAY_SIZE, args->downTime, args->eventTime, | 
|  | 3889 | args->pointerCount, args->pointerProperties, args->pointerCoords); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3890 |  | 
|  | 3891 | policyFlags |= POLICY_FLAG_FILTERED; | 
|  | 3892 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
|  | 3893 | return; // event was consumed by the filter | 
|  | 3894 | } | 
|  | 3895 |  | 
|  | 3896 | mLock.lock(); | 
|  | 3897 | } | 
|  | 3898 |  | 
|  | 3899 | // Just enqueue a new motion event. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3900 | std::unique_ptr<MotionEntry> newEntry = | 
|  | 3901 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, | 
|  | 3902 | args->source, args->displayId, policyFlags, | 
|  | 3903 | args->action, args->actionButton, args->flags, | 
|  | 3904 | args->metaState, args->buttonState, | 
|  | 3905 | args->classification, args->edgeFlags, | 
|  | 3906 | args->xPrecision, args->yPrecision, | 
|  | 3907 | args->xCursorPosition, args->yCursorPosition, | 
|  | 3908 | args->downTime, args->pointerCount, | 
|  | 3909 | args->pointerProperties, args->pointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3910 |  | 
| Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 3911 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && | 
|  | 3912 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && | 
|  | 3913 | !mInputFilterEnabled) { | 
|  | 3914 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; | 
|  | 3915 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); | 
|  | 3916 | } | 
|  | 3917 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3918 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3919 | mLock.unlock(); | 
|  | 3920 | } // release lock | 
|  | 3921 |  | 
|  | 3922 | if (needWake) { | 
|  | 3923 | mLooper->wake(); | 
|  | 3924 | } | 
|  | 3925 | } | 
|  | 3926 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3927 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3928 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 3929 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " | 
|  | 3930 | " sensorType=%s", | 
|  | 3931 | args->id, args->eventTime, args->deviceId, args->source, | 
|  | 3932 | NamedEnum::string(args->sensorType).c_str()); | 
|  | 3933 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3934 |  | 
|  | 3935 | bool needWake; | 
|  | 3936 | { // acquire lock | 
|  | 3937 | mLock.lock(); | 
|  | 3938 |  | 
|  | 3939 | // Just enqueue a new sensor event. | 
|  | 3940 | std::unique_ptr<SensorEntry> newEntry = | 
|  | 3941 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, | 
|  | 3942 | args->source, 0 /* policyFlags*/, args->hwTimestamp, | 
|  | 3943 | args->sensorType, args->accuracy, | 
|  | 3944 | args->accuracyChanged, args->values); | 
|  | 3945 |  | 
|  | 3946 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
|  | 3947 | mLock.unlock(); | 
|  | 3948 | } // release lock | 
|  | 3949 |  | 
|  | 3950 | if (needWake) { | 
|  | 3951 | mLooper->wake(); | 
|  | 3952 | } | 
|  | 3953 | } | 
|  | 3954 |  | 
| Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 3955 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3956 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 3957 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d,  isOn=%d", args->eventTime, | 
|  | 3958 | args->deviceId, args->isOn); | 
|  | 3959 | } | 
| Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 3960 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); | 
|  | 3961 | } | 
|  | 3962 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3963 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3964 | return mInputFilterEnabled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3965 | } | 
|  | 3966 |  | 
|  | 3967 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3968 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 3969 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " | 
|  | 3970 | "switchMask=0x%08x", | 
|  | 3971 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); | 
|  | 3972 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3973 |  | 
|  | 3974 | uint32_t policyFlags = args->policyFlags; | 
|  | 3975 | policyFlags |= POLICY_FLAG_TRUSTED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3976 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3977 | } | 
|  | 3978 |  | 
|  | 3979 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3980 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 3981 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, | 
|  | 3982 | args->deviceId); | 
|  | 3983 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3984 |  | 
|  | 3985 | bool needWake; | 
|  | 3986 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3987 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3988 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3989 | std::unique_ptr<DeviceResetEntry> newEntry = | 
|  | 3990 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); | 
|  | 3991 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3992 | } // release lock | 
|  | 3993 |  | 
|  | 3994 | if (needWake) { | 
|  | 3995 | mLooper->wake(); | 
|  | 3996 | } | 
|  | 3997 | } | 
|  | 3998 |  | 
| Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3999 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4000 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 4001 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, | 
|  | 4002 | args->enabled ? "true" : "false"); | 
|  | 4003 | } | 
| Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4004 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4005 | bool needWake; | 
|  | 4006 | { // acquire lock | 
|  | 4007 | std::scoped_lock _l(mLock); | 
|  | 4008 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, | 
|  | 4009 | args->enabled); | 
|  | 4010 | needWake = enqueueInboundEventLocked(std::move(entry)); | 
|  | 4011 | } // release lock | 
|  | 4012 |  | 
|  | 4013 | if (needWake) { | 
|  | 4014 | mLooper->wake(); | 
|  | 4015 | } | 
| Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4016 | } | 
|  | 4017 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4018 | InputEventInjectionResult InputDispatcher::injectInputEvent( | 
|  | 4019 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, | 
|  | 4020 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4021 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 4022 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " | 
|  | 4023 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", | 
|  | 4024 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); | 
|  | 4025 | } | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4026 | 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] | 4027 |  | 
|  | 4028 | policyFlags |= POLICY_FLAG_INJECTED; | 
|  | 4029 | if (hasInjectionPermission(injectorPid, injectorUid)) { | 
|  | 4030 | policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 4031 | } | 
|  | 4032 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4033 | // 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] | 4034 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign | 
|  | 4035 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes | 
|  | 4036 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. | 
|  | 4037 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them | 
|  | 4038 | // from events that originate from actual hardware. | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4039 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4040 | if (policyFlags & POLICY_FLAG_FILTERED) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4041 | resolvedDeviceId = event->getDeviceId(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4042 | } | 
|  | 4043 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4044 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4045 | switch (event->getType()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4046 | case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4047 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); | 
|  | 4048 | int32_t action = incomingKey.getAction(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4049 | if (!validateKeyEvent(action)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4050 | return InputEventInjectionResult::FAILED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4051 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4052 |  | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4053 | int32_t flags = incomingKey.getFlags(); | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4054 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { | 
|  | 4055 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; | 
|  | 4056 | } | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4057 | int32_t keyCode = incomingKey.getKeyCode(); | 
|  | 4058 | int32_t metaState = incomingKey.getMetaState(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4059 | accelerateMetaShortcuts(resolvedDeviceId, action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4060 | /*byref*/ keyCode, /*byref*/ metaState); | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4061 | KeyEvent keyEvent; | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4062 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4063 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, | 
|  | 4064 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), | 
|  | 4065 | incomingKey.getDownTime(), incomingKey.getEventTime()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4066 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4067 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { | 
|  | 4068 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4069 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4070 |  | 
|  | 4071 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
|  | 4072 | android::base::Timer t; | 
|  | 4073 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); | 
|  | 4074 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 4075 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
|  | 4076 | std::to_string(t.duration().count()).c_str()); | 
|  | 4077 | } | 
|  | 4078 | } | 
|  | 4079 |  | 
|  | 4080 | mLock.lock(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4081 | std::unique_ptr<KeyEntry> injectedEntry = | 
|  | 4082 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4083 | resolvedDeviceId, incomingKey.getSource(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4084 | incomingKey.getDisplayId(), policyFlags, action, | 
|  | 4085 | flags, keyCode, incomingKey.getScanCode(), metaState, | 
|  | 4086 | incomingKey.getRepeatCount(), | 
|  | 4087 | incomingKey.getDownTime()); | 
|  | 4088 | injectedEntries.push(std::move(injectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4089 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4090 | } | 
|  | 4091 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4092 | case AINPUT_EVENT_TYPE_MOTION: { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4093 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
|  | 4094 | int32_t action = motionEvent.getAction(); | 
|  | 4095 | size_t pointerCount = motionEvent.getPointerCount(); | 
|  | 4096 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); | 
|  | 4097 | int32_t actionButton = motionEvent.getActionButton(); | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4098 | int32_t flags = motionEvent.getFlags(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4099 | int32_t displayId = motionEvent.getDisplayId(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4100 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4101 | return InputEventInjectionResult::FAILED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4102 | } | 
|  | 4103 |  | 
|  | 4104 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4105 | nsecs_t eventTime = motionEvent.getEventTime(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4106 | android::base::Timer t; | 
|  | 4107 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); | 
|  | 4108 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 4109 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
|  | 4110 | std::to_string(t.duration().count()).c_str()); | 
|  | 4111 | } | 
|  | 4112 | } | 
|  | 4113 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4114 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { | 
|  | 4115 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; | 
|  | 4116 | } | 
|  | 4117 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4118 | mLock.lock(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4119 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); | 
|  | 4120 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4121 | std::unique_ptr<MotionEntry> injectedEntry = | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4122 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, | 
|  | 4123 | resolvedDeviceId, motionEvent.getSource(), | 
|  | 4124 | motionEvent.getDisplayId(), policyFlags, action, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4125 | actionButton, flags, motionEvent.getMetaState(), | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4126 | motionEvent.getButtonState(), | 
|  | 4127 | motionEvent.getClassification(), | 
|  | 4128 | motionEvent.getEdgeFlags(), | 
|  | 4129 | motionEvent.getXPrecision(), | 
|  | 4130 | motionEvent.getYPrecision(), | 
|  | 4131 | motionEvent.getRawXCursorPosition(), | 
|  | 4132 | motionEvent.getRawYCursorPosition(), | 
|  | 4133 | motionEvent.getDownTime(), uint32_t(pointerCount), | 
|  | 4134 | pointerProperties, samplePointerCoords, | 
|  | 4135 | motionEvent.getXOffset(), | 
|  | 4136 | motionEvent.getYOffset()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4137 | injectedEntries.push(std::move(injectedEntry)); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4138 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4139 | sampleEventTimes += 1; | 
|  | 4140 | samplePointerCoords += pointerCount; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4141 | std::unique_ptr<MotionEntry> nextInjectedEntry = | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4142 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, | 
|  | 4143 | resolvedDeviceId, motionEvent.getSource(), | 
|  | 4144 | motionEvent.getDisplayId(), policyFlags, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4145 | action, actionButton, flags, | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4146 | motionEvent.getMetaState(), | 
|  | 4147 | motionEvent.getButtonState(), | 
|  | 4148 | motionEvent.getClassification(), | 
|  | 4149 | motionEvent.getEdgeFlags(), | 
|  | 4150 | motionEvent.getXPrecision(), | 
|  | 4151 | motionEvent.getYPrecision(), | 
|  | 4152 | motionEvent.getRawXCursorPosition(), | 
|  | 4153 | motionEvent.getRawYCursorPosition(), | 
|  | 4154 | motionEvent.getDownTime(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4155 | uint32_t(pointerCount), pointerProperties, | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4156 | samplePointerCoords, motionEvent.getXOffset(), | 
|  | 4157 | motionEvent.getYOffset()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4158 | injectedEntries.push(std::move(nextInjectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4159 | } | 
|  | 4160 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4161 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4162 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4163 | default: | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4164 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4165 | return InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4166 | } | 
|  | 4167 |  | 
|  | 4168 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4169 | if (syncMode == InputEventInjectionSync::NONE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4170 | injectionState->injectionIsAsync = true; | 
|  | 4171 | } | 
|  | 4172 |  | 
|  | 4173 | injectionState->refCount += 1; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4174 | injectedEntries.back()->injectionState = injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4175 |  | 
|  | 4176 | bool needWake = false; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4177 | while (!injectedEntries.empty()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4178 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4179 | injectedEntries.pop(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4180 | } | 
|  | 4181 |  | 
|  | 4182 | mLock.unlock(); | 
|  | 4183 |  | 
|  | 4184 | if (needWake) { | 
|  | 4185 | mLooper->wake(); | 
|  | 4186 | } | 
|  | 4187 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4188 | InputEventInjectionResult injectionResult; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4189 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4190 | std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4191 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4192 | if (syncMode == InputEventInjectionSync::NONE) { | 
|  | 4193 | injectionResult = InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4194 | } else { | 
|  | 4195 | for (;;) { | 
|  | 4196 | injectionResult = injectionState->injectionResult; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4197 | if (injectionResult != InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4198 | break; | 
|  | 4199 | } | 
|  | 4200 |  | 
|  | 4201 | nsecs_t remainingTimeout = endTime - now(); | 
|  | 4202 | if (remainingTimeout <= 0) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4203 | if (DEBUG_INJECTION) { | 
|  | 4204 | ALOGD("injectInputEvent - Timed out waiting for injection result " | 
|  | 4205 | "to become available."); | 
|  | 4206 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4207 | injectionResult = InputEventInjectionResult::TIMED_OUT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4208 | break; | 
|  | 4209 | } | 
|  | 4210 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4211 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4212 | } | 
|  | 4213 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4214 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && | 
|  | 4215 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4216 | while (injectionState->pendingForegroundDispatches != 0) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4217 | if (DEBUG_INJECTION) { | 
|  | 4218 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", | 
|  | 4219 | injectionState->pendingForegroundDispatches); | 
|  | 4220 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4221 | nsecs_t remainingTimeout = endTime - now(); | 
|  | 4222 | if (remainingTimeout <= 0) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4223 | if (DEBUG_INJECTION) { | 
|  | 4224 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " | 
|  | 4225 | "dispatches to finish."); | 
|  | 4226 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4227 | injectionResult = InputEventInjectionResult::TIMED_OUT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4228 | break; | 
|  | 4229 | } | 
|  | 4230 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4231 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4232 | } | 
|  | 4233 | } | 
|  | 4234 | } | 
|  | 4235 |  | 
|  | 4236 | injectionState->release(); | 
|  | 4237 | } // release lock | 
|  | 4238 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4239 | if (DEBUG_INJECTION) { | 
|  | 4240 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", | 
|  | 4241 | injectionResult, injectorPid, injectorUid); | 
|  | 4242 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4243 |  | 
|  | 4244 | return injectionResult; | 
|  | 4245 | } | 
|  | 4246 |  | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4247 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4248 | std::array<uint8_t, 32> calculatedHmac; | 
|  | 4249 | std::unique_ptr<VerifiedInputEvent> result; | 
|  | 4250 | switch (event.getType()) { | 
|  | 4251 | case AINPUT_EVENT_TYPE_KEY: { | 
|  | 4252 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); | 
|  | 4253 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); | 
|  | 4254 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4255 | calculatedHmac = sign(verifiedKeyEvent); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4256 | break; | 
|  | 4257 | } | 
|  | 4258 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 4259 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); | 
|  | 4260 | VerifiedMotionEvent verifiedMotionEvent = | 
|  | 4261 | verifiedMotionEventFromMotionEvent(motionEvent); | 
|  | 4262 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4263 | calculatedHmac = sign(verifiedMotionEvent); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4264 | break; | 
|  | 4265 | } | 
|  | 4266 | default: { | 
|  | 4267 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); | 
|  | 4268 | return nullptr; | 
|  | 4269 | } | 
|  | 4270 | } | 
|  | 4271 | if (calculatedHmac == INVALID_HMAC) { | 
|  | 4272 | return nullptr; | 
|  | 4273 | } | 
|  | 4274 | if (calculatedHmac != event.getHmac()) { | 
|  | 4275 | return nullptr; | 
|  | 4276 | } | 
|  | 4277 | return result; | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4278 | } | 
|  | 4279 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4280 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4281 | return injectorUid == 0 || | 
|  | 4282 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4283 | } | 
|  | 4284 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4285 | void InputDispatcher::setInjectionResult(EventEntry& entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4286 | InputEventInjectionResult injectionResult) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4287 | InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4288 | if (injectionState) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4289 | if (DEBUG_INJECTION) { | 
|  | 4290 | ALOGD("Setting input event injection result to %d.  " | 
|  | 4291 | "injectorPid=%d, injectorUid=%d", | 
|  | 4292 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); | 
|  | 4293 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4294 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4295 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4296 | // Log the outcome since the injector did not wait for the injection result. | 
|  | 4297 | switch (injectionResult) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4298 | case InputEventInjectionResult::SUCCEEDED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4299 | ALOGV("Asynchronous input event injection succeeded."); | 
|  | 4300 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4301 | case InputEventInjectionResult::FAILED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4302 | ALOGW("Asynchronous input event injection failed."); | 
|  | 4303 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4304 | case InputEventInjectionResult::PERMISSION_DENIED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4305 | ALOGW("Asynchronous input event injection permission denied."); | 
|  | 4306 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4307 | case InputEventInjectionResult::TIMED_OUT: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4308 | ALOGW("Asynchronous input event injection timed out."); | 
|  | 4309 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4310 | case InputEventInjectionResult::PENDING: | 
|  | 4311 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); | 
|  | 4312 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4313 | } | 
|  | 4314 | } | 
|  | 4315 |  | 
|  | 4316 | injectionState->injectionResult = injectionResult; | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4317 | mInjectionResultAvailable.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4318 | } | 
|  | 4319 | } | 
|  | 4320 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4321 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { | 
|  | 4322 | InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4323 | if (injectionState) { | 
|  | 4324 | injectionState->pendingForegroundDispatches += 1; | 
|  | 4325 | } | 
|  | 4326 | } | 
|  | 4327 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4328 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { | 
|  | 4329 | InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4330 | if (injectionState) { | 
|  | 4331 | injectionState->pendingForegroundDispatches -= 1; | 
|  | 4332 |  | 
|  | 4333 | if (injectionState->pendingForegroundDispatches == 0) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4334 | mInjectionSyncFinished.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4335 | } | 
|  | 4336 | } | 
|  | 4337 | } | 
|  | 4338 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4339 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4340 | int32_t displayId) const { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4341 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4342 | auto it = mWindowHandlesByDisplay.find(displayId); | 
|  | 4343 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4344 | } | 
|  | 4345 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4346 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4347 | const sp<IBinder>& windowHandleToken) const { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4348 | if (windowHandleToken == nullptr) { | 
|  | 4349 | return nullptr; | 
|  | 4350 | } | 
|  | 4351 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4352 | for (auto& it : mWindowHandlesByDisplay) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4353 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; | 
|  | 4354 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4355 | if (windowHandle->getToken() == windowHandleToken) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4356 | return windowHandle; | 
|  | 4357 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4358 | } | 
|  | 4359 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4360 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4361 | } | 
|  | 4362 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4363 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, | 
|  | 4364 | int displayId) const { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4365 | if (windowHandleToken == nullptr) { | 
|  | 4366 | return nullptr; | 
|  | 4367 | } | 
|  | 4368 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4369 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4370 | if (windowHandle->getToken() == windowHandleToken) { | 
|  | 4371 | return windowHandle; | 
|  | 4372 | } | 
|  | 4373 | } | 
|  | 4374 | return nullptr; | 
|  | 4375 | } | 
|  | 4376 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4377 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( | 
|  | 4378 | const sp<WindowInfoHandle>& windowHandle) const { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4379 | for (auto& it : mWindowHandlesByDisplay) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4380 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; | 
|  | 4381 | for (const sp<WindowInfoHandle>& handle : windowHandles) { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4382 | if (handle->getId() == windowHandle->getId() && | 
|  | 4383 | handle->getToken() == windowHandle->getToken()) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4384 | if (windowHandle->getInfo()->displayId != it.first) { | 
|  | 4385 | ALOGE("Found window %s in display %" PRId32 | 
|  | 4386 | ", but it should belong to display %" PRId32, | 
|  | 4387 | windowHandle->getName().c_str(), it.first, | 
|  | 4388 | windowHandle->getInfo()->displayId); | 
|  | 4389 | } | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4390 | return handle; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4391 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4392 | } | 
|  | 4393 | } | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4394 | return nullptr; | 
|  | 4395 | } | 
|  | 4396 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4397 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4398 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); | 
|  | 4399 | return getWindowHandleLocked(focusedToken, displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4400 | } | 
|  | 4401 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4402 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4403 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); | 
|  | 4404 | const bool noInputChannel = | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4405 | windowHandle.getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4406 | if (connection != nullptr && noInputChannel) { | 
|  | 4407 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", | 
|  | 4408 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); | 
|  | 4409 | return false; | 
|  | 4410 | } | 
|  | 4411 |  | 
|  | 4412 | if (connection == nullptr) { | 
|  | 4413 | if (!noInputChannel) { | 
|  | 4414 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); | 
|  | 4415 | } | 
|  | 4416 | return false; | 
|  | 4417 | } | 
|  | 4418 | if (!connection->responsive) { | 
|  | 4419 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); | 
|  | 4420 | return false; | 
|  | 4421 | } | 
|  | 4422 | return true; | 
|  | 4423 | } | 
|  | 4424 |  | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4425 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( | 
|  | 4426 | const sp<IBinder>& token) const { | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4427 | auto connectionIt = mConnectionsByToken.find(token); | 
|  | 4428 | if (connectionIt == mConnectionsByToken.end()) { | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4429 | return nullptr; | 
|  | 4430 | } | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4431 | return connectionIt->second->inputChannel; | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4432 | } | 
|  | 4433 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4434 | void InputDispatcher::updateWindowHandlesForDisplayLocked( | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4435 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { | 
|  | 4436 | if (windowInfoHandles.empty()) { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4437 | // Remove all handles on a display if there are no windows left. | 
|  | 4438 | mWindowHandlesByDisplay.erase(displayId); | 
|  | 4439 | return; | 
|  | 4440 | } | 
|  | 4441 |  | 
|  | 4442 | // Since we compare the pointer of input window handles across window updates, we need | 
|  | 4443 | // 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] | 4444 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); | 
|  | 4445 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; | 
|  | 4446 | for (const sp<WindowInfoHandle>& handle : oldHandles) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4447 | oldHandlesById[handle->getId()] = handle; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4448 | } | 
|  | 4449 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4450 | std::vector<sp<WindowInfoHandle>> newHandles; | 
|  | 4451 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4452 | const WindowInfo* info = handle->getInfo(); | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4453 | if (getInputChannelLocked(handle->getToken()) == nullptr) { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4454 | const bool noInputChannel = | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4455 | info->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 4456 | const bool canReceiveInput = !info->flags.test(WindowInfo::Flag::NOT_TOUCHABLE) || | 
|  | 4457 | !info->flags.test(WindowInfo::Flag::NOT_FOCUSABLE); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4458 | if (canReceiveInput && !noInputChannel) { | 
| John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4459 | ALOGV("Window handle %s has no registered input channel", | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4460 | handle->getName().c_str()); | 
| Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4461 | continue; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4462 | } | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4463 | } | 
|  | 4464 |  | 
|  | 4465 | if (info->displayId != displayId) { | 
|  | 4466 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", | 
|  | 4467 | handle->getName().c_str(), displayId, info->displayId); | 
|  | 4468 | continue; | 
|  | 4469 | } | 
|  | 4470 |  | 
| Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4471 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && | 
|  | 4472 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4473 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4474 | oldHandle->updateFrom(handle); | 
|  | 4475 | newHandles.push_back(oldHandle); | 
|  | 4476 | } else { | 
|  | 4477 | newHandles.push_back(handle); | 
|  | 4478 | } | 
|  | 4479 | } | 
|  | 4480 |  | 
|  | 4481 | // Insert or replace | 
|  | 4482 | mWindowHandlesByDisplay[displayId] = newHandles; | 
|  | 4483 | } | 
|  | 4484 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4485 | void InputDispatcher::setInputWindows( | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4486 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4487 | { // acquire lock | 
|  | 4488 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4489 | for (const auto& [displayId, handles] : handlesPerDisplay) { | 
|  | 4490 | setInputWindowsLocked(handles, displayId); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4491 | } | 
|  | 4492 | } | 
|  | 4493 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4494 | mLooper->wake(); | 
|  | 4495 | } | 
|  | 4496 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4497 | /** | 
|  | 4498 | * Called from InputManagerService, update window handle list by displayId that can receive input. | 
|  | 4499 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... | 
|  | 4500 | * If set an empty list, remove all handles from the specific display. | 
|  | 4501 | * For focused handle, check if need to change and send a cancel event to previous one. | 
|  | 4502 | * For removed handle, check if need to send a cancel event if already in touch. | 
|  | 4503 | */ | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4504 | void InputDispatcher::setInputWindowsLocked( | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4505 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4506 | if (DEBUG_FOCUS) { | 
|  | 4507 | std::string windowList; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4508 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4509 | windowList += iwh->getName() + " "; | 
|  | 4510 | } | 
|  | 4511 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); | 
|  | 4512 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4513 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4514 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4515 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4516 | const bool noInputWindow = | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4517 | window->getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4518 | if (noInputWindow && window->getToken() != nullptr) { | 
|  | 4519 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", | 
|  | 4520 | window->getName().c_str()); | 
|  | 4521 | window->releaseChannel(); | 
|  | 4522 | } | 
|  | 4523 | } | 
|  | 4524 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4525 | // Copy old handles for release if they are no longer present. | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4526 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4527 |  | 
| Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4528 | // Save the old windows' orientation by ID before it gets updated. | 
|  | 4529 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4530 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { | 
| Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4531 | oldWindowOrientations.emplace(handle->getId(), | 
|  | 4532 | handle->getInfo()->transform.getOrientation()); | 
|  | 4533 | } | 
|  | 4534 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4535 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4536 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4537 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4538 | if (mLastHoverWindowHandle && | 
|  | 4539 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == | 
|  | 4540 | windowHandles.end()) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4541 | mLastHoverWindowHandle = nullptr; | 
|  | 4542 | } | 
|  | 4543 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4544 | std::optional<FocusResolver::FocusChanges> changes = | 
|  | 4545 | mFocusResolver.setInputWindows(displayId, windowHandles); | 
|  | 4546 | if (changes) { | 
|  | 4547 | onFocusChangedLocked(*changes); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4548 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4549 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4550 | std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
|  | 4551 | mTouchStatesByDisplay.find(displayId); | 
|  | 4552 | if (stateIt != mTouchStatesByDisplay.end()) { | 
|  | 4553 | TouchState& state = stateIt->second; | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4554 | for (size_t i = 0; i < state.windows.size();) { | 
|  | 4555 | TouchedWindow& touchedWindow = state.windows[i]; | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4556 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4557 | if (DEBUG_FOCUS) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4558 | ALOGD("Touched window was removed: %s in display %" PRId32, | 
|  | 4559 | touchedWindow.windowHandle->getName().c_str(), displayId); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4560 | } | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4561 | std::shared_ptr<InputChannel> touchedInputChannel = | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4562 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); | 
|  | 4563 | if (touchedInputChannel != nullptr) { | 
|  | 4564 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 4565 | "touched window was removed"); | 
|  | 4566 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4567 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4568 | state.windows.erase(state.windows.begin() + i); | 
|  | 4569 | } else { | 
|  | 4570 | ++i; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4571 | } | 
|  | 4572 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4573 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4574 | // 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] | 4575 | // could just clear the state here. | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4576 | if (mDragState && | 
|  | 4577 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4578 | windowHandles.end()) { | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4579 | mDragState.reset(); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4580 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4581 | } | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4582 |  | 
| Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4583 | if (isPerWindowInputRotationEnabled()) { | 
|  | 4584 | // Determine if the orientation of any of the input windows have changed, and cancel all | 
|  | 4585 | // pointer events if necessary. | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4586 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { | 
|  | 4587 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); | 
| Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4588 | if (newWindowHandle != nullptr && | 
|  | 4589 | newWindowHandle->getInfo()->transform.getOrientation() != | 
|  | 4590 | oldWindowOrientations[oldWindowHandle->getId()]) { | 
|  | 4591 | std::shared_ptr<InputChannel> inputChannel = | 
|  | 4592 | getInputChannelLocked(newWindowHandle->getToken()); | 
|  | 4593 | if (inputChannel != nullptr) { | 
|  | 4594 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 4595 | "touched window's orientation changed"); | 
|  | 4596 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); | 
|  | 4597 | } | 
|  | 4598 | } | 
|  | 4599 | } | 
|  | 4600 | } | 
|  | 4601 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4602 | // Release information for windows that are no longer present. | 
|  | 4603 | // This ensures that unused input channels are released promptly. | 
|  | 4604 | // Otherwise, they might stick around until the window handle is destroyed | 
|  | 4605 | // which might not happen until the next GC. | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4606 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4607 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4608 | if (DEBUG_FOCUS) { | 
|  | 4609 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4610 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4611 | oldWindowHandle->releaseChannel(); | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4612 | // To avoid making too many calls into the compat framework, only | 
|  | 4613 | // check for window flags when windows are going away. | 
|  | 4614 | // TODO(b/157929241) : delete this. This is only needed temporarily | 
|  | 4615 | // in order to gather some data about the flag usage | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4616 | if (oldWindowHandle->getInfo()->flags.test(WindowInfo::Flag::SLIPPERY)) { | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4617 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", | 
|  | 4618 | oldWindowHandle->getName().c_str()); | 
|  | 4619 | if (mCompatService != nullptr) { | 
|  | 4620 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, | 
|  | 4621 | oldWindowHandle->getInfo()->ownerUid); | 
|  | 4622 | } | 
|  | 4623 | } | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4624 | } | 
| chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4625 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4626 | } | 
|  | 4627 |  | 
|  | 4628 | void InputDispatcher::setFocusedApplication( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4629 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4630 | if (DEBUG_FOCUS) { | 
|  | 4631 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, | 
|  | 4632 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); | 
|  | 4633 | } | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4634 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4635 | std::scoped_lock _l(mLock); | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4636 | setFocusedApplicationLocked(displayId, inputApplicationHandle); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4637 | } // release lock | 
|  | 4638 |  | 
|  | 4639 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4640 | mLooper->wake(); | 
|  | 4641 | } | 
|  | 4642 |  | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4643 | void InputDispatcher::setFocusedApplicationLocked( | 
|  | 4644 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { | 
|  | 4645 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = | 
|  | 4646 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
|  | 4647 |  | 
|  | 4648 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { | 
|  | 4649 | return; // This application is already focused. No need to wake up or change anything. | 
|  | 4650 | } | 
|  | 4651 |  | 
|  | 4652 | // Set the new application handle. | 
|  | 4653 | if (inputApplicationHandle != nullptr) { | 
|  | 4654 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; | 
|  | 4655 | } else { | 
|  | 4656 | mFocusedApplicationHandlesByDisplay.erase(displayId); | 
|  | 4657 | } | 
|  | 4658 |  | 
|  | 4659 | // No matter what the old focused application was, stop waiting on it because it is | 
|  | 4660 | // no longer focused. | 
|  | 4661 | resetNoFocusedWindowTimeoutLocked(); | 
|  | 4662 | } | 
|  | 4663 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4664 | /** | 
|  | 4665 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where | 
|  | 4666 | * the display not specified. | 
|  | 4667 | * | 
|  | 4668 | * We track any unreleased events for each window. If a window loses the ability to receive the | 
|  | 4669 | * released event, we will send a cancel event to it. So when the focused display is changed, we | 
|  | 4670 | * cancel all the unreleased display-unspecified events for the focused window on the old focused | 
|  | 4671 | * display. The display-specified events won't be affected. | 
|  | 4672 | */ | 
|  | 4673 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4674 | if (DEBUG_FOCUS) { | 
|  | 4675 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); | 
|  | 4676 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4677 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4678 | std::scoped_lock _l(mLock); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4679 |  | 
|  | 4680 | if (mFocusedDisplayId != displayId) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4681 | sp<IBinder> oldFocusedWindowToken = | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4682 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4683 | if (oldFocusedWindowToken != nullptr) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4684 | std::shared_ptr<InputChannel> inputChannel = | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4685 | getInputChannelLocked(oldFocusedWindowToken); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4686 | if (inputChannel != nullptr) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4687 | CancelationOptions | 
|  | 4688 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
|  | 4689 | "The display which contains this window no longer has focus."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4690 | options.displayId = ADISPLAY_ID_NONE; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4691 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); | 
|  | 4692 | } | 
|  | 4693 | } | 
|  | 4694 | mFocusedDisplayId = displayId; | 
|  | 4695 |  | 
| Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4696 | // Find new focused window and validate | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4697 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4698 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4699 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4700 | if (newFocusedWindowToken == nullptr) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4701 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4702 | if (mFocusResolver.hasFocusedWindowTokens()) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4703 | ALOGE("But another display has a focused window\n%s", | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4704 | mFocusResolver.dumpFocusedWindows().c_str()); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4705 | } | 
|  | 4706 | } | 
|  | 4707 | } | 
|  | 4708 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4709 | if (DEBUG_FOCUS) { | 
|  | 4710 | logDispatchStateLocked(); | 
|  | 4711 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4712 | } // release lock | 
|  | 4713 |  | 
|  | 4714 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4715 | mLooper->wake(); | 
|  | 4716 | } | 
|  | 4717 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4718 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4719 | if (DEBUG_FOCUS) { | 
|  | 4720 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); | 
|  | 4721 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4722 |  | 
|  | 4723 | bool changed; | 
|  | 4724 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4725 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4726 |  | 
|  | 4727 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { | 
|  | 4728 | if (mDispatchFrozen && !frozen) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4729 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4730 | } | 
|  | 4731 |  | 
|  | 4732 | if (mDispatchEnabled && !enabled) { | 
|  | 4733 | resetAndDropEverythingLocked("dispatcher is being disabled"); | 
|  | 4734 | } | 
|  | 4735 |  | 
|  | 4736 | mDispatchEnabled = enabled; | 
|  | 4737 | mDispatchFrozen = frozen; | 
|  | 4738 | changed = true; | 
|  | 4739 | } else { | 
|  | 4740 | changed = false; | 
|  | 4741 | } | 
|  | 4742 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4743 | if (DEBUG_FOCUS) { | 
|  | 4744 | logDispatchStateLocked(); | 
|  | 4745 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4746 | } // release lock | 
|  | 4747 |  | 
|  | 4748 | if (changed) { | 
|  | 4749 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4750 | mLooper->wake(); | 
|  | 4751 | } | 
|  | 4752 | } | 
|  | 4753 |  | 
|  | 4754 | void InputDispatcher::setInputFilterEnabled(bool enabled) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4755 | if (DEBUG_FOCUS) { | 
|  | 4756 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); | 
|  | 4757 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4758 |  | 
|  | 4759 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4760 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4761 |  | 
|  | 4762 | if (mInputFilterEnabled == enabled) { | 
|  | 4763 | return; | 
|  | 4764 | } | 
|  | 4765 |  | 
|  | 4766 | mInputFilterEnabled = enabled; | 
|  | 4767 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); | 
|  | 4768 | } // release lock | 
|  | 4769 |  | 
|  | 4770 | // Wake up poll loop since there might be work to do to drop everything. | 
|  | 4771 | mLooper->wake(); | 
|  | 4772 | } | 
|  | 4773 |  | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4774 | void InputDispatcher::setInTouchMode(bool inTouchMode) { | 
|  | 4775 | std::scoped_lock lock(mLock); | 
|  | 4776 | mInTouchMode = inTouchMode; | 
|  | 4777 | } | 
|  | 4778 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4779 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { | 
|  | 4780 | if (opacity < 0 || opacity > 1) { | 
|  | 4781 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); | 
|  | 4782 | return; | 
|  | 4783 | } | 
|  | 4784 |  | 
|  | 4785 | std::scoped_lock lock(mLock); | 
|  | 4786 | mMaximumObscuringOpacityForTouch = opacity; | 
|  | 4787 | } | 
|  | 4788 |  | 
|  | 4789 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { | 
|  | 4790 | std::scoped_lock lock(mLock); | 
|  | 4791 | mBlockUntrustedTouchesMode = mode; | 
|  | 4792 | } | 
|  | 4793 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4794 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, | 
|  | 4795 | bool isDragDrop) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4796 | if (fromToken == toToken) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4797 | if (DEBUG_FOCUS) { | 
|  | 4798 | ALOGD("Trivial transfer to same window."); | 
|  | 4799 | } | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4800 | return true; | 
|  | 4801 | } | 
|  | 4802 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4803 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4804 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4805 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4806 | sp<WindowInfoHandle> fromWindowHandle = getWindowHandleLocked(fromToken); | 
|  | 4807 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4808 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4809 | ALOGW("Cannot transfer focus because from or to window not found."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4810 | return false; | 
|  | 4811 | } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4812 | if (DEBUG_FOCUS) { | 
|  | 4813 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", | 
|  | 4814 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); | 
|  | 4815 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4816 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4817 | if (DEBUG_FOCUS) { | 
|  | 4818 | ALOGD("Cannot transfer focus because windows are on different displays."); | 
|  | 4819 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4820 | return false; | 
|  | 4821 | } | 
|  | 4822 |  | 
|  | 4823 | bool found = false; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4824 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
|  | 4825 | TouchState& state = pair.second; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4826 | for (size_t i = 0; i < state.windows.size(); i++) { | 
|  | 4827 | const TouchedWindow& touchedWindow = state.windows[i]; | 
|  | 4828 | if (touchedWindow.windowHandle == fromWindowHandle) { | 
|  | 4829 | int32_t oldTargetFlags = touchedWindow.targetFlags; | 
|  | 4830 | BitSet32 pointerIds = touchedWindow.pointerIds; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4831 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4832 | state.windows.erase(state.windows.begin() + i); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4833 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4834 | int32_t newTargetFlags = oldTargetFlags & | 
|  | 4835 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | | 
|  | 4836 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4837 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4838 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4839 | // Store the dragging window. | 
|  | 4840 | if (isDragDrop) { | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4841 | mDragState = std::make_unique<DragState>(toWindowHandle); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4842 | } | 
|  | 4843 |  | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4844 | found = true; | 
|  | 4845 | goto Found; | 
|  | 4846 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4847 | } | 
|  | 4848 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4849 | Found: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4850 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4851 | if (!found) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4852 | if (DEBUG_FOCUS) { | 
|  | 4853 | ALOGD("Focus transfer failed because from window did not have focus."); | 
|  | 4854 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4855 | return false; | 
|  | 4856 | } | 
|  | 4857 |  | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4858 | sp<Connection> fromConnection = getConnectionLocked(fromToken); | 
|  | 4859 | sp<Connection> toConnection = getConnectionLocked(toToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4860 | if (fromConnection != nullptr && toConnection != nullptr) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4861 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4862 | CancelationOptions | 
|  | 4863 | options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 4864 | "transferring touch focus from this window to another window"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4865 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4866 | synthesizePointerDownEventsForConnectionLocked(toConnection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4867 | } | 
|  | 4868 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4869 | if (DEBUG_FOCUS) { | 
|  | 4870 | logDispatchStateLocked(); | 
|  | 4871 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4872 | } // release lock | 
|  | 4873 |  | 
|  | 4874 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4875 | mLooper->wake(); | 
|  | 4876 | return true; | 
|  | 4877 | } | 
|  | 4878 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4879 | // Binder call | 
|  | 4880 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { | 
|  | 4881 | sp<IBinder> fromToken; | 
|  | 4882 | { // acquire lock | 
|  | 4883 | std::scoped_lock _l(mLock); | 
|  | 4884 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4885 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4886 | if (toWindowHandle == nullptr) { | 
|  | 4887 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); | 
|  | 4888 | return false; | 
|  | 4889 | } | 
|  | 4890 |  | 
|  | 4891 | const int32_t displayId = toWindowHandle->getInfo()->displayId; | 
|  | 4892 |  | 
|  | 4893 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); | 
|  | 4894 | if (touchStateIt == mTouchStatesByDisplay.end()) { | 
|  | 4895 | ALOGD("Could not transfer touch because the display %" PRId32 " is not being touched", | 
|  | 4896 | displayId); | 
|  | 4897 | return false; | 
|  | 4898 | } | 
|  | 4899 |  | 
|  | 4900 | TouchState& state = touchStateIt->second; | 
|  | 4901 | if (state.windows.size() != 1) { | 
|  | 4902 | ALOGW("Cannot transfer touch state because there are %zu windows being touched", | 
|  | 4903 | state.windows.size()); | 
|  | 4904 | return false; | 
|  | 4905 | } | 
|  | 4906 | const TouchedWindow& touchedWindow = state.windows[0]; | 
|  | 4907 | fromToken = touchedWindow.windowHandle->getToken(); | 
|  | 4908 | } // release lock | 
|  | 4909 |  | 
|  | 4910 | return transferTouchFocus(fromToken, destChannelToken); | 
|  | 4911 | } | 
|  | 4912 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4913 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4914 | if (DEBUG_FOCUS) { | 
|  | 4915 | ALOGD("Resetting and dropping all events (%s).", reason); | 
|  | 4916 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4917 |  | 
|  | 4918 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); | 
|  | 4919 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 4920 |  | 
|  | 4921 | resetKeyRepeatLocked(); | 
|  | 4922 | releasePendingEventLocked(); | 
|  | 4923 | drainInboundQueueLocked(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4924 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4925 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4926 | mAnrTracker.clear(); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4927 | mTouchStatesByDisplay.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4928 | mLastHoverWindowHandle.clear(); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4929 | mReplacedKeys.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4930 | } | 
|  | 4931 |  | 
|  | 4932 | void InputDispatcher::logDispatchStateLocked() { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4933 | std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4934 | dumpDispatchStateLocked(dump); | 
|  | 4935 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4936 | std::istringstream stream(dump); | 
|  | 4937 | std::string line; | 
|  | 4938 |  | 
|  | 4939 | while (std::getline(stream, line, '\n')) { | 
|  | 4940 | ALOGD("%s", line.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4941 | } | 
|  | 4942 | } | 
|  | 4943 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4944 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { | 
|  | 4945 | std::string dump; | 
|  | 4946 |  | 
|  | 4947 | dump += StringPrintf(INDENT "FocusedWindowRequestedPointerCapture: %s\n", | 
|  | 4948 | toString(mFocusedWindowRequestedPointerCapture)); | 
|  | 4949 |  | 
|  | 4950 | std::string windowName = "None"; | 
|  | 4951 | if (mWindowTokenWithPointerCapture) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4952 | const sp<WindowInfoHandle> captureWindowHandle = | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4953 | getWindowHandleLocked(mWindowTokenWithPointerCapture); | 
|  | 4954 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() | 
|  | 4955 | : "token has capture without window"; | 
|  | 4956 | } | 
|  | 4957 | dump += StringPrintf(INDENT "CurrentWindowWithPointerCapture: %s\n", windowName.c_str()); | 
|  | 4958 |  | 
|  | 4959 | return dump; | 
|  | 4960 | } | 
|  | 4961 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4962 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { | 
| Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4963 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); | 
|  | 4964 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); | 
|  | 4965 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4966 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4967 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4968 | if (!mFocusedApplicationHandlesByDisplay.empty()) { | 
|  | 4969 | dump += StringPrintf(INDENT "FocusedApplications:\n"); | 
|  | 4970 | for (auto& it : mFocusedApplicationHandlesByDisplay) { | 
|  | 4971 | const int32_t displayId = it.first; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4972 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4973 | const std::chrono::duration timeout = | 
|  | 4974 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4975 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4976 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4977 | displayId, applicationHandle->getName().c_str(), millis(timeout)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4978 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4979 | } else { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4980 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4981 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4982 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4983 | dump += mFocusResolver.dump(); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4984 | dump += dumpPointerCaptureStateLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4985 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4986 | if (!mTouchStatesByDisplay.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4987 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4988 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
|  | 4989 | const TouchState& state = pair.second; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4990 | 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] | 4991 | state.displayId, toString(state.down), toString(state.split), | 
|  | 4992 | state.deviceId, state.source); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4993 | if (!state.windows.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4994 | dump += INDENT3 "Windows:\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4995 | for (size_t i = 0; i < state.windows.size(); i++) { | 
|  | 4996 | const TouchedWindow& touchedWindow = state.windows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4997 | dump += StringPrintf(INDENT4 | 
|  | 4998 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", | 
|  | 4999 | i, touchedWindow.windowHandle->getName().c_str(), | 
|  | 5000 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5001 | } | 
|  | 5002 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5003 | dump += INDENT3 "Windows: <none>\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5004 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5005 | } | 
|  | 5006 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5007 | dump += INDENT "TouchStates: <no displays touched>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5008 | } | 
|  | 5009 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5010 | if (mDragState) { | 
|  | 5011 | dump += StringPrintf(INDENT "DragState:\n"); | 
|  | 5012 | mDragState->dump(dump, INDENT2); | 
|  | 5013 | } | 
|  | 5014 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5015 | if (!mWindowHandlesByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5016 | for (auto& it : mWindowHandlesByDisplay) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5017 | const std::vector<sp<WindowInfoHandle>> windowHandles = it.second; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5018 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5019 | if (!windowHandles.empty()) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5020 | dump += INDENT2 "Windows:\n"; | 
|  | 5021 | for (size_t i = 0; i < windowHandles.size(); i++) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5022 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; | 
|  | 5023 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5024 |  | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5025 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5026 | "paused=%s, focusable=%s, " | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5027 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5028 | "flags=%s, type=%s, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5029 | "frame=[%d,%d][%d,%d], globalScale=%f, " | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5030 | "applicationInfo.name=%s, " | 
|  | 5031 | "applicationInfo.token=%s, " | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5032 | "touchableRegion=", | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5033 | i, windowInfo->name.c_str(), windowInfo->id, | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5034 | windowInfo->displayId, toString(windowInfo->paused), | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5035 | toString(windowInfo->focusable), | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5036 | toString(windowInfo->hasWallpaper), | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5037 | toString(windowInfo->visible), windowInfo->alpha, | 
| Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 5038 | windowInfo->flags.string().c_str(), | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5039 | NamedEnum::string(windowInfo->type).c_str(), | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5040 | windowInfo->frameLeft, windowInfo->frameTop, | 
|  | 5041 | windowInfo->frameRight, windowInfo->frameBottom, | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5042 | windowInfo->globalScaleFactor, | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5043 | windowInfo->applicationInfo.name.c_str(), | 
|  | 5044 | toString(windowInfo->applicationInfo.token).c_str()); | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5045 | dump += dumpRegion(windowInfo->touchableRegion); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5046 | dump += StringPrintf(", inputFeatures=%s", | 
|  | 5047 | windowInfo->inputFeatures.string().c_str()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5048 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5049 | "ms, trustedOverlay=%s, hasToken=%s, " | 
|  | 5050 | "touchOcclusionMode=%s\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5051 | windowInfo->ownerPid, windowInfo->ownerUid, | 
| Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5052 | millis(windowInfo->dispatchingTimeout), | 
|  | 5053 | toString(windowInfo->trustedOverlay), | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5054 | toString(windowInfo->token != nullptr), | 
|  | 5055 | toString(windowInfo->touchOcclusionMode).c_str()); | 
| chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5056 | windowInfo->transform.dump(dump, "transform", INDENT4); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5057 | } | 
|  | 5058 | } else { | 
|  | 5059 | dump += INDENT2 "Windows: <none>\n"; | 
|  | 5060 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5061 | } | 
|  | 5062 | } else { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5063 | dump += INDENT "Displays: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5064 | } | 
|  | 5065 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5066 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5067 | for (auto& it : mGlobalMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5068 | const std::vector<Monitor>& monitors = it.second; | 
|  | 5069 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); | 
|  | 5070 | dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5071 | } | 
|  | 5072 | for (auto& it : mGestureMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5073 | const std::vector<Monitor>& monitors = it.second; | 
|  | 5074 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); | 
|  | 5075 | dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5076 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5077 | } else { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5078 | dump += INDENT "Monitors: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5079 | } | 
|  | 5080 |  | 
|  | 5081 | nsecs_t currentTime = now(); | 
|  | 5082 |  | 
|  | 5083 | // Dump recently dispatched or dropped events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5084 | if (!mRecentQueue.empty()) { | 
|  | 5085 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5086 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5087 | dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5088 | dump += entry->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5089 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5090 | } | 
|  | 5091 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5092 | dump += INDENT "RecentQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5093 | } | 
|  | 5094 |  | 
|  | 5095 | // Dump event currently being dispatched. | 
|  | 5096 | if (mPendingEvent) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5097 | dump += INDENT "PendingEvent:\n"; | 
|  | 5098 | dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5099 | dump += mPendingEvent->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5100 | dump += StringPrintf(", age=%" PRId64 "ms\n", | 
|  | 5101 | ns2ms(currentTime - mPendingEvent->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5102 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5103 | dump += INDENT "PendingEvent: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5104 | } | 
|  | 5105 |  | 
|  | 5106 | // Dump inbound events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5107 | if (!mInboundQueue.empty()) { | 
|  | 5108 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5109 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5110 | dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5111 | dump += entry->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5112 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5113 | } | 
|  | 5114 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5115 | dump += INDENT "InboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5116 | } | 
|  | 5117 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5118 | if (!mReplacedKeys.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5119 | dump += INDENT "ReplacedKeys:\n"; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5120 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { | 
|  | 5121 | const KeyReplacement& replacement = pair.first; | 
|  | 5122 | int32_t newKeyCode = pair.second; | 
|  | 5123 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5124 | replacement.keyCode, replacement.deviceId, newKeyCode); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5125 | } | 
|  | 5126 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5127 | dump += INDENT "ReplacedKeys: <empty>\n"; | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5128 | } | 
|  | 5129 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5130 | if (!mCommandQueue.empty()) { | 
|  | 5131 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); | 
|  | 5132 | } else { | 
|  | 5133 | dump += INDENT "CommandQueue: <empty>\n"; | 
|  | 5134 | } | 
|  | 5135 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5136 | if (!mConnectionsByToken.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5137 | dump += INDENT "Connections:\n"; | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5138 | for (const auto& [token, connection] : mConnectionsByToken) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5139 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5140 | "status=%s, monitor=%s, responsive=%s\n", | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5141 | connection->inputChannel->getFd().get(), | 
|  | 5142 | connection->getInputChannelName().c_str(), | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5143 | connection->getWindowName().c_str(), connection->getStatusLabel(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5144 | toString(connection->monitor), toString(connection->responsive)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5145 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5146 | if (!connection->outboundQueue.empty()) { | 
|  | 5147 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", | 
|  | 5148 | connection->outboundQueue.size()); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5149 | dump += dumpQueue(connection->outboundQueue, currentTime); | 
|  | 5150 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5151 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5152 | dump += INDENT3 "OutboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5153 | } | 
|  | 5154 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5155 | if (!connection->waitQueue.empty()) { | 
|  | 5156 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", | 
|  | 5157 | connection->waitQueue.size()); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5158 | dump += dumpQueue(connection->waitQueue, currentTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5159 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5160 | dump += INDENT3 "WaitQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5161 | } | 
|  | 5162 | } | 
|  | 5163 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5164 | dump += INDENT "Connections: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5165 | } | 
|  | 5166 |  | 
|  | 5167 | if (isAppSwitchPendingLocked()) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5168 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", | 
|  | 5169 | ns2ms(mAppSwitchDueTime - now())); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5170 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5171 | dump += INDENT "AppSwitch: not pending\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5172 | } | 
|  | 5173 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5174 | dump += INDENT "Configuration:\n"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5175 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); | 
|  | 5176 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", | 
|  | 5177 | ns2ms(mConfig.keyRepeatTimeout)); | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5178 | dump += mLatencyTracker.dump(INDENT2); | 
| Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5179 | dump += mLatencyAggregator.dump(INDENT2); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5180 | } | 
|  | 5181 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5182 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { | 
|  | 5183 | const size_t numMonitors = monitors.size(); | 
|  | 5184 | for (size_t i = 0; i < numMonitors; i++) { | 
|  | 5185 | const Monitor& monitor = monitors[i]; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5186 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5187 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); | 
|  | 5188 | dump += "\n"; | 
|  | 5189 | } | 
|  | 5190 | } | 
|  | 5191 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5192 | class LooperEventCallback : public LooperCallback { | 
|  | 5193 | public: | 
|  | 5194 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} | 
|  | 5195 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } | 
|  | 5196 |  | 
|  | 5197 | private: | 
|  | 5198 | std::function<int(int events)> mCallback; | 
|  | 5199 | }; | 
|  | 5200 |  | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5201 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5202 | if (DEBUG_CHANNEL_CREATION) { | 
|  | 5203 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); | 
|  | 5204 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5205 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5206 | std::unique_ptr<InputChannel> serverChannel; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5207 | std::unique_ptr<InputChannel> clientChannel; | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5208 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5209 |  | 
|  | 5210 | if (result) { | 
|  | 5211 | return base::Error(result) << "Failed to open input channel pair with name " << name; | 
|  | 5212 | } | 
|  | 5213 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5214 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5215 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5216 | const sp<IBinder>& token = serverChannel->getConnectionToken(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5217 | int fd = serverChannel->getFd(); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5218 | sp<Connection> connection = | 
|  | 5219 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5220 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5221 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { | 
|  | 5222 | ALOGE("Created a new connection, but the token %p is already known", token.get()); | 
|  | 5223 | } | 
|  | 5224 | mConnectionsByToken.emplace(token, connection); | 
|  | 5225 |  | 
|  | 5226 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, | 
|  | 5227 | this, std::placeholders::_1, token); | 
|  | 5228 |  | 
|  | 5229 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5230 | } // release lock | 
|  | 5231 |  | 
|  | 5232 | // Wake the looper because some connections have changed. | 
|  | 5233 | mLooper->wake(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5234 | return clientChannel; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5235 | } | 
|  | 5236 |  | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5237 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, | 
|  | 5238 | bool isGestureMonitor, | 
|  | 5239 | const std::string& name, | 
|  | 5240 | int32_t pid) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5241 | std::shared_ptr<InputChannel> serverChannel; | 
|  | 5242 | std::unique_ptr<InputChannel> clientChannel; | 
|  | 5243 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); | 
|  | 5244 | if (result) { | 
|  | 5245 | return base::Error(result) << "Failed to open input channel pair with name " << name; | 
|  | 5246 | } | 
|  | 5247 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5248 | { // acquire lock | 
|  | 5249 | std::scoped_lock _l(mLock); | 
|  | 5250 |  | 
|  | 5251 | if (displayId < 0) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5252 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name | 
|  | 5253 | << " without a specified display."; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5254 | } | 
|  | 5255 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5256 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5257 | const sp<IBinder>& token = serverChannel->getConnectionToken(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5258 | const int fd = serverChannel->getFd(); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5259 |  | 
|  | 5260 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { | 
|  | 5261 | ALOGE("Created a new connection, but the token %p is already known", token.get()); | 
|  | 5262 | } | 
|  | 5263 | mConnectionsByToken.emplace(token, connection); | 
|  | 5264 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, | 
|  | 5265 | this, std::placeholders::_1, token); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5266 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5267 | auto& monitorsByDisplay = | 
|  | 5268 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5269 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5270 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5271 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); | 
| Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5272 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), | 
|  | 5273 | displayId, toString(isGestureMonitor), pid); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5274 | } | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5275 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5276 | // Wake the looper because some connections have changed. | 
|  | 5277 | mLooper->wake(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5278 | return clientChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5279 | } | 
|  | 5280 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5281 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5282 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5283 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5284 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5285 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5286 | if (status) { | 
|  | 5287 | return status; | 
|  | 5288 | } | 
|  | 5289 | } // release lock | 
|  | 5290 |  | 
|  | 5291 | // Wake the poll loop because removing the connection may have changed the current | 
|  | 5292 | // synchronization state. | 
|  | 5293 | mLooper->wake(); | 
|  | 5294 | return OK; | 
|  | 5295 | } | 
|  | 5296 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5297 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, | 
|  | 5298 | bool notify) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5299 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5300 | if (connection == nullptr) { | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5301 | // 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] | 5302 | return BAD_VALUE; | 
|  | 5303 | } | 
|  | 5304 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5305 | removeConnectionLocked(connection); | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5306 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5307 | if (connection->monitor) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5308 | removeMonitorChannelLocked(connectionToken); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5309 | } | 
|  | 5310 |  | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5311 | mLooper->removeFd(connection->inputChannel->getFd()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5312 |  | 
|  | 5313 | nsecs_t currentTime = now(); | 
|  | 5314 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); | 
|  | 5315 |  | 
|  | 5316 | connection->status = Connection::STATUS_ZOMBIE; | 
|  | 5317 | return OK; | 
|  | 5318 | } | 
|  | 5319 |  | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5320 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { | 
|  | 5321 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); | 
|  | 5322 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5323 | } | 
|  | 5324 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5325 | void InputDispatcher::removeMonitorChannelLocked( | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5326 | const sp<IBinder>& connectionToken, | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5327 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5328 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5329 | std::vector<Monitor>& monitors = it->second; | 
|  | 5330 | const size_t numMonitors = monitors.size(); | 
|  | 5331 | for (size_t i = 0; i < numMonitors; i++) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5332 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { | 
| Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5333 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, | 
|  | 5334 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5335 | monitors.erase(monitors.begin() + i); | 
|  | 5336 | break; | 
|  | 5337 | } | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5338 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5339 | if (monitors.empty()) { | 
|  | 5340 | it = monitorsByDisplay.erase(it); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5341 | } else { | 
|  | 5342 | ++it; | 
|  | 5343 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5344 | } | 
|  | 5345 | } | 
|  | 5346 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5347 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { | 
|  | 5348 | { // acquire lock | 
|  | 5349 | std::scoped_lock _l(mLock); | 
|  | 5350 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); | 
|  | 5351 |  | 
|  | 5352 | if (!foundDisplayId) { | 
|  | 5353 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); | 
|  | 5354 | return BAD_VALUE; | 
|  | 5355 | } | 
|  | 5356 | int32_t displayId = foundDisplayId.value(); | 
|  | 5357 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5358 | std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
|  | 5359 | mTouchStatesByDisplay.find(displayId); | 
|  | 5360 | if (stateIt == mTouchStatesByDisplay.end()) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5361 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); | 
|  | 5362 | return BAD_VALUE; | 
|  | 5363 | } | 
|  | 5364 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5365 | TouchState& state = stateIt->second; | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5366 | std::shared_ptr<InputChannel> requestingChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5367 | std::optional<int32_t> foundDeviceId; | 
|  | 5368 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5369 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5370 | requestingChannel = touchedMonitor.monitor.inputChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5371 | foundDeviceId = state.deviceId; | 
|  | 5372 | } | 
|  | 5373 | } | 
|  | 5374 | if (!foundDeviceId || !state.down) { | 
|  | 5375 | ALOGW("Attempted to pilfer points from a monitor without any on-going pointer streams." | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5376 | " Ignoring."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5377 | return BAD_VALUE; | 
|  | 5378 | } | 
|  | 5379 | int32_t deviceId = foundDeviceId.value(); | 
|  | 5380 |  | 
|  | 5381 | // Send cancel events to all the input channels we're stealing from. | 
|  | 5382 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5383 | "gesture monitor stole pointer stream"); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5384 | options.deviceId = deviceId; | 
|  | 5385 | options.displayId = displayId; | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5386 | std::string canceledWindows = "["; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5387 | for (const TouchedWindow& window : state.windows) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5388 | std::shared_ptr<InputChannel> channel = | 
|  | 5389 | getInputChannelLocked(window.windowHandle->getToken()); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5390 | if (channel != nullptr) { | 
|  | 5391 | synthesizeCancelationEventsForInputChannelLocked(channel, options); | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5392 | canceledWindows += channel->getName() + ", "; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5393 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5394 | } | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5395 | canceledWindows += "]"; | 
|  | 5396 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), | 
|  | 5397 | canceledWindows.c_str()); | 
|  | 5398 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5399 | // Then clear the current touch state so we stop dispatching to them as well. | 
|  | 5400 | state.filterNonMonitors(); | 
|  | 5401 | } | 
|  | 5402 | return OK; | 
|  | 5403 | } | 
|  | 5404 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5405 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { | 
|  | 5406 | { // acquire lock | 
|  | 5407 | std::scoped_lock _l(mLock); | 
|  | 5408 | if (DEBUG_FOCUS) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5409 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5410 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", | 
|  | 5411 | windowHandle != nullptr ? windowHandle->getName().c_str() | 
|  | 5412 | : "token without window"); | 
|  | 5413 | } | 
|  | 5414 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5415 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5416 | if (focusedToken != windowToken) { | 
|  | 5417 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", | 
|  | 5418 | enabled ? "enable" : "disable"); | 
|  | 5419 | return; | 
|  | 5420 | } | 
|  | 5421 |  | 
|  | 5422 | if (enabled == mFocusedWindowRequestedPointerCapture) { | 
|  | 5423 | ALOGW("Ignoring request to %s Pointer Capture: " | 
|  | 5424 | "window has %s requested pointer capture.", | 
|  | 5425 | enabled ? "enable" : "disable", enabled ? "already" : "not"); | 
|  | 5426 | return; | 
|  | 5427 | } | 
|  | 5428 |  | 
|  | 5429 | mFocusedWindowRequestedPointerCapture = enabled; | 
|  | 5430 | setPointerCaptureLocked(enabled); | 
|  | 5431 | } // release lock | 
|  | 5432 |  | 
|  | 5433 | // Wake the thread to process command entries. | 
|  | 5434 | mLooper->wake(); | 
|  | 5435 | } | 
|  | 5436 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5437 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( | 
|  | 5438 | const sp<IBinder>& token) { | 
|  | 5439 | for (const auto& it : mGestureMonitorsByDisplay) { | 
|  | 5440 | const std::vector<Monitor>& monitors = it.second; | 
|  | 5441 | for (const Monitor& monitor : monitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5442 | if (monitor.inputChannel->getConnectionToken() == token) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5443 | return it.first; | 
|  | 5444 | } | 
|  | 5445 | } | 
|  | 5446 | } | 
|  | 5447 | return std::nullopt; | 
|  | 5448 | } | 
|  | 5449 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5450 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { | 
|  | 5451 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); | 
|  | 5452 | if (gesturePid.has_value()) { | 
|  | 5453 | return gesturePid; | 
|  | 5454 | } | 
|  | 5455 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); | 
|  | 5456 | } | 
|  | 5457 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5458 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5459 | if (inputConnectionToken == nullptr) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5460 | return nullptr; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5461 | } | 
|  | 5462 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5463 | for (const auto& [token, connection] : mConnectionsByToken) { | 
|  | 5464 | if (token == inputConnectionToken) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5465 | return connection; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5466 | } | 
|  | 5467 | } | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5468 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5469 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5470 | } | 
|  | 5471 |  | 
| Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5472 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { | 
|  | 5473 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
|  | 5474 | if (connection == nullptr) { | 
|  | 5475 | return "<nullptr>"; | 
|  | 5476 | } | 
|  | 5477 | return connection->getInputChannelName(); | 
|  | 5478 | } | 
|  | 5479 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5480 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5481 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5482 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5483 | } | 
|  | 5484 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5485 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, | 
|  | 5486 | const sp<Connection>& connection, uint32_t seq, | 
|  | 5487 | bool handled, nsecs_t consumeTime) { | 
|  | 5488 | // Handle post-event policy actions. | 
|  | 5489 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
|  | 5490 | if (dispatchEntryIt == connection->waitQueue.end()) { | 
|  | 5491 | return; | 
|  | 5492 | } | 
|  | 5493 | DispatchEntry* dispatchEntry = *dispatchEntryIt; | 
|  | 5494 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; | 
|  | 5495 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { | 
|  | 5496 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), | 
|  | 5497 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); | 
|  | 5498 | } | 
|  | 5499 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { | 
|  | 5500 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, | 
|  | 5501 | connection->inputChannel->getConnectionToken(), | 
|  | 5502 | dispatchEntry->deliveryTime, consumeTime, finishTime); | 
|  | 5503 | } | 
|  | 5504 |  | 
|  | 5505 | bool restartEvent; | 
|  | 5506 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { | 
|  | 5507 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); | 
|  | 5508 | restartEvent = | 
|  | 5509 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); | 
|  | 5510 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { | 
|  | 5511 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); | 
|  | 5512 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, | 
|  | 5513 | handled); | 
|  | 5514 | } else { | 
|  | 5515 | restartEvent = false; | 
|  | 5516 | } | 
|  | 5517 |  | 
|  | 5518 | // Dequeue the event and start the next cycle. | 
|  | 5519 | // Because the lock might have been released, it is possible that the | 
|  | 5520 | // contents of the wait queue to have been drained, so we need to double-check | 
|  | 5521 | // a few things. | 
|  | 5522 | dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
|  | 5523 | if (dispatchEntryIt != connection->waitQueue.end()) { | 
|  | 5524 | dispatchEntry = *dispatchEntryIt; | 
|  | 5525 | connection->waitQueue.erase(dispatchEntryIt); | 
|  | 5526 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); | 
|  | 5527 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); | 
|  | 5528 | if (!connection->responsive) { | 
|  | 5529 | connection->responsive = isConnectionResponsive(*connection); | 
|  | 5530 | if (connection->responsive) { | 
|  | 5531 | // The connection was unresponsive, and now it's responsive. | 
|  | 5532 | processConnectionResponsiveLocked(*connection); | 
|  | 5533 | } | 
|  | 5534 | } | 
|  | 5535 | traceWaitQueueLength(*connection); | 
|  | 5536 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { | 
|  | 5537 | connection->outboundQueue.push_front(dispatchEntry); | 
|  | 5538 | traceOutboundQueueLength(*connection); | 
|  | 5539 | } else { | 
|  | 5540 | releaseDispatchEntry(dispatchEntry); | 
|  | 5541 | } | 
|  | 5542 | } | 
|  | 5543 |  | 
|  | 5544 | // Start the next dispatch cycle for this connection. | 
|  | 5545 | startDispatchCycleLocked(now(), connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5546 | } | 
|  | 5547 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5548 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, | 
|  | 5549 | const sp<IBinder>& newToken) { | 
|  | 5550 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { | 
|  | 5551 | scoped_unlock unlock(mLock); | 
|  | 5552 | mPolicy->notifyFocusChanged(oldToken, newToken); | 
|  | 5553 | }; | 
|  | 5554 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5555 | } | 
|  | 5556 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5557 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { | 
|  | 5558 | auto command = [this, token, x, y]() REQUIRES(mLock) { | 
|  | 5559 | scoped_unlock unlock(mLock); | 
|  | 5560 | mPolicy->notifyDropWindow(token, x, y); | 
|  | 5561 | }; | 
|  | 5562 | postCommandLocked(std::move(command)); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5563 | } | 
|  | 5564 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5565 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { | 
|  | 5566 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { | 
|  | 5567 | scoped_unlock unlock(mLock); | 
|  | 5568 | mPolicy->notifyUntrustedTouch(obscuringPackage); | 
|  | 5569 | }; | 
|  | 5570 | postCommandLocked(std::move(command)); | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5571 | } | 
|  | 5572 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5573 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { | 
|  | 5574 | if (connection == nullptr) { | 
|  | 5575 | LOG_ALWAYS_FATAL("Caller must check for nullness"); | 
|  | 5576 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5577 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue | 
|  | 5578 | // is already healthy again. Don't raise ANR in this situation | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5579 | if (connection->waitQueue.empty()) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5580 | ALOGI("Not raising ANR because the connection %s has recovered", | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5581 | connection->inputChannel->getName().c_str()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5582 | return; | 
|  | 5583 | } | 
|  | 5584 | /** | 
|  | 5585 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, | 
|  | 5586 | * may not be the one that caused the timeout to occur. One possibility is that window timeout | 
|  | 5587 | * has changed. This could cause newer entries to time out before the already dispatched | 
|  | 5588 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app | 
|  | 5589 | * processes the events linearly. So providing information about the oldest entry seems to be | 
|  | 5590 | * most useful. | 
|  | 5591 | */ | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5592 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5593 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; | 
|  | 5594 | std::string reason = | 
|  | 5595 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5596 | connection->inputChannel->getName().c_str(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5597 | ns2ms(currentWait), | 
|  | 5598 | oldestEntry->eventEntry->getDescription().c_str()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5599 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5600 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5601 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5602 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); | 
|  | 5603 |  | 
|  | 5604 | // Stop waking up for events on this connection, it is already unresponsive | 
|  | 5605 | cancelEventsForAnrLocked(connection); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5606 | } | 
|  | 5607 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5608 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { | 
|  | 5609 | std::string reason = | 
|  | 5610 | StringPrintf("%s does not have a focused window", application->getName().c_str()); | 
|  | 5611 | updateLastAnrStateLocked(*application, reason); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5612 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5613 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { | 
|  | 5614 | scoped_unlock unlock(mLock); | 
|  | 5615 | mPolicy->notifyNoFocusedWindowAnr(application); | 
|  | 5616 | }; | 
|  | 5617 | postCommandLocked(std::move(command)); | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5618 | } | 
|  | 5619 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5620 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5621 | const std::string& reason) { | 
|  | 5622 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); | 
|  | 5623 | updateLastAnrStateLocked(windowLabel, reason); | 
|  | 5624 | } | 
|  | 5625 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5626 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, | 
|  | 5627 | const std::string& reason) { | 
|  | 5628 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5629 | updateLastAnrStateLocked(windowLabel, reason); | 
|  | 5630 | } | 
|  | 5631 |  | 
|  | 5632 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, | 
|  | 5633 | const std::string& reason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5634 | // 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] | 5635 | time_t t = time(nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5636 | struct tm tm; | 
|  | 5637 | localtime_r(&t, &tm); | 
|  | 5638 | char timestr[64]; | 
|  | 5639 | strftime(timestr, sizeof(timestr), "%F %T", &tm); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5640 | mLastAnrState.clear(); | 
|  | 5641 | mLastAnrState += INDENT "ANR:\n"; | 
|  | 5642 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5643 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); | 
|  | 5644 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5645 | dumpDispatchStateLocked(mLastAnrState); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5646 | } | 
|  | 5647 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5648 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, | 
|  | 5649 | KeyEntry& entry) { | 
|  | 5650 | const KeyEvent event = createKeyEvent(entry); | 
|  | 5651 | nsecs_t delay = 0; | 
|  | 5652 | { // release lock | 
|  | 5653 | scoped_unlock unlock(mLock); | 
|  | 5654 | android::base::Timer t; | 
|  | 5655 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, | 
|  | 5656 | entry.policyFlags); | 
|  | 5657 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 5658 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", | 
|  | 5659 | std::to_string(t.duration().count()).c_str()); | 
|  | 5660 | } | 
|  | 5661 | } // acquire lock | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5662 |  | 
|  | 5663 | if (delay < 0) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5664 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5665 | } else if (delay == 0) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5666 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5667 | } else { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5668 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; | 
|  | 5669 | entry.interceptKeyWakeupTime = now() + delay; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5670 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5671 | } | 
|  | 5672 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5673 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5674 | auto command = [this, pid, reason = std::move(reason)]() REQUIRES(mLock) { | 
|  | 5675 | scoped_unlock unlock(mLock); | 
|  | 5676 | mPolicy->notifyMonitorUnresponsive(pid, reason); | 
|  | 5677 | }; | 
|  | 5678 | postCommandLocked(std::move(command)); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5679 | } | 
|  | 5680 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5681 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5682 | std::string reason) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5683 | auto command = [this, token, reason = std::move(reason)]() REQUIRES(mLock) { | 
|  | 5684 | scoped_unlock unlock(mLock); | 
|  | 5685 | mPolicy->notifyWindowUnresponsive(token, reason); | 
|  | 5686 | }; | 
|  | 5687 | postCommandLocked(std::move(command)); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5688 | } | 
|  | 5689 |  | 
|  | 5690 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5691 | auto command = [this, pid]() REQUIRES(mLock) { | 
|  | 5692 | scoped_unlock unlock(mLock); | 
|  | 5693 | mPolicy->notifyMonitorResponsive(pid); | 
|  | 5694 | }; | 
|  | 5695 | postCommandLocked(std::move(command)); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5696 | } | 
|  | 5697 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5698 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken) { | 
|  | 5699 | auto command = [this, connectionToken]() REQUIRES(mLock) { | 
|  | 5700 | scoped_unlock unlock(mLock); | 
|  | 5701 | mPolicy->notifyWindowResponsive(connectionToken); | 
|  | 5702 | }; | 
|  | 5703 | postCommandLocked(std::move(command)); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5704 | } | 
|  | 5705 |  | 
|  | 5706 | /** | 
|  | 5707 | * Tell the policy that a connection has become unresponsive so that it can start ANR. | 
|  | 5708 | * Check whether the connection of interest is a monitor or a window, and add the corresponding | 
|  | 5709 | * command entry to the command queue. | 
|  | 5710 | */ | 
|  | 5711 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, | 
|  | 5712 | std::string reason) { | 
|  | 5713 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); | 
|  | 5714 | if (connection.monitor) { | 
|  | 5715 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), | 
|  | 5716 | reason.c_str()); | 
|  | 5717 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); | 
|  | 5718 | if (!pid.has_value()) { | 
|  | 5719 | ALOGE("Could not find unresponsive monitor for connection %s", | 
|  | 5720 | connection.inputChannel->getName().c_str()); | 
|  | 5721 | return; | 
|  | 5722 | } | 
|  | 5723 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); | 
|  | 5724 | return; | 
|  | 5725 | } | 
|  | 5726 | // If not a monitor, must be a window | 
|  | 5727 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), | 
|  | 5728 | reason.c_str()); | 
|  | 5729 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); | 
|  | 5730 | } | 
|  | 5731 |  | 
|  | 5732 | /** | 
|  | 5733 | * Tell the policy that a connection has become responsive so that it can stop ANR. | 
|  | 5734 | */ | 
|  | 5735 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { | 
|  | 5736 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); | 
|  | 5737 | if (connection.monitor) { | 
|  | 5738 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); | 
|  | 5739 | if (!pid.has_value()) { | 
|  | 5740 | ALOGE("Could not find responsive monitor for connection %s", | 
|  | 5741 | connection.inputChannel->getName().c_str()); | 
|  | 5742 | return; | 
|  | 5743 | } | 
|  | 5744 | sendMonitorResponsiveCommandLocked(pid.value()); | 
|  | 5745 | return; | 
|  | 5746 | } | 
|  | 5747 | // If not a monitor, must be a window | 
|  | 5748 | sendWindowResponsiveCommandLocked(connectionToken); | 
|  | 5749 | } | 
|  | 5750 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5751 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5752 | DispatchEntry* dispatchEntry, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5753 | KeyEntry& keyEntry, bool handled) { | 
|  | 5754 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5755 | if (!handled) { | 
|  | 5756 | // Report the key as unhandled, since the fallback was not handled. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5757 | mReporter->reportUnhandledKey(keyEntry.id); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5758 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5759 | return false; | 
|  | 5760 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5761 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5762 | // Get the fallback key state. | 
|  | 5763 | // Clear it out after dispatching the UP. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5764 | int32_t originalKeyCode = keyEntry.keyCode; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5765 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5766 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5767 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 5768 | } | 
|  | 5769 |  | 
|  | 5770 | if (handled || !dispatchEntry->hasForegroundTarget()) { | 
|  | 5771 | // If the application handles the original key for which we previously | 
|  | 5772 | // generated a fallback or if the window is not a foreground window, | 
|  | 5773 | // then cancel the associated fallback key, if any. | 
|  | 5774 | if (fallbackKeyCode != -1) { | 
|  | 5775 | // Dispatch the unhandled key to the policy with the cancel flag. | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5776 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 5777 | ALOGD("Unhandled key event: Asking policy to cancel fallback action.  " | 
|  | 5778 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
|  | 5779 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, | 
|  | 5780 | keyEntry.policyFlags); | 
|  | 5781 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5782 | KeyEvent event = createKeyEvent(keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5783 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5784 |  | 
|  | 5785 | mLock.unlock(); | 
|  | 5786 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5787 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5788 | keyEntry.policyFlags, &event); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5789 |  | 
|  | 5790 | mLock.lock(); | 
|  | 5791 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5792 | // Cancel the fallback key. | 
|  | 5793 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5794 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5795 | "application handled the original non-fallback key " | 
|  | 5796 | "or is no longer a foreground target, " | 
|  | 5797 | "canceling previously dispatched fallback key"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5798 | options.keyCode = fallbackKeyCode; | 
|  | 5799 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5800 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5801 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 5802 | } | 
|  | 5803 | } else { | 
|  | 5804 | // If the application did not handle a non-fallback key, first check | 
|  | 5805 | // that we are in a good state to perform unhandled key event processing | 
|  | 5806 | // Then ask the policy what to do with it. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5807 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5808 | if (fallbackKeyCode == -1 && !initialDown) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5809 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 5810 | ALOGD("Unhandled key event: Skipping unhandled key event processing " | 
|  | 5811 | "since this is not an initial down.  " | 
|  | 5812 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
|  | 5813 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
|  | 5814 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5815 | return false; | 
|  | 5816 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5817 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5818 | // Dispatch the unhandled key to the policy. | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5819 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 5820 | ALOGD("Unhandled key event: Asking policy to perform fallback action.  " | 
|  | 5821 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
|  | 5822 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
|  | 5823 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5824 | KeyEvent event = createKeyEvent(keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5825 |  | 
|  | 5826 | mLock.unlock(); | 
|  | 5827 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5828 | bool fallback = | 
|  | 5829 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5830 | &event, keyEntry.policyFlags, &event); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5831 |  | 
|  | 5832 | mLock.lock(); | 
|  | 5833 |  | 
|  | 5834 | if (connection->status != Connection::STATUS_NORMAL) { | 
|  | 5835 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 5836 | return false; | 
|  | 5837 | } | 
|  | 5838 |  | 
|  | 5839 | // Latch the fallback keycode for this key on an initial down. | 
|  | 5840 | // The fallback keycode cannot change at any other point in the lifecycle. | 
|  | 5841 | if (initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5842 | if (fallback) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5843 | fallbackKeyCode = event.getKeyCode(); | 
|  | 5844 | } else { | 
|  | 5845 | fallbackKeyCode = AKEYCODE_UNKNOWN; | 
|  | 5846 | } | 
|  | 5847 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
|  | 5848 | } | 
|  | 5849 |  | 
|  | 5850 | ALOG_ASSERT(fallbackKeyCode != -1); | 
|  | 5851 |  | 
|  | 5852 | // Cancel the fallback key if the policy decides not to send it anymore. | 
|  | 5853 | // We will continue to dispatch the key to the policy but we will no | 
|  | 5854 | // longer dispatch a fallback key to the application. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5855 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && | 
|  | 5856 | (!fallback || fallbackKeyCode != event.getKeyCode())) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5857 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 5858 | if (fallback) { | 
|  | 5859 | ALOGD("Unhandled key event: Policy requested to send key %d" | 
|  | 5860 | "as a fallback for %d, but on the DOWN it had requested " | 
|  | 5861 | "to send %d instead.  Fallback canceled.", | 
|  | 5862 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); | 
|  | 5863 | } else { | 
|  | 5864 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " | 
|  | 5865 | "but on the DOWN it had requested to send %d.  " | 
|  | 5866 | "Fallback canceled.", | 
|  | 5867 | originalKeyCode, fallbackKeyCode); | 
|  | 5868 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5869 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5870 |  | 
|  | 5871 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
|  | 5872 | "canceling fallback, policy no longer desires it"); | 
|  | 5873 | options.keyCode = fallbackKeyCode; | 
|  | 5874 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
|  | 5875 |  | 
|  | 5876 | fallback = false; | 
|  | 5877 | fallbackKeyCode = AKEYCODE_UNKNOWN; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5878 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5879 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5880 | } | 
|  | 5881 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5882 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5883 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 5884 | { | 
|  | 5885 | std::string msg; | 
|  | 5886 | const KeyedVector<int32_t, int32_t>& fallbackKeys = | 
|  | 5887 | connection->inputState.getFallbackKeys(); | 
|  | 5888 | for (size_t i = 0; i < fallbackKeys.size(); i++) { | 
|  | 5889 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); | 
|  | 5890 | } | 
|  | 5891 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", | 
|  | 5892 | fallbackKeys.size(), msg.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5893 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5894 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5895 |  | 
|  | 5896 | if (fallback) { | 
|  | 5897 | // Restart the dispatch cycle using the fallback key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5898 | keyEntry.eventTime = event.getEventTime(); | 
|  | 5899 | keyEntry.deviceId = event.getDeviceId(); | 
|  | 5900 | keyEntry.source = event.getSource(); | 
|  | 5901 | keyEntry.displayId = event.getDisplayId(); | 
|  | 5902 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; | 
|  | 5903 | keyEntry.keyCode = fallbackKeyCode; | 
|  | 5904 | keyEntry.scanCode = event.getScanCode(); | 
|  | 5905 | keyEntry.metaState = event.getMetaState(); | 
|  | 5906 | keyEntry.repeatCount = event.getRepeatCount(); | 
|  | 5907 | keyEntry.downTime = event.getDownTime(); | 
|  | 5908 | keyEntry.syntheticRepeat = false; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5909 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5910 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 5911 | ALOGD("Unhandled key event: Dispatching fallback key.  " | 
|  | 5912 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", | 
|  | 5913 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); | 
|  | 5914 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5915 | return true; // restart the event | 
|  | 5916 | } else { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5917 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 5918 | ALOGD("Unhandled key event: No fallback key."); | 
|  | 5919 | } | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5920 |  | 
|  | 5921 | // Report the key as unhandled, since there is no fallback key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5922 | mReporter->reportUnhandledKey(keyEntry.id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5923 | } | 
|  | 5924 | } | 
|  | 5925 | return false; | 
|  | 5926 | } | 
|  | 5927 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5928 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5929 | DispatchEntry* dispatchEntry, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5930 | MotionEntry& motionEntry, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5931 | return false; | 
|  | 5932 | } | 
|  | 5933 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5934 | void InputDispatcher::traceInboundQueueLengthLocked() { | 
|  | 5935 | if (ATRACE_ENABLED()) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5936 | ATRACE_INT("iq", mInboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5937 | } | 
|  | 5938 | } | 
|  | 5939 |  | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 5940 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5941 | if (ATRACE_ENABLED()) { | 
|  | 5942 | char counterName[40]; | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 5943 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); | 
|  | 5944 | ATRACE_INT(counterName, connection.outboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5945 | } | 
|  | 5946 | } | 
|  | 5947 |  | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 5948 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5949 | if (ATRACE_ENABLED()) { | 
|  | 5950 | char counterName[40]; | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 5951 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); | 
|  | 5952 | ATRACE_INT(counterName, connection.waitQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5953 | } | 
|  | 5954 | } | 
|  | 5955 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5956 | void InputDispatcher::dump(std::string& dump) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5957 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5958 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5959 | dump += "Input Dispatcher State:\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5960 | dumpDispatchStateLocked(dump); | 
|  | 5961 |  | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5962 | if (!mLastAnrState.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5963 | dump += "\nInput Dispatcher State at time of last ANR:\n"; | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5964 | dump += mLastAnrState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5965 | } | 
|  | 5966 | } | 
|  | 5967 |  | 
|  | 5968 | void InputDispatcher::monitor() { | 
|  | 5969 | // 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] | 5970 | std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5971 | mLooper->wake(); | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5972 | mDispatcherIsAlive.wait(_l); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5973 | } | 
|  | 5974 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5975 | /** | 
|  | 5976 | * Wake up the dispatcher and wait until it processes all events and commands. | 
|  | 5977 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so | 
|  | 5978 | * this method can be safely called from any thread, as long as you've ensured that | 
|  | 5979 | * the work you are interested in completing has already been queued. | 
|  | 5980 | */ | 
|  | 5981 | bool InputDispatcher::waitForIdle() { | 
|  | 5982 | /** | 
|  | 5983 | * Timeout should represent the longest possible time that a device might spend processing | 
|  | 5984 | * events and commands. | 
|  | 5985 | */ | 
|  | 5986 | constexpr std::chrono::duration TIMEOUT = 100ms; | 
|  | 5987 | std::unique_lock lock(mLock); | 
|  | 5988 | mLooper->wake(); | 
|  | 5989 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); | 
|  | 5990 | return result == std::cv_status::no_timeout; | 
|  | 5991 | } | 
|  | 5992 |  | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 5993 | /** | 
|  | 5994 | * Sets focus to the window identified by the token. This must be called | 
|  | 5995 | * after updating any input window handles. | 
|  | 5996 | * | 
|  | 5997 | * Params: | 
|  | 5998 | *  request.token - input channel token used to identify the window that should gain focus. | 
|  | 5999 | *  request.focusedToken - the token that the caller expects currently to be focused. If the | 
|  | 6000 | *  specified token does not match the currently focused window, this request will be dropped. | 
|  | 6001 | *  If the specified focused token matches the currently focused window, the call will succeed. | 
|  | 6002 | *  Set this to "null" if this call should succeed no matter what the currently focused token is. | 
|  | 6003 | *  request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) | 
|  | 6004 | *  when requesting the focus change. This determines which request gets | 
|  | 6005 | *  precedence if there is a focus change request from another source such as pointer down. | 
|  | 6006 | */ | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6007 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { | 
|  | 6008 | { // acquire lock | 
|  | 6009 | std::scoped_lock _l(mLock); | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6010 | std::optional<FocusResolver::FocusChanges> changes = | 
|  | 6011 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); | 
|  | 6012 | if (changes) { | 
|  | 6013 | onFocusChangedLocked(*changes); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6014 | } | 
|  | 6015 | } // release lock | 
|  | 6016 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 6017 | mLooper->wake(); | 
|  | 6018 | } | 
|  | 6019 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6020 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { | 
|  | 6021 | if (changes.oldFocus) { | 
|  | 6022 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6023 | if (focusedInputChannel) { | 
|  | 6024 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
|  | 6025 | "focus left window"); | 
|  | 6026 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6027 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6028 | } | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6029 | } | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6030 | if (changes.newFocus) { | 
|  | 6031 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6032 | } | 
|  | 6033 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6034 | // If a window has pointer capture, then it must have focus. We need to ensure that this | 
|  | 6035 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. | 
|  | 6036 | // If the window loses focus before it loses pointer capture, then the window can be in a state | 
|  | 6037 | // where it has pointer capture but not focus, violating the contract. Therefore we must | 
|  | 6038 | // dispatch the pointer capture event before the focus event. Since focus events are added to | 
|  | 6039 | // the front of the queue (above), we add the pointer capture event to the front of the queue | 
|  | 6040 | // after the focus events are added. This ensures the pointer capture event ends up at the | 
|  | 6041 | // front. | 
|  | 6042 | disablePointerCaptureForcedLocked(); | 
|  | 6043 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6044 | if (mFocusedDisplayId == changes.displayId) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6045 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6046 | } | 
|  | 6047 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6048 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6049 | void InputDispatcher::disablePointerCaptureForcedLocked() { | 
|  | 6050 | if (!mFocusedWindowRequestedPointerCapture && !mWindowTokenWithPointerCapture) { | 
|  | 6051 | return; | 
|  | 6052 | } | 
|  | 6053 |  | 
|  | 6054 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); | 
|  | 6055 |  | 
|  | 6056 | if (mFocusedWindowRequestedPointerCapture) { | 
|  | 6057 | mFocusedWindowRequestedPointerCapture = false; | 
|  | 6058 | setPointerCaptureLocked(false); | 
|  | 6059 | } | 
|  | 6060 |  | 
|  | 6061 | if (!mWindowTokenWithPointerCapture) { | 
|  | 6062 | // No need to send capture changes because no window has capture. | 
|  | 6063 | return; | 
|  | 6064 | } | 
|  | 6065 |  | 
|  | 6066 | if (mPendingEvent != nullptr) { | 
|  | 6067 | // Move the pending event to the front of the queue. This will give the chance | 
|  | 6068 | // for the pending event to be dropped if it is a captured event. | 
|  | 6069 | mInboundQueue.push_front(mPendingEvent); | 
|  | 6070 | mPendingEvent = nullptr; | 
|  | 6071 | } | 
|  | 6072 |  | 
|  | 6073 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), | 
|  | 6074 | false /* hasCapture */); | 
|  | 6075 | mInboundQueue.push_front(std::move(entry)); | 
|  | 6076 | } | 
|  | 6077 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6078 | void InputDispatcher::setPointerCaptureLocked(bool enabled) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6079 | auto command = [this, enabled]() REQUIRES(mLock) { | 
|  | 6080 | scoped_unlock unlock(mLock); | 
|  | 6081 | mPolicy->setPointerCapture(enabled); | 
|  | 6082 | }; | 
|  | 6083 | postCommandLocked(std::move(command)); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6084 | } | 
|  | 6085 |  | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6086 | void InputDispatcher::displayRemoved(int32_t displayId) { | 
|  | 6087 | { // acquire lock | 
|  | 6088 | std::scoped_lock _l(mLock); | 
|  | 6089 | // Set an empty list to remove all handles from the specific display. | 
|  | 6090 | setInputWindowsLocked(/* window handles */ {}, displayId); | 
|  | 6091 | setFocusedApplicationLocked(displayId, nullptr); | 
|  | 6092 | // Call focus resolver to clean up stale requests. This must be called after input windows | 
|  | 6093 | // have been removed for the removed display. | 
|  | 6094 | mFocusResolver.displayRemoved(displayId); | 
|  | 6095 | } // release lock | 
|  | 6096 |  | 
|  | 6097 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 6098 | mLooper->wake(); | 
|  | 6099 | } | 
|  | 6100 |  | 
| chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6101 | void InputDispatcher::onWindowInfosChanged(const std::vector<gui::WindowInfo>& windowInfos) { | 
|  | 6102 | // The listener sends the windows as a flattened array. Separate the windows by display for | 
|  | 6103 | // more convenient parsing. | 
|  | 6104 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; | 
|  | 6105 |  | 
|  | 6106 | for (const auto& info : windowInfos) { | 
|  | 6107 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); | 
|  | 6108 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); | 
|  | 6109 | } | 
|  | 6110 | setInputWindows(handlesPerDisplay); | 
|  | 6111 | } | 
|  | 6112 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6113 | } // namespace android::inputdispatcher |