| 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 |  | 
|  | 22 | // Log detailed debug messages about each inbound event notification to the dispatcher. | 
|  | 23 | #define DEBUG_INBOUND_EVENT_DETAILS 0 | 
|  | 24 |  | 
|  | 25 | // Log detailed debug messages about each outbound event processed by the dispatcher. | 
|  | 26 | #define DEBUG_OUTBOUND_EVENT_DETAILS 0 | 
|  | 27 |  | 
|  | 28 | // Log debug messages about the dispatch cycle. | 
|  | 29 | #define DEBUG_DISPATCH_CYCLE 0 | 
|  | 30 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 31 | // Log debug messages about channel creation | 
|  | 32 | #define DEBUG_CHANNEL_CREATION 0 | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 33 |  | 
|  | 34 | // Log debug messages about input event injection. | 
|  | 35 | #define DEBUG_INJECTION 0 | 
|  | 36 |  | 
|  | 37 | // Log debug messages about input focus tracking. | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 38 | static constexpr bool DEBUG_FOCUS = false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 39 |  | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 40 | // Log debug messages about touch occlusion | 
|  | 41 | // STOPSHIP(b/169067926): Set to false | 
|  | 42 | static constexpr bool DEBUG_TOUCH_OCCLUSION = true; | 
|  | 43 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 44 | // Log debug messages about the app switch latency optimization. | 
|  | 45 | #define DEBUG_APP_SWITCH 0 | 
|  | 46 |  | 
|  | 47 | // Log debug messages about hover events. | 
|  | 48 | #define DEBUG_HOVER 0 | 
|  | 49 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 50 | #include <android-base/chrono_utils.h> | 
| Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 51 | #include <android-base/properties.h> | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 52 | #include <android-base/stringprintf.h> | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 53 | #include <android/os/IInputConstants.h> | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 54 | #include <binder/Binder.h> | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 55 | #include <binder/IServiceManager.h> | 
|  | 56 | #include <com/android/internal/compat/IPlatformCompatNative.h> | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 57 | #include <input/InputDevice.h> | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 58 | #include <input/InputWindow.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 59 | #include <log/log.h> | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 60 | #include <log/log_event_list.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 61 | #include <powermanager/PowerManager.h> | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 62 | #include <statslog.h> | 
|  | 63 | #include <unistd.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 64 | #include <utils/Trace.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 65 |  | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 66 | #include <cerrno> | 
|  | 67 | #include <cinttypes> | 
|  | 68 | #include <climits> | 
|  | 69 | #include <cstddef> | 
|  | 70 | #include <ctime> | 
|  | 71 | #include <queue> | 
|  | 72 | #include <sstream> | 
|  | 73 |  | 
|  | 74 | #include "Connection.h" | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 75 | #include "InputDispatcher.h" | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 76 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 77 | #define INDENT "  " | 
|  | 78 | #define INDENT2 "    " | 
|  | 79 | #define INDENT3 "      " | 
|  | 80 | #define INDENT4 "        " | 
|  | 81 |  | 
| Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 82 | using android::base::HwTimeoutMultiplier; | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 83 | using android::base::Result; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 84 | using android::base::StringPrintf; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 85 | using android::os::BlockUntrustedTouchesMode; | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 86 | using android::os::IInputConstants; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 87 | using android::os::InputEventInjectionResult; | 
|  | 88 | using android::os::InputEventInjectionSync; | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 89 | using com::android::internal::compat::IPlatformCompatNative; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 90 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 91 | namespace android::inputdispatcher { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 |  | 
|  | 93 | // Default input dispatching timeout if there is no focused application or paused window | 
|  | 94 | // from which to determine an appropriate dispatching timeout. | 
| Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 95 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( | 
|  | 96 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * | 
|  | 97 | HwTimeoutMultiplier()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 98 |  | 
|  | 99 | // Amount of time to allow for all pending events to be processed when an app switch | 
|  | 100 | // key is on the way.  This is used to preempt input dispatch and drop input events | 
|  | 101 | // 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] | 102 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 103 |  | 
|  | 104 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) | 
|  | 105 | // before considering it stale and dropping it. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 106 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 108 | // 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] | 109 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec | 
|  | 110 |  | 
|  | 111 | // Log a warning when an interception call takes longer than this to process. | 
|  | 112 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 113 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 114 | // Additional key latency in case a connection is still processing some motion events. | 
|  | 115 | // This will help with the case when a user touched a button that opens a new window, | 
|  | 116 | // and gives us the chance to dispatch the key to this new window. | 
|  | 117 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; | 
|  | 118 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 119 | // Number of recent events to keep for debugging purposes. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 120 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; | 
|  | 121 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 122 | // Event log tags. See EventLogTags.logtags for reference | 
|  | 123 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; | 
|  | 124 | constexpr int LOGTAG_INPUT_FOCUS = 62001; | 
|  | 125 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 126 | static inline nsecs_t now() { | 
|  | 127 | return systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | static inline const char* toString(bool value) { | 
|  | 131 | return value ? "true" : "false"; | 
|  | 132 | } | 
|  | 133 |  | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 134 | static inline const std::string toString(sp<IBinder> binder) { | 
|  | 135 | if (binder == nullptr) { | 
|  | 136 | return "<null>"; | 
|  | 137 | } | 
|  | 138 | return StringPrintf("%p", binder.get()); | 
|  | 139 | } | 
|  | 140 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 141 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 142 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> | 
|  | 143 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
|  | 146 | static bool isValidKeyAction(int32_t action) { | 
|  | 147 | switch (action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 148 | case AKEY_EVENT_ACTION_DOWN: | 
|  | 149 | case AKEY_EVENT_ACTION_UP: | 
|  | 150 | return true; | 
|  | 151 | default: | 
|  | 152 | return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 153 | } | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | static bool validateKeyEvent(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 157 | if (!isValidKeyAction(action)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 158 | ALOGE("Key event has invalid action code 0x%x", action); | 
|  | 159 | return false; | 
|  | 160 | } | 
|  | 161 | return true; | 
|  | 162 | } | 
|  | 163 |  | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 164 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | switch (action & AMOTION_EVENT_ACTION_MASK) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 166 | case AMOTION_EVENT_ACTION_DOWN: | 
|  | 167 | case AMOTION_EVENT_ACTION_UP: | 
|  | 168 | case AMOTION_EVENT_ACTION_CANCEL: | 
|  | 169 | case AMOTION_EVENT_ACTION_MOVE: | 
|  | 170 | case AMOTION_EVENT_ACTION_OUTSIDE: | 
|  | 171 | case AMOTION_EVENT_ACTION_HOVER_ENTER: | 
|  | 172 | case AMOTION_EVENT_ACTION_HOVER_MOVE: | 
|  | 173 | case AMOTION_EVENT_ACTION_HOVER_EXIT: | 
|  | 174 | case AMOTION_EVENT_ACTION_SCROLL: | 
|  | 175 | return true; | 
|  | 176 | case AMOTION_EVENT_ACTION_POINTER_DOWN: | 
|  | 177 | case AMOTION_EVENT_ACTION_POINTER_UP: { | 
|  | 178 | int32_t index = getMotionEventActionPointerIndex(action); | 
|  | 179 | return index >= 0 && index < pointerCount; | 
|  | 180 | } | 
|  | 181 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: | 
|  | 182 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: | 
|  | 183 | return actionButton != 0; | 
|  | 184 | default: | 
|  | 185 | return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 186 | } | 
|  | 187 | } | 
|  | 188 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 189 | static int64_t millis(std::chrono::nanoseconds t) { | 
|  | 190 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); | 
|  | 191 | } | 
|  | 192 |  | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 193 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 194 | const PointerProperties* pointerProperties) { | 
|  | 195 | if (!isValidMotionAction(action, actionButton, pointerCount)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 196 | ALOGE("Motion event has invalid action code 0x%x", action); | 
|  | 197 | return false; | 
|  | 198 | } | 
|  | 199 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { | 
| Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 200 | 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] | 201 | pointerCount, MAX_POINTERS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 202 | return false; | 
|  | 203 | } | 
|  | 204 | BitSet32 pointerIdBits; | 
|  | 205 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 206 | int32_t id = pointerProperties[i].id; | 
|  | 207 | if (id < 0 || id > MAX_POINTER_ID) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 208 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, | 
|  | 209 | MAX_POINTER_ID); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 210 | return false; | 
|  | 211 | } | 
|  | 212 | if (pointerIdBits.hasBit(id)) { | 
|  | 213 | ALOGE("Motion event has duplicate pointer id %d", id); | 
|  | 214 | return false; | 
|  | 215 | } | 
|  | 216 | pointerIdBits.markBit(id); | 
|  | 217 | } | 
|  | 218 | return true; | 
|  | 219 | } | 
|  | 220 |  | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 221 | static std::string dumpRegion(const Region& region) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 222 | if (region.isEmpty()) { | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 223 | return "<empty>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 226 | std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 227 | bool first = true; | 
|  | 228 | Region::const_iterator cur = region.begin(); | 
|  | 229 | Region::const_iterator const tail = region.end(); | 
|  | 230 | while (cur != tail) { | 
|  | 231 | if (first) { | 
|  | 232 | first = false; | 
|  | 233 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 234 | dump += "|"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 235 | } | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 236 | 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] | 237 | cur++; | 
|  | 238 | } | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 239 | return dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 240 | } | 
|  | 241 |  | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 242 | static std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { | 
|  | 243 | constexpr size_t maxEntries = 50; // max events to print | 
|  | 244 | constexpr size_t skipBegin = maxEntries / 2; | 
|  | 245 | const size_t skipEnd = queue.size() - maxEntries / 2; | 
|  | 246 | // skip from maxEntries / 2 ... size() - maxEntries/2 | 
|  | 247 | // only print from 0 .. skipBegin and then from skipEnd .. size() | 
|  | 248 |  | 
|  | 249 | std::string dump; | 
|  | 250 | for (size_t i = 0; i < queue.size(); i++) { | 
|  | 251 | const DispatchEntry& entry = *queue[i]; | 
|  | 252 | if (i >= skipBegin && i < skipEnd) { | 
|  | 253 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); | 
|  | 254 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' | 
|  | 255 | continue; | 
|  | 256 | } | 
|  | 257 | dump.append(INDENT4); | 
|  | 258 | dump += entry.eventEntry->getDescription(); | 
|  | 259 | dump += StringPrintf(", seq=%" PRIu32 | 
|  | 260 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", | 
|  | 261 | entry.seq, entry.targetFlags, entry.resolvedAction, | 
|  | 262 | ns2ms(currentTime - entry.eventEntry->eventTime)); | 
|  | 263 | if (entry.deliveryTime != 0) { | 
|  | 264 | // This entry was delivered, so add information on how long we've been waiting | 
|  | 265 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); | 
|  | 266 | } | 
|  | 267 | dump.append("\n"); | 
|  | 268 | } | 
|  | 269 | return dump; | 
|  | 270 | } | 
|  | 271 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 272 | /** | 
|  | 273 | * Find the entry in std::unordered_map by key, and return it. | 
|  | 274 | * If the entry is not found, return a default constructed entry. | 
|  | 275 | * | 
|  | 276 | * Useful when the entries are vectors, since an empty vector will be returned | 
|  | 277 | * if the entry is not found. | 
|  | 278 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. | 
|  | 279 | */ | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 280 | template <typename K, typename V> | 
|  | 281 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 282 | auto it = map.find(key); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 283 | return it != map.end() ? it->second : V{}; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 284 | } | 
|  | 285 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 286 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { | 
|  | 287 | if (first == second) { | 
|  | 288 | return true; | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | if (first == nullptr || second == nullptr) { | 
|  | 292 | return false; | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | return first->getToken() == second->getToken(); | 
|  | 296 | } | 
|  | 297 |  | 
| Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 298 | static bool haveSameApplicationToken(const InputWindowInfo* first, const InputWindowInfo* second) { | 
|  | 299 | if (first == nullptr || second == nullptr) { | 
|  | 300 | return false; | 
|  | 301 | } | 
|  | 302 | return first->applicationInfo.token != nullptr && | 
|  | 303 | first->applicationInfo.token == second->applicationInfo.token; | 
|  | 304 | } | 
|  | 305 |  | 
| Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 306 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { | 
|  | 307 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; | 
|  | 308 | } | 
|  | 309 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 310 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 311 | std::shared_ptr<EventEntry> eventEntry, | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 312 | int32_t inputTargetFlags) { | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 313 | if (eventEntry->type == EventEntry::Type::MOTION) { | 
|  | 314 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 315 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) == 0) { | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 316 | const ui::Transform identityTransform; | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 317 | // Use identity transform for events that are not pointer events because their axes | 
|  | 318 | // values do not represent on-screen coordinates, so they should not have any window | 
|  | 319 | // transformations applied to them. | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 320 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, identityTransform, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 321 | 1.0f /*globalScaleFactor*/, | 
|  | 322 | inputTarget.displaySize); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 323 | } | 
|  | 324 | } | 
|  | 325 |  | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 326 | if (inputTarget.useDefaultPointerTransform()) { | 
|  | 327 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 328 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 329 | inputTarget.globalScaleFactor, | 
|  | 330 | inputTarget.displaySize); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 331 | } | 
|  | 332 |  | 
|  | 333 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); | 
|  | 334 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
|  | 335 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 336 | std::vector<PointerCoords> pointerCoords; | 
|  | 337 | pointerCoords.resize(motionEntry.pointerCount); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 338 |  | 
|  | 339 | // Use the first pointer information to normalize all other pointers. This could be any pointer | 
|  | 340 | // 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] | 341 | // uses the transform for the normalized pointer. | 
|  | 342 | const ui::Transform& firstPointerTransform = | 
|  | 343 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; | 
|  | 344 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 345 |  | 
|  | 346 | // Iterate through all pointers in the event to normalize against the first. | 
|  | 347 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { | 
|  | 348 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; | 
|  | 349 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 350 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 351 |  | 
|  | 352 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 353 | // First, apply the current pointer's transform to update the coordinates into | 
|  | 354 | // window space. | 
|  | 355 | pointerCoords[pointerIndex].transform(currTransform); | 
|  | 356 | // Next, apply the inverse transform of the normalized coordinates so the | 
|  | 357 | // current coordinates are transformed into the normalized coordinate space. | 
|  | 358 | pointerCoords[pointerIndex].transform(inverseFirstTransform); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 359 | } | 
|  | 360 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 361 | std::unique_ptr<MotionEntry> combinedMotionEntry = | 
|  | 362 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, | 
|  | 363 | motionEntry.deviceId, motionEntry.source, | 
|  | 364 | motionEntry.displayId, motionEntry.policyFlags, | 
|  | 365 | motionEntry.action, motionEntry.actionButton, | 
|  | 366 | motionEntry.flags, motionEntry.metaState, | 
|  | 367 | motionEntry.buttonState, motionEntry.classification, | 
|  | 368 | motionEntry.edgeFlags, motionEntry.xPrecision, | 
|  | 369 | motionEntry.yPrecision, motionEntry.xCursorPosition, | 
|  | 370 | motionEntry.yCursorPosition, motionEntry.downTime, | 
|  | 371 | motionEntry.pointerCount, motionEntry.pointerProperties, | 
|  | 372 | pointerCoords.data(), 0 /* xOffset */, 0 /* yOffset */); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 373 |  | 
|  | 374 | if (motionEntry.injectionState) { | 
|  | 375 | combinedMotionEntry->injectionState = motionEntry.injectionState; | 
|  | 376 | combinedMotionEntry->injectionState->refCount += 1; | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 380 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 381 | firstPointerTransform, inputTarget.globalScaleFactor, | 
|  | 382 | inputTarget.displaySize); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 383 | return dispatchEntry; | 
|  | 384 | } | 
|  | 385 |  | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 386 | static void addGestureMonitors(const std::vector<Monitor>& monitors, | 
|  | 387 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, | 
|  | 388 | float yOffset = 0) { | 
|  | 389 | if (monitors.empty()) { | 
|  | 390 | return; | 
|  | 391 | } | 
|  | 392 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); | 
|  | 393 | for (const Monitor& monitor : monitors) { | 
|  | 394 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); | 
|  | 395 | } | 
|  | 396 | } | 
|  | 397 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 398 | static status_t openInputChannelPair(const std::string& name, | 
|  | 399 | std::shared_ptr<InputChannel>& serverChannel, | 
|  | 400 | std::unique_ptr<InputChannel>& clientChannel) { | 
|  | 401 | std::unique_ptr<InputChannel> uniqueServerChannel; | 
|  | 402 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); | 
|  | 403 |  | 
|  | 404 | serverChannel = std::move(uniqueServerChannel); | 
|  | 405 | return result; | 
|  | 406 | } | 
|  | 407 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 408 | template <typename T> | 
|  | 409 | static bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { | 
|  | 410 | if (lhs == nullptr && rhs == nullptr) { | 
|  | 411 | return true; | 
|  | 412 | } | 
|  | 413 | if (lhs == nullptr || rhs == nullptr) { | 
|  | 414 | return false; | 
|  | 415 | } | 
|  | 416 | return *lhs == *rhs; | 
|  | 417 | } | 
|  | 418 |  | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 419 | static sp<IPlatformCompatNative> getCompatService() { | 
|  | 420 | sp<IBinder> service(defaultServiceManager()->getService(String16("platform_compat_native"))); | 
|  | 421 | if (service == nullptr) { | 
|  | 422 | ALOGE("Failed to link to compat service"); | 
|  | 423 | return nullptr; | 
|  | 424 | } | 
|  | 425 | return interface_cast<IPlatformCompatNative>(service); | 
|  | 426 | } | 
|  | 427 |  | 
| Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 428 | static KeyEvent createKeyEvent(const KeyEntry& entry) { | 
|  | 429 | KeyEvent event; | 
|  | 430 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, | 
|  | 431 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
|  | 432 | entry.repeatCount, entry.downTime, entry.eventTime); | 
|  | 433 | return event; | 
|  | 434 | } | 
|  | 435 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 436 | static std::optional<int32_t> findMonitorPidByToken( | 
|  | 437 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, | 
|  | 438 | const sp<IBinder>& token) { | 
|  | 439 | for (const auto& it : monitorsByDisplay) { | 
|  | 440 | const std::vector<Monitor>& monitors = it.second; | 
|  | 441 | for (const Monitor& monitor : monitors) { | 
|  | 442 | if (monitor.inputChannel->getConnectionToken() == token) { | 
|  | 443 | return monitor.pid; | 
|  | 444 | } | 
|  | 445 | } | 
|  | 446 | } | 
|  | 447 | return std::nullopt; | 
|  | 448 | } | 
|  | 449 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 450 | // --- InputDispatcher --- | 
|  | 451 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 452 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) | 
|  | 453 | : mPolicy(policy), | 
|  | 454 | mPendingEvent(nullptr), | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 455 | mLastDropReason(DropReason::NOT_DROPPED), | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 456 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 457 | mAppSwitchSawKeyDown(false), | 
|  | 458 | mAppSwitchDueTime(LONG_LONG_MAX), | 
|  | 459 | mNextUnblockedEvent(nullptr), | 
|  | 460 | mDispatchEnabled(false), | 
|  | 461 | mDispatchFrozen(false), | 
|  | 462 | mInputFilterEnabled(false), | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 463 | // mInTouchMode will be initialized by the WindowManager to the default device config. | 
|  | 464 | // To avoid leaking stack in case that call never comes, and for tests, | 
|  | 465 | // initialize it here anyways. | 
|  | 466 | mInTouchMode(true), | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 467 | mMaximumObscuringOpacityForTouch(1.0f), | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 468 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 469 | mFocusedWindowRequestedPointerCapture(false), | 
|  | 470 | mWindowTokenWithPointerCapture(nullptr), | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 471 | mCompatService(getCompatService()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 472 | mLooper = new Looper(false); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 473 | mReporter = createInputReporter(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 474 |  | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 475 | mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 476 |  | 
|  | 477 | policy->getDispatcherConfiguration(&mConfig); | 
|  | 478 | } | 
|  | 479 |  | 
|  | 480 | InputDispatcher::~InputDispatcher() { | 
|  | 481 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 482 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 483 |  | 
|  | 484 | resetKeyRepeatLocked(); | 
|  | 485 | releasePendingEventLocked(); | 
|  | 486 | drainInboundQueueLocked(); | 
|  | 487 | } | 
|  | 488 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 489 | while (!mConnectionsByToken.empty()) { | 
|  | 490 | sp<Connection> connection = mConnectionsByToken.begin()->second; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 491 | removeInputChannel(connection->inputChannel->getConnectionToken()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 492 | } | 
|  | 493 | } | 
|  | 494 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 495 | status_t InputDispatcher::start() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 496 | if (mThread) { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 497 | return ALREADY_EXISTS; | 
|  | 498 | } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 499 | mThread = std::make_unique<InputThread>( | 
|  | 500 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); | 
|  | 501 | return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 502 | } | 
|  | 503 |  | 
|  | 504 | status_t InputDispatcher::stop() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 505 | if (mThread && mThread->isCallingThread()) { | 
|  | 506 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 507 | return INVALID_OPERATION; | 
|  | 508 | } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 509 | mThread.reset(); | 
|  | 510 | return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 511 | } | 
|  | 512 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 513 | void InputDispatcher::dispatchOnce() { | 
|  | 514 | nsecs_t nextWakeupTime = LONG_LONG_MAX; | 
|  | 515 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 516 | std::scoped_lock _l(mLock); | 
|  | 517 | mDispatcherIsAlive.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 518 |  | 
|  | 519 | // Run a dispatch loop if there are no pending commands. | 
|  | 520 | // The dispatch loop might enqueue commands to run afterwards. | 
|  | 521 | if (!haveCommandsLocked()) { | 
|  | 522 | dispatchOnceInnerLocked(&nextWakeupTime); | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | // Run all pending commands if there are any. | 
|  | 526 | // If any commands were run then force the next poll to wake up immediately. | 
|  | 527 | if (runCommandsLockedInterruptible()) { | 
|  | 528 | nextWakeupTime = LONG_LONG_MIN; | 
|  | 529 | } | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 530 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 531 | // If we are still waiting for ack on some events, | 
|  | 532 | // we might have to wake up earlier to check if an app is anr'ing. | 
|  | 533 | const nsecs_t nextAnrCheck = processAnrsLocked(); | 
|  | 534 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); | 
|  | 535 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 536 | // We are about to enter an infinitely long sleep, because we have no commands or | 
|  | 537 | // pending or queued events | 
|  | 538 | if (nextWakeupTime == LONG_LONG_MAX) { | 
|  | 539 | mDispatcherEnteredIdle.notify_all(); | 
|  | 540 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 541 | } // release lock | 
|  | 542 |  | 
|  | 543 | // Wait for callback or timeout or wake.  (make sure we round up, not down) | 
|  | 544 | nsecs_t currentTime = now(); | 
|  | 545 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); | 
|  | 546 | mLooper->pollOnce(timeoutMillis); | 
|  | 547 | } | 
|  | 548 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 549 | /** | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 550 | * Raise ANR if there is no focused window. | 
|  | 551 | * Before the ANR is raised, do a final state check: | 
|  | 552 | * 1. The currently focused application must be the same one we are waiting for. | 
|  | 553 | * 2. Ensure we still don't have a focused window. | 
|  | 554 | */ | 
|  | 555 | void InputDispatcher::processNoFocusedWindowAnrLocked() { | 
|  | 556 | // Check if the application that we are waiting for is still focused. | 
|  | 557 | std::shared_ptr<InputApplicationHandle> focusedApplication = | 
|  | 558 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); | 
|  | 559 | if (focusedApplication == nullptr || | 
|  | 560 | focusedApplication->getApplicationToken() != | 
|  | 561 | mAwaitedFocusedApplication->getApplicationToken()) { | 
|  | 562 | // Unexpected because we should have reset the ANR timer when focused application changed | 
|  | 563 | ALOGE("Waited for a focused window, but focused application has already changed to %s", | 
|  | 564 | focusedApplication->getName().c_str()); | 
|  | 565 | return; // The focused application has changed. | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | const sp<InputWindowHandle>& focusedWindowHandle = | 
|  | 569 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); | 
|  | 570 | if (focusedWindowHandle != nullptr) { | 
|  | 571 | return; // We now have a focused window. No need for ANR. | 
|  | 572 | } | 
|  | 573 | onAnrLocked(mAwaitedFocusedApplication); | 
|  | 574 | } | 
|  | 575 |  | 
|  | 576 | /** | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 577 | * Check if any of the connections' wait queues have events that are too old. | 
|  | 578 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. | 
|  | 579 | * Return the time at which we should wake up next. | 
|  | 580 | */ | 
|  | 581 | nsecs_t InputDispatcher::processAnrsLocked() { | 
|  | 582 | const nsecs_t currentTime = now(); | 
|  | 583 | nsecs_t nextAnrCheck = LONG_LONG_MAX; | 
|  | 584 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long | 
|  | 585 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { | 
|  | 586 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 587 | processNoFocusedWindowAnrLocked(); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 588 | mAwaitedFocusedApplication.reset(); | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 589 | mNoFocusedWindowTimeoutTime = std::nullopt; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 590 | return LONG_LONG_MIN; | 
|  | 591 | } else { | 
| Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 592 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 593 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; | 
|  | 594 | } | 
|  | 595 | } | 
|  | 596 |  | 
|  | 597 | // Check if any connection ANRs are due | 
|  | 598 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); | 
|  | 599 | if (currentTime < nextAnrCheck) { // most likely scenario | 
|  | 600 | return nextAnrCheck;          // everything is normal. Let's check again at nextAnrCheck | 
|  | 601 | } | 
|  | 602 |  | 
|  | 603 | // If we reached here, we have an unresponsive connection. | 
|  | 604 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); | 
|  | 605 | if (connection == nullptr) { | 
|  | 606 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); | 
|  | 607 | return nextAnrCheck; | 
|  | 608 | } | 
|  | 609 | connection->responsive = false; | 
|  | 610 | // Stop waking up for this unresponsive connection | 
|  | 611 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 612 | onAnrLocked(connection); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 613 | return LONG_LONG_MIN; | 
|  | 614 | } | 
|  | 615 |  | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 616 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 617 | sp<InputWindowHandle> window = getWindowHandleLocked(token); | 
|  | 618 | if (window != nullptr) { | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 619 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 620 | } | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 621 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 622 | } | 
|  | 623 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 624 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { | 
|  | 625 | nsecs_t currentTime = now(); | 
|  | 626 |  | 
| Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 627 | // Reset the key repeat timer whenever normal dispatch is suspended while the | 
|  | 628 | // device is in a non-interactive state.  This is to ensure that we abort a key | 
|  | 629 | // repeat if the device is just coming out of sleep. | 
|  | 630 | if (!mDispatchEnabled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 631 | resetKeyRepeatLocked(); | 
|  | 632 | } | 
|  | 633 |  | 
|  | 634 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. | 
|  | 635 | if (mDispatchFrozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 636 | if (DEBUG_FOCUS) { | 
|  | 637 | ALOGD("Dispatch frozen.  Waiting some more."); | 
|  | 638 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 639 | return; | 
|  | 640 | } | 
|  | 641 |  | 
|  | 642 | // Optimize latency of app switches. | 
|  | 643 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has | 
|  | 644 | // been pressed.  When it expires, we preempt dispatch and drop all other pending events. | 
|  | 645 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; | 
|  | 646 | if (mAppSwitchDueTime < *nextWakeupTime) { | 
|  | 647 | *nextWakeupTime = mAppSwitchDueTime; | 
|  | 648 | } | 
|  | 649 |  | 
|  | 650 | // Ready to start a new event. | 
|  | 651 | // If we don't already have a pending event, go grab one. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 652 | if (!mPendingEvent) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 653 | if (mInboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 654 | if (isAppSwitchDue) { | 
|  | 655 | // The inbound queue is empty so the app switch key we were waiting | 
|  | 656 | // for will never arrive.  Stop waiting for it. | 
|  | 657 | resetPendingAppSwitchLocked(false); | 
|  | 658 | isAppSwitchDue = false; | 
|  | 659 | } | 
|  | 660 |  | 
|  | 661 | // Synthesize a key repeat if appropriate. | 
|  | 662 | if (mKeyRepeatState.lastKeyEntry) { | 
|  | 663 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { | 
|  | 664 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); | 
|  | 665 | } else { | 
|  | 666 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { | 
|  | 667 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; | 
|  | 668 | } | 
|  | 669 | } | 
|  | 670 | } | 
|  | 671 |  | 
|  | 672 | // Nothing to do if there is no pending event. | 
|  | 673 | if (!mPendingEvent) { | 
|  | 674 | return; | 
|  | 675 | } | 
|  | 676 | } else { | 
|  | 677 | // Inbound queue has at least one entry. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 678 | mPendingEvent = mInboundQueue.front(); | 
|  | 679 | mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 680 | traceInboundQueueLengthLocked(); | 
|  | 681 | } | 
|  | 682 |  | 
|  | 683 | // Poke user activity for this event. | 
|  | 684 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 685 | pokeUserActivityLocked(*mPendingEvent); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 686 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 687 | } | 
|  | 688 |  | 
|  | 689 | // Now we have an event to dispatch. | 
|  | 690 | // 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] | 691 | ALOG_ASSERT(mPendingEvent != nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 692 | bool done = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 693 | DropReason dropReason = DropReason::NOT_DROPPED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 694 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 695 | dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 696 | } else if (!mDispatchEnabled) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 697 | dropReason = DropReason::DISABLED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 698 | } | 
|  | 699 |  | 
|  | 700 | if (mNextUnblockedEvent == mPendingEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 701 | mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 702 | } | 
|  | 703 |  | 
|  | 704 | switch (mPendingEvent->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 705 | case EventEntry::Type::CONFIGURATION_CHANGED: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 706 | const ConfigurationChangedEntry& typedEntry = | 
|  | 707 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 708 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 709 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 710 | break; | 
|  | 711 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 712 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 713 | case EventEntry::Type::DEVICE_RESET: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 714 | const DeviceResetEntry& typedEntry = | 
|  | 715 | static_cast<const DeviceResetEntry&>(*mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 716 | done = dispatchDeviceResetLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 717 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 718 | break; | 
|  | 719 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 720 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 721 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 722 | std::shared_ptr<FocusEntry> typedEntry = | 
|  | 723 | std::static_pointer_cast<FocusEntry>(mPendingEvent); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 724 | dispatchFocusLocked(currentTime, typedEntry); | 
|  | 725 | done = true; | 
|  | 726 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped | 
|  | 727 | break; | 
|  | 728 | } | 
|  | 729 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 730 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
|  | 731 | const auto typedEntry = | 
|  | 732 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); | 
|  | 733 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); | 
|  | 734 | done = true; | 
|  | 735 | break; | 
|  | 736 | } | 
|  | 737 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 738 | case EventEntry::Type::DRAG: { | 
|  | 739 | std::shared_ptr<DragEntry> typedEntry = | 
|  | 740 | std::static_pointer_cast<DragEntry>(mPendingEvent); | 
|  | 741 | dispatchDragLocked(currentTime, typedEntry); | 
|  | 742 | done = true; | 
|  | 743 | break; | 
|  | 744 | } | 
|  | 745 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 746 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 747 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 748 | if (isAppSwitchDue) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 749 | if (isAppSwitchKeyEvent(*keyEntry)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 750 | resetPendingAppSwitchLocked(true); | 
|  | 751 | isAppSwitchDue = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 752 | } else if (dropReason == DropReason::NOT_DROPPED) { | 
|  | 753 | dropReason = DropReason::APP_SWITCH; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 754 | } | 
|  | 755 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 756 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 757 | dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 758 | } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 759 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
|  | 760 | dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 761 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 762 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 763 | break; | 
|  | 764 | } | 
|  | 765 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 766 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 767 | std::shared_ptr<MotionEntry> motionEntry = | 
|  | 768 | std::static_pointer_cast<MotionEntry>(mPendingEvent); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 769 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { | 
|  | 770 | dropReason = DropReason::APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 771 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 772 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 773 | dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 774 | } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 775 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
|  | 776 | dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 777 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 778 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 779 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 781 |  | 
|  | 782 | case EventEntry::Type::SENSOR: { | 
|  | 783 | std::shared_ptr<SensorEntry> sensorEntry = | 
|  | 784 | std::static_pointer_cast<SensorEntry>(mPendingEvent); | 
|  | 785 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { | 
|  | 786 | dropReason = DropReason::APP_SWITCH; | 
|  | 787 | } | 
|  | 788 | //  Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use | 
|  | 789 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. | 
|  | 790 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); | 
|  | 791 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { | 
|  | 792 | dropReason = DropReason::STALE; | 
|  | 793 | } | 
|  | 794 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); | 
|  | 795 | done = true; | 
|  | 796 | break; | 
|  | 797 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 798 | } | 
|  | 799 |  | 
|  | 800 | if (done) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 801 | if (dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 802 | dropInboundEventLocked(*mPendingEvent, dropReason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 803 | } | 
| Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 804 | mLastDropReason = dropReason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 805 |  | 
|  | 806 | releasePendingEventLocked(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 807 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 808 | } | 
|  | 809 | } | 
|  | 810 |  | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 811 | /** | 
|  | 812 | * Return true if the events preceding this incoming motion event should be dropped | 
|  | 813 | * Return false otherwise (the default behaviour) | 
|  | 814 | */ | 
|  | 815 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 816 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 817 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 818 |  | 
|  | 819 | // Optimize case where the current application is unresponsive and the user | 
|  | 820 | // decides to touch a window in a different application. | 
|  | 821 | // If the application takes too long to catch up then we drop all events preceding | 
|  | 822 | // the touch into the other window. | 
|  | 823 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 824 | int32_t displayId = motionEntry.displayId; | 
|  | 825 | int32_t x = static_cast<int32_t>( | 
|  | 826 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 827 | int32_t y = static_cast<int32_t>( | 
|  | 828 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
|  | 829 | sp<InputWindowHandle> touchedWindowHandle = | 
|  | 830 | findTouchedWindowAtLocked(displayId, x, y, nullptr); | 
|  | 831 | if (touchedWindowHandle != nullptr && | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 832 | touchedWindowHandle->getApplicationToken() != | 
|  | 833 | mAwaitedFocusedApplication->getApplicationToken()) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 834 | // User touched a different application than the one we are waiting on. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 835 | ALOGI("Pruning input queue because user touched a different application while waiting " | 
|  | 836 | "for %s", | 
|  | 837 | mAwaitedFocusedApplication->getName().c_str()); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 838 | return true; | 
|  | 839 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 840 |  | 
|  | 841 | // Alternatively, maybe there's a gesture monitor that could handle this event | 
|  | 842 | std::vector<TouchedMonitor> gestureMonitors = | 
|  | 843 | findTouchedGestureMonitorsLocked(displayId, {}); | 
|  | 844 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { | 
|  | 845 | sp<Connection> connection = | 
|  | 846 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 847 | if (connection != nullptr && connection->responsive) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 848 | // This monitor could take more input. Drop all events preceding this | 
|  | 849 | // event, so that gesture monitor could get a chance to receive the stream | 
|  | 850 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " | 
|  | 851 | "responsive gesture monitor that may handle the event", | 
|  | 852 | mAwaitedFocusedApplication->getName().c_str()); | 
|  | 853 | return true; | 
|  | 854 | } | 
|  | 855 | } | 
|  | 856 | } | 
|  | 857 |  | 
|  | 858 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not | 
|  | 859 | // yet been processed by some connections, the dispatcher will wait for these motion | 
|  | 860 | // events to be processed before dispatching the key event. This is because these motion events | 
|  | 861 | // may cause a new window to be launched, which the user might expect to receive focus. | 
|  | 862 | // To prevent waiting forever for such events, just send the key to the currently focused window | 
|  | 863 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { | 
|  | 864 | ALOGD("Received a new pointer down event, stop waiting for events to process and " | 
|  | 865 | "just send the pending key event to the focused window."); | 
|  | 866 | mKeyIsWaitingForEventsTimeout = now(); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 867 | } | 
|  | 868 | return false; | 
|  | 869 | } | 
|  | 870 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 871 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 872 | bool needWake = mInboundQueue.empty(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 873 | mInboundQueue.push_back(std::move(newEntry)); | 
|  | 874 | EventEntry& entry = *(mInboundQueue.back()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 875 | traceInboundQueueLengthLocked(); | 
|  | 876 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 877 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 878 | case EventEntry::Type::KEY: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 879 | // Optimize app switch latency. | 
|  | 880 | // If the application takes too long to catch up then we drop all events preceding | 
|  | 881 | // the app switch key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 882 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 883 | if (isAppSwitchKeyEvent(keyEntry)) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 884 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 885 | mAppSwitchSawKeyDown = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 886 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 887 | if (mAppSwitchSawKeyDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 888 | #if DEBUG_APP_SWITCH | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 889 | ALOGD("App switch is pending!"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 890 | #endif | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 891 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 892 | mAppSwitchSawKeyDown = false; | 
|  | 893 | needWake = true; | 
|  | 894 | } | 
|  | 895 | } | 
|  | 896 | } | 
|  | 897 | break; | 
|  | 898 | } | 
|  | 899 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 900 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 901 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { | 
|  | 902 | mNextUnblockedEvent = mInboundQueue.back(); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 903 | needWake = true; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 904 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 905 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 906 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 907 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 908 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); | 
|  | 909 | break; | 
|  | 910 | } | 
|  | 911 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 912 | case EventEntry::Type::DEVICE_RESET: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 913 | case EventEntry::Type::SENSOR: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 914 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 915 | case EventEntry::Type::DRAG: { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 916 | // nothing to do | 
|  | 917 | break; | 
|  | 918 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 919 | } | 
|  | 920 |  | 
|  | 921 | return needWake; | 
|  | 922 | } | 
|  | 923 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 924 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 925 | // Do not store sensor event in recent queue to avoid flooding the queue. | 
|  | 926 | if (entry->type != EventEntry::Type::SENSOR) { | 
|  | 927 | mRecentQueue.push_back(entry); | 
|  | 928 | } | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 929 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 930 | mRecentQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 931 | } | 
|  | 932 | } | 
|  | 933 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 934 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 935 | int32_t y, TouchState* touchState, | 
|  | 936 | bool addOutsideTargets, | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 937 | bool addPortalWindows, | 
|  | 938 | bool ignoreDragWindow) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 939 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { | 
|  | 940 | LOG_ALWAYS_FATAL( | 
|  | 941 | "Must provide a valid touch state if adding portal windows or outside targets"); | 
|  | 942 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 943 | // Traverse windows from front to back to find touched window. | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 944 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 945 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 946 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 947 | continue; | 
|  | 948 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 949 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
|  | 950 | if (windowInfo->displayId == displayId) { | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 951 | auto flags = windowInfo->flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 952 |  | 
|  | 953 | if (windowInfo->visible) { | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 954 | if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { | 
|  | 955 | bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && | 
|  | 956 | !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 957 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 958 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 959 | if (portalToDisplayId != ADISPLAY_ID_NONE && | 
|  | 960 | portalToDisplayId != displayId) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 961 | if (addPortalWindows) { | 
|  | 962 | // For the monitoring channels of the display. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 963 | touchState->addPortalWindow(windowHandle); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 964 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 965 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 966 | addOutsideTargets, addPortalWindows); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 967 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 968 | // Found window. | 
|  | 969 | return windowHandle; | 
|  | 970 | } | 
|  | 971 | } | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 972 |  | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 973 | if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 974 | touchState->addOrUpdateWindow(windowHandle, | 
|  | 975 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, | 
|  | 976 | BitSet32(0)); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 977 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 978 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 979 | } | 
|  | 980 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 981 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 982 | } | 
|  | 983 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 984 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 985 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 986 | std::vector<TouchedMonitor> touchedMonitors; | 
|  | 987 |  | 
|  | 988 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); | 
|  | 989 | addGestureMonitors(monitors, touchedMonitors); | 
|  | 990 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { | 
|  | 991 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); | 
|  | 992 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 993 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, | 
|  | 994 | -windowInfo->frameTop); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 995 | } | 
|  | 996 | return touchedMonitors; | 
|  | 997 | } | 
|  | 998 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 999 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1000 | const char* reason; | 
|  | 1001 | switch (dropReason) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1002 | case DropReason::POLICY: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1003 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1004 | ALOGD("Dropped event because policy consumed it."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1005 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1006 | reason = "inbound event was dropped because the policy consumed it"; | 
|  | 1007 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1008 | case DropReason::DISABLED: | 
|  | 1009 | if (mLastDropReason != DropReason::DISABLED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1010 | ALOGI("Dropped event because input dispatch is disabled."); | 
|  | 1011 | } | 
|  | 1012 | reason = "inbound event was dropped because input dispatch is disabled"; | 
|  | 1013 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1014 | case DropReason::APP_SWITCH: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1015 | ALOGI("Dropped event because of pending overdue app switch."); | 
|  | 1016 | reason = "inbound event was dropped because of pending overdue app switch"; | 
|  | 1017 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1018 | case DropReason::BLOCKED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1019 | ALOGI("Dropped event because the current application is not responding and the user " | 
|  | 1020 | "has started interacting with a different application."); | 
|  | 1021 | reason = "inbound event was dropped because the current application is not responding " | 
|  | 1022 | "and the user has started interacting with a different application"; | 
|  | 1023 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1024 | case DropReason::STALE: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1025 | ALOGI("Dropped event because it is stale."); | 
|  | 1026 | reason = "inbound event was dropped because it is stale"; | 
|  | 1027 | break; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1028 | case DropReason::NO_POINTER_CAPTURE: | 
|  | 1029 | ALOGI("Dropped event because there is no window with Pointer Capture."); | 
|  | 1030 | reason = "inbound event was dropped because there is no window with Pointer Capture"; | 
|  | 1031 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1032 | case DropReason::NOT_DROPPED: { | 
|  | 1033 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1034 | return; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1035 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1036 | } | 
|  | 1037 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1038 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1039 | case EventEntry::Type::KEY: { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1040 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
|  | 1041 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1042 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1043 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1044 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1045 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 1046 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1047 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); | 
|  | 1048 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1049 | } else { | 
|  | 1050 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
|  | 1051 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1052 | } | 
|  | 1053 | break; | 
|  | 1054 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1055 | case EventEntry::Type::SENSOR: { | 
|  | 1056 | break; | 
|  | 1057 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1058 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 1059 | case EventEntry::Type::DRAG: { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1060 | break; | 
|  | 1061 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1062 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1063 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 1064 | case EventEntry::Type::DEVICE_RESET: { | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1065 | 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] | 1066 | break; | 
|  | 1067 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1068 | } | 
|  | 1069 | } | 
|  | 1070 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1071 | static bool isAppSwitchKeyCode(int32_t keyCode) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1072 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || | 
|  | 1073 | keyCode == AKEYCODE_APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1074 | } | 
|  | 1075 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1076 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { | 
|  | 1077 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && | 
|  | 1078 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && | 
|  | 1079 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1080 | } | 
|  | 1081 |  | 
|  | 1082 | bool InputDispatcher::isAppSwitchPendingLocked() { | 
|  | 1083 | return mAppSwitchDueTime != LONG_LONG_MAX; | 
|  | 1084 | } | 
|  | 1085 |  | 
|  | 1086 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { | 
|  | 1087 | mAppSwitchDueTime = LONG_LONG_MAX; | 
|  | 1088 |  | 
|  | 1089 | #if DEBUG_APP_SWITCH | 
|  | 1090 | if (handled) { | 
|  | 1091 | ALOGD("App switch has arrived."); | 
|  | 1092 | } else { | 
|  | 1093 | ALOGD("App switch was abandoned."); | 
|  | 1094 | } | 
|  | 1095 | #endif | 
|  | 1096 | } | 
|  | 1097 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1098 | bool InputDispatcher::haveCommandsLocked() const { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1099 | return !mCommandQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1100 | } | 
|  | 1101 |  | 
|  | 1102 | bool InputDispatcher::runCommandsLockedInterruptible() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1103 | if (mCommandQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1104 | return false; | 
|  | 1105 | } | 
|  | 1106 |  | 
|  | 1107 | do { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1108 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1109 | mCommandQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1110 | Command command = commandEntry->command; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1111 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1112 |  | 
|  | 1113 | commandEntry->connection.clear(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1114 | } while (!mCommandQueue.empty()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1115 | return true; | 
|  | 1116 | } | 
|  | 1117 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1118 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { | 
|  | 1119 | mCommandQueue.push_back(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1120 | } | 
|  | 1121 |  | 
|  | 1122 | void InputDispatcher::drainInboundQueueLocked() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1123 | while (!mInboundQueue.empty()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1124 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1125 | mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1126 | releaseInboundEventLocked(entry); | 
|  | 1127 | } | 
|  | 1128 | traceInboundQueueLengthLocked(); | 
|  | 1129 | } | 
|  | 1130 |  | 
|  | 1131 | void InputDispatcher::releasePendingEventLocked() { | 
|  | 1132 | if (mPendingEvent) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1133 | releaseInboundEventLocked(mPendingEvent); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1134 | mPendingEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1135 | } | 
|  | 1136 | } | 
|  | 1137 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1138 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1139 | InjectionState* injectionState = entry->injectionState; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1140 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1141 | #if DEBUG_DISPATCH_CYCLE | 
|  | 1142 | ALOGD("Injected inbound event was dropped."); | 
|  | 1143 | #endif | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1144 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1145 | } | 
|  | 1146 | if (entry == mNextUnblockedEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1147 | mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1148 | } | 
|  | 1149 | addRecentEventLocked(entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1150 | } | 
|  | 1151 |  | 
|  | 1152 | void InputDispatcher::resetKeyRepeatLocked() { | 
|  | 1153 | if (mKeyRepeatState.lastKeyEntry) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1154 | mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1155 | } | 
|  | 1156 | } | 
|  | 1157 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1158 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { | 
|  | 1159 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1160 |  | 
| Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1161 | uint32_t policyFlags = entry->policyFlags & | 
|  | 1162 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1163 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1164 | std::shared_ptr<KeyEntry> newEntry = | 
|  | 1165 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, | 
|  | 1166 | entry->source, entry->displayId, policyFlags, entry->action, | 
|  | 1167 | entry->flags, entry->keyCode, entry->scanCode, | 
|  | 1168 | entry->metaState, entry->repeatCount + 1, entry->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1169 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1170 | newEntry->syntheticRepeat = true; | 
|  | 1171 | mKeyRepeatState.lastKeyEntry = newEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1172 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1173 | return newEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1174 | } | 
|  | 1175 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1176 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1177 | const ConfigurationChangedEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1178 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1179 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1180 | #endif | 
|  | 1181 |  | 
|  | 1182 | // Reset key repeating in case a keyboard device was added or removed or something. | 
|  | 1183 | resetKeyRepeatLocked(); | 
|  | 1184 |  | 
|  | 1185 | // Enqueue a command to run outside the lock to tell the policy that the configuration changed. | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1186 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 1187 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1188 | commandEntry->eventTime = entry.eventTime; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1189 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1190 | return true; | 
|  | 1191 | } | 
|  | 1192 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1193 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, | 
|  | 1194 | const DeviceResetEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1195 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1196 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, | 
|  | 1197 | entry.deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1198 | #endif | 
|  | 1199 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1200 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1201 | options.deviceId = entry.deviceId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1202 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1203 | return true; | 
|  | 1204 | } | 
|  | 1205 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1206 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1207 | const std::string& reason) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1208 | if (mPendingEvent != nullptr) { | 
|  | 1209 | // Move the pending event to the front of the queue. This will give the chance | 
|  | 1210 | // for the pending event to get dispatched to the newly focused window | 
|  | 1211 | mInboundQueue.push_front(mPendingEvent); | 
|  | 1212 | mPendingEvent = nullptr; | 
|  | 1213 | } | 
|  | 1214 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1215 | std::unique_ptr<FocusEntry> focusEntry = | 
|  | 1216 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, | 
|  | 1217 | reason); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1218 |  | 
|  | 1219 | // This event should go to the front of the queue, but behind all other focus events | 
|  | 1220 | // Find the last focus event, and insert right after it | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1221 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1222 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1223 | [](const std::shared_ptr<EventEntry>& event) { | 
|  | 1224 | return event->type == EventEntry::Type::FOCUS; | 
|  | 1225 | }); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1226 |  | 
|  | 1227 | // 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] | 1228 | mInboundQueue.insert(it.base(), std::move(focusEntry)); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1229 | } | 
|  | 1230 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1231 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1232 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1233 | if (channel == nullptr) { | 
|  | 1234 | return; // Window has gone away | 
|  | 1235 | } | 
|  | 1236 | InputTarget target; | 
|  | 1237 | target.inputChannel = channel; | 
|  | 1238 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1239 | entry->dispatchInProgress = true; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1240 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + | 
|  | 1241 | channel->getName(); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1242 | std::string reason = std::string("reason=").append(entry->reason); | 
|  | 1243 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1244 | dispatchEventLocked(currentTime, entry, {target}); | 
|  | 1245 | } | 
|  | 1246 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1247 | void InputDispatcher::dispatchPointerCaptureChangedLocked( | 
|  | 1248 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, | 
|  | 1249 | DropReason& dropReason) { | 
|  | 1250 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 1251 | if (entry->pointerCaptureEnabled && haveWindowWithPointerCapture) { | 
|  | 1252 | LOG_ALWAYS_FATAL("Pointer Capture has already been enabled for the window."); | 
|  | 1253 | } | 
|  | 1254 | if (!entry->pointerCaptureEnabled && !haveWindowWithPointerCapture) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1255 | // Pointer capture was already forcefully disabled because of focus change. | 
|  | 1256 | dropReason = DropReason::NOT_DROPPED; | 
|  | 1257 | return; | 
|  | 1258 | } | 
|  | 1259 |  | 
|  | 1260 | // Set drop reason for early returns | 
|  | 1261 | dropReason = DropReason::NO_POINTER_CAPTURE; | 
|  | 1262 |  | 
|  | 1263 | sp<IBinder> token; | 
|  | 1264 | if (entry->pointerCaptureEnabled) { | 
|  | 1265 | // Enable Pointer Capture | 
|  | 1266 | if (!mFocusedWindowRequestedPointerCapture) { | 
|  | 1267 | // This can happen if a window requests capture and immediately releases capture. | 
|  | 1268 | ALOGW("No window requested Pointer Capture."); | 
|  | 1269 | return; | 
|  | 1270 | } | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1271 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1272 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); | 
|  | 1273 | mWindowTokenWithPointerCapture = token; | 
|  | 1274 | } else { | 
|  | 1275 | // Disable Pointer Capture | 
|  | 1276 | token = mWindowTokenWithPointerCapture; | 
|  | 1277 | mWindowTokenWithPointerCapture = nullptr; | 
| Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1278 | if (mFocusedWindowRequestedPointerCapture) { | 
|  | 1279 | mFocusedWindowRequestedPointerCapture = false; | 
|  | 1280 | setPointerCaptureLocked(false); | 
|  | 1281 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1282 | } | 
|  | 1283 |  | 
|  | 1284 | auto channel = getInputChannelLocked(token); | 
|  | 1285 | if (channel == nullptr) { | 
|  | 1286 | // Window has gone away, clean up Pointer Capture state. | 
|  | 1287 | mWindowTokenWithPointerCapture = nullptr; | 
| Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1288 | if (mFocusedWindowRequestedPointerCapture) { | 
|  | 1289 | mFocusedWindowRequestedPointerCapture = false; | 
|  | 1290 | setPointerCaptureLocked(false); | 
|  | 1291 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1292 | return; | 
|  | 1293 | } | 
|  | 1294 | InputTarget target; | 
|  | 1295 | target.inputChannel = channel; | 
|  | 1296 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1297 | entry->dispatchInProgress = true; | 
|  | 1298 | dispatchEventLocked(currentTime, entry, {target}); | 
|  | 1299 |  | 
|  | 1300 | dropReason = DropReason::NOT_DROPPED; | 
|  | 1301 | } | 
|  | 1302 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1303 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1304 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1305 | // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1306 | if (!entry->dispatchInProgress) { | 
|  | 1307 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && | 
|  | 1308 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && | 
|  | 1309 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { | 
|  | 1310 | if (mKeyRepeatState.lastKeyEntry && | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1311 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1312 | // We have seen two identical key downs in a row which indicates that the device | 
|  | 1313 | // driver is automatically generating key repeats itself.  We take note of the | 
|  | 1314 | // repeat here, but we disable our own next key repeat timer since it is clear that | 
|  | 1315 | // we will not need to synthesize key repeats ourselves. | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1316 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { | 
|  | 1317 | // Make sure we don't get key down from a different device. If a different | 
|  | 1318 | // device Id has same key pressed down, the new device Id will replace the | 
|  | 1319 | // current one to hold the key repeat with repeat count reset. | 
|  | 1320 | // In the future when got a KEY_UP on the device id, drop it and do not | 
|  | 1321 | // stop the key repeat on current device. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1322 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; | 
|  | 1323 | resetKeyRepeatLocked(); | 
|  | 1324 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves | 
|  | 1325 | } else { | 
|  | 1326 | // Not a repeat.  Save key down state in case we do see a repeat later. | 
|  | 1327 | resetKeyRepeatLocked(); | 
|  | 1328 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; | 
|  | 1329 | } | 
|  | 1330 | mKeyRepeatState.lastKeyEntry = entry; | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1331 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && | 
|  | 1332 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1333 | // The key on device 'deviceId' is still down, do not stop key repeat | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1334 | #if DEBUG_INBOUND_EVENT_DETAILS | 
|  | 1335 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); | 
|  | 1336 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1337 | } else if (!entry->syntheticRepeat) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1338 | resetKeyRepeatLocked(); | 
|  | 1339 | } | 
|  | 1340 |  | 
|  | 1341 | if (entry->repeatCount == 1) { | 
|  | 1342 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; | 
|  | 1343 | } else { | 
|  | 1344 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; | 
|  | 1345 | } | 
|  | 1346 |  | 
|  | 1347 | entry->dispatchInProgress = true; | 
|  | 1348 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1349 | logOutboundKeyDetails("dispatchKey - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1350 | } | 
|  | 1351 |  | 
|  | 1352 | // Handle case where the policy asked us to try again later last time. | 
|  | 1353 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { | 
|  | 1354 | if (currentTime < entry->interceptKeyWakeupTime) { | 
|  | 1355 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { | 
|  | 1356 | *nextWakeupTime = entry->interceptKeyWakeupTime; | 
|  | 1357 | } | 
|  | 1358 | return false; // wait until next wakeup | 
|  | 1359 | } | 
|  | 1360 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; | 
|  | 1361 | entry->interceptKeyWakeupTime = 0; | 
|  | 1362 | } | 
|  | 1363 |  | 
|  | 1364 | // Give the policy a chance to intercept the key. | 
|  | 1365 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { | 
|  | 1366 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1367 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
| Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1368 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1369 | sp<IBinder> focusedWindowToken = | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1370 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 1371 | commandEntry->connectionToken = focusedWindowToken; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1372 | commandEntry->keyEntry = entry; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1373 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1374 | return false; // wait for the command to run | 
|  | 1375 | } else { | 
|  | 1376 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
|  | 1377 | } | 
|  | 1378 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1379 | if (*dropReason == DropReason::NOT_DROPPED) { | 
|  | 1380 | *dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1381 | } | 
|  | 1382 | } | 
|  | 1383 |  | 
|  | 1384 | // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1385 | if (*dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1386 | setInjectionResult(*entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1387 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED | 
|  | 1388 | : InputEventInjectionResult::FAILED); | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1389 | mReporter->reportDroppedKey(entry->id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1390 | return true; | 
|  | 1391 | } | 
|  | 1392 |  | 
|  | 1393 | // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1394 | std::vector<InputTarget> inputTargets; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1395 | InputEventInjectionResult injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1396 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1397 | if (injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1398 | return false; | 
|  | 1399 | } | 
|  | 1400 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1401 | setInjectionResult(*entry, injectionResult); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1402 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1403 | return true; | 
|  | 1404 | } | 
|  | 1405 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1406 | // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1407 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1408 |  | 
|  | 1409 | // Dispatch the key. | 
|  | 1410 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1411 | return true; | 
|  | 1412 | } | 
|  | 1413 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1414 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1415 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1416 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1417 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " | 
|  | 1418 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1419 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, | 
|  | 1420 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
|  | 1421 | entry.repeatCount, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1422 | #endif | 
|  | 1423 | } | 
|  | 1424 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1425 | void InputDispatcher::doNotifySensorLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 1426 | mLock.unlock(); | 
|  | 1427 |  | 
|  | 1428 | const std::shared_ptr<SensorEntry>& entry = commandEntry->sensorEntry; | 
|  | 1429 | if (entry->accuracyChanged) { | 
|  | 1430 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); | 
|  | 1431 | } | 
|  | 1432 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, | 
|  | 1433 | entry->hwTimestamp, entry->values); | 
|  | 1434 | mLock.lock(); | 
|  | 1435 | } | 
|  | 1436 |  | 
|  | 1437 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, std::shared_ptr<SensorEntry> entry, | 
|  | 1438 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
|  | 1439 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 1440 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " | 
|  | 1441 | "source=0x%x, sensorType=%s", | 
|  | 1442 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, | 
| Prabir Pradhan | be05b5b | 2021-02-24 16:39:43 -0800 | [diff] [blame] | 1443 | NamedEnum::string(entry->sensorType).c_str()); | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1444 | #endif | 
|  | 1445 | std::unique_ptr<CommandEntry> commandEntry = | 
|  | 1446 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifySensorLockedInterruptible); | 
|  | 1447 | commandEntry->sensorEntry = entry; | 
|  | 1448 | postCommandLocked(std::move(commandEntry)); | 
|  | 1449 | } | 
|  | 1450 |  | 
|  | 1451 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { | 
|  | 1452 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 1453 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, | 
|  | 1454 | NamedEnum::string(sensorType).c_str()); | 
|  | 1455 | #endif | 
|  | 1456 | { // acquire lock | 
|  | 1457 | std::scoped_lock _l(mLock); | 
|  | 1458 |  | 
|  | 1459 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { | 
|  | 1460 | std::shared_ptr<EventEntry> entry = *it; | 
|  | 1461 | if (entry->type == EventEntry::Type::SENSOR) { | 
|  | 1462 | it = mInboundQueue.erase(it); | 
|  | 1463 | releaseInboundEventLocked(entry); | 
|  | 1464 | } | 
|  | 1465 | } | 
|  | 1466 | } | 
|  | 1467 | return true; | 
|  | 1468 | } | 
|  | 1469 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1470 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1471 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1472 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1473 | // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1474 | if (!entry->dispatchInProgress) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1475 | entry->dispatchInProgress = true; | 
|  | 1476 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1477 | logOutboundMotionDetails("dispatchMotion - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1478 | } | 
|  | 1479 |  | 
|  | 1480 | // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1481 | if (*dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1482 | setInjectionResult(*entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1483 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED | 
|  | 1484 | : InputEventInjectionResult::FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1485 | return true; | 
|  | 1486 | } | 
|  | 1487 |  | 
|  | 1488 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; | 
|  | 1489 |  | 
|  | 1490 | // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1491 | std::vector<InputTarget> inputTargets; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1492 |  | 
|  | 1493 | bool conflictingPointerActions = false; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1494 | InputEventInjectionResult injectionResult; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1495 | if (isPointerEvent) { | 
|  | 1496 | // Pointer event.  (eg. touchscreen) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1497 | injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1498 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1499 | &conflictingPointerActions); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1500 | } else { | 
|  | 1501 | // Non touch event.  (eg. trackball) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1502 | injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1503 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1504 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1505 | if (injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1506 | return false; | 
|  | 1507 | } | 
|  | 1508 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1509 | setInjectionResult(*entry, injectionResult); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1510 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1511 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); | 
|  | 1512 | return true; | 
|  | 1513 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1514 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1515 | CancelationOptions::Mode mode(isPointerEvent | 
|  | 1516 | ? CancelationOptions::CANCEL_POINTER_EVENTS | 
|  | 1517 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); | 
|  | 1518 | CancelationOptions options(mode, "input event injection failed"); | 
|  | 1519 | synthesizeCancelationEventsForMonitorsLocked(options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1520 | return true; | 
|  | 1521 | } | 
|  | 1522 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1523 | // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1524 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1525 |  | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1526 | if (isPointerEvent) { | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1527 | std::unordered_map<int32_t, TouchState>::iterator it = | 
|  | 1528 | mTouchStatesByDisplay.find(entry->displayId); | 
|  | 1529 | if (it != mTouchStatesByDisplay.end()) { | 
|  | 1530 | const TouchState& state = it->second; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1531 | if (!state.portalWindows.empty()) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1532 | // The event has gone through these portal windows, so we add monitoring targets of | 
|  | 1533 | // the corresponding displays as well. | 
|  | 1534 | for (size_t i = 0; i < state.portalWindows.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1535 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1536 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1537 | -windowInfo->frameLeft, -windowInfo->frameTop); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1538 | } | 
|  | 1539 | } | 
|  | 1540 | } | 
|  | 1541 | } | 
|  | 1542 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1543 | // Dispatch the motion. | 
|  | 1544 | if (conflictingPointerActions) { | 
|  | 1545 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1546 | "conflicting pointer actions"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1547 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1548 | } | 
|  | 1549 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1550 | return true; | 
|  | 1551 | } | 
|  | 1552 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1553 | void InputDispatcher::enqueueDragEventLocked(const sp<InputWindowHandle>& windowHandle, | 
|  | 1554 | bool isExiting, const MotionEntry& motionEntry) { | 
|  | 1555 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should | 
|  | 1556 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. | 
|  | 1557 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); | 
|  | 1558 | PointerCoords pointerCoords; | 
|  | 1559 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); | 
|  | 1560 | pointerCoords.transform(windowHandle->getInfo()->transform); | 
|  | 1561 |  | 
|  | 1562 | std::unique_ptr<DragEntry> dragEntry = | 
|  | 1563 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, | 
|  | 1564 | windowHandle->getToken(), isExiting, pointerCoords.getX(), | 
|  | 1565 | pointerCoords.getY()); | 
|  | 1566 |  | 
|  | 1567 | enqueueInboundEventLocked(std::move(dragEntry)); | 
|  | 1568 | } | 
|  | 1569 |  | 
|  | 1570 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { | 
|  | 1571 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); | 
|  | 1572 | if (channel == nullptr) { | 
|  | 1573 | return; // Window has gone away | 
|  | 1574 | } | 
|  | 1575 | InputTarget target; | 
|  | 1576 | target.inputChannel = channel; | 
|  | 1577 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1578 | entry->dispatchInProgress = true; | 
|  | 1579 | dispatchEventLocked(currentTime, entry, {target}); | 
|  | 1580 | } | 
|  | 1581 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1582 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1583 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1584 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1585 | ", policyFlags=0x%x, " | 
|  | 1586 | "action=0x%x, actionButton=0x%x, flags=0x%x, " | 
|  | 1587 | "metaState=0x%x, buttonState=0x%x," | 
|  | 1588 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1589 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, | 
|  | 1590 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, | 
|  | 1591 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1592 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1593 | for (uint32_t i = 0; i < entry.pointerCount; i++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1594 | ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1595 | "x=%f, y=%f, pressure=%f, size=%f, " | 
|  | 1596 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
|  | 1597 | "orientation=%f", | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1598 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, | 
|  | 1599 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 1600 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 1601 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 1602 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
|  | 1603 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 1604 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 1605 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 1606 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 1607 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1608 | } | 
|  | 1609 | #endif | 
|  | 1610 | } | 
|  | 1611 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1612 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, | 
|  | 1613 | std::shared_ptr<EventEntry> eventEntry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1614 | const std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1615 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1616 | #if DEBUG_DISPATCH_CYCLE | 
|  | 1617 | ALOGD("dispatchEventToCurrentInputTargets"); | 
|  | 1618 | #endif | 
|  | 1619 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1620 | updateInteractionTokensLocked(*eventEntry, inputTargets); | 
|  | 1621 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1622 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true | 
|  | 1623 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1624 | pokeUserActivityLocked(*eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1625 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1626 | for (const InputTarget& inputTarget : inputTargets) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1627 | sp<Connection> connection = | 
|  | 1628 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1629 | if (connection != nullptr) { | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1630 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1631 | } else { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1632 | if (DEBUG_FOCUS) { | 
|  | 1633 | ALOGD("Dropping event delivery to target with channel '%s' because it " | 
|  | 1634 | "is no longer registered with the input dispatcher.", | 
|  | 1635 | inputTarget.inputChannel->getName().c_str()); | 
|  | 1636 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1637 | } | 
|  | 1638 | } | 
|  | 1639 | } | 
|  | 1640 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1641 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { | 
|  | 1642 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. | 
|  | 1643 | // If the policy decides to close the app, we will get a channel removal event via | 
|  | 1644 | // unregisterInputChannel, and will clean up the connection that way. We are already not | 
|  | 1645 | // sending new pointers to the connection when it blocked, but focused events will continue to | 
|  | 1646 | // pile up. | 
|  | 1647 | ALOGW("Canceling events for %s because it is unresponsive", | 
|  | 1648 | connection->inputChannel->getName().c_str()); | 
|  | 1649 | if (connection->status == Connection::STATUS_NORMAL) { | 
|  | 1650 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, | 
|  | 1651 | "application not responding"); | 
|  | 1652 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1653 | } | 
|  | 1654 | } | 
|  | 1655 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1656 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1657 | if (DEBUG_FOCUS) { | 
|  | 1658 | ALOGD("Resetting ANR timeouts."); | 
|  | 1659 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1660 |  | 
|  | 1661 | // Reset input target wait timeout. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1662 | mNoFocusedWindowTimeoutTime = std::nullopt; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1663 | mAwaitedFocusedApplication.reset(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1664 | } | 
|  | 1665 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1666 | /** | 
|  | 1667 | * Get the display id that the given event should go to. If this event specifies a valid display id, | 
|  | 1668 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. | 
|  | 1669 | * Focused display is the display that the user most recently interacted with. | 
|  | 1670 | */ | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1671 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1672 | int32_t displayId; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1673 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1674 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1675 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
|  | 1676 | displayId = keyEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1677 | break; | 
|  | 1678 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1679 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1680 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 1681 | displayId = motionEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1682 | break; | 
|  | 1683 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1684 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1685 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1686 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1687 | case EventEntry::Type::DEVICE_RESET: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1688 | case EventEntry::Type::SENSOR: | 
|  | 1689 | case EventEntry::Type::DRAG: { | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1690 | 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] | 1691 | return ADISPLAY_ID_NONE; | 
|  | 1692 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1693 | } | 
|  | 1694 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; | 
|  | 1695 | } | 
|  | 1696 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1697 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, | 
|  | 1698 | const char* focusedWindowName) { | 
|  | 1699 | if (mAnrTracker.empty()) { | 
|  | 1700 | // already processed all events that we waited for | 
|  | 1701 | mKeyIsWaitingForEventsTimeout = std::nullopt; | 
|  | 1702 | return false; | 
|  | 1703 | } | 
|  | 1704 |  | 
|  | 1705 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { | 
|  | 1706 | // Start the timer | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1707 | // 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] | 1708 | mKeyIsWaitingForEventsTimeout = currentTime + | 
|  | 1709 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) | 
|  | 1710 | .count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1711 | return true; | 
|  | 1712 | } | 
|  | 1713 |  | 
|  | 1714 | // We still have pending events, and already started the timer | 
|  | 1715 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { | 
|  | 1716 | return true; // Still waiting | 
|  | 1717 | } | 
|  | 1718 |  | 
|  | 1719 | // Waited too long, and some connection still hasn't processed all motions | 
|  | 1720 | // Just send the key to the focused window | 
|  | 1721 | ALOGW("Dispatching key to %s even though there are other unprocessed events", | 
|  | 1722 | focusedWindowName); | 
|  | 1723 | mKeyIsWaitingForEventsTimeout = std::nullopt; | 
|  | 1724 | return false; | 
|  | 1725 | } | 
|  | 1726 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1727 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( | 
|  | 1728 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, | 
|  | 1729 | nsecs_t* nextWakeupTime) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1730 | std::string reason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1731 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1732 | int32_t displayId = getTargetDisplayId(entry); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1733 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1734 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1735 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
|  | 1736 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1737 | // If there is no currently focused window and no focused application | 
|  | 1738 | // then drop the event. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1739 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { | 
|  | 1740 | ALOGI("Dropping %s event because there is no focused window or focused application in " | 
|  | 1741 | "display %" PRId32 ".", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1742 | NamedEnum::string(entry.type).c_str(), displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1743 | return InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1744 | } | 
|  | 1745 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1746 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. | 
|  | 1747 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we | 
|  | 1748 | // start interacting with another application via touch (app switch). This code can be removed | 
|  | 1749 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether | 
|  | 1750 | // an app is expected to have a focused window. | 
|  | 1751 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { | 
|  | 1752 | if (!mNoFocusedWindowTimeoutTime.has_value()) { | 
|  | 1753 | // 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] | 1754 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( | 
|  | 1755 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
|  | 1756 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1757 | mAwaitedFocusedApplication = focusedApplicationHandle; | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1758 | mAwaitedApplicationDisplayId = displayId; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1759 | ALOGW("Waiting because no window has focus but %s may eventually add a " | 
|  | 1760 | "window when it finishes starting up. Will wait for %" PRId64 "ms", | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1761 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1762 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1763 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1764 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { | 
|  | 1765 | // Already raised ANR. Drop the event | 
|  | 1766 | ALOGE("Dropping %s event because there is no focused window", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1767 | NamedEnum::string(entry.type).c_str()); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1768 | return InputEventInjectionResult::FAILED; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1769 | } else { | 
|  | 1770 | // Still waiting for the focused window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1771 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1772 | } | 
|  | 1773 | } | 
|  | 1774 |  | 
|  | 1775 | // we have a valid, non-null focused window | 
|  | 1776 | resetNoFocusedWindowTimeoutLocked(); | 
|  | 1777 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1778 | // Check permissions. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1779 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1780 | return InputEventInjectionResult::PERMISSION_DENIED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1781 | } | 
|  | 1782 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1783 | if (focusedWindowHandle->getInfo()->paused) { | 
|  | 1784 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1785 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1786 | } | 
|  | 1787 |  | 
|  | 1788 | // If the event is a key event, then we must wait for all previous events to | 
|  | 1789 | // complete before delivering it because previous events may have the | 
|  | 1790 | // side-effect of transferring focus to a different window and we want to | 
|  | 1791 | // ensure that the following keys are sent to the new window. | 
|  | 1792 | // | 
|  | 1793 | // Suppose the user touches a button in a window then immediately presses "A". | 
|  | 1794 | // If the button causes a pop-up window to appear then we want to ensure that | 
|  | 1795 | // the "A" key is delivered to the new pop-up window.  This is because users | 
|  | 1796 | // often anticipate pending UI changes when typing on a keyboard. | 
|  | 1797 | // To obtain this behavior, we must serialize key events with respect to all | 
|  | 1798 | // prior input events. | 
|  | 1799 | if (entry.type == EventEntry::Type::KEY) { | 
|  | 1800 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { | 
|  | 1801 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1802 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1803 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1804 | } | 
|  | 1805 |  | 
|  | 1806 | // Success!  Output targets. | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1807 | addWindowTargetLocked(focusedWindowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1808 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, | 
|  | 1809 | BitSet32(0), inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1810 |  | 
|  | 1811 | // Done. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1812 | return InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1813 | } | 
|  | 1814 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1815 | /** | 
|  | 1816 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones | 
|  | 1817 | * that are currently unresponsive. | 
|  | 1818 | */ | 
|  | 1819 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( | 
|  | 1820 | const std::vector<TouchedMonitor>& monitors) const { | 
|  | 1821 | std::vector<TouchedMonitor> responsiveMonitors; | 
|  | 1822 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), | 
|  | 1823 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { | 
|  | 1824 | sp<Connection> connection = getConnectionLocked( | 
|  | 1825 | monitor.monitor.inputChannel->getConnectionToken()); | 
|  | 1826 | if (connection == nullptr) { | 
|  | 1827 | ALOGE("Could not find connection for monitor %s", | 
|  | 1828 | monitor.monitor.inputChannel->getName().c_str()); | 
|  | 1829 | return false; | 
|  | 1830 | } | 
|  | 1831 | if (!connection->responsive) { | 
|  | 1832 | ALOGW("Unresponsive monitor %s will not get the new gesture", | 
|  | 1833 | connection->inputChannel->getName().c_str()); | 
|  | 1834 | return false; | 
|  | 1835 | } | 
|  | 1836 | return true; | 
|  | 1837 | }); | 
|  | 1838 | return responsiveMonitors; | 
|  | 1839 | } | 
|  | 1840 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1841 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( | 
|  | 1842 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, | 
|  | 1843 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1844 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1845 | enum InjectionPermission { | 
|  | 1846 | INJECTION_PERMISSION_UNKNOWN, | 
|  | 1847 | INJECTION_PERMISSION_GRANTED, | 
|  | 1848 | INJECTION_PERMISSION_DENIED | 
|  | 1849 | }; | 
|  | 1850 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1851 | // For security reasons, we defer updating the touch state until we are sure that | 
|  | 1852 | // event injection will be allowed. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1853 | int32_t displayId = entry.displayId; | 
|  | 1854 | int32_t action = entry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1855 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
|  | 1856 |  | 
|  | 1857 | // 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] | 1858 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1859 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1860 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); | 
|  | 1861 | sp<InputWindowHandle> newTouchedWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1862 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1863 | // Copy current touch state into tempTouchState. | 
|  | 1864 | // This state will be used to update mTouchStatesByDisplay at the end of this function. | 
|  | 1865 | // 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] | 1866 | const TouchState* oldState = nullptr; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1867 | TouchState tempTouchState; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1868 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = | 
|  | 1869 | mTouchStatesByDisplay.find(displayId); | 
|  | 1870 | if (oldStateIt != mTouchStatesByDisplay.end()) { | 
|  | 1871 | oldState = &(oldStateIt->second); | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1872 | tempTouchState.copyFrom(*oldState); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1873 | } | 
|  | 1874 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1875 | bool isSplit = tempTouchState.split; | 
|  | 1876 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && | 
|  | 1877 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || | 
|  | 1878 | tempTouchState.displayId != displayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1879 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || | 
|  | 1880 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 1881 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); | 
|  | 1882 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || | 
|  | 1883 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1884 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1885 | bool wrongDevice = false; | 
|  | 1886 | if (newGesture) { | 
|  | 1887 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1888 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1889 | ALOGI("Dropping event because a pointer for a different device is already down " | 
|  | 1890 | "in display %" PRId32, | 
|  | 1891 | displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1892 | // TODO: test multiple simultaneous input streams. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1893 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1894 | switchedDevice = false; | 
|  | 1895 | wrongDevice = true; | 
|  | 1896 | goto Failed; | 
|  | 1897 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1898 | tempTouchState.reset(); | 
|  | 1899 | tempTouchState.down = down; | 
|  | 1900 | tempTouchState.deviceId = entry.deviceId; | 
|  | 1901 | tempTouchState.source = entry.source; | 
|  | 1902 | tempTouchState.displayId = displayId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1903 | isSplit = false; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1904 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1905 | ALOGI("Dropping move event because a pointer for a different device is already active " | 
|  | 1906 | "in display %" PRId32, | 
|  | 1907 | displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1908 | // TODO: test multiple simultaneous input streams. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1909 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1910 | switchedDevice = false; | 
|  | 1911 | wrongDevice = true; | 
|  | 1912 | goto Failed; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1913 | } | 
|  | 1914 |  | 
|  | 1915 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { | 
|  | 1916 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ | 
|  | 1917 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1918 | int32_t x; | 
|  | 1919 | int32_t y; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1920 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1921 | // Always dispatch mouse events to cursor position. | 
|  | 1922 | if (isFromMouse) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1923 | x = int32_t(entry.xCursorPosition); | 
|  | 1924 | y = int32_t(entry.yCursorPosition); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1925 | } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1926 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 1927 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1928 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1929 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1930 | newTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1931 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, | 
|  | 1932 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1933 |  | 
|  | 1934 | std::vector<TouchedMonitor> newGestureMonitors = isDown | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1935 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1936 | : std::vector<TouchedMonitor>{}; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1937 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1938 | // Figure out whether splitting will be allowed for this window. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1939 | if (newTouchedWindowHandle != nullptr && | 
|  | 1940 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
| Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1941 | // New window supports splitting, but we should never split mouse events. | 
|  | 1942 | isSplit = !isFromMouse; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1943 | } else if (isSplit) { | 
|  | 1944 | // New window does not support splitting but we have already split events. | 
|  | 1945 | // Ignore the new window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1946 | newTouchedWindowHandle = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1947 | } | 
|  | 1948 |  | 
|  | 1949 | // Handle the case where we did not find a window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1950 | if (newTouchedWindowHandle == nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1951 | // 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] | 1952 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1953 | } | 
|  | 1954 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1955 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { | 
|  | 1956 | ALOGI("Not sending touch event to %s because it is paused", | 
|  | 1957 | newTouchedWindowHandle->getName().c_str()); | 
|  | 1958 | newTouchedWindowHandle = nullptr; | 
|  | 1959 | } | 
|  | 1960 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1961 | // Ensure the window has a connection and the connection is responsive | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1962 | if (newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1963 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); | 
|  | 1964 | if (!isResponsive) { | 
|  | 1965 | ALOGW("%s will not receive the new gesture at %" PRIu64, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1966 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); | 
|  | 1967 | newTouchedWindowHandle = nullptr; | 
|  | 1968 | } | 
|  | 1969 | } | 
|  | 1970 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 1971 | // Drop events that can't be trusted due to occlusion | 
|  | 1972 | if (newTouchedWindowHandle != nullptr && | 
|  | 1973 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { | 
|  | 1974 | TouchOcclusionInfo occlusionInfo = | 
|  | 1975 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 1976 | if (!isTouchTrustedLocked(occlusionInfo)) { | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 1977 | if (DEBUG_TOUCH_OCCLUSION) { | 
|  | 1978 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); | 
|  | 1979 | for (const auto& log : occlusionInfo.debugInfo) { | 
|  | 1980 | ALOGD("%s", log.c_str()); | 
|  | 1981 | } | 
|  | 1982 | } | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 1983 | onUntrustedTouchLocked(occlusionInfo.obscuringPackage); | 
|  | 1984 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { | 
|  | 1985 | ALOGW("Dropping untrusted touch event due to %s/%d", | 
|  | 1986 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); | 
|  | 1987 | newTouchedWindowHandle = nullptr; | 
|  | 1988 | } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 1989 | } | 
|  | 1990 | } | 
|  | 1991 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1992 | // Also don't send the new touch event to unresponsive gesture monitors | 
|  | 1993 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); | 
|  | 1994 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1995 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { | 
|  | 1996 | 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] | 1997 | "(%d, %d) in display %" PRId32 ".", | 
|  | 1998 | x, y, displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1999 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2000 | goto Failed; | 
|  | 2001 | } | 
|  | 2002 |  | 
|  | 2003 | if (newTouchedWindowHandle != nullptr) { | 
|  | 2004 | // Set target flags. | 
|  | 2005 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 2006 | if (isSplit) { | 
|  | 2007 | targetFlags |= InputTarget::FLAG_SPLIT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2008 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2009 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
|  | 2010 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
|  | 2011 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { | 
|  | 2012 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 2013 | } | 
|  | 2014 |  | 
|  | 2015 | // Update hover state. | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2016 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { | 
|  | 2017 | newHoverWindowHandle = nullptr; | 
|  | 2018 | } else if (isHoverAction) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2019 | newHoverWindowHandle = newTouchedWindowHandle; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2020 | } | 
|  | 2021 |  | 
|  | 2022 | // Update the temporary touch state. | 
|  | 2023 | BitSet32 pointerIds; | 
|  | 2024 | if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2025 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2026 | pointerIds.markBit(pointerId); | 
|  | 2027 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2028 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2029 | } | 
|  | 2030 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2031 | tempTouchState.addGestureMonitors(newGestureMonitors); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2032 | } else { | 
|  | 2033 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ | 
|  | 2034 |  | 
|  | 2035 | // If the pointer is not currently down, then ignore the event. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2036 | if (!tempTouchState.down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2037 | if (DEBUG_FOCUS) { | 
|  | 2038 | ALOGD("Dropping event because the pointer is not down or we previously " | 
|  | 2039 | "dropped the pointer down event in display %" PRId32, | 
|  | 2040 | displayId); | 
|  | 2041 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2042 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2043 | goto Failed; | 
|  | 2044 | } | 
|  | 2045 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2046 | addDragEventLocked(entry); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2047 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2048 | // Check whether touches should slip outside of the current foreground window. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2049 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2050 | tempTouchState.isSlippery()) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2051 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 2052 | 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] | 2053 |  | 
|  | 2054 | sp<InputWindowHandle> oldTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2055 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2056 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2057 | if (oldTouchedWindowHandle != newTouchedWindowHandle && | 
|  | 2058 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2059 | if (DEBUG_FOCUS) { | 
|  | 2060 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, | 
|  | 2061 | oldTouchedWindowHandle->getName().c_str(), | 
|  | 2062 | newTouchedWindowHandle->getName().c_str(), displayId); | 
|  | 2063 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2064 | // Make a slippery exit from the old window. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2065 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, | 
|  | 2066 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, | 
|  | 2067 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2068 |  | 
|  | 2069 | // Make a slippery entrance into the new window. | 
|  | 2070 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
|  | 2071 | isSplit = true; | 
|  | 2072 | } | 
|  | 2073 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2074 | int32_t targetFlags = | 
|  | 2075 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2076 | if (isSplit) { | 
|  | 2077 | targetFlags |= InputTarget::FLAG_SPLIT; | 
|  | 2078 | } | 
|  | 2079 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
|  | 2080 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2081 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { | 
|  | 2082 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2083 | } | 
|  | 2084 |  | 
|  | 2085 | BitSet32 pointerIds; | 
|  | 2086 | if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2087 | pointerIds.markBit(entry.pointerProperties[0].id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2088 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2089 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2090 | } | 
|  | 2091 | } | 
|  | 2092 | } | 
|  | 2093 |  | 
|  | 2094 | if (newHoverWindowHandle != mLastHoverWindowHandle) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2095 | // Let the previous window know that the hover sequence is over, unless we already did it | 
|  | 2096 | // when dispatching it as is to newTouchedWindowHandle. | 
|  | 2097 | if (mLastHoverWindowHandle != nullptr && | 
|  | 2098 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || | 
|  | 2099 | mLastHoverWindowHandle != newTouchedWindowHandle)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2100 | #if DEBUG_HOVER | 
|  | 2101 | ALOGD("Sending hover exit event to window %s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2102 | mLastHoverWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2103 | #endif | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2104 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, | 
|  | 2105 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2106 | } | 
|  | 2107 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2108 | // Let the new window know that the hover sequence is starting, unless we already did it | 
|  | 2109 | // when dispatching it as is to newTouchedWindowHandle. | 
|  | 2110 | if (newHoverWindowHandle != nullptr && | 
|  | 2111 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 2112 | newHoverWindowHandle != newTouchedWindowHandle)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2113 | #if DEBUG_HOVER | 
|  | 2114 | ALOGD("Sending hover enter event to window %s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2115 | newHoverWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2116 | #endif | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2117 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, | 
|  | 2118 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, | 
|  | 2119 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2120 | } | 
|  | 2121 | } | 
|  | 2122 |  | 
|  | 2123 | // Check permission to inject into all touched foreground windows and ensure there | 
|  | 2124 | // is at least one touched foreground window. | 
|  | 2125 | { | 
|  | 2126 | bool haveForegroundWindow = false; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2127 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2128 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { | 
|  | 2129 | haveForegroundWindow = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2130 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2131 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2132 | injectionPermission = INJECTION_PERMISSION_DENIED; | 
|  | 2133 | goto Failed; | 
|  | 2134 | } | 
|  | 2135 | } | 
|  | 2136 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2137 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2138 | if (!haveForegroundWindow && !hasGestureMonitor) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2139 | ALOGI("Dropping event because there is no touched foreground window in display " | 
|  | 2140 | "%" PRId32 " or gesture monitor to receive it.", | 
|  | 2141 | displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2142 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2143 | goto Failed; | 
|  | 2144 | } | 
|  | 2145 |  | 
|  | 2146 | // Permission granted to injection into all touched foreground windows. | 
|  | 2147 | injectionPermission = INJECTION_PERMISSION_GRANTED; | 
|  | 2148 | } | 
|  | 2149 |  | 
|  | 2150 | // Check whether windows listening for outside touches are owned by the same UID. If it is | 
|  | 2151 | // set the policy flag that we will not reveal coordinate information to this window. | 
|  | 2152 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 2153 | sp<InputWindowHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2154 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2155 | if (foregroundWindowHandle) { | 
|  | 2156 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2157 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2158 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 2159 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; | 
|  | 2160 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2161 | tempTouchState.addOrUpdateWindow(inputWindowHandle, | 
|  | 2162 | InputTarget::FLAG_ZERO_COORDS, | 
|  | 2163 | BitSet32(0)); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2164 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2165 | } | 
|  | 2166 | } | 
|  | 2167 | } | 
|  | 2168 | } | 
|  | 2169 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2170 | // If this is the first pointer going down and the touched window has a wallpaper | 
|  | 2171 | // then also add the touched wallpaper windows so they are locked in for the duration | 
|  | 2172 | // of the touch gesture. | 
|  | 2173 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper | 
|  | 2174 | // engine only supports touch events.  We would need to add a mechanism similar | 
|  | 2175 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. | 
|  | 2176 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 2177 | sp<InputWindowHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2178 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2179 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2180 | const std::vector<sp<InputWindowHandle>>& windowHandles = | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2181 | getWindowHandlesLocked(displayId); | 
|  | 2182 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2183 | const InputWindowInfo* info = windowHandle->getInfo(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2184 | if (info->displayId == displayId && | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2185 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2186 | tempTouchState | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2187 | .addOrUpdateWindow(windowHandle, | 
|  | 2188 | InputTarget::FLAG_WINDOW_IS_OBSCURED | | 
|  | 2189 | InputTarget:: | 
|  | 2190 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | | 
|  | 2191 | InputTarget::FLAG_DISPATCH_AS_IS, | 
|  | 2192 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2193 | } | 
|  | 2194 | } | 
|  | 2195 | } | 
|  | 2196 | } | 
|  | 2197 |  | 
|  | 2198 | // Success!  Output targets. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2199 | injectionResult = InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2200 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2201 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2202 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2203 | touchedWindow.pointerIds, inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2204 | } | 
|  | 2205 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2206 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2207 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2208 | touchedMonitor.yOffset, inputTargets); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2209 | } | 
|  | 2210 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2211 | // Drop the outside or hover touch windows since we will not care about them | 
|  | 2212 | // in the next iteration. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2213 | tempTouchState.filterNonAsIsTouchWindows(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2214 |  | 
|  | 2215 | Failed: | 
|  | 2216 | // Check injection permission once and for all. | 
|  | 2217 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2218 | if (checkInjectionPermission(nullptr, entry.injectionState)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2219 | injectionPermission = INJECTION_PERMISSION_GRANTED; | 
|  | 2220 | } else { | 
|  | 2221 | injectionPermission = INJECTION_PERMISSION_DENIED; | 
|  | 2222 | } | 
|  | 2223 | } | 
|  | 2224 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2225 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { | 
|  | 2226 | return injectionResult; | 
|  | 2227 | } | 
|  | 2228 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2229 | // Update final pieces of touch state if the injector had permission. | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2230 | if (!wrongDevice) { | 
|  | 2231 | if (switchedDevice) { | 
|  | 2232 | if (DEBUG_FOCUS) { | 
|  | 2233 | ALOGD("Conflicting pointer actions: Switched to a different device."); | 
|  | 2234 | } | 
|  | 2235 | *outConflictingPointerActions = true; | 
|  | 2236 | } | 
|  | 2237 |  | 
|  | 2238 | if (isHoverAction) { | 
|  | 2239 | // Started hovering, therefore no longer down. | 
|  | 2240 | if (oldState && oldState->down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2241 | if (DEBUG_FOCUS) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2242 | ALOGD("Conflicting pointer actions: Hover received while pointer was " | 
|  | 2243 | "down."); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2244 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2245 | *outConflictingPointerActions = true; | 
|  | 2246 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2247 | tempTouchState.reset(); | 
|  | 2248 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 2249 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { | 
|  | 2250 | tempTouchState.deviceId = entry.deviceId; | 
|  | 2251 | tempTouchState.source = entry.source; | 
|  | 2252 | tempTouchState.displayId = displayId; | 
|  | 2253 | } | 
|  | 2254 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || | 
|  | 2255 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 2256 | // All pointers up or canceled. | 
|  | 2257 | tempTouchState.reset(); | 
|  | 2258 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 2259 | // First pointer went down. | 
|  | 2260 | if (oldState && oldState->down) { | 
|  | 2261 | if (DEBUG_FOCUS) { | 
|  | 2262 | ALOGD("Conflicting pointer actions: Down received while already down."); | 
|  | 2263 | } | 
|  | 2264 | *outConflictingPointerActions = true; | 
|  | 2265 | } | 
|  | 2266 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
|  | 2267 | // One pointer went up. | 
|  | 2268 | if (isSplit) { | 
|  | 2269 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 2270 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2271 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2272 | for (size_t i = 0; i < tempTouchState.windows.size();) { | 
|  | 2273 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; | 
|  | 2274 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { | 
|  | 2275 | touchedWindow.pointerIds.clearBit(pointerId); | 
|  | 2276 | if (touchedWindow.pointerIds.isEmpty()) { | 
|  | 2277 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); | 
|  | 2278 | continue; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2279 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2280 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2281 | i += 1; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2282 | } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2283 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2284 | } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2285 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2286 | // Save changes unless the action was scroll in which case the temporary touch | 
|  | 2287 | // state was only valid for this one action. | 
|  | 2288 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { | 
|  | 2289 | if (tempTouchState.displayId >= 0) { | 
|  | 2290 | mTouchStatesByDisplay[displayId] = tempTouchState; | 
|  | 2291 | } else { | 
|  | 2292 | mTouchStatesByDisplay.erase(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2293 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2294 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2295 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2296 | // Update hover state. | 
|  | 2297 | mLastHoverWindowHandle = newHoverWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2298 | } | 
|  | 2299 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2300 | return injectionResult; | 
|  | 2301 | } | 
|  | 2302 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2303 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { | 
|  | 2304 | const sp<InputWindowHandle> dropWindow = | 
|  | 2305 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, | 
|  | 2306 | false /*addOutsideTargets*/, false /*addPortalWindows*/, | 
|  | 2307 | true /*ignoreDragWindow*/); | 
|  | 2308 | if (dropWindow) { | 
|  | 2309 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); | 
|  | 2310 | notifyDropWindowLocked(dropWindow->getToken(), local.x, local.y); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2311 | } else { | 
|  | 2312 | notifyDropWindowLocked(nullptr, 0, 0); | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2313 | } | 
|  | 2314 | mDragState.reset(); | 
|  | 2315 | } | 
|  | 2316 |  | 
|  | 2317 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { | 
|  | 2318 | if (entry.pointerCount != 1 || !mDragState) { | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2319 | return; | 
|  | 2320 | } | 
|  | 2321 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2322 | if (!mDragState->isStartDrag) { | 
|  | 2323 | mDragState->isStartDrag = true; | 
|  | 2324 | mDragState->isStylusButtonDownAtStart = | 
|  | 2325 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; | 
|  | 2326 | } | 
|  | 2327 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2328 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; | 
|  | 2329 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 2330 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
|  | 2331 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2332 | // Handle the special case : stylus button no longer pressed. | 
|  | 2333 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; | 
|  | 2334 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { | 
|  | 2335 | finishDragAndDrop(entry.displayId, x, y); | 
|  | 2336 | return; | 
|  | 2337 | } | 
|  | 2338 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2339 | const sp<InputWindowHandle> hoverWindowHandle = | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2340 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2341 | false /*addOutsideTargets*/, false /*addPortalWindows*/, | 
|  | 2342 | true /*ignoreDragWindow*/); | 
|  | 2343 | // enqueue drag exit if needed. | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2344 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && | 
|  | 2345 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { | 
|  | 2346 | if (mDragState->dragHoverWindowHandle != nullptr) { | 
|  | 2347 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, | 
|  | 2348 | entry); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2349 | } | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2350 | mDragState->dragHoverWindowHandle = hoverWindowHandle; | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2351 | } | 
|  | 2352 | // enqueue drag location if needed. | 
|  | 2353 | if (hoverWindowHandle != nullptr) { | 
|  | 2354 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); | 
|  | 2355 | } | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2356 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { | 
|  | 2357 | finishDragAndDrop(entry.displayId, x, y); | 
|  | 2358 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2359 | notifyDropWindowLocked(nullptr, 0, 0); | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2360 | mDragState.reset(); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2361 | } | 
|  | 2362 | } | 
|  | 2363 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2364 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2365 | int32_t targetFlags, BitSet32 pointerIds, | 
|  | 2366 | std::vector<InputTarget>& inputTargets) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2367 | std::vector<InputTarget>::iterator it = | 
|  | 2368 | std::find_if(inputTargets.begin(), inputTargets.end(), | 
|  | 2369 | [&windowHandle](const InputTarget& inputTarget) { | 
|  | 2370 | return inputTarget.inputChannel->getConnectionToken() == | 
|  | 2371 | windowHandle->getToken(); | 
|  | 2372 | }); | 
| Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2373 |  | 
| Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2374 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2375 |  | 
|  | 2376 | if (it == inputTargets.end()) { | 
|  | 2377 | InputTarget inputTarget; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2378 | std::shared_ptr<InputChannel> inputChannel = | 
|  | 2379 | getInputChannelLocked(windowHandle->getToken()); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2380 | if (inputChannel == nullptr) { | 
|  | 2381 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); | 
|  | 2382 | return; | 
|  | 2383 | } | 
|  | 2384 | inputTarget.inputChannel = inputChannel; | 
|  | 2385 | inputTarget.flags = targetFlags; | 
|  | 2386 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2387 | inputTarget.displaySize = | 
|  | 2388 | vec2(windowHandle->getInfo()->displayWidth, windowHandle->getInfo()->displayHeight); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2389 | inputTargets.push_back(inputTarget); | 
|  | 2390 | it = inputTargets.end() - 1; | 
|  | 2391 | } | 
|  | 2392 |  | 
|  | 2393 | ALOG_ASSERT(it->flags == targetFlags); | 
|  | 2394 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); | 
|  | 2395 |  | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2396 | it->addPointers(pointerIds, windowInfo->transform); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2397 | } | 
|  | 2398 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2399 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2400 | int32_t displayId, float xOffset, | 
|  | 2401 | float yOffset) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2402 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = | 
|  | 2403 | mGlobalMonitorsByDisplay.find(displayId); | 
|  | 2404 |  | 
|  | 2405 | if (it != mGlobalMonitorsByDisplay.end()) { | 
|  | 2406 | const std::vector<Monitor>& monitors = it->second; | 
|  | 2407 | for (const Monitor& monitor : monitors) { | 
|  | 2408 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2409 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2410 | } | 
|  | 2411 | } | 
|  | 2412 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2413 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, | 
|  | 2414 | float yOffset, | 
|  | 2415 | std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2416 | InputTarget target; | 
|  | 2417 | target.inputChannel = monitor.inputChannel; | 
|  | 2418 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2419 | ui::Transform t; | 
|  | 2420 | t.set(xOffset, yOffset); | 
|  | 2421 | target.setDefaultPointerTransform(t); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2422 | inputTargets.push_back(target); | 
|  | 2423 | } | 
|  | 2424 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2425 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2426 | const InjectionState* injectionState) { | 
|  | 2427 | if (injectionState && | 
|  | 2428 | (windowHandle == nullptr || | 
|  | 2429 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && | 
|  | 2430 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2431 | if (windowHandle != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2432 | 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] | 2433 | "owned by uid %d", | 
|  | 2434 | injectionState->injectorPid, injectionState->injectorUid, | 
|  | 2435 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2436 | } else { | 
|  | 2437 | ALOGW("Permission denied: injecting event from pid %d uid %d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2438 | injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2439 | } | 
|  | 2440 | return false; | 
|  | 2441 | } | 
|  | 2442 | return true; | 
|  | 2443 | } | 
|  | 2444 |  | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2445 | /** | 
|  | 2446 | * Indicate whether one window handle should be considered as obscuring | 
|  | 2447 | * another window handle. We only check a few preconditions. Actually | 
|  | 2448 | * checking the bounds is left to the caller. | 
|  | 2449 | */ | 
|  | 2450 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, | 
|  | 2451 | const sp<InputWindowHandle>& otherHandle) { | 
|  | 2452 | // Compare by token so cloned layers aren't counted | 
|  | 2453 | if (haveSameToken(windowHandle, otherHandle)) { | 
|  | 2454 | return false; | 
|  | 2455 | } | 
|  | 2456 | auto info = windowHandle->getInfo(); | 
|  | 2457 | auto otherInfo = otherHandle->getInfo(); | 
|  | 2458 | if (!otherInfo->visible) { | 
|  | 2459 | return false; | 
| Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2460 | } else if (otherInfo->alpha == 0 && | 
|  | 2461 | otherInfo->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { | 
|  | 2462 | // Those act as if they were invisible, so we don't need to flag them. | 
|  | 2463 | // We do want to potentially flag touchable windows even if they have 0 | 
|  | 2464 | // opacity, since they can consume touches and alter the effects of the | 
|  | 2465 | // user interaction (eg. apps that rely on | 
|  | 2466 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those | 
|  | 2467 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. | 
|  | 2468 | return false; | 
| Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2469 | } else if (info->ownerUid == otherInfo->ownerUid) { | 
|  | 2470 | // If ownerUid is the same we don't generate occlusion events as there | 
|  | 2471 | // is no security boundary within an uid. | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2472 | return false; | 
| Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2473 | } else if (otherInfo->trustedOverlay) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2474 | return false; | 
|  | 2475 | } else if (otherInfo->displayId != info->displayId) { | 
|  | 2476 | return false; | 
|  | 2477 | } | 
|  | 2478 | return true; | 
|  | 2479 | } | 
|  | 2480 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2481 | /** | 
|  | 2482 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is | 
|  | 2483 | * untrusted, one should check: | 
|  | 2484 | * | 
|  | 2485 | * 1. If result.hasBlockingOcclusion is true. | 
|  | 2486 | *    If it's, it means the touch should be blocked due to a window with occlusion mode of | 
|  | 2487 | *    BLOCK_UNTRUSTED. | 
|  | 2488 | * | 
|  | 2489 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. | 
|  | 2490 | *    If it is (and 1 is false), then the touch should be blocked because a stack of windows | 
|  | 2491 | *    (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed | 
|  | 2492 | *    obscuring opacity above the threshold. Note that if there was no window of occlusion mode | 
|  | 2493 | *    USE_OPACITY, result.obscuringOpacity would've been 0 and since | 
|  | 2494 | *    mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. | 
|  | 2495 | * | 
|  | 2496 | * If neither of those is true, then it means the touch can be allowed. | 
|  | 2497 | */ | 
|  | 2498 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( | 
|  | 2499 | const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2500 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
|  | 2501 | int32_t displayId = windowInfo->displayId; | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2502 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
|  | 2503 | TouchOcclusionInfo info; | 
|  | 2504 | info.hasBlockingOcclusion = false; | 
|  | 2505 | info.obscuringOpacity = 0; | 
|  | 2506 | info.obscuringUid = -1; | 
|  | 2507 | std::map<int32_t, float> opacityByUid; | 
|  | 2508 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
|  | 2509 | if (windowHandle == otherHandle) { | 
|  | 2510 | break; // All future windows are below us. Exit early. | 
|  | 2511 | } | 
|  | 2512 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2513 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && | 
|  | 2514 | !haveSameApplicationToken(windowInfo, otherInfo)) { | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2515 | if (DEBUG_TOUCH_OCCLUSION) { | 
|  | 2516 | info.debugInfo.push_back( | 
|  | 2517 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); | 
|  | 2518 | } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2519 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so | 
|  | 2520 | // we perform the checks below to see if the touch can be propagated or not based on the | 
|  | 2521 | // window's touch occlusion mode | 
|  | 2522 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { | 
|  | 2523 | info.hasBlockingOcclusion = true; | 
|  | 2524 | info.obscuringUid = otherInfo->ownerUid; | 
|  | 2525 | info.obscuringPackage = otherInfo->packageName; | 
|  | 2526 | break; | 
|  | 2527 | } | 
|  | 2528 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { | 
|  | 2529 | uint32_t uid = otherInfo->ownerUid; | 
|  | 2530 | float opacity = | 
|  | 2531 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; | 
|  | 2532 | // Given windows A and B: | 
|  | 2533 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] | 
|  | 2534 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); | 
|  | 2535 | opacityByUid[uid] = opacity; | 
|  | 2536 | if (opacity > info.obscuringOpacity) { | 
|  | 2537 | info.obscuringOpacity = opacity; | 
|  | 2538 | info.obscuringUid = uid; | 
|  | 2539 | info.obscuringPackage = otherInfo->packageName; | 
|  | 2540 | } | 
|  | 2541 | } | 
|  | 2542 | } | 
|  | 2543 | } | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2544 | if (DEBUG_TOUCH_OCCLUSION) { | 
|  | 2545 | info.debugInfo.push_back( | 
|  | 2546 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); | 
|  | 2547 | } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2548 | return info; | 
|  | 2549 | } | 
|  | 2550 |  | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2551 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const InputWindowInfo* info, | 
|  | 2552 | bool isTouchedWindow) const { | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2553 | return StringPrintf(INDENT2 | 
|  | 2554 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " | 
|  | 2555 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 | 
|  | 2556 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " | 
|  | 2557 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2558 | (isTouchedWindow) ? "[TOUCHED] " : "", | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2559 | NamedEnum::string(info->type, "%" PRId32).c_str(), | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2560 | info->packageName.c_str(), info->ownerUid, info->id, | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2561 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, | 
|  | 2562 | info->frameTop, info->frameRight, info->frameBottom, | 
|  | 2563 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2564 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), | 
|  | 2565 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), | 
|  | 2566 | toString(info->applicationInfo.token).c_str()); | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2567 | } | 
|  | 2568 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2569 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { | 
|  | 2570 | if (occlusionInfo.hasBlockingOcclusion) { | 
|  | 2571 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), | 
|  | 2572 | occlusionInfo.obscuringUid); | 
|  | 2573 | return false; | 
|  | 2574 | } | 
|  | 2575 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { | 
|  | 2576 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " | 
|  | 2577 | "%.2f, maximum allowed = %.2f)", | 
|  | 2578 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, | 
|  | 2579 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); | 
|  | 2580 | return false; | 
|  | 2581 | } | 
|  | 2582 | return true; | 
|  | 2583 | } | 
|  | 2584 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2585 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, | 
|  | 2586 | int32_t x, int32_t y) const { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2587 | int32_t displayId = windowHandle->getInfo()->displayId; | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2588 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2589 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2590 | if (windowHandle == otherHandle) { | 
|  | 2591 | break; // All future windows are below us. Exit early. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2592 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2593 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2594 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2595 | otherInfo->frameContainsPoint(x, y)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2596 | return true; | 
|  | 2597 | } | 
|  | 2598 | } | 
|  | 2599 | return false; | 
|  | 2600 | } | 
|  | 2601 |  | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2602 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { | 
|  | 2603 | int32_t displayId = windowHandle->getInfo()->displayId; | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2604 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2605 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2606 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2607 | if (windowHandle == otherHandle) { | 
|  | 2608 | break; // All future windows are below us. Exit early. | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2609 | } | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2610 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2611 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2612 | otherInfo->overlaps(windowInfo)) { | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2613 | return true; | 
|  | 2614 | } | 
|  | 2615 | } | 
|  | 2616 | return false; | 
|  | 2617 | } | 
|  | 2618 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2619 | std::string InputDispatcher::getApplicationWindowLabel( | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 2620 | const InputApplicationHandle* applicationHandle, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2621 | const sp<InputWindowHandle>& windowHandle) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2622 | if (applicationHandle != nullptr) { | 
|  | 2623 | if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2624 | return applicationHandle->getName() + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2625 | } else { | 
|  | 2626 | return applicationHandle->getName(); | 
|  | 2627 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2628 | } else if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2629 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2630 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2631 | return "<unknown application or window>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2632 | } | 
|  | 2633 | } | 
|  | 2634 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2635 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2636 | if (eventEntry.type == EventEntry::Type::FOCUS || | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2637 | eventEntry.type == EventEntry::Type::POINTER_CAPTURE_CHANGED || | 
|  | 2638 | eventEntry.type == EventEntry::Type::DRAG) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2639 | // Focus or pointer capture changed events are passed to apps, but do not represent user | 
|  | 2640 | // activity. | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2641 | return; | 
|  | 2642 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2643 | int32_t displayId = getTargetDisplayId(eventEntry); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2644 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2645 | if (focusedWindowHandle != nullptr) { | 
|  | 2646 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2647 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2648 | #if DEBUG_DISPATCH_CYCLE | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2649 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2650 | #endif | 
|  | 2651 | return; | 
|  | 2652 | } | 
|  | 2653 | } | 
|  | 2654 |  | 
|  | 2655 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2656 | switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2657 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2658 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
|  | 2659 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2660 | return; | 
|  | 2661 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2662 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2663 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2664 | eventType = USER_ACTIVITY_EVENT_TOUCH; | 
|  | 2665 | } | 
|  | 2666 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2667 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2668 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2669 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
|  | 2670 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2671 | return; | 
|  | 2672 | } | 
|  | 2673 | eventType = USER_ACTIVITY_EVENT_BUTTON; | 
|  | 2674 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2675 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2676 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2677 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2678 | case EventEntry::Type::DEVICE_RESET: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2679 | case EventEntry::Type::SENSOR: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2680 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 2681 | case EventEntry::Type::DRAG: { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2682 | LOG_ALWAYS_FATAL("%s events are not user activity", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2683 | NamedEnum::string(eventEntry.type).c_str()); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2684 | break; | 
|  | 2685 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2686 | } | 
|  | 2687 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2688 | std::unique_ptr<CommandEntry> commandEntry = | 
|  | 2689 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2690 | commandEntry->eventTime = eventEntry.eventTime; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2691 | commandEntry->userActivityEventType = eventType; | 
| Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 2692 | commandEntry->displayId = displayId; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2693 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2694 | } | 
|  | 2695 |  | 
|  | 2696 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2697 | const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2698 | std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2699 | const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2700 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2701 | std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2702 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2703 | connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2704 | ATRACE_NAME(message.c_str()); | 
|  | 2705 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2706 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2707 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2708 | "globalScaleFactor=%f, pointerIds=0x%x %s", | 
|  | 2709 | connection->getInputChannelName().c_str(), inputTarget.flags, | 
|  | 2710 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, | 
|  | 2711 | inputTarget.getPointerInfoString().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2712 | #endif | 
|  | 2713 |  | 
|  | 2714 | // Skip this event if the connection status is not normal. | 
|  | 2715 | // We don't want to enqueue additional outbound events if the connection is broken. | 
|  | 2716 | if (connection->status != Connection::STATUS_NORMAL) { | 
|  | 2717 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2718 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2719 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2720 | #endif | 
|  | 2721 | return; | 
|  | 2722 | } | 
|  | 2723 |  | 
|  | 2724 | // Split a motion event if needed. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2725 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { | 
|  | 2726 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, | 
|  | 2727 | "Entry type %s should not have FLAG_SPLIT", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2728 | NamedEnum::string(eventEntry->type).c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2729 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2730 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2731 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2732 | std::unique_ptr<MotionEntry> splitMotionEntry = | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2733 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2734 | if (!splitMotionEntry) { | 
|  | 2735 | return; // split event was dropped | 
|  | 2736 | } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2737 | if (DEBUG_FOCUS) { | 
|  | 2738 | ALOGD("channel '%s' ~ Split motion event.", | 
|  | 2739 | connection->getInputChannelName().c_str()); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2740 | logOutboundMotionDetails("  ", *splitMotionEntry); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2741 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2742 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), | 
|  | 2743 | inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2744 | return; | 
|  | 2745 | } | 
|  | 2746 | } | 
|  | 2747 |  | 
|  | 2748 | // Not splitting.  Enqueue dispatch entries for the event as is. | 
|  | 2749 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); | 
|  | 2750 | } | 
|  | 2751 |  | 
|  | 2752 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2753 | const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2754 | std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2755 | const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2756 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2757 | std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2758 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2759 | connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2760 | ATRACE_NAME(message.c_str()); | 
|  | 2761 | } | 
|  | 2762 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2763 | bool wasEmpty = connection->outboundQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2764 |  | 
|  | 2765 | // Enqueue dispatch entries for the requested modes. | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2766 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2767 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2768 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2769 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2770 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2771 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2772 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2773 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2774 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2775 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2776 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2777 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2778 |  | 
|  | 2779 | // If the outbound queue was previously empty, start the dispatch cycle going. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2780 | if (wasEmpty && !connection->outboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2781 | startDispatchCycleLocked(currentTime, connection); | 
|  | 2782 | } | 
|  | 2783 | } | 
|  | 2784 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2785 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2786 | std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2787 | const InputTarget& inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2788 | int32_t dispatchMode) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2789 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2790 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", | 
|  | 2791 | connection->getInputChannelName().c_str(), | 
|  | 2792 | dispatchModeToString(dispatchMode).c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2793 | ATRACE_NAME(message.c_str()); | 
|  | 2794 | } | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2795 | int32_t inputTargetFlags = inputTarget.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2796 | if (!(inputTargetFlags & dispatchMode)) { | 
|  | 2797 | return; | 
|  | 2798 | } | 
|  | 2799 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; | 
|  | 2800 |  | 
|  | 2801 | // This is a new event. | 
|  | 2802 | // Enqueue a new dispatch entry onto the outbound queue for this connection. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2803 | std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2804 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2805 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2806 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a | 
|  | 2807 | // different EventEntry than what was passed in. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2808 | EventEntry& newEntry = *(dispatchEntry->eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2809 | // Apply target flags and update the connection's input state. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2810 | switch (newEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2811 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2812 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2813 | dispatchEntry->resolvedEventId = keyEntry.id; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2814 | dispatchEntry->resolvedAction = keyEntry.action; | 
|  | 2815 | dispatchEntry->resolvedFlags = keyEntry.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2816 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2817 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, | 
|  | 2818 | dispatchEntry->resolvedFlags)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2819 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2820 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", | 
|  | 2821 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2822 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2823 | return; // skip the inconsistent event | 
|  | 2824 | } | 
|  | 2825 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2826 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2827 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2828 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2829 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2830 | // Assign a default value to dispatchEntry that will never be generated by InputReader, | 
|  | 2831 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. | 
|  | 2832 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = | 
|  | 2833 | static_cast<int32_t>(IdGenerator::Source::OTHER); | 
|  | 2834 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2835 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 2836 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; | 
|  | 2837 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { | 
|  | 2838 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; | 
|  | 2839 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { | 
|  | 2840 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
|  | 2841 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { | 
|  | 2842 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; | 
|  | 2843 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { | 
|  | 2844 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; | 
|  | 2845 | } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2846 | dispatchEntry->resolvedAction = motionEntry.action; | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2847 | dispatchEntry->resolvedEventId = motionEntry.id; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2848 | } | 
|  | 2849 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2850 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, | 
|  | 2851 | motionEntry.displayId)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2852 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2853 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " | 
|  | 2854 | "event", | 
|  | 2855 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2856 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2857 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
|  | 2858 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2859 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2860 | dispatchEntry->resolvedFlags = motionEntry.flags; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2861 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { | 
|  | 2862 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; | 
|  | 2863 | } | 
|  | 2864 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { | 
|  | 2865 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 2866 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2867 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2868 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, | 
|  | 2869 | dispatchEntry->resolvedFlags)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2870 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2871 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " | 
|  | 2872 | "event", | 
|  | 2873 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2874 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2875 | return; // skip the inconsistent event | 
|  | 2876 | } | 
|  | 2877 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2878 | dispatchEntry->resolvedEventId = | 
|  | 2879 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID | 
|  | 2880 | ? mIdGenerator.nextId() | 
|  | 2881 | : motionEntry.id; | 
|  | 2882 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { | 
|  | 2883 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 | 
|  | 2884 | ") to MotionEvent(id=0x%" PRIx32 ").", | 
|  | 2885 | motionEntry.id, dispatchEntry->resolvedEventId); | 
|  | 2886 | ATRACE_NAME(message.c_str()); | 
|  | 2887 | } | 
|  | 2888 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 2889 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && | 
|  | 2890 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { | 
|  | 2891 | // Skip reporting pointer down outside focus to the policy. | 
|  | 2892 | break; | 
|  | 2893 | } | 
|  | 2894 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2895 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2896 | inputTarget.inputChannel->getConnectionToken()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2897 |  | 
|  | 2898 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2899 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2900 | case EventEntry::Type::FOCUS: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2901 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 2902 | case EventEntry::Type::DRAG: { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2903 | break; | 
|  | 2904 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2905 | case EventEntry::Type::SENSOR: { | 
|  | 2906 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); | 
|  | 2907 | break; | 
|  | 2908 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2909 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2910 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2911 | LOG_ALWAYS_FATAL("%s events should not go to apps", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2912 | NamedEnum::string(newEntry.type).c_str()); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2913 | break; | 
|  | 2914 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2915 | } | 
|  | 2916 |  | 
|  | 2917 | // Remember that we are waiting for this dispatch to complete. | 
|  | 2918 | if (dispatchEntry->hasForegroundTarget()) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2919 | incrementPendingForegroundDispatches(newEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2920 | } | 
|  | 2921 |  | 
|  | 2922 | // Enqueue the dispatch entry. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2923 | connection->outboundQueue.push_back(dispatchEntry.release()); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2924 | traceOutboundQueueLength(connection); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2925 | } | 
|  | 2926 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2927 | /** | 
|  | 2928 | * This function is purely for debugging. It helps us understand where the user interaction | 
|  | 2929 | * was taking place. For example, if user is touching launcher, we will see a log that user | 
|  | 2930 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. | 
|  | 2931 | * We will see both launcher and wallpaper in that list. | 
|  | 2932 | * Once the interaction with a particular set of connections starts, no new logs will be printed | 
|  | 2933 | * until the set of interacted connections changes. | 
|  | 2934 | * | 
|  | 2935 | * The following items are skipped, to reduce the logspam: | 
|  | 2936 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged | 
|  | 2937 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). | 
|  | 2938 | * This includes situations like the soft BACK button key. When the user releases (lifts up the | 
|  | 2939 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. | 
|  | 2940 | * Both of those ACTION_UP events would not be logged | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2941 | */ | 
|  | 2942 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, | 
|  | 2943 | const std::vector<InputTarget>& targets) { | 
|  | 2944 | // Skip ACTION_UP events, and all events other than keys and motions | 
|  | 2945 | if (entry.type == EventEntry::Type::KEY) { | 
|  | 2946 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
|  | 2947 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
|  | 2948 | return; | 
|  | 2949 | } | 
|  | 2950 | } else if (entry.type == EventEntry::Type::MOTION) { | 
|  | 2951 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 2952 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || | 
|  | 2953 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 2954 | return; | 
|  | 2955 | } | 
|  | 2956 | } else { | 
|  | 2957 | return; // Not a key or a motion | 
|  | 2958 | } | 
|  | 2959 |  | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 2960 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2961 | std::vector<sp<Connection>> newConnections; | 
|  | 2962 | for (const InputTarget& target : targets) { | 
|  | 2963 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == | 
|  | 2964 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 2965 | continue; // Skip windows that receive ACTION_OUTSIDE | 
|  | 2966 | } | 
|  | 2967 |  | 
|  | 2968 | sp<IBinder> token = target.inputChannel->getConnectionToken(); | 
|  | 2969 | sp<Connection> connection = getConnectionLocked(token); | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2970 | if (connection == nullptr) { | 
|  | 2971 | continue; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2972 | } | 
|  | 2973 | newConnectionTokens.insert(std::move(token)); | 
|  | 2974 | newConnections.emplace_back(connection); | 
|  | 2975 | } | 
|  | 2976 | if (newConnectionTokens == mInteractionConnectionTokens) { | 
|  | 2977 | return; // no change | 
|  | 2978 | } | 
|  | 2979 | mInteractionConnectionTokens = newConnectionTokens; | 
|  | 2980 |  | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2981 | std::string targetList; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2982 | for (const sp<Connection>& connection : newConnections) { | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2983 | targetList += connection->getWindowName() + ", "; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2984 | } | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2985 | std::string message = "Interaction with: " + targetList; | 
|  | 2986 | if (targetList.empty()) { | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2987 | message += "<none>"; | 
|  | 2988 | } | 
|  | 2989 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; | 
|  | 2990 | } | 
|  | 2991 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2992 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2993 | const sp<IBinder>& token) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2994 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2995 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; | 
|  | 2996 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2997 | return; | 
|  | 2998 | } | 
|  | 2999 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3000 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3001 | if (focusedToken == token) { | 
|  | 3002 | // ignore since token is focused | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3003 | return; | 
|  | 3004 | } | 
|  | 3005 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3006 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 3007 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3008 | commandEntry->newToken = token; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3009 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3010 | } | 
|  | 3011 |  | 
|  | 3012 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3013 | const sp<Connection>& connection) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3014 | if (ATRACE_ENABLED()) { | 
|  | 3015 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3016 | connection->getInputChannelName().c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3017 | ATRACE_NAME(message.c_str()); | 
|  | 3018 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3019 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3020 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3021 | #endif | 
|  | 3022 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3023 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { | 
|  | 3024 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3025 | dispatchEntry->deliveryTime = currentTime; | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3026 | const std::chrono::nanoseconds timeout = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3027 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3028 | dispatchEntry->timeoutTime = currentTime + timeout.count(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3029 |  | 
|  | 3030 | // Publish the event. | 
|  | 3031 | status_t status; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3032 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); | 
|  | 3033 | switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3034 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3035 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
|  | 3036 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3037 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3038 | // Publish the key event. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3039 | status = connection->inputPublisher | 
|  | 3040 | .publishKeyEvent(dispatchEntry->seq, | 
|  | 3041 | dispatchEntry->resolvedEventId, keyEntry.deviceId, | 
|  | 3042 | keyEntry.source, keyEntry.displayId, | 
|  | 3043 | std::move(hmac), dispatchEntry->resolvedAction, | 
|  | 3044 | dispatchEntry->resolvedFlags, keyEntry.keyCode, | 
|  | 3045 | keyEntry.scanCode, keyEntry.metaState, | 
|  | 3046 | keyEntry.repeatCount, keyEntry.downTime, | 
|  | 3047 | keyEntry.eventTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3048 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3049 | } | 
|  | 3050 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3051 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3052 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3053 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3054 | PointerCoords scaledCoords[MAX_POINTERS]; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3055 | const PointerCoords* usingCoords = motionEntry.pointerCoords; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3056 |  | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3057 | // 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] | 3058 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3059 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { | 
|  | 3060 | float globalScaleFactor = dispatchEntry->globalScaleFactor; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3061 | if (globalScaleFactor != 1.0f) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3062 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { | 
|  | 3063 | scaledCoords[i] = motionEntry.pointerCoords[i]; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3064 | // Don't apply window scale here since we don't want scale to affect raw | 
|  | 3065 | // coordinates. The scale will be sent back to the client and applied | 
|  | 3066 | // later when requesting relative coordinates. | 
|  | 3067 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, | 
|  | 3068 | 1 /* windowYScale */); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3069 | } | 
|  | 3070 | usingCoords = scaledCoords; | 
|  | 3071 | } | 
|  | 3072 | } else { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3073 | // We don't want the dispatch target to know. | 
|  | 3074 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3075 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3076 | scaledCoords[i].clear(); | 
|  | 3077 | } | 
|  | 3078 | usingCoords = scaledCoords; | 
|  | 3079 | } | 
|  | 3080 | } | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3081 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3082 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3083 |  | 
|  | 3084 | // Publish the motion event. | 
|  | 3085 | status = connection->inputPublisher | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3086 | .publishMotionEvent(dispatchEntry->seq, | 
|  | 3087 | dispatchEntry->resolvedEventId, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3088 | motionEntry.deviceId, motionEntry.source, | 
|  | 3089 | motionEntry.displayId, std::move(hmac), | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3090 | dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3091 | motionEntry.actionButton, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3092 | dispatchEntry->resolvedFlags, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3093 | motionEntry.edgeFlags, motionEntry.metaState, | 
|  | 3094 | motionEntry.buttonState, | 
|  | 3095 | motionEntry.classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3096 | dispatchEntry->transform, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3097 | motionEntry.xPrecision, motionEntry.yPrecision, | 
|  | 3098 | motionEntry.xCursorPosition, | 
|  | 3099 | motionEntry.yCursorPosition, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3100 | dispatchEntry->displaySize.x, | 
|  | 3101 | dispatchEntry->displaySize.y, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3102 | motionEntry.downTime, motionEntry.eventTime, | 
|  | 3103 | motionEntry.pointerCount, | 
|  | 3104 | motionEntry.pointerProperties, usingCoords); | 
|  | 3105 | reportTouchEventForStatistics(motionEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3106 | break; | 
|  | 3107 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3108 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3109 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3110 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3111 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3112 | focusEntry.id, | 
|  | 3113 | focusEntry.hasFocus, | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3114 | mInTouchMode); | 
|  | 3115 | break; | 
|  | 3116 | } | 
|  | 3117 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3118 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
|  | 3119 | const auto& captureEntry = | 
|  | 3120 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); | 
|  | 3121 | status = connection->inputPublisher | 
|  | 3122 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, | 
|  | 3123 | captureEntry.pointerCaptureEnabled); | 
|  | 3124 | break; | 
|  | 3125 | } | 
|  | 3126 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3127 | case EventEntry::Type::DRAG: { | 
|  | 3128 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); | 
|  | 3129 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, | 
|  | 3130 | dragEntry.id, dragEntry.x, | 
|  | 3131 | dragEntry.y, | 
|  | 3132 | dragEntry.isExiting); | 
|  | 3133 | break; | 
|  | 3134 | } | 
|  | 3135 |  | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3136 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3137 | case EventEntry::Type::DEVICE_RESET: | 
|  | 3138 | case EventEntry::Type::SENSOR: { | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3139 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3140 | NamedEnum::string(eventEntry.type).c_str()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3141 | return; | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3142 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3143 | } | 
|  | 3144 |  | 
|  | 3145 | // Check the result. | 
|  | 3146 | if (status) { | 
|  | 3147 | if (status == WOULD_BLOCK) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3148 | if (connection->waitQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3149 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3150 | "This is unexpected because the wait queue is empty, so the pipe " | 
|  | 3151 | "should be empty and we shouldn't have any problems writing an " | 
| Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3152 | "event to it, status=%s(%d)", | 
|  | 3153 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), | 
|  | 3154 | status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3155 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
|  | 3156 | } else { | 
|  | 3157 | // Pipe is full and we are waiting for the app to finish process some events | 
|  | 3158 | // before sending more events to it. | 
|  | 3159 | #if DEBUG_DISPATCH_CYCLE | 
|  | 3160 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3161 | "waiting for the application to catch up", | 
|  | 3162 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3163 | #endif | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3164 | } | 
|  | 3165 | } else { | 
|  | 3166 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " | 
| Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3167 | "status=%s(%d)", | 
|  | 3168 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), | 
|  | 3169 | status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3170 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
|  | 3171 | } | 
|  | 3172 | return; | 
|  | 3173 | } | 
|  | 3174 |  | 
|  | 3175 | // Re-enqueue the event on the wait queue. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3176 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), | 
|  | 3177 | connection->outboundQueue.end(), | 
|  | 3178 | dispatchEntry)); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3179 | traceOutboundQueueLength(connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3180 | connection->waitQueue.push_back(dispatchEntry); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3181 | if (connection->responsive) { | 
|  | 3182 | mAnrTracker.insert(dispatchEntry->timeoutTime, | 
|  | 3183 | connection->inputChannel->getConnectionToken()); | 
|  | 3184 | } | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3185 | traceWaitQueueLength(connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3186 | } | 
|  | 3187 | } | 
|  | 3188 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3189 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { | 
|  | 3190 | size_t size; | 
|  | 3191 | switch (event.type) { | 
|  | 3192 | case VerifiedInputEvent::Type::KEY: { | 
|  | 3193 | size = sizeof(VerifiedKeyEvent); | 
|  | 3194 | break; | 
|  | 3195 | } | 
|  | 3196 | case VerifiedInputEvent::Type::MOTION: { | 
|  | 3197 | size = sizeof(VerifiedMotionEvent); | 
|  | 3198 | break; | 
|  | 3199 | } | 
|  | 3200 | } | 
|  | 3201 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); | 
|  | 3202 | return mHmacKeyManager.sign(start, size); | 
|  | 3203 | } | 
|  | 3204 |  | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3205 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
|  | 3206 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { | 
|  | 3207 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; | 
|  | 3208 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { | 
|  | 3209 | // Only sign events up and down events as the purely move events | 
|  | 3210 | // are tied to their up/down counterparts so signing would be redundant. | 
|  | 3211 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); | 
|  | 3212 | verifiedEvent.actionMasked = actionMasked; | 
|  | 3213 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3214 | return sign(verifiedEvent); | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3215 | } | 
|  | 3216 | return INVALID_HMAC; | 
|  | 3217 | } | 
|  | 3218 |  | 
|  | 3219 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
|  | 3220 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { | 
|  | 3221 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); | 
|  | 3222 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; | 
|  | 3223 | verifiedEvent.action = dispatchEntry.resolvedAction; | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3224 | return sign(verifiedEvent); | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3225 | } | 
|  | 3226 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3227 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3228 | const sp<Connection>& connection, uint32_t seq, | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3229 | bool handled, nsecs_t consumeTime) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3230 | #if DEBUG_DISPATCH_CYCLE | 
|  | 3231 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3232 | connection->getInputChannelName().c_str(), seq, toString(handled)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3233 | #endif | 
|  | 3234 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3235 | if (connection->status == Connection::STATUS_BROKEN || | 
|  | 3236 | connection->status == Connection::STATUS_ZOMBIE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3237 | return; | 
|  | 3238 | } | 
|  | 3239 |  | 
|  | 3240 | // Notify other system components and prepare to start the next dispatch cycle. | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3241 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled, consumeTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3242 | } | 
|  | 3243 |  | 
|  | 3244 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3245 | const sp<Connection>& connection, | 
|  | 3246 | bool notify) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3247 | #if DEBUG_DISPATCH_CYCLE | 
|  | 3248 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3249 | connection->getInputChannelName().c_str(), toString(notify)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3250 | #endif | 
|  | 3251 |  | 
|  | 3252 | // Clear the dispatch queues. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3253 | drainDispatchQueue(connection->outboundQueue); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3254 | traceOutboundQueueLength(connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3255 | drainDispatchQueue(connection->waitQueue); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3256 | traceWaitQueueLength(connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3257 |  | 
|  | 3258 | // The connection appears to be unrecoverably broken. | 
|  | 3259 | // Ignore already broken or zombie connections. | 
|  | 3260 | if (connection->status == Connection::STATUS_NORMAL) { | 
|  | 3261 | connection->status = Connection::STATUS_BROKEN; | 
|  | 3262 |  | 
|  | 3263 | if (notify) { | 
|  | 3264 | // Notify other system components. | 
|  | 3265 | onDispatchCycleBrokenLocked(currentTime, connection); | 
|  | 3266 | } | 
|  | 3267 | } | 
|  | 3268 | } | 
|  | 3269 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3270 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { | 
|  | 3271 | while (!queue.empty()) { | 
|  | 3272 | DispatchEntry* dispatchEntry = queue.front(); | 
|  | 3273 | queue.pop_front(); | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3274 | releaseDispatchEntry(dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3275 | } | 
|  | 3276 | } | 
|  | 3277 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3278 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3279 | if (dispatchEntry->hasForegroundTarget()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3280 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3281 | } | 
|  | 3282 | delete dispatchEntry; | 
|  | 3283 | } | 
|  | 3284 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3285 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { | 
|  | 3286 | std::scoped_lock _l(mLock); | 
|  | 3287 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
|  | 3288 | if (connection == nullptr) { | 
|  | 3289 | ALOGW("Received looper callback for unknown input channel token %p.  events=0x%x", | 
|  | 3290 | connectionToken.get(), events); | 
|  | 3291 | return 0; // remove the callback | 
|  | 3292 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3293 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3294 | bool notify; | 
|  | 3295 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { | 
|  | 3296 | if (!(events & ALOOPER_EVENT_INPUT)) { | 
|  | 3297 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event.  " | 
|  | 3298 | "events=0x%x", | 
|  | 3299 | connection->getInputChannelName().c_str(), events); | 
|  | 3300 | return 1; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3301 | } | 
|  | 3302 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3303 | nsecs_t currentTime = now(); | 
|  | 3304 | bool gotOne = false; | 
|  | 3305 | status_t status = OK; | 
|  | 3306 | for (;;) { | 
|  | 3307 | Result<InputPublisher::ConsumerResponse> result = | 
|  | 3308 | connection->inputPublisher.receiveConsumerResponse(); | 
|  | 3309 | if (!result.ok()) { | 
|  | 3310 | status = result.error().code(); | 
|  | 3311 | break; | 
|  | 3312 | } | 
|  | 3313 |  | 
|  | 3314 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { | 
|  | 3315 | const InputPublisher::Finished& finish = | 
|  | 3316 | std::get<InputPublisher::Finished>(*result); | 
|  | 3317 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, | 
|  | 3318 | finish.consumeTime); | 
|  | 3319 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { | 
|  | 3320 | // TODO(b/167947340): Report this data to LatencyTracker | 
|  | 3321 | } | 
|  | 3322 | gotOne = true; | 
|  | 3323 | } | 
|  | 3324 | if (gotOne) { | 
|  | 3325 | runCommandsLockedInterruptible(); | 
|  | 3326 | if (status == WOULD_BLOCK) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3327 | return 1; | 
|  | 3328 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3329 | } | 
|  | 3330 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3331 | notify = status != DEAD_OBJECT || !connection->monitor; | 
|  | 3332 | if (notify) { | 
|  | 3333 | ALOGE("channel '%s' ~ Failed to receive finished signal.  status=%s(%d)", | 
|  | 3334 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), | 
|  | 3335 | status); | 
|  | 3336 | } | 
|  | 3337 | } else { | 
|  | 3338 | // Monitor channels are never explicitly unregistered. | 
|  | 3339 | // We do it automatically when the remote endpoint is closed so don't warn about them. | 
|  | 3340 | const bool stillHaveWindowHandle = | 
|  | 3341 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; | 
|  | 3342 | notify = !connection->monitor && stillHaveWindowHandle; | 
|  | 3343 | if (notify) { | 
|  | 3344 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred.  events=0x%x", | 
|  | 3345 | connection->getInputChannelName().c_str(), events); | 
|  | 3346 | } | 
|  | 3347 | } | 
|  | 3348 |  | 
|  | 3349 | // Remove the channel. | 
|  | 3350 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); | 
|  | 3351 | return 0; // remove the callback | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3352 | } | 
|  | 3353 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3354 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3355 | const CancelationOptions& options) { | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3356 | for (const auto& [token, connection] : mConnectionsByToken) { | 
| Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3357 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3358 | } | 
|  | 3359 | } | 
|  | 3360 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3361 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3362 | const CancelationOptions& options) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3363 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); | 
|  | 3364 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); | 
|  | 3365 | } | 
|  | 3366 |  | 
|  | 3367 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
|  | 3368 | const CancelationOptions& options, | 
|  | 3369 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
|  | 3370 | for (const auto& it : monitorsByDisplay) { | 
|  | 3371 | const std::vector<Monitor>& monitors = it.second; | 
|  | 3372 | for (const Monitor& monitor : monitors) { | 
|  | 3373 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3374 | } | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3375 | } | 
|  | 3376 | } | 
|  | 3377 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3378 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3379 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3380 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3381 | if (connection == nullptr) { | 
|  | 3382 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3383 | } | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3384 |  | 
|  | 3385 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3386 | } | 
|  | 3387 |  | 
|  | 3388 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( | 
|  | 3389 | const sp<Connection>& connection, const CancelationOptions& options) { | 
|  | 3390 | if (connection->status == Connection::STATUS_BROKEN) { | 
|  | 3391 | return; | 
|  | 3392 | } | 
|  | 3393 |  | 
|  | 3394 | nsecs_t currentTime = now(); | 
|  | 3395 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3396 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = | 
| Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3397 | connection->inputState.synthesizeCancelationEvents(currentTime, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3398 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3399 | if (cancelationEvents.empty()) { | 
|  | 3400 | return; | 
|  | 3401 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3402 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3403 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " | 
|  | 3404 | "with reality: %s, mode=%d.", | 
|  | 3405 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, | 
|  | 3406 | options.mode); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3407 | #endif | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3408 |  | 
|  | 3409 | InputTarget target; | 
|  | 3410 | sp<InputWindowHandle> windowHandle = | 
|  | 3411 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
|  | 3412 | if (windowHandle != nullptr) { | 
|  | 3413 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3414 | target.setDefaultPointerTransform(windowInfo->transform); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3415 | target.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 3416 | } | 
|  | 3417 | target.inputChannel = connection->inputChannel; | 
|  | 3418 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 3419 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3420 | for (size_t i = 0; i < cancelationEvents.size(); i++) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3421 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3422 | switch (cancelationEventEntry->type) { | 
|  | 3423 | case EventEntry::Type::KEY: { | 
|  | 3424 | logOutboundKeyDetails("cancel - ", | 
|  | 3425 | static_cast<const KeyEntry&>(*cancelationEventEntry)); | 
|  | 3426 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3427 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3428 | case EventEntry::Type::MOTION: { | 
|  | 3429 | logOutboundMotionDetails("cancel - ", | 
|  | 3430 | static_cast<const MotionEntry&>(*cancelationEventEntry)); | 
|  | 3431 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3432 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3433 | case EventEntry::Type::FOCUS: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3434 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 3435 | case EventEntry::Type::DRAG: { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3436 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3437 | NamedEnum::string(cancelationEventEntry->type).c_str()); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3438 | break; | 
|  | 3439 | } | 
|  | 3440 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3441 | case EventEntry::Type::DEVICE_RESET: | 
|  | 3442 | case EventEntry::Type::SENSOR: { | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3443 | 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] | 3444 | NamedEnum::string(cancelationEventEntry->type).c_str()); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3445 | break; | 
|  | 3446 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3447 | } | 
|  | 3448 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3449 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, | 
|  | 3450 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3451 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3452 |  | 
|  | 3453 | startDispatchCycleLocked(currentTime, connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3454 | } | 
|  | 3455 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3456 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( | 
|  | 3457 | const sp<Connection>& connection) { | 
|  | 3458 | if (connection->status == Connection::STATUS_BROKEN) { | 
|  | 3459 | return; | 
|  | 3460 | } | 
|  | 3461 |  | 
|  | 3462 | nsecs_t currentTime = now(); | 
|  | 3463 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3464 | std::vector<std::unique_ptr<EventEntry>> downEvents = | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3465 | connection->inputState.synthesizePointerDownEvents(currentTime); | 
|  | 3466 |  | 
|  | 3467 | if (downEvents.empty()) { | 
|  | 3468 | return; | 
|  | 3469 | } | 
|  | 3470 |  | 
|  | 3471 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 3472 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", | 
|  | 3473 | connection->getInputChannelName().c_str(), downEvents.size()); | 
|  | 3474 | #endif | 
|  | 3475 |  | 
|  | 3476 | InputTarget target; | 
|  | 3477 | sp<InputWindowHandle> windowHandle = | 
|  | 3478 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
|  | 3479 | if (windowHandle != nullptr) { | 
|  | 3480 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3481 | target.setDefaultPointerTransform(windowInfo->transform); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3482 | target.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 3483 | } | 
|  | 3484 | target.inputChannel = connection->inputChannel; | 
|  | 3485 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 3486 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3487 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3488 | switch (downEventEntry->type) { | 
|  | 3489 | case EventEntry::Type::MOTION: { | 
|  | 3490 | logOutboundMotionDetails("down - ", | 
|  | 3491 | static_cast<const MotionEntry&>(*downEventEntry)); | 
|  | 3492 | break; | 
|  | 3493 | } | 
|  | 3494 |  | 
|  | 3495 | case EventEntry::Type::KEY: | 
|  | 3496 | case EventEntry::Type::FOCUS: | 
|  | 3497 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3498 | case EventEntry::Type::DEVICE_RESET: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3499 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3500 | case EventEntry::Type::SENSOR: | 
|  | 3501 | case EventEntry::Type::DRAG: { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3502 | 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] | 3503 | NamedEnum::string(downEventEntry->type).c_str()); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3504 | break; | 
|  | 3505 | } | 
|  | 3506 | } | 
|  | 3507 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3508 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, | 
|  | 3509 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3510 | } | 
|  | 3511 |  | 
|  | 3512 | startDispatchCycleLocked(currentTime, connection); | 
|  | 3513 | } | 
|  | 3514 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3515 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( | 
|  | 3516 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3517 | ALOG_ASSERT(pointerIds.value != 0); | 
|  | 3518 |  | 
|  | 3519 | uint32_t splitPointerIndexMap[MAX_POINTERS]; | 
|  | 3520 | PointerProperties splitPointerProperties[MAX_POINTERS]; | 
|  | 3521 | PointerCoords splitPointerCoords[MAX_POINTERS]; | 
|  | 3522 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3523 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3524 | uint32_t splitPointerCount = 0; | 
|  | 3525 |  | 
|  | 3526 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3527 | originalPointerIndex++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3528 | const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3529 | originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3530 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 3531 | if (pointerIds.hasBit(pointerId)) { | 
|  | 3532 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; | 
|  | 3533 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); | 
|  | 3534 | splitPointerCoords[splitPointerCount].copyFrom( | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3535 | originalMotionEntry.pointerCoords[originalPointerIndex]); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3536 | splitPointerCount += 1; | 
|  | 3537 | } | 
|  | 3538 | } | 
|  | 3539 |  | 
|  | 3540 | if (splitPointerCount != pointerIds.count()) { | 
|  | 3541 | // This is bad.  We are missing some of the pointers that we expected to deliver. | 
|  | 3542 | // Most likely this indicates that we received an ACTION_MOVE events that has | 
|  | 3543 | // different pointer ids than we expected based on the previous ACTION_DOWN | 
|  | 3544 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers | 
|  | 3545 | // in this way. | 
|  | 3546 | ALOGW("Dropping split motion event because the pointer count is %d but " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3547 | "we expected there to be %d pointers.  This probably means we received " | 
|  | 3548 | "a broken sequence of pointer ids from the input device.", | 
|  | 3549 | splitPointerCount, pointerIds.count()); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3550 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3551 | } | 
|  | 3552 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3553 | int32_t action = originalMotionEntry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3554 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3555 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || | 
|  | 3556 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3557 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 3558 | const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3559 | originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3560 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 3561 | if (pointerIds.hasBit(pointerId)) { | 
|  | 3562 | if (pointerIds.count() == 1) { | 
|  | 3563 | // The first/last pointer went down/up. | 
|  | 3564 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3565 | ? AMOTION_EVENT_ACTION_DOWN | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3566 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 | 
|  | 3567 | ? AMOTION_EVENT_ACTION_CANCEL | 
|  | 3568 | : AMOTION_EVENT_ACTION_UP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3569 | } else { | 
|  | 3570 | // A secondary pointer went down/up. | 
|  | 3571 | uint32_t splitPointerIndex = 0; | 
|  | 3572 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { | 
|  | 3573 | splitPointerIndex += 1; | 
|  | 3574 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3575 | action = maskedAction | | 
|  | 3576 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3577 | } | 
|  | 3578 | } else { | 
|  | 3579 | // An unrelated pointer changed. | 
|  | 3580 | action = AMOTION_EVENT_ACTION_MOVE; | 
|  | 3581 | } | 
|  | 3582 | } | 
|  | 3583 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3584 | int32_t newId = mIdGenerator.nextId(); | 
|  | 3585 | if (ATRACE_ENABLED()) { | 
|  | 3586 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 | 
|  | 3587 | ") to MotionEvent(id=0x%" PRIx32 ").", | 
|  | 3588 | originalMotionEntry.id, newId); | 
|  | 3589 | ATRACE_NAME(message.c_str()); | 
|  | 3590 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3591 | std::unique_ptr<MotionEntry> splitMotionEntry = | 
|  | 3592 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, | 
|  | 3593 | originalMotionEntry.deviceId, originalMotionEntry.source, | 
|  | 3594 | originalMotionEntry.displayId, | 
|  | 3595 | originalMotionEntry.policyFlags, action, | 
|  | 3596 | originalMotionEntry.actionButton, | 
|  | 3597 | originalMotionEntry.flags, originalMotionEntry.metaState, | 
|  | 3598 | originalMotionEntry.buttonState, | 
|  | 3599 | originalMotionEntry.classification, | 
|  | 3600 | originalMotionEntry.edgeFlags, | 
|  | 3601 | originalMotionEntry.xPrecision, | 
|  | 3602 | originalMotionEntry.yPrecision, | 
|  | 3603 | originalMotionEntry.xCursorPosition, | 
|  | 3604 | originalMotionEntry.yCursorPosition, | 
|  | 3605 | originalMotionEntry.downTime, splitPointerCount, | 
|  | 3606 | splitPointerProperties, splitPointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3607 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3608 | if (originalMotionEntry.injectionState) { | 
|  | 3609 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3610 | splitMotionEntry->injectionState->refCount += 1; | 
|  | 3611 | } | 
|  | 3612 |  | 
|  | 3613 | return splitMotionEntry; | 
|  | 3614 | } | 
|  | 3615 |  | 
|  | 3616 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { | 
|  | 3617 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3618 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3619 | #endif | 
|  | 3620 |  | 
|  | 3621 | bool needWake; | 
|  | 3622 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3623 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3624 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3625 | std::unique_ptr<ConfigurationChangedEntry> newEntry = | 
|  | 3626 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); | 
|  | 3627 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3628 | } // release lock | 
|  | 3629 |  | 
|  | 3630 | if (needWake) { | 
|  | 3631 | mLooper->wake(); | 
|  | 3632 | } | 
|  | 3633 | } | 
|  | 3634 |  | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3635 | /** | 
|  | 3636 | * If one of the meta shortcuts is detected, process them here: | 
|  | 3637 | *     Meta + Backspace -> generate BACK | 
|  | 3638 | *     Meta + Enter -> generate HOME | 
|  | 3639 | * This will potentially overwrite keyCode and metaState. | 
|  | 3640 | */ | 
|  | 3641 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3642 | int32_t& keyCode, int32_t& metaState) { | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3643 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { | 
|  | 3644 | int32_t newKeyCode = AKEYCODE_UNKNOWN; | 
|  | 3645 | if (keyCode == AKEYCODE_DEL) { | 
|  | 3646 | newKeyCode = AKEYCODE_BACK; | 
|  | 3647 | } else if (keyCode == AKEYCODE_ENTER) { | 
|  | 3648 | newKeyCode = AKEYCODE_HOME; | 
|  | 3649 | } | 
|  | 3650 | if (newKeyCode != AKEYCODE_UNKNOWN) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3651 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3652 | struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3653 | mReplacedKeys[replacement] = newKeyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3654 | keyCode = newKeyCode; | 
|  | 3655 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
|  | 3656 | } | 
|  | 3657 | } else if (action == AKEY_EVENT_ACTION_UP) { | 
|  | 3658 | // In order to maintain a consistent stream of up and down events, check to see if the key | 
|  | 3659 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, | 
|  | 3660 | // 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] | 3661 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3662 | struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3663 | auto replacementIt = mReplacedKeys.find(replacement); | 
|  | 3664 | if (replacementIt != mReplacedKeys.end()) { | 
|  | 3665 | keyCode = replacementIt->second; | 
|  | 3666 | mReplacedKeys.erase(replacementIt); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3667 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
|  | 3668 | } | 
|  | 3669 | } | 
|  | 3670 | } | 
|  | 3671 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3672 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { | 
|  | 3673 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3674 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
|  | 3675 | "policyFlags=0x%x, action=0x%x, " | 
|  | 3676 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, | 
|  | 3677 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, | 
|  | 3678 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, | 
|  | 3679 | args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3680 | #endif | 
|  | 3681 | if (!validateKeyEvent(args->action)) { | 
|  | 3682 | return; | 
|  | 3683 | } | 
|  | 3684 |  | 
|  | 3685 | uint32_t policyFlags = args->policyFlags; | 
|  | 3686 | int32_t flags = args->flags; | 
|  | 3687 | int32_t metaState = args->metaState; | 
| Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3688 | // InputDispatcher tracks and generates key repeats on behalf of | 
|  | 3689 | // whatever notifies it, so repeatCount should always be set to 0 | 
|  | 3690 | constexpr int32_t repeatCount = 0; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3691 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { | 
|  | 3692 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
|  | 3693 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; | 
|  | 3694 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3695 | if (policyFlags & POLICY_FLAG_FUNCTION) { | 
|  | 3696 | metaState |= AMETA_FUNCTION_ON; | 
|  | 3697 | } | 
|  | 3698 |  | 
|  | 3699 | policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 3700 |  | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3701 | int32_t keyCode = args->keyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3702 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3703 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3704 | KeyEvent event; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3705 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3706 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, | 
|  | 3707 | args->downTime, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3708 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3709 | android::base::Timer t; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3710 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3711 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3712 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3713 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3714 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3715 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3716 | bool needWake; | 
|  | 3717 | { // acquire lock | 
|  | 3718 | mLock.lock(); | 
|  | 3719 |  | 
|  | 3720 | if (shouldSendKeyToInputFilterLocked(args)) { | 
|  | 3721 | mLock.unlock(); | 
|  | 3722 |  | 
|  | 3723 | policyFlags |= POLICY_FLAG_FILTERED; | 
|  | 3724 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
|  | 3725 | return; // event was consumed by the filter | 
|  | 3726 | } | 
|  | 3727 |  | 
|  | 3728 | mLock.lock(); | 
|  | 3729 | } | 
|  | 3730 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3731 | std::unique_ptr<KeyEntry> newEntry = | 
|  | 3732 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, | 
|  | 3733 | args->displayId, policyFlags, args->action, flags, | 
|  | 3734 | keyCode, args->scanCode, metaState, repeatCount, | 
|  | 3735 | args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3736 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3737 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3738 | mLock.unlock(); | 
|  | 3739 | } // release lock | 
|  | 3740 |  | 
|  | 3741 | if (needWake) { | 
|  | 3742 | mLooper->wake(); | 
|  | 3743 | } | 
|  | 3744 | } | 
|  | 3745 |  | 
|  | 3746 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { | 
|  | 3747 | return mInputFilterEnabled; | 
|  | 3748 | } | 
|  | 3749 |  | 
|  | 3750 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { | 
|  | 3751 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3752 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " | 
|  | 3753 | "displayId=%" PRId32 ", policyFlags=0x%x, " | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3754 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " | 
|  | 3755 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " | 
| Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3756 | "yCursorPosition=%f, downTime=%" PRId64, | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3757 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, | 
|  | 3758 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, | 
|  | 3759 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, | 
|  | 3760 | args->xCursorPosition, args->yCursorPosition, args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3761 | for (uint32_t i = 0; i < args->pointerCount; i++) { | 
|  | 3762 | ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3763 | "x=%f, y=%f, pressure=%f, size=%f, " | 
|  | 3764 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
|  | 3765 | "orientation=%f", | 
|  | 3766 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, | 
|  | 3767 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 3768 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 3769 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 3770 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
|  | 3771 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 3772 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 3773 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 3774 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 3775 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3776 | } | 
|  | 3777 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3778 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, | 
|  | 3779 | args->pointerProperties)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3780 | return; | 
|  | 3781 | } | 
|  | 3782 |  | 
|  | 3783 | uint32_t policyFlags = args->policyFlags; | 
|  | 3784 | policyFlags |= POLICY_FLAG_TRUSTED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3785 |  | 
|  | 3786 | android::base::Timer t; | 
| Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3787 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3788 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3789 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3790 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3791 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3792 |  | 
|  | 3793 | bool needWake; | 
|  | 3794 | { // acquire lock | 
|  | 3795 | mLock.lock(); | 
|  | 3796 |  | 
|  | 3797 | if (shouldSendMotionToInputFilterLocked(args)) { | 
|  | 3798 | mLock.unlock(); | 
|  | 3799 |  | 
|  | 3800 | MotionEvent event; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3801 | ui::Transform transform; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3802 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
|  | 3803 | args->action, args->actionButton, args->flags, args->edgeFlags, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3804 | args->metaState, args->buttonState, args->classification, transform, | 
|  | 3805 | args->xPrecision, args->yPrecision, args->xCursorPosition, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3806 | args->yCursorPosition, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 3807 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, args->downTime, args->eventTime, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3808 | args->pointerCount, args->pointerProperties, args->pointerCoords); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3809 |  | 
|  | 3810 | policyFlags |= POLICY_FLAG_FILTERED; | 
|  | 3811 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
|  | 3812 | return; // event was consumed by the filter | 
|  | 3813 | } | 
|  | 3814 |  | 
|  | 3815 | mLock.lock(); | 
|  | 3816 | } | 
|  | 3817 |  | 
|  | 3818 | // Just enqueue a new motion event. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3819 | std::unique_ptr<MotionEntry> newEntry = | 
|  | 3820 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, | 
|  | 3821 | args->source, args->displayId, policyFlags, | 
|  | 3822 | args->action, args->actionButton, args->flags, | 
|  | 3823 | args->metaState, args->buttonState, | 
|  | 3824 | args->classification, args->edgeFlags, | 
|  | 3825 | args->xPrecision, args->yPrecision, | 
|  | 3826 | args->xCursorPosition, args->yCursorPosition, | 
|  | 3827 | args->downTime, args->pointerCount, | 
|  | 3828 | args->pointerProperties, args->pointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3829 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3830 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3831 | mLock.unlock(); | 
|  | 3832 | } // release lock | 
|  | 3833 |  | 
|  | 3834 | if (needWake) { | 
|  | 3835 | mLooper->wake(); | 
|  | 3836 | } | 
|  | 3837 | } | 
|  | 3838 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3839 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { | 
|  | 3840 | #if DEBUG_INBOUND_EVENT_DETAILS | 
|  | 3841 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " | 
|  | 3842 | " sensorType=%s", | 
|  | 3843 | args->id, args->eventTime, args->deviceId, args->source, | 
|  | 3844 | NamedEnum::string(args->sensorType).c_str()); | 
|  | 3845 | #endif | 
|  | 3846 |  | 
|  | 3847 | bool needWake; | 
|  | 3848 | { // acquire lock | 
|  | 3849 | mLock.lock(); | 
|  | 3850 |  | 
|  | 3851 | // Just enqueue a new sensor event. | 
|  | 3852 | std::unique_ptr<SensorEntry> newEntry = | 
|  | 3853 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, | 
|  | 3854 | args->source, 0 /* policyFlags*/, args->hwTimestamp, | 
|  | 3855 | args->sensorType, args->accuracy, | 
|  | 3856 | args->accuracyChanged, args->values); | 
|  | 3857 |  | 
|  | 3858 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
|  | 3859 | mLock.unlock(); | 
|  | 3860 | } // release lock | 
|  | 3861 |  | 
|  | 3862 | if (needWake) { | 
|  | 3863 | mLooper->wake(); | 
|  | 3864 | } | 
|  | 3865 | } | 
|  | 3866 |  | 
| Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 3867 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { | 
|  | 3868 | #if DEBUG_INBOUND_EVENT_DETAILS | 
|  | 3869 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d,  isOn=%d", args->eventTime, | 
|  | 3870 | args->deviceId, args->isOn); | 
|  | 3871 | #endif | 
|  | 3872 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); | 
|  | 3873 | } | 
|  | 3874 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3875 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3876 | return mInputFilterEnabled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3877 | } | 
|  | 3878 |  | 
|  | 3879 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { | 
|  | 3880 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3881 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3882 | "switchMask=0x%08x", | 
|  | 3883 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3884 | #endif | 
|  | 3885 |  | 
|  | 3886 | uint32_t policyFlags = args->policyFlags; | 
|  | 3887 | policyFlags |= POLICY_FLAG_TRUSTED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3888 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3889 | } | 
|  | 3890 |  | 
|  | 3891 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { | 
|  | 3892 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3893 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, | 
|  | 3894 | args->deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3895 | #endif | 
|  | 3896 |  | 
|  | 3897 | bool needWake; | 
|  | 3898 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3899 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3900 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3901 | std::unique_ptr<DeviceResetEntry> newEntry = | 
|  | 3902 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); | 
|  | 3903 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3904 | } // release lock | 
|  | 3905 |  | 
|  | 3906 | if (needWake) { | 
|  | 3907 | mLooper->wake(); | 
|  | 3908 | } | 
|  | 3909 | } | 
|  | 3910 |  | 
| Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3911 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { | 
|  | 3912 | #if DEBUG_INBOUND_EVENT_DETAILS | 
|  | 3913 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, | 
|  | 3914 | args->enabled ? "true" : "false"); | 
|  | 3915 | #endif | 
|  | 3916 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3917 | bool needWake; | 
|  | 3918 | { // acquire lock | 
|  | 3919 | std::scoped_lock _l(mLock); | 
|  | 3920 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, | 
|  | 3921 | args->enabled); | 
|  | 3922 | needWake = enqueueInboundEventLocked(std::move(entry)); | 
|  | 3923 | } // release lock | 
|  | 3924 |  | 
|  | 3925 | if (needWake) { | 
|  | 3926 | mLooper->wake(); | 
|  | 3927 | } | 
| Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3928 | } | 
|  | 3929 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3930 | InputEventInjectionResult InputDispatcher::injectInputEvent( | 
|  | 3931 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, | 
|  | 3932 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3933 | #if DEBUG_INBOUND_EVENT_DETAILS | 
|  | 3934 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3935 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", | 
|  | 3936 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3937 | #endif | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3938 | 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] | 3939 |  | 
|  | 3940 | policyFlags |= POLICY_FLAG_INJECTED; | 
|  | 3941 | if (hasInjectionPermission(injectorPid, injectorUid)) { | 
|  | 3942 | policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 3943 | } | 
|  | 3944 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3945 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3946 | switch (event->getType()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3947 | case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3948 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); | 
|  | 3949 | int32_t action = incomingKey.getAction(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3950 | if (!validateKeyEvent(action)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3951 | return InputEventInjectionResult::FAILED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3952 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3953 |  | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3954 | int32_t flags = incomingKey.getFlags(); | 
|  | 3955 | int32_t keyCode = incomingKey.getKeyCode(); | 
|  | 3956 | int32_t metaState = incomingKey.getMetaState(); | 
|  | 3957 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3958 | /*byref*/ keyCode, /*byref*/ metaState); | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3959 | KeyEvent keyEvent; | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3960 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3961 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, | 
|  | 3962 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), | 
|  | 3963 | incomingKey.getDownTime(), incomingKey.getEventTime()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3964 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3965 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { | 
|  | 3966 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3967 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3968 |  | 
|  | 3969 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
|  | 3970 | android::base::Timer t; | 
|  | 3971 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); | 
|  | 3972 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3973 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
|  | 3974 | std::to_string(t.duration().count()).c_str()); | 
|  | 3975 | } | 
|  | 3976 | } | 
|  | 3977 |  | 
|  | 3978 | mLock.lock(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3979 | std::unique_ptr<KeyEntry> injectedEntry = | 
|  | 3980 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), | 
|  | 3981 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), | 
|  | 3982 | incomingKey.getDisplayId(), policyFlags, action, | 
|  | 3983 | flags, keyCode, incomingKey.getScanCode(), metaState, | 
|  | 3984 | incomingKey.getRepeatCount(), | 
|  | 3985 | incomingKey.getDownTime()); | 
|  | 3986 | injectedEntries.push(std::move(injectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3987 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3988 | } | 
|  | 3989 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3990 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 3991 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); | 
|  | 3992 | int32_t action = motionEvent->getAction(); | 
|  | 3993 | size_t pointerCount = motionEvent->getPointerCount(); | 
|  | 3994 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); | 
|  | 3995 | int32_t actionButton = motionEvent->getActionButton(); | 
|  | 3996 | int32_t displayId = motionEvent->getDisplayId(); | 
|  | 3997 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3998 | return InputEventInjectionResult::FAILED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3999 | } | 
|  | 4000 |  | 
|  | 4001 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
|  | 4002 | nsecs_t eventTime = motionEvent->getEventTime(); | 
|  | 4003 | android::base::Timer t; | 
|  | 4004 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); | 
|  | 4005 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 4006 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
|  | 4007 | std::to_string(t.duration().count()).c_str()); | 
|  | 4008 | } | 
|  | 4009 | } | 
|  | 4010 |  | 
|  | 4011 | mLock.lock(); | 
|  | 4012 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); | 
|  | 4013 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4014 | std::unique_ptr<MotionEntry> injectedEntry = | 
|  | 4015 | std::make_unique<MotionEntry>(motionEvent->getId(), *sampleEventTimes, | 
|  | 4016 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), | 
|  | 4017 | motionEvent->getDisplayId(), policyFlags, action, | 
|  | 4018 | actionButton, motionEvent->getFlags(), | 
|  | 4019 | motionEvent->getMetaState(), | 
|  | 4020 | motionEvent->getButtonState(), | 
|  | 4021 | motionEvent->getClassification(), | 
|  | 4022 | motionEvent->getEdgeFlags(), | 
|  | 4023 | motionEvent->getXPrecision(), | 
|  | 4024 | motionEvent->getYPrecision(), | 
|  | 4025 | motionEvent->getRawXCursorPosition(), | 
|  | 4026 | motionEvent->getRawYCursorPosition(), | 
|  | 4027 | motionEvent->getDownTime(), | 
|  | 4028 | uint32_t(pointerCount), pointerProperties, | 
|  | 4029 | samplePointerCoords, motionEvent->getXOffset(), | 
|  | 4030 | motionEvent->getYOffset()); | 
|  | 4031 | injectedEntries.push(std::move(injectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4032 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { | 
|  | 4033 | sampleEventTimes += 1; | 
|  | 4034 | samplePointerCoords += pointerCount; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4035 | std::unique_ptr<MotionEntry> nextInjectedEntry = | 
|  | 4036 | std::make_unique<MotionEntry>(motionEvent->getId(), *sampleEventTimes, | 
|  | 4037 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), | 
|  | 4038 | motionEvent->getDisplayId(), policyFlags, | 
|  | 4039 | action, actionButton, motionEvent->getFlags(), | 
|  | 4040 | motionEvent->getMetaState(), | 
|  | 4041 | motionEvent->getButtonState(), | 
|  | 4042 | motionEvent->getClassification(), | 
|  | 4043 | motionEvent->getEdgeFlags(), | 
|  | 4044 | motionEvent->getXPrecision(), | 
|  | 4045 | motionEvent->getYPrecision(), | 
|  | 4046 | motionEvent->getRawXCursorPosition(), | 
|  | 4047 | motionEvent->getRawYCursorPosition(), | 
|  | 4048 | motionEvent->getDownTime(), | 
|  | 4049 | uint32_t(pointerCount), pointerProperties, | 
|  | 4050 | samplePointerCoords, | 
|  | 4051 | motionEvent->getXOffset(), | 
|  | 4052 | motionEvent->getYOffset()); | 
|  | 4053 | injectedEntries.push(std::move(nextInjectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4054 | } | 
|  | 4055 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4056 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4057 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4058 | default: | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4059 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4060 | return InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4061 | } | 
|  | 4062 |  | 
|  | 4063 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4064 | if (syncMode == InputEventInjectionSync::NONE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4065 | injectionState->injectionIsAsync = true; | 
|  | 4066 | } | 
|  | 4067 |  | 
|  | 4068 | injectionState->refCount += 1; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4069 | injectedEntries.back()->injectionState = injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4070 |  | 
|  | 4071 | bool needWake = false; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4072 | while (!injectedEntries.empty()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4073 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4074 | injectedEntries.pop(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4075 | } | 
|  | 4076 |  | 
|  | 4077 | mLock.unlock(); | 
|  | 4078 |  | 
|  | 4079 | if (needWake) { | 
|  | 4080 | mLooper->wake(); | 
|  | 4081 | } | 
|  | 4082 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4083 | InputEventInjectionResult injectionResult; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4084 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4085 | std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4086 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4087 | if (syncMode == InputEventInjectionSync::NONE) { | 
|  | 4088 | injectionResult = InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4089 | } else { | 
|  | 4090 | for (;;) { | 
|  | 4091 | injectionResult = injectionState->injectionResult; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4092 | if (injectionResult != InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4093 | break; | 
|  | 4094 | } | 
|  | 4095 |  | 
|  | 4096 | nsecs_t remainingTimeout = endTime - now(); | 
|  | 4097 | if (remainingTimeout <= 0) { | 
|  | 4098 | #if DEBUG_INJECTION | 
|  | 4099 | ALOGD("injectInputEvent - Timed out waiting for injection result " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4100 | "to become available."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4101 | #endif | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4102 | injectionResult = InputEventInjectionResult::TIMED_OUT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4103 | break; | 
|  | 4104 | } | 
|  | 4105 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4106 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4107 | } | 
|  | 4108 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4109 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && | 
|  | 4110 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4111 | while (injectionState->pendingForegroundDispatches != 0) { | 
|  | 4112 | #if DEBUG_INJECTION | 
|  | 4113 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4114 | injectionState->pendingForegroundDispatches); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4115 | #endif | 
|  | 4116 | nsecs_t remainingTimeout = endTime - now(); | 
|  | 4117 | if (remainingTimeout <= 0) { | 
|  | 4118 | #if DEBUG_INJECTION | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4119 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " | 
|  | 4120 | "dispatches to finish."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4121 | #endif | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4122 | injectionResult = InputEventInjectionResult::TIMED_OUT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4123 | break; | 
|  | 4124 | } | 
|  | 4125 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4126 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4127 | } | 
|  | 4128 | } | 
|  | 4129 | } | 
|  | 4130 |  | 
|  | 4131 | injectionState->release(); | 
|  | 4132 | } // release lock | 
|  | 4133 |  | 
|  | 4134 | #if DEBUG_INJECTION | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4135 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4136 | injectionResult, injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4137 | #endif | 
|  | 4138 |  | 
|  | 4139 | return injectionResult; | 
|  | 4140 | } | 
|  | 4141 |  | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4142 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4143 | std::array<uint8_t, 32> calculatedHmac; | 
|  | 4144 | std::unique_ptr<VerifiedInputEvent> result; | 
|  | 4145 | switch (event.getType()) { | 
|  | 4146 | case AINPUT_EVENT_TYPE_KEY: { | 
|  | 4147 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); | 
|  | 4148 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); | 
|  | 4149 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4150 | calculatedHmac = sign(verifiedKeyEvent); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4151 | break; | 
|  | 4152 | } | 
|  | 4153 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 4154 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); | 
|  | 4155 | VerifiedMotionEvent verifiedMotionEvent = | 
|  | 4156 | verifiedMotionEventFromMotionEvent(motionEvent); | 
|  | 4157 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4158 | calculatedHmac = sign(verifiedMotionEvent); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4159 | break; | 
|  | 4160 | } | 
|  | 4161 | default: { | 
|  | 4162 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); | 
|  | 4163 | return nullptr; | 
|  | 4164 | } | 
|  | 4165 | } | 
|  | 4166 | if (calculatedHmac == INVALID_HMAC) { | 
|  | 4167 | return nullptr; | 
|  | 4168 | } | 
|  | 4169 | if (calculatedHmac != event.getHmac()) { | 
|  | 4170 | return nullptr; | 
|  | 4171 | } | 
|  | 4172 | return result; | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4173 | } | 
|  | 4174 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4175 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4176 | return injectorUid == 0 || | 
|  | 4177 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4178 | } | 
|  | 4179 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4180 | void InputDispatcher::setInjectionResult(EventEntry& entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4181 | InputEventInjectionResult injectionResult) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4182 | InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4183 | if (injectionState) { | 
|  | 4184 | #if DEBUG_INJECTION | 
|  | 4185 | ALOGD("Setting input event injection result to %d.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4186 | "injectorPid=%d, injectorUid=%d", | 
|  | 4187 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4188 | #endif | 
|  | 4189 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4190 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4191 | // Log the outcome since the injector did not wait for the injection result. | 
|  | 4192 | switch (injectionResult) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4193 | case InputEventInjectionResult::SUCCEEDED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4194 | ALOGV("Asynchronous input event injection succeeded."); | 
|  | 4195 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4196 | case InputEventInjectionResult::FAILED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4197 | ALOGW("Asynchronous input event injection failed."); | 
|  | 4198 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4199 | case InputEventInjectionResult::PERMISSION_DENIED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4200 | ALOGW("Asynchronous input event injection permission denied."); | 
|  | 4201 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4202 | case InputEventInjectionResult::TIMED_OUT: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4203 | ALOGW("Asynchronous input event injection timed out."); | 
|  | 4204 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4205 | case InputEventInjectionResult::PENDING: | 
|  | 4206 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); | 
|  | 4207 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4208 | } | 
|  | 4209 | } | 
|  | 4210 |  | 
|  | 4211 | injectionState->injectionResult = injectionResult; | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4212 | mInjectionResultAvailable.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4213 | } | 
|  | 4214 | } | 
|  | 4215 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4216 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { | 
|  | 4217 | InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4218 | if (injectionState) { | 
|  | 4219 | injectionState->pendingForegroundDispatches += 1; | 
|  | 4220 | } | 
|  | 4221 | } | 
|  | 4222 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4223 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { | 
|  | 4224 | InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4225 | if (injectionState) { | 
|  | 4226 | injectionState->pendingForegroundDispatches -= 1; | 
|  | 4227 |  | 
|  | 4228 | if (injectionState->pendingForegroundDispatches == 0) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4229 | mInjectionSyncFinished.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4230 | } | 
|  | 4231 | } | 
|  | 4232 | } | 
|  | 4233 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4234 | const std::vector<sp<InputWindowHandle>>& InputDispatcher::getWindowHandlesLocked( | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4235 | int32_t displayId) const { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4236 | static const std::vector<sp<InputWindowHandle>> EMPTY_WINDOW_HANDLES; | 
|  | 4237 | auto it = mWindowHandlesByDisplay.find(displayId); | 
|  | 4238 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4239 | } | 
|  | 4240 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4241 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4242 | const sp<IBinder>& windowHandleToken) const { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4243 | if (windowHandleToken == nullptr) { | 
|  | 4244 | return nullptr; | 
|  | 4245 | } | 
|  | 4246 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4247 | for (auto& it : mWindowHandlesByDisplay) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4248 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4249 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4250 | if (windowHandle->getToken() == windowHandleToken) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4251 | return windowHandle; | 
|  | 4252 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4253 | } | 
|  | 4254 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4255 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4256 | } | 
|  | 4257 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4258 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, | 
|  | 4259 | int displayId) const { | 
|  | 4260 | if (windowHandleToken == nullptr) { | 
|  | 4261 | return nullptr; | 
|  | 4262 | } | 
|  | 4263 |  | 
|  | 4264 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { | 
|  | 4265 | if (windowHandle->getToken() == windowHandleToken) { | 
|  | 4266 | return windowHandle; | 
|  | 4267 | } | 
|  | 4268 | } | 
|  | 4269 | return nullptr; | 
|  | 4270 | } | 
|  | 4271 |  | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4272 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( | 
|  | 4273 | const sp<InputWindowHandle>& windowHandle) const { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4274 | for (auto& it : mWindowHandlesByDisplay) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4275 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4276 | for (const sp<InputWindowHandle>& handle : windowHandles) { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4277 | if (handle->getId() == windowHandle->getId() && | 
|  | 4278 | handle->getToken() == windowHandle->getToken()) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4279 | if (windowHandle->getInfo()->displayId != it.first) { | 
|  | 4280 | ALOGE("Found window %s in display %" PRId32 | 
|  | 4281 | ", but it should belong to display %" PRId32, | 
|  | 4282 | windowHandle->getName().c_str(), it.first, | 
|  | 4283 | windowHandle->getInfo()->displayId); | 
|  | 4284 | } | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4285 | return handle; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4286 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4287 | } | 
|  | 4288 | } | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4289 | return nullptr; | 
|  | 4290 | } | 
|  | 4291 |  | 
|  | 4292 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { | 
|  | 4293 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); | 
|  | 4294 | return getWindowHandleLocked(focusedToken, displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4295 | } | 
|  | 4296 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4297 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { | 
|  | 4298 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); | 
|  | 4299 | const bool noInputChannel = | 
|  | 4300 | windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 4301 | if (connection != nullptr && noInputChannel) { | 
|  | 4302 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", | 
|  | 4303 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); | 
|  | 4304 | return false; | 
|  | 4305 | } | 
|  | 4306 |  | 
|  | 4307 | if (connection == nullptr) { | 
|  | 4308 | if (!noInputChannel) { | 
|  | 4309 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); | 
|  | 4310 | } | 
|  | 4311 | return false; | 
|  | 4312 | } | 
|  | 4313 | if (!connection->responsive) { | 
|  | 4314 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); | 
|  | 4315 | return false; | 
|  | 4316 | } | 
|  | 4317 | return true; | 
|  | 4318 | } | 
|  | 4319 |  | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4320 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( | 
|  | 4321 | const sp<IBinder>& token) const { | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4322 | auto connectionIt = mConnectionsByToken.find(token); | 
|  | 4323 | if (connectionIt == mConnectionsByToken.end()) { | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4324 | return nullptr; | 
|  | 4325 | } | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4326 | return connectionIt->second->inputChannel; | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4327 | } | 
|  | 4328 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4329 | void InputDispatcher::updateWindowHandlesForDisplayLocked( | 
|  | 4330 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { | 
|  | 4331 | if (inputWindowHandles.empty()) { | 
|  | 4332 | // Remove all handles on a display if there are no windows left. | 
|  | 4333 | mWindowHandlesByDisplay.erase(displayId); | 
|  | 4334 | return; | 
|  | 4335 | } | 
|  | 4336 |  | 
|  | 4337 | // Since we compare the pointer of input window handles across window updates, we need | 
|  | 4338 | // to make sure the handle object for the same window stays unchanged across updates. | 
|  | 4339 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4340 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4341 | for (const sp<InputWindowHandle>& handle : oldHandles) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4342 | oldHandlesById[handle->getId()] = handle; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4343 | } | 
|  | 4344 |  | 
|  | 4345 | std::vector<sp<InputWindowHandle>> newHandles; | 
|  | 4346 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { | 
|  | 4347 | if (!handle->updateInfo()) { | 
|  | 4348 | // handle no longer valid | 
|  | 4349 | continue; | 
|  | 4350 | } | 
|  | 4351 |  | 
|  | 4352 | const InputWindowInfo* info = handle->getInfo(); | 
|  | 4353 | if ((getInputChannelLocked(handle->getToken()) == nullptr && | 
|  | 4354 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { | 
|  | 4355 | const bool noInputChannel = | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4356 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 4357 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || | 
|  | 4358 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4359 | if (canReceiveInput && !noInputChannel) { | 
| John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4360 | ALOGV("Window handle %s has no registered input channel", | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4361 | handle->getName().c_str()); | 
| Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4362 | continue; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4363 | } | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4364 | } | 
|  | 4365 |  | 
|  | 4366 | if (info->displayId != displayId) { | 
|  | 4367 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", | 
|  | 4368 | handle->getName().c_str(), displayId, info->displayId); | 
|  | 4369 | continue; | 
|  | 4370 | } | 
|  | 4371 |  | 
| Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4372 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && | 
|  | 4373 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4374 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4375 | oldHandle->updateFrom(handle); | 
|  | 4376 | newHandles.push_back(oldHandle); | 
|  | 4377 | } else { | 
|  | 4378 | newHandles.push_back(handle); | 
|  | 4379 | } | 
|  | 4380 | } | 
|  | 4381 |  | 
|  | 4382 | // Insert or replace | 
|  | 4383 | mWindowHandlesByDisplay[displayId] = newHandles; | 
|  | 4384 | } | 
|  | 4385 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4386 | void InputDispatcher::setInputWindows( | 
|  | 4387 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { | 
|  | 4388 | { // acquire lock | 
|  | 4389 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4390 | for (const auto& [displayId, handles] : handlesPerDisplay) { | 
|  | 4391 | setInputWindowsLocked(handles, displayId); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4392 | } | 
|  | 4393 | } | 
|  | 4394 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4395 | mLooper->wake(); | 
|  | 4396 | } | 
|  | 4397 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4398 | /** | 
|  | 4399 | * Called from InputManagerService, update window handle list by displayId that can receive input. | 
|  | 4400 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... | 
|  | 4401 | * If set an empty list, remove all handles from the specific display. | 
|  | 4402 | * For focused handle, check if need to change and send a cancel event to previous one. | 
|  | 4403 | * For removed handle, check if need to send a cancel event if already in touch. | 
|  | 4404 | */ | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4405 | void InputDispatcher::setInputWindowsLocked( | 
|  | 4406 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4407 | if (DEBUG_FOCUS) { | 
|  | 4408 | std::string windowList; | 
|  | 4409 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { | 
|  | 4410 | windowList += iwh->getName() + " "; | 
|  | 4411 | } | 
|  | 4412 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); | 
|  | 4413 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4414 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4415 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL | 
|  | 4416 | for (const sp<InputWindowHandle>& window : inputWindowHandles) { | 
|  | 4417 | const bool noInputWindow = | 
|  | 4418 | window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 4419 | if (noInputWindow && window->getToken() != nullptr) { | 
|  | 4420 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", | 
|  | 4421 | window->getName().c_str()); | 
|  | 4422 | window->releaseChannel(); | 
|  | 4423 | } | 
|  | 4424 | } | 
|  | 4425 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4426 | // Copy old handles for release if they are no longer present. | 
|  | 4427 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4428 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4429 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4430 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4431 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
|  | 4432 | if (mLastHoverWindowHandle && | 
|  | 4433 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == | 
|  | 4434 | windowHandles.end()) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4435 | mLastHoverWindowHandle = nullptr; | 
|  | 4436 | } | 
|  | 4437 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4438 | std::optional<FocusResolver::FocusChanges> changes = | 
|  | 4439 | mFocusResolver.setInputWindows(displayId, windowHandles); | 
|  | 4440 | if (changes) { | 
|  | 4441 | onFocusChangedLocked(*changes); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4442 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4443 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4444 | std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
|  | 4445 | mTouchStatesByDisplay.find(displayId); | 
|  | 4446 | if (stateIt != mTouchStatesByDisplay.end()) { | 
|  | 4447 | TouchState& state = stateIt->second; | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4448 | for (size_t i = 0; i < state.windows.size();) { | 
|  | 4449 | TouchedWindow& touchedWindow = state.windows[i]; | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4450 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4451 | if (DEBUG_FOCUS) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4452 | ALOGD("Touched window was removed: %s in display %" PRId32, | 
|  | 4453 | touchedWindow.windowHandle->getName().c_str(), displayId); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4454 | } | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4455 | std::shared_ptr<InputChannel> touchedInputChannel = | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4456 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); | 
|  | 4457 | if (touchedInputChannel != nullptr) { | 
|  | 4458 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 4459 | "touched window was removed"); | 
|  | 4460 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4461 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4462 | state.windows.erase(state.windows.begin() + i); | 
|  | 4463 | } else { | 
|  | 4464 | ++i; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4465 | } | 
|  | 4466 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4467 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4468 | // 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] | 4469 | // could just clear the state here. | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4470 | if (mDragState && | 
|  | 4471 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4472 | windowHandles.end()) { | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4473 | mDragState.reset(); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4474 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4475 | } | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4476 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4477 | // Release information for windows that are no longer present. | 
|  | 4478 | // This ensures that unused input channels are released promptly. | 
|  | 4479 | // Otherwise, they might stick around until the window handle is destroyed | 
|  | 4480 | // which might not happen until the next GC. | 
|  | 4481 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4482 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4483 | if (DEBUG_FOCUS) { | 
|  | 4484 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4485 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4486 | oldWindowHandle->releaseChannel(); | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4487 | // To avoid making too many calls into the compat framework, only | 
|  | 4488 | // check for window flags when windows are going away. | 
|  | 4489 | // TODO(b/157929241) : delete this. This is only needed temporarily | 
|  | 4490 | // in order to gather some data about the flag usage | 
|  | 4491 | if (oldWindowHandle->getInfo()->flags.test(InputWindowInfo::Flag::SLIPPERY)) { | 
|  | 4492 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", | 
|  | 4493 | oldWindowHandle->getName().c_str()); | 
|  | 4494 | if (mCompatService != nullptr) { | 
|  | 4495 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, | 
|  | 4496 | oldWindowHandle->getInfo()->ownerUid); | 
|  | 4497 | } | 
|  | 4498 | } | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4499 | } | 
| chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4500 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4501 | } | 
|  | 4502 |  | 
|  | 4503 | void InputDispatcher::setFocusedApplication( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4504 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4505 | if (DEBUG_FOCUS) { | 
|  | 4506 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, | 
|  | 4507 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); | 
|  | 4508 | } | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4509 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4510 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4511 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4512 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4513 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4514 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4515 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { | 
|  | 4516 | return; // This application is already focused. No need to wake up or change anything. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4517 | } | 
|  | 4518 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4519 | // Set the new application handle. | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4520 | if (inputApplicationHandle != nullptr) { | 
|  | 4521 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; | 
|  | 4522 | } else { | 
|  | 4523 | mFocusedApplicationHandlesByDisplay.erase(displayId); | 
|  | 4524 | } | 
|  | 4525 |  | 
|  | 4526 | // No matter what the old focused application was, stop waiting on it because it is | 
|  | 4527 | // no longer focused. | 
|  | 4528 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4529 | } // release lock | 
|  | 4530 |  | 
|  | 4531 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4532 | mLooper->wake(); | 
|  | 4533 | } | 
|  | 4534 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4535 | /** | 
|  | 4536 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where | 
|  | 4537 | * the display not specified. | 
|  | 4538 | * | 
|  | 4539 | * We track any unreleased events for each window. If a window loses the ability to receive the | 
|  | 4540 | * released event, we will send a cancel event to it. So when the focused display is changed, we | 
|  | 4541 | * cancel all the unreleased display-unspecified events for the focused window on the old focused | 
|  | 4542 | * display. The display-specified events won't be affected. | 
|  | 4543 | */ | 
|  | 4544 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4545 | if (DEBUG_FOCUS) { | 
|  | 4546 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); | 
|  | 4547 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4548 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4549 | std::scoped_lock _l(mLock); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4550 |  | 
|  | 4551 | if (mFocusedDisplayId != displayId) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4552 | sp<IBinder> oldFocusedWindowToken = | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4553 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4554 | if (oldFocusedWindowToken != nullptr) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4555 | std::shared_ptr<InputChannel> inputChannel = | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4556 | getInputChannelLocked(oldFocusedWindowToken); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4557 | if (inputChannel != nullptr) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4558 | CancelationOptions | 
|  | 4559 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
|  | 4560 | "The display which contains this window no longer has focus."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4561 | options.displayId = ADISPLAY_ID_NONE; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4562 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); | 
|  | 4563 | } | 
|  | 4564 | } | 
|  | 4565 | mFocusedDisplayId = displayId; | 
|  | 4566 |  | 
| Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4567 | // Find new focused window and validate | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4568 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4569 | notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4570 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4571 | if (newFocusedWindowToken == nullptr) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4572 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4573 | if (mFocusResolver.hasFocusedWindowTokens()) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4574 | ALOGE("But another display has a focused window\n%s", | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4575 | mFocusResolver.dumpFocusedWindows().c_str()); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4576 | } | 
|  | 4577 | } | 
|  | 4578 | } | 
|  | 4579 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4580 | if (DEBUG_FOCUS) { | 
|  | 4581 | logDispatchStateLocked(); | 
|  | 4582 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4583 | } // release lock | 
|  | 4584 |  | 
|  | 4585 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4586 | mLooper->wake(); | 
|  | 4587 | } | 
|  | 4588 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4589 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4590 | if (DEBUG_FOCUS) { | 
|  | 4591 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); | 
|  | 4592 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4593 |  | 
|  | 4594 | bool changed; | 
|  | 4595 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4596 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4597 |  | 
|  | 4598 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { | 
|  | 4599 | if (mDispatchFrozen && !frozen) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4600 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4601 | } | 
|  | 4602 |  | 
|  | 4603 | if (mDispatchEnabled && !enabled) { | 
|  | 4604 | resetAndDropEverythingLocked("dispatcher is being disabled"); | 
|  | 4605 | } | 
|  | 4606 |  | 
|  | 4607 | mDispatchEnabled = enabled; | 
|  | 4608 | mDispatchFrozen = frozen; | 
|  | 4609 | changed = true; | 
|  | 4610 | } else { | 
|  | 4611 | changed = false; | 
|  | 4612 | } | 
|  | 4613 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4614 | if (DEBUG_FOCUS) { | 
|  | 4615 | logDispatchStateLocked(); | 
|  | 4616 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4617 | } // release lock | 
|  | 4618 |  | 
|  | 4619 | if (changed) { | 
|  | 4620 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4621 | mLooper->wake(); | 
|  | 4622 | } | 
|  | 4623 | } | 
|  | 4624 |  | 
|  | 4625 | void InputDispatcher::setInputFilterEnabled(bool enabled) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4626 | if (DEBUG_FOCUS) { | 
|  | 4627 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); | 
|  | 4628 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4629 |  | 
|  | 4630 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4631 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4632 |  | 
|  | 4633 | if (mInputFilterEnabled == enabled) { | 
|  | 4634 | return; | 
|  | 4635 | } | 
|  | 4636 |  | 
|  | 4637 | mInputFilterEnabled = enabled; | 
|  | 4638 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); | 
|  | 4639 | } // release lock | 
|  | 4640 |  | 
|  | 4641 | // Wake up poll loop since there might be work to do to drop everything. | 
|  | 4642 | mLooper->wake(); | 
|  | 4643 | } | 
|  | 4644 |  | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4645 | void InputDispatcher::setInTouchMode(bool inTouchMode) { | 
|  | 4646 | std::scoped_lock lock(mLock); | 
|  | 4647 | mInTouchMode = inTouchMode; | 
|  | 4648 | } | 
|  | 4649 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4650 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { | 
|  | 4651 | if (opacity < 0 || opacity > 1) { | 
|  | 4652 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); | 
|  | 4653 | return; | 
|  | 4654 | } | 
|  | 4655 |  | 
|  | 4656 | std::scoped_lock lock(mLock); | 
|  | 4657 | mMaximumObscuringOpacityForTouch = opacity; | 
|  | 4658 | } | 
|  | 4659 |  | 
|  | 4660 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { | 
|  | 4661 | std::scoped_lock lock(mLock); | 
|  | 4662 | mBlockUntrustedTouchesMode = mode; | 
|  | 4663 | } | 
|  | 4664 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4665 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, | 
|  | 4666 | bool isDragDrop) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4667 | if (fromToken == toToken) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4668 | if (DEBUG_FOCUS) { | 
|  | 4669 | ALOGD("Trivial transfer to same window."); | 
|  | 4670 | } | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4671 | return true; | 
|  | 4672 | } | 
|  | 4673 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4674 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4675 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4676 |  | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4677 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); | 
|  | 4678 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4679 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4680 | ALOGW("Cannot transfer focus because from or to window not found."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4681 | return false; | 
|  | 4682 | } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4683 | if (DEBUG_FOCUS) { | 
|  | 4684 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", | 
|  | 4685 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); | 
|  | 4686 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4687 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4688 | if (DEBUG_FOCUS) { | 
|  | 4689 | ALOGD("Cannot transfer focus because windows are on different displays."); | 
|  | 4690 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4691 | return false; | 
|  | 4692 | } | 
|  | 4693 |  | 
|  | 4694 | bool found = false; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4695 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
|  | 4696 | TouchState& state = pair.second; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4697 | for (size_t i = 0; i < state.windows.size(); i++) { | 
|  | 4698 | const TouchedWindow& touchedWindow = state.windows[i]; | 
|  | 4699 | if (touchedWindow.windowHandle == fromWindowHandle) { | 
|  | 4700 | int32_t oldTargetFlags = touchedWindow.targetFlags; | 
|  | 4701 | BitSet32 pointerIds = touchedWindow.pointerIds; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4702 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4703 | state.windows.erase(state.windows.begin() + i); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4704 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4705 | int32_t newTargetFlags = oldTargetFlags & | 
|  | 4706 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | | 
|  | 4707 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4708 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4709 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4710 | // Store the dragging window. | 
|  | 4711 | if (isDragDrop) { | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4712 | mDragState = std::make_unique<DragState>(toWindowHandle); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4713 | } | 
|  | 4714 |  | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4715 | found = true; | 
|  | 4716 | goto Found; | 
|  | 4717 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4718 | } | 
|  | 4719 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4720 | Found: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4721 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4722 | if (!found) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4723 | if (DEBUG_FOCUS) { | 
|  | 4724 | ALOGD("Focus transfer failed because from window did not have focus."); | 
|  | 4725 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4726 | return false; | 
|  | 4727 | } | 
|  | 4728 |  | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4729 | sp<Connection> fromConnection = getConnectionLocked(fromToken); | 
|  | 4730 | sp<Connection> toConnection = getConnectionLocked(toToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4731 | if (fromConnection != nullptr && toConnection != nullptr) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4732 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4733 | CancelationOptions | 
|  | 4734 | options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 4735 | "transferring touch focus from this window to another window"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4736 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4737 | synthesizePointerDownEventsForConnectionLocked(toConnection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4738 | } | 
|  | 4739 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4740 | if (DEBUG_FOCUS) { | 
|  | 4741 | logDispatchStateLocked(); | 
|  | 4742 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4743 | } // release lock | 
|  | 4744 |  | 
|  | 4745 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4746 | mLooper->wake(); | 
|  | 4747 | return true; | 
|  | 4748 | } | 
|  | 4749 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4750 | // Binder call | 
|  | 4751 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { | 
|  | 4752 | sp<IBinder> fromToken; | 
|  | 4753 | { // acquire lock | 
|  | 4754 | std::scoped_lock _l(mLock); | 
|  | 4755 |  | 
|  | 4756 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(destChannelToken); | 
|  | 4757 | if (toWindowHandle == nullptr) { | 
|  | 4758 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); | 
|  | 4759 | return false; | 
|  | 4760 | } | 
|  | 4761 |  | 
|  | 4762 | const int32_t displayId = toWindowHandle->getInfo()->displayId; | 
|  | 4763 |  | 
|  | 4764 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); | 
|  | 4765 | if (touchStateIt == mTouchStatesByDisplay.end()) { | 
|  | 4766 | ALOGD("Could not transfer touch because the display %" PRId32 " is not being touched", | 
|  | 4767 | displayId); | 
|  | 4768 | return false; | 
|  | 4769 | } | 
|  | 4770 |  | 
|  | 4771 | TouchState& state = touchStateIt->second; | 
|  | 4772 | if (state.windows.size() != 1) { | 
|  | 4773 | ALOGW("Cannot transfer touch state because there are %zu windows being touched", | 
|  | 4774 | state.windows.size()); | 
|  | 4775 | return false; | 
|  | 4776 | } | 
|  | 4777 | const TouchedWindow& touchedWindow = state.windows[0]; | 
|  | 4778 | fromToken = touchedWindow.windowHandle->getToken(); | 
|  | 4779 | } // release lock | 
|  | 4780 |  | 
|  | 4781 | return transferTouchFocus(fromToken, destChannelToken); | 
|  | 4782 | } | 
|  | 4783 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4784 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4785 | if (DEBUG_FOCUS) { | 
|  | 4786 | ALOGD("Resetting and dropping all events (%s).", reason); | 
|  | 4787 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4788 |  | 
|  | 4789 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); | 
|  | 4790 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 4791 |  | 
|  | 4792 | resetKeyRepeatLocked(); | 
|  | 4793 | releasePendingEventLocked(); | 
|  | 4794 | drainInboundQueueLocked(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4795 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4796 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4797 | mAnrTracker.clear(); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4798 | mTouchStatesByDisplay.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4799 | mLastHoverWindowHandle.clear(); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4800 | mReplacedKeys.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4801 | } | 
|  | 4802 |  | 
|  | 4803 | void InputDispatcher::logDispatchStateLocked() { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4804 | std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4805 | dumpDispatchStateLocked(dump); | 
|  | 4806 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4807 | std::istringstream stream(dump); | 
|  | 4808 | std::string line; | 
|  | 4809 |  | 
|  | 4810 | while (std::getline(stream, line, '\n')) { | 
|  | 4811 | ALOGD("%s", line.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4812 | } | 
|  | 4813 | } | 
|  | 4814 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4815 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { | 
|  | 4816 | std::string dump; | 
|  | 4817 |  | 
|  | 4818 | dump += StringPrintf(INDENT "FocusedWindowRequestedPointerCapture: %s\n", | 
|  | 4819 | toString(mFocusedWindowRequestedPointerCapture)); | 
|  | 4820 |  | 
|  | 4821 | std::string windowName = "None"; | 
|  | 4822 | if (mWindowTokenWithPointerCapture) { | 
|  | 4823 | const sp<InputWindowHandle> captureWindowHandle = | 
|  | 4824 | getWindowHandleLocked(mWindowTokenWithPointerCapture); | 
|  | 4825 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() | 
|  | 4826 | : "token has capture without window"; | 
|  | 4827 | } | 
|  | 4828 | dump += StringPrintf(INDENT "CurrentWindowWithPointerCapture: %s\n", windowName.c_str()); | 
|  | 4829 |  | 
|  | 4830 | return dump; | 
|  | 4831 | } | 
|  | 4832 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4833 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { | 
| Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4834 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); | 
|  | 4835 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); | 
|  | 4836 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4837 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4838 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4839 | if (!mFocusedApplicationHandlesByDisplay.empty()) { | 
|  | 4840 | dump += StringPrintf(INDENT "FocusedApplications:\n"); | 
|  | 4841 | for (auto& it : mFocusedApplicationHandlesByDisplay) { | 
|  | 4842 | const int32_t displayId = it.first; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4843 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4844 | const std::chrono::duration timeout = | 
|  | 4845 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4846 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4847 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4848 | displayId, applicationHandle->getName().c_str(), millis(timeout)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4849 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4850 | } else { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4851 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4852 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4853 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4854 | dump += mFocusResolver.dump(); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4855 | dump += dumpPointerCaptureStateLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4856 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4857 | if (!mTouchStatesByDisplay.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4858 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4859 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
|  | 4860 | const TouchState& state = pair.second; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4861 | 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] | 4862 | state.displayId, toString(state.down), toString(state.split), | 
|  | 4863 | state.deviceId, state.source); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4864 | if (!state.windows.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4865 | dump += INDENT3 "Windows:\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4866 | for (size_t i = 0; i < state.windows.size(); i++) { | 
|  | 4867 | const TouchedWindow& touchedWindow = state.windows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4868 | dump += StringPrintf(INDENT4 | 
|  | 4869 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", | 
|  | 4870 | i, touchedWindow.windowHandle->getName().c_str(), | 
|  | 4871 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4872 | } | 
|  | 4873 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4874 | dump += INDENT3 "Windows: <none>\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4875 | } | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4876 | if (!state.portalWindows.empty()) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4877 | dump += INDENT3 "Portal windows:\n"; | 
|  | 4878 | for (size_t i = 0; i < state.portalWindows.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4879 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4880 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, | 
|  | 4881 | portalWindowHandle->getName().c_str()); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4882 | } | 
|  | 4883 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4884 | } | 
|  | 4885 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4886 | dump += INDENT "TouchStates: <no displays touched>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4887 | } | 
|  | 4888 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4889 | if (mDragState) { | 
|  | 4890 | dump += StringPrintf(INDENT "DragState:\n"); | 
|  | 4891 | mDragState->dump(dump, INDENT2); | 
|  | 4892 | } | 
|  | 4893 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4894 | if (!mWindowHandlesByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4895 | for (auto& it : mWindowHandlesByDisplay) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4896 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4897 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4898 | if (!windowHandles.empty()) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4899 | dump += INDENT2 "Windows:\n"; | 
|  | 4900 | for (size_t i = 0; i < windowHandles.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4901 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4902 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4903 |  | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4904 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4905 | "portalToDisplayId=%d, paused=%s, focusable=%s, " | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4906 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4907 | "flags=%s, type=%s, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4908 | "frame=[%d,%d][%d,%d], globalScale=%f, " | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 4909 | "applicationInfo.name=%s, " | 
|  | 4910 | "applicationInfo.token=%s, " | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4911 | "touchableRegion=", | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4912 | i, windowInfo->name.c_str(), windowInfo->id, | 
|  | 4913 | windowInfo->displayId, windowInfo->portalToDisplayId, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4914 | toString(windowInfo->paused), | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4915 | toString(windowInfo->focusable), | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4916 | toString(windowInfo->hasWallpaper), | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4917 | toString(windowInfo->visible), windowInfo->alpha, | 
| Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 4918 | windowInfo->flags.string().c_str(), | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4919 | NamedEnum::string(windowInfo->type).c_str(), | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4920 | windowInfo->frameLeft, windowInfo->frameTop, | 
|  | 4921 | windowInfo->frameRight, windowInfo->frameBottom, | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4922 | windowInfo->globalScaleFactor, | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 4923 | windowInfo->applicationInfo.name.c_str(), | 
|  | 4924 | toString(windowInfo->applicationInfo.token).c_str()); | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 4925 | dump += dumpRegion(windowInfo->touchableRegion); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4926 | dump += StringPrintf(", inputFeatures=%s", | 
|  | 4927 | windowInfo->inputFeatures.string().c_str()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4928 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4929 | "ms, trustedOverlay=%s, hasToken=%s, " | 
|  | 4930 | "touchOcclusionMode=%s\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4931 | windowInfo->ownerPid, windowInfo->ownerUid, | 
| Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 4932 | millis(windowInfo->dispatchingTimeout), | 
|  | 4933 | toString(windowInfo->trustedOverlay), | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4934 | toString(windowInfo->token != nullptr), | 
|  | 4935 | toString(windowInfo->touchOcclusionMode).c_str()); | 
| chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 4936 | windowInfo->transform.dump(dump, "transform", INDENT4); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4937 | } | 
|  | 4938 | } else { | 
|  | 4939 | dump += INDENT2 "Windows: <none>\n"; | 
|  | 4940 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4941 | } | 
|  | 4942 | } else { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4943 | dump += INDENT "Displays: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4944 | } | 
|  | 4945 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4946 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4947 | for (auto& it : mGlobalMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4948 | const std::vector<Monitor>& monitors = it.second; | 
|  | 4949 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); | 
|  | 4950 | dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4951 | } | 
|  | 4952 | for (auto& it : mGestureMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4953 | const std::vector<Monitor>& monitors = it.second; | 
|  | 4954 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); | 
|  | 4955 | dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4956 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4957 | } else { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4958 | dump += INDENT "Monitors: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4959 | } | 
|  | 4960 |  | 
|  | 4961 | nsecs_t currentTime = now(); | 
|  | 4962 |  | 
|  | 4963 | // Dump recently dispatched or dropped events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4964 | if (!mRecentQueue.empty()) { | 
|  | 4965 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4966 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4967 | dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4968 | dump += entry->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4969 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4970 | } | 
|  | 4971 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4972 | dump += INDENT "RecentQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4973 | } | 
|  | 4974 |  | 
|  | 4975 | // Dump event currently being dispatched. | 
|  | 4976 | if (mPendingEvent) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4977 | dump += INDENT "PendingEvent:\n"; | 
|  | 4978 | dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4979 | dump += mPendingEvent->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4980 | dump += StringPrintf(", age=%" PRId64 "ms\n", | 
|  | 4981 | ns2ms(currentTime - mPendingEvent->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4982 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4983 | dump += INDENT "PendingEvent: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4984 | } | 
|  | 4985 |  | 
|  | 4986 | // Dump inbound events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4987 | if (!mInboundQueue.empty()) { | 
|  | 4988 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4989 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4990 | dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4991 | dump += entry->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4992 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4993 | } | 
|  | 4994 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4995 | dump += INDENT "InboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4996 | } | 
|  | 4997 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4998 | if (!mReplacedKeys.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4999 | dump += INDENT "ReplacedKeys:\n"; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5000 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { | 
|  | 5001 | const KeyReplacement& replacement = pair.first; | 
|  | 5002 | int32_t newKeyCode = pair.second; | 
|  | 5003 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5004 | replacement.keyCode, replacement.deviceId, newKeyCode); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5005 | } | 
|  | 5006 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5007 | dump += INDENT "ReplacedKeys: <empty>\n"; | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5008 | } | 
|  | 5009 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5010 | if (!mConnectionsByToken.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5011 | dump += INDENT "Connections:\n"; | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5012 | for (const auto& [token, connection] : mConnectionsByToken) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5013 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5014 | "status=%s, monitor=%s, responsive=%s\n", | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5015 | connection->inputChannel->getFd().get(), | 
|  | 5016 | connection->getInputChannelName().c_str(), | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5017 | connection->getWindowName().c_str(), connection->getStatusLabel(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5018 | toString(connection->monitor), toString(connection->responsive)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5019 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5020 | if (!connection->outboundQueue.empty()) { | 
|  | 5021 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", | 
|  | 5022 | connection->outboundQueue.size()); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5023 | dump += dumpQueue(connection->outboundQueue, currentTime); | 
|  | 5024 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5025 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5026 | dump += INDENT3 "OutboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5027 | } | 
|  | 5028 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5029 | if (!connection->waitQueue.empty()) { | 
|  | 5030 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", | 
|  | 5031 | connection->waitQueue.size()); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5032 | dump += dumpQueue(connection->waitQueue, currentTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5033 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5034 | dump += INDENT3 "WaitQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5035 | } | 
|  | 5036 | } | 
|  | 5037 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5038 | dump += INDENT "Connections: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5039 | } | 
|  | 5040 |  | 
|  | 5041 | if (isAppSwitchPendingLocked()) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5042 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", | 
|  | 5043 | ns2ms(mAppSwitchDueTime - now())); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5044 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5045 | dump += INDENT "AppSwitch: not pending\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5046 | } | 
|  | 5047 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5048 | dump += INDENT "Configuration:\n"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5049 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); | 
|  | 5050 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", | 
|  | 5051 | ns2ms(mConfig.keyRepeatTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5052 | } | 
|  | 5053 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5054 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { | 
|  | 5055 | const size_t numMonitors = monitors.size(); | 
|  | 5056 | for (size_t i = 0; i < numMonitors; i++) { | 
|  | 5057 | const Monitor& monitor = monitors[i]; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5058 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5059 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); | 
|  | 5060 | dump += "\n"; | 
|  | 5061 | } | 
|  | 5062 | } | 
|  | 5063 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5064 | class LooperEventCallback : public LooperCallback { | 
|  | 5065 | public: | 
|  | 5066 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} | 
|  | 5067 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } | 
|  | 5068 |  | 
|  | 5069 | private: | 
|  | 5070 | std::function<int(int events)> mCallback; | 
|  | 5071 | }; | 
|  | 5072 |  | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5073 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5074 | #if DEBUG_CHANNEL_CREATION | 
|  | 5075 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5076 | #endif | 
|  | 5077 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5078 | std::unique_ptr<InputChannel> serverChannel; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5079 | std::unique_ptr<InputChannel> clientChannel; | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5080 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5081 |  | 
|  | 5082 | if (result) { | 
|  | 5083 | return base::Error(result) << "Failed to open input channel pair with name " << name; | 
|  | 5084 | } | 
|  | 5085 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5086 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5087 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5088 | const sp<IBinder>& token = serverChannel->getConnectionToken(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5089 | int fd = serverChannel->getFd(); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5090 | sp<Connection> connection = | 
|  | 5091 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5092 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5093 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { | 
|  | 5094 | ALOGE("Created a new connection, but the token %p is already known", token.get()); | 
|  | 5095 | } | 
|  | 5096 | mConnectionsByToken.emplace(token, connection); | 
|  | 5097 |  | 
|  | 5098 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, | 
|  | 5099 | this, std::placeholders::_1, token); | 
|  | 5100 |  | 
|  | 5101 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5102 | } // release lock | 
|  | 5103 |  | 
|  | 5104 | // Wake the looper because some connections have changed. | 
|  | 5105 | mLooper->wake(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5106 | return clientChannel; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5107 | } | 
|  | 5108 |  | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5109 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, | 
|  | 5110 | bool isGestureMonitor, | 
|  | 5111 | const std::string& name, | 
|  | 5112 | int32_t pid) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5113 | std::shared_ptr<InputChannel> serverChannel; | 
|  | 5114 | std::unique_ptr<InputChannel> clientChannel; | 
|  | 5115 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); | 
|  | 5116 | if (result) { | 
|  | 5117 | return base::Error(result) << "Failed to open input channel pair with name " << name; | 
|  | 5118 | } | 
|  | 5119 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5120 | { // acquire lock | 
|  | 5121 | std::scoped_lock _l(mLock); | 
|  | 5122 |  | 
|  | 5123 | if (displayId < 0) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5124 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name | 
|  | 5125 | << " without a specified display."; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5126 | } | 
|  | 5127 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5128 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5129 | const sp<IBinder>& token = serverChannel->getConnectionToken(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5130 | const int fd = serverChannel->getFd(); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5131 |  | 
|  | 5132 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { | 
|  | 5133 | ALOGE("Created a new connection, but the token %p is already known", token.get()); | 
|  | 5134 | } | 
|  | 5135 | mConnectionsByToken.emplace(token, connection); | 
|  | 5136 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, | 
|  | 5137 | this, std::placeholders::_1, token); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5138 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5139 | auto& monitorsByDisplay = | 
|  | 5140 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5141 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5142 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5143 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5144 | } | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5145 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5146 | // Wake the looper because some connections have changed. | 
|  | 5147 | mLooper->wake(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5148 | return clientChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5149 | } | 
|  | 5150 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5151 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5152 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5153 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5154 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5155 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5156 | if (status) { | 
|  | 5157 | return status; | 
|  | 5158 | } | 
|  | 5159 | } // release lock | 
|  | 5160 |  | 
|  | 5161 | // Wake the poll loop because removing the connection may have changed the current | 
|  | 5162 | // synchronization state. | 
|  | 5163 | mLooper->wake(); | 
|  | 5164 | return OK; | 
|  | 5165 | } | 
|  | 5166 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5167 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, | 
|  | 5168 | bool notify) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5169 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5170 | if (connection == nullptr) { | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5171 | // 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] | 5172 | return BAD_VALUE; | 
|  | 5173 | } | 
|  | 5174 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5175 | removeConnectionLocked(connection); | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5176 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5177 | if (connection->monitor) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5178 | removeMonitorChannelLocked(connectionToken); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5179 | } | 
|  | 5180 |  | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5181 | mLooper->removeFd(connection->inputChannel->getFd()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5182 |  | 
|  | 5183 | nsecs_t currentTime = now(); | 
|  | 5184 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); | 
|  | 5185 |  | 
|  | 5186 | connection->status = Connection::STATUS_ZOMBIE; | 
|  | 5187 | return OK; | 
|  | 5188 | } | 
|  | 5189 |  | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5190 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { | 
|  | 5191 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); | 
|  | 5192 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5193 | } | 
|  | 5194 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5195 | void InputDispatcher::removeMonitorChannelLocked( | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5196 | const sp<IBinder>& connectionToken, | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5197 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5198 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5199 | std::vector<Monitor>& monitors = it->second; | 
|  | 5200 | const size_t numMonitors = monitors.size(); | 
|  | 5201 | for (size_t i = 0; i < numMonitors; i++) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5202 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { | 
| Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5203 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, | 
|  | 5204 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5205 | monitors.erase(monitors.begin() + i); | 
|  | 5206 | break; | 
|  | 5207 | } | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5208 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5209 | if (monitors.empty()) { | 
|  | 5210 | it = monitorsByDisplay.erase(it); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5211 | } else { | 
|  | 5212 | ++it; | 
|  | 5213 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5214 | } | 
|  | 5215 | } | 
|  | 5216 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5217 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { | 
|  | 5218 | { // acquire lock | 
|  | 5219 | std::scoped_lock _l(mLock); | 
|  | 5220 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); | 
|  | 5221 |  | 
|  | 5222 | if (!foundDisplayId) { | 
|  | 5223 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); | 
|  | 5224 | return BAD_VALUE; | 
|  | 5225 | } | 
|  | 5226 | int32_t displayId = foundDisplayId.value(); | 
|  | 5227 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5228 | std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
|  | 5229 | mTouchStatesByDisplay.find(displayId); | 
|  | 5230 | if (stateIt == mTouchStatesByDisplay.end()) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5231 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); | 
|  | 5232 | return BAD_VALUE; | 
|  | 5233 | } | 
|  | 5234 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5235 | TouchState& state = stateIt->second; | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5236 | std::shared_ptr<InputChannel> requestingChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5237 | std::optional<int32_t> foundDeviceId; | 
|  | 5238 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5239 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5240 | requestingChannel = touchedMonitor.monitor.inputChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5241 | foundDeviceId = state.deviceId; | 
|  | 5242 | } | 
|  | 5243 | } | 
|  | 5244 | if (!foundDeviceId || !state.down) { | 
|  | 5245 | 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] | 5246 | " Ignoring."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5247 | return BAD_VALUE; | 
|  | 5248 | } | 
|  | 5249 | int32_t deviceId = foundDeviceId.value(); | 
|  | 5250 |  | 
|  | 5251 | // Send cancel events to all the input channels we're stealing from. | 
|  | 5252 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5253 | "gesture monitor stole pointer stream"); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5254 | options.deviceId = deviceId; | 
|  | 5255 | options.displayId = displayId; | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5256 | std::string canceledWindows = "["; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5257 | for (const TouchedWindow& window : state.windows) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5258 | std::shared_ptr<InputChannel> channel = | 
|  | 5259 | getInputChannelLocked(window.windowHandle->getToken()); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5260 | if (channel != nullptr) { | 
|  | 5261 | synthesizeCancelationEventsForInputChannelLocked(channel, options); | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5262 | canceledWindows += channel->getName() + ", "; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5263 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5264 | } | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5265 | canceledWindows += "]"; | 
|  | 5266 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), | 
|  | 5267 | canceledWindows.c_str()); | 
|  | 5268 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5269 | // Then clear the current touch state so we stop dispatching to them as well. | 
|  | 5270 | state.filterNonMonitors(); | 
|  | 5271 | } | 
|  | 5272 | return OK; | 
|  | 5273 | } | 
|  | 5274 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5275 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { | 
|  | 5276 | { // acquire lock | 
|  | 5277 | std::scoped_lock _l(mLock); | 
|  | 5278 | if (DEBUG_FOCUS) { | 
|  | 5279 | const sp<InputWindowHandle> windowHandle = getWindowHandleLocked(windowToken); | 
|  | 5280 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", | 
|  | 5281 | windowHandle != nullptr ? windowHandle->getName().c_str() | 
|  | 5282 | : "token without window"); | 
|  | 5283 | } | 
|  | 5284 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5285 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5286 | if (focusedToken != windowToken) { | 
|  | 5287 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", | 
|  | 5288 | enabled ? "enable" : "disable"); | 
|  | 5289 | return; | 
|  | 5290 | } | 
|  | 5291 |  | 
|  | 5292 | if (enabled == mFocusedWindowRequestedPointerCapture) { | 
|  | 5293 | ALOGW("Ignoring request to %s Pointer Capture: " | 
|  | 5294 | "window has %s requested pointer capture.", | 
|  | 5295 | enabled ? "enable" : "disable", enabled ? "already" : "not"); | 
|  | 5296 | return; | 
|  | 5297 | } | 
|  | 5298 |  | 
|  | 5299 | mFocusedWindowRequestedPointerCapture = enabled; | 
|  | 5300 | setPointerCaptureLocked(enabled); | 
|  | 5301 | } // release lock | 
|  | 5302 |  | 
|  | 5303 | // Wake the thread to process command entries. | 
|  | 5304 | mLooper->wake(); | 
|  | 5305 | } | 
|  | 5306 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5307 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( | 
|  | 5308 | const sp<IBinder>& token) { | 
|  | 5309 | for (const auto& it : mGestureMonitorsByDisplay) { | 
|  | 5310 | const std::vector<Monitor>& monitors = it.second; | 
|  | 5311 | for (const Monitor& monitor : monitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5312 | if (monitor.inputChannel->getConnectionToken() == token) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5313 | return it.first; | 
|  | 5314 | } | 
|  | 5315 | } | 
|  | 5316 | } | 
|  | 5317 | return std::nullopt; | 
|  | 5318 | } | 
|  | 5319 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5320 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { | 
|  | 5321 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); | 
|  | 5322 | if (gesturePid.has_value()) { | 
|  | 5323 | return gesturePid; | 
|  | 5324 | } | 
|  | 5325 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); | 
|  | 5326 | } | 
|  | 5327 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5328 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5329 | if (inputConnectionToken == nullptr) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5330 | return nullptr; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5331 | } | 
|  | 5332 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5333 | for (const auto& [token, connection] : mConnectionsByToken) { | 
|  | 5334 | if (token == inputConnectionToken) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5335 | return connection; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5336 | } | 
|  | 5337 | } | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5338 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5339 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5340 | } | 
|  | 5341 |  | 
| Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5342 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { | 
|  | 5343 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
|  | 5344 | if (connection == nullptr) { | 
|  | 5345 | return "<nullptr>"; | 
|  | 5346 | } | 
|  | 5347 | return connection->getInputChannelName(); | 
|  | 5348 | } | 
|  | 5349 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5350 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5351 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5352 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5353 | } | 
|  | 5354 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5355 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, | 
|  | 5356 | const sp<Connection>& connection, uint32_t seq, | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5357 | bool handled, nsecs_t consumeTime) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5358 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 5359 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5360 | commandEntry->connection = connection; | 
|  | 5361 | commandEntry->eventTime = currentTime; | 
|  | 5362 | commandEntry->seq = seq; | 
|  | 5363 | commandEntry->handled = handled; | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5364 | commandEntry->consumeTime = consumeTime; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5365 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5366 | } | 
|  | 5367 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5368 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, | 
|  | 5369 | const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5370 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5371 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5372 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5373 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 5374 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5375 | commandEntry->connection = connection; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5376 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5377 | } | 
|  | 5378 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5379 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, | 
|  | 5380 | const sp<IBinder>& newToken) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5381 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 5382 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5383 | commandEntry->oldToken = oldToken; | 
|  | 5384 | commandEntry->newToken = newToken; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5385 | postCommandLocked(std::move(commandEntry)); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5386 | } | 
|  | 5387 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5388 | void InputDispatcher::notifyDropWindowLocked(const sp<IBinder>& token, float x, float y) { | 
|  | 5389 | std::unique_ptr<CommandEntry> commandEntry = | 
|  | 5390 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyDropWindowLockedInterruptible); | 
|  | 5391 | commandEntry->newToken = token; | 
|  | 5392 | commandEntry->x = x; | 
|  | 5393 | commandEntry->y = y; | 
|  | 5394 | postCommandLocked(std::move(commandEntry)); | 
|  | 5395 | } | 
|  | 5396 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5397 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { | 
|  | 5398 | if (connection == nullptr) { | 
|  | 5399 | LOG_ALWAYS_FATAL("Caller must check for nullness"); | 
|  | 5400 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5401 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue | 
|  | 5402 | // is already healthy again. Don't raise ANR in this situation | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5403 | if (connection->waitQueue.empty()) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5404 | ALOGI("Not raising ANR because the connection %s has recovered", | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5405 | connection->inputChannel->getName().c_str()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5406 | return; | 
|  | 5407 | } | 
|  | 5408 | /** | 
|  | 5409 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, | 
|  | 5410 | * may not be the one that caused the timeout to occur. One possibility is that window timeout | 
|  | 5411 | * has changed. This could cause newer entries to time out before the already dispatched | 
|  | 5412 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app | 
|  | 5413 | * processes the events linearly. So providing information about the oldest entry seems to be | 
|  | 5414 | * most useful. | 
|  | 5415 | */ | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5416 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5417 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; | 
|  | 5418 | std::string reason = | 
|  | 5419 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5420 | connection->inputChannel->getName().c_str(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5421 | ns2ms(currentWait), | 
|  | 5422 | oldestEntry->eventEntry->getDescription().c_str()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5423 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5424 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5425 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5426 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); | 
|  | 5427 |  | 
|  | 5428 | // Stop waking up for events on this connection, it is already unresponsive | 
|  | 5429 | cancelEventsForAnrLocked(connection); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5430 | } | 
|  | 5431 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5432 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { | 
|  | 5433 | std::string reason = | 
|  | 5434 | StringPrintf("%s does not have a focused window", application->getName().c_str()); | 
|  | 5435 | updateLastAnrStateLocked(*application, reason); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5436 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5437 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 5438 | &InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible); | 
|  | 5439 | commandEntry->inputApplicationHandle = std::move(application); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5440 | postCommandLocked(std::move(commandEntry)); | 
|  | 5441 | } | 
|  | 5442 |  | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5443 | void InputDispatcher::onUntrustedTouchLocked(const std::string& obscuringPackage) { | 
|  | 5444 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 5445 | &InputDispatcher::doNotifyUntrustedTouchLockedInterruptible); | 
|  | 5446 | commandEntry->obscuringPackage = obscuringPackage; | 
|  | 5447 | postCommandLocked(std::move(commandEntry)); | 
|  | 5448 | } | 
|  | 5449 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5450 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, | 
|  | 5451 | const std::string& reason) { | 
|  | 5452 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); | 
|  | 5453 | updateLastAnrStateLocked(windowLabel, reason); | 
|  | 5454 | } | 
|  | 5455 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5456 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, | 
|  | 5457 | const std::string& reason) { | 
|  | 5458 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5459 | updateLastAnrStateLocked(windowLabel, reason); | 
|  | 5460 | } | 
|  | 5461 |  | 
|  | 5462 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, | 
|  | 5463 | const std::string& reason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5464 | // 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] | 5465 | time_t t = time(nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5466 | struct tm tm; | 
|  | 5467 | localtime_r(&t, &tm); | 
|  | 5468 | char timestr[64]; | 
|  | 5469 | strftime(timestr, sizeof(timestr), "%F %T", &tm); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5470 | mLastAnrState.clear(); | 
|  | 5471 | mLastAnrState += INDENT "ANR:\n"; | 
|  | 5472 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5473 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); | 
|  | 5474 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5475 | dumpDispatchStateLocked(mLastAnrState); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5476 | } | 
|  | 5477 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5478 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5479 | mLock.unlock(); | 
|  | 5480 |  | 
|  | 5481 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); | 
|  | 5482 |  | 
|  | 5483 | mLock.lock(); | 
|  | 5484 | } | 
|  | 5485 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5486 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5487 | sp<Connection> connection = commandEntry->connection; | 
|  | 5488 |  | 
|  | 5489 | if (connection->status != Connection::STATUS_ZOMBIE) { | 
|  | 5490 | mLock.unlock(); | 
|  | 5491 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5492 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5493 |  | 
|  | 5494 | mLock.lock(); | 
|  | 5495 | } | 
|  | 5496 | } | 
|  | 5497 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5498 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5499 | sp<IBinder> oldToken = commandEntry->oldToken; | 
|  | 5500 | sp<IBinder> newToken = commandEntry->newToken; | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5501 | mLock.unlock(); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5502 | mPolicy->notifyFocusChanged(oldToken, newToken); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5503 | mLock.lock(); | 
|  | 5504 | } | 
|  | 5505 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5506 | void InputDispatcher::doNotifyDropWindowLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 5507 | sp<IBinder> newToken = commandEntry->newToken; | 
|  | 5508 | mLock.unlock(); | 
|  | 5509 | mPolicy->notifyDropWindow(newToken, commandEntry->x, commandEntry->y); | 
|  | 5510 | mLock.lock(); | 
|  | 5511 | } | 
|  | 5512 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5513 | void InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5514 | mLock.unlock(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5515 |  | 
|  | 5516 | mPolicy->notifyNoFocusedWindowAnr(commandEntry->inputApplicationHandle); | 
|  | 5517 |  | 
|  | 5518 | mLock.lock(); | 
|  | 5519 | } | 
|  | 5520 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5521 | void InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5522 | mLock.unlock(); | 
|  | 5523 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5524 | mPolicy->notifyWindowUnresponsive(commandEntry->connectionToken, commandEntry->reason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5525 |  | 
|  | 5526 | mLock.lock(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5527 | } | 
|  | 5528 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5529 | void InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5530 | mLock.unlock(); | 
|  | 5531 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5532 | mPolicy->notifyMonitorUnresponsive(commandEntry->pid, commandEntry->reason); | 
|  | 5533 |  | 
|  | 5534 | mLock.lock(); | 
|  | 5535 | } | 
|  | 5536 |  | 
|  | 5537 | void InputDispatcher::doNotifyWindowResponsiveLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 5538 | mLock.unlock(); | 
|  | 5539 |  | 
|  | 5540 | mPolicy->notifyWindowResponsive(commandEntry->connectionToken); | 
|  | 5541 |  | 
|  | 5542 | mLock.lock(); | 
|  | 5543 | } | 
|  | 5544 |  | 
|  | 5545 | void InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 5546 | mLock.unlock(); | 
|  | 5547 |  | 
|  | 5548 | mPolicy->notifyMonitorResponsive(commandEntry->pid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5549 |  | 
|  | 5550 | mLock.lock(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5551 | } | 
|  | 5552 |  | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5553 | void InputDispatcher::doNotifyUntrustedTouchLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 5554 | mLock.unlock(); | 
|  | 5555 |  | 
|  | 5556 | mPolicy->notifyUntrustedTouch(commandEntry->obscuringPackage); | 
|  | 5557 |  | 
|  | 5558 | mLock.lock(); | 
|  | 5559 | } | 
|  | 5560 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5561 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( | 
|  | 5562 | CommandEntry* commandEntry) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5563 | KeyEntry& entry = *(commandEntry->keyEntry); | 
|  | 5564 | KeyEvent event = createKeyEvent(entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5565 |  | 
|  | 5566 | mLock.unlock(); | 
|  | 5567 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5568 | android::base::Timer t; | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5569 | const sp<IBinder>& token = commandEntry->connectionToken; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5570 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry.policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5571 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 5572 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5573 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5574 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5575 |  | 
|  | 5576 | mLock.lock(); | 
|  | 5577 |  | 
|  | 5578 | if (delay < 0) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5579 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5580 | } else if (!delay) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5581 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5582 | } else { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5583 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; | 
|  | 5584 | entry.interceptKeyWakeupTime = now() + delay; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5585 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5586 | } | 
|  | 5587 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5588 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 5589 | mLock.unlock(); | 
|  | 5590 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); | 
|  | 5591 | mLock.lock(); | 
|  | 5592 | } | 
|  | 5593 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5594 | /** | 
|  | 5595 | * Connection is responsive if it has no events in the waitQueue that are older than the | 
|  | 5596 | * current time. | 
|  | 5597 | */ | 
|  | 5598 | static bool isConnectionResponsive(const Connection& connection) { | 
|  | 5599 | const nsecs_t currentTime = now(); | 
|  | 5600 | for (const DispatchEntry* entry : connection.waitQueue) { | 
|  | 5601 | if (entry->timeoutTime < currentTime) { | 
|  | 5602 | return false; | 
|  | 5603 | } | 
|  | 5604 | } | 
|  | 5605 | return true; | 
|  | 5606 | } | 
|  | 5607 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5608 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5609 | sp<Connection> connection = commandEntry->connection; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5610 | const nsecs_t finishTime = commandEntry->eventTime; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5611 | uint32_t seq = commandEntry->seq; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5612 | const bool handled = commandEntry->handled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5613 |  | 
|  | 5614 | // Handle post-event policy actions. | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5615 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5616 | if (dispatchEntryIt == connection->waitQueue.end()) { | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5617 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5618 | } | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5619 | DispatchEntry* dispatchEntry = *dispatchEntryIt; | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5620 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5621 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5622 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), | 
|  | 5623 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5624 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5625 | reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5626 |  | 
|  | 5627 | bool restartEvent; | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5628 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5629 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5630 | restartEvent = | 
|  | 5631 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5632 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5633 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5634 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, | 
|  | 5635 | handled); | 
|  | 5636 | } else { | 
|  | 5637 | restartEvent = false; | 
|  | 5638 | } | 
|  | 5639 |  | 
|  | 5640 | // Dequeue the event and start the next cycle. | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5641 | // Because the lock might have been released, it is possible that the | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5642 | // contents of the wait queue to have been drained, so we need to double-check | 
|  | 5643 | // a few things. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5644 | dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
|  | 5645 | if (dispatchEntryIt != connection->waitQueue.end()) { | 
|  | 5646 | dispatchEntry = *dispatchEntryIt; | 
|  | 5647 | connection->waitQueue.erase(dispatchEntryIt); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5648 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); | 
|  | 5649 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5650 | if (!connection->responsive) { | 
|  | 5651 | connection->responsive = isConnectionResponsive(*connection); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5652 | if (connection->responsive) { | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5653 | // The connection was unresponsive, and now it's responsive. | 
|  | 5654 | processConnectionResponsiveLocked(*connection); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5655 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5656 | } | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5657 | traceWaitQueueLength(connection); | 
|  | 5658 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5659 | connection->outboundQueue.push_front(dispatchEntry); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5660 | traceOutboundQueueLength(connection); | 
|  | 5661 | } else { | 
|  | 5662 | releaseDispatchEntry(dispatchEntry); | 
|  | 5663 | } | 
|  | 5664 | } | 
|  | 5665 |  | 
|  | 5666 | // Start the next dispatch cycle for this connection. | 
|  | 5667 | startDispatchCycleLocked(now(), connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5668 | } | 
|  | 5669 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5670 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { | 
|  | 5671 | std::unique_ptr<CommandEntry> monitorUnresponsiveCommand = std::make_unique<CommandEntry>( | 
|  | 5672 | &InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible); | 
|  | 5673 | monitorUnresponsiveCommand->pid = pid; | 
|  | 5674 | monitorUnresponsiveCommand->reason = std::move(reason); | 
|  | 5675 | postCommandLocked(std::move(monitorUnresponsiveCommand)); | 
|  | 5676 | } | 
|  | 5677 |  | 
|  | 5678 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(sp<IBinder> connectionToken, | 
|  | 5679 | std::string reason) { | 
|  | 5680 | std::unique_ptr<CommandEntry> windowUnresponsiveCommand = std::make_unique<CommandEntry>( | 
|  | 5681 | &InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible); | 
|  | 5682 | windowUnresponsiveCommand->connectionToken = std::move(connectionToken); | 
|  | 5683 | windowUnresponsiveCommand->reason = std::move(reason); | 
|  | 5684 | postCommandLocked(std::move(windowUnresponsiveCommand)); | 
|  | 5685 | } | 
|  | 5686 |  | 
|  | 5687 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { | 
|  | 5688 | std::unique_ptr<CommandEntry> monitorResponsiveCommand = std::make_unique<CommandEntry>( | 
|  | 5689 | &InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible); | 
|  | 5690 | monitorResponsiveCommand->pid = pid; | 
|  | 5691 | postCommandLocked(std::move(monitorResponsiveCommand)); | 
|  | 5692 | } | 
|  | 5693 |  | 
|  | 5694 | void InputDispatcher::sendWindowResponsiveCommandLocked(sp<IBinder> connectionToken) { | 
|  | 5695 | std::unique_ptr<CommandEntry> windowResponsiveCommand = std::make_unique<CommandEntry>( | 
|  | 5696 | &InputDispatcher::doNotifyWindowResponsiveLockedInterruptible); | 
|  | 5697 | windowResponsiveCommand->connectionToken = std::move(connectionToken); | 
|  | 5698 | postCommandLocked(std::move(windowResponsiveCommand)); | 
|  | 5699 | } | 
|  | 5700 |  | 
|  | 5701 | /** | 
|  | 5702 | * Tell the policy that a connection has become unresponsive so that it can start ANR. | 
|  | 5703 | * Check whether the connection of interest is a monitor or a window, and add the corresponding | 
|  | 5704 | * command entry to the command queue. | 
|  | 5705 | */ | 
|  | 5706 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, | 
|  | 5707 | std::string reason) { | 
|  | 5708 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); | 
|  | 5709 | if (connection.monitor) { | 
|  | 5710 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), | 
|  | 5711 | reason.c_str()); | 
|  | 5712 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); | 
|  | 5713 | if (!pid.has_value()) { | 
|  | 5714 | ALOGE("Could not find unresponsive monitor for connection %s", | 
|  | 5715 | connection.inputChannel->getName().c_str()); | 
|  | 5716 | return; | 
|  | 5717 | } | 
|  | 5718 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); | 
|  | 5719 | return; | 
|  | 5720 | } | 
|  | 5721 | // If not a monitor, must be a window | 
|  | 5722 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), | 
|  | 5723 | reason.c_str()); | 
|  | 5724 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); | 
|  | 5725 | } | 
|  | 5726 |  | 
|  | 5727 | /** | 
|  | 5728 | * Tell the policy that a connection has become responsive so that it can stop ANR. | 
|  | 5729 | */ | 
|  | 5730 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { | 
|  | 5731 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); | 
|  | 5732 | if (connection.monitor) { | 
|  | 5733 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); | 
|  | 5734 | if (!pid.has_value()) { | 
|  | 5735 | ALOGE("Could not find responsive monitor for connection %s", | 
|  | 5736 | connection.inputChannel->getName().c_str()); | 
|  | 5737 | return; | 
|  | 5738 | } | 
|  | 5739 | sendMonitorResponsiveCommandLocked(pid.value()); | 
|  | 5740 | return; | 
|  | 5741 | } | 
|  | 5742 | // If not a monitor, must be a window | 
|  | 5743 | sendWindowResponsiveCommandLocked(connectionToken); | 
|  | 5744 | } | 
|  | 5745 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5746 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5747 | DispatchEntry* dispatchEntry, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5748 | KeyEntry& keyEntry, bool handled) { | 
|  | 5749 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5750 | if (!handled) { | 
|  | 5751 | // Report the key as unhandled, since the fallback was not handled. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5752 | mReporter->reportUnhandledKey(keyEntry.id); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5753 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5754 | return false; | 
|  | 5755 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5756 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5757 | // Get the fallback key state. | 
|  | 5758 | // Clear it out after dispatching the UP. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5759 | int32_t originalKeyCode = keyEntry.keyCode; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5760 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5761 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5762 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 5763 | } | 
|  | 5764 |  | 
|  | 5765 | if (handled || !dispatchEntry->hasForegroundTarget()) { | 
|  | 5766 | // If the application handles the original key for which we previously | 
|  | 5767 | // generated a fallback or if the window is not a foreground window, | 
|  | 5768 | // then cancel the associated fallback key, if any. | 
|  | 5769 | if (fallbackKeyCode != -1) { | 
|  | 5770 | // Dispatch the unhandled key to the policy with the cancel flag. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5771 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5772 | ALOGD("Unhandled key event: Asking policy to cancel fallback action.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5773 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5774 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5775 | #endif | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5776 | KeyEvent event = createKeyEvent(keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5777 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5778 |  | 
|  | 5779 | mLock.unlock(); | 
|  | 5780 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5781 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5782 | keyEntry.policyFlags, &event); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5783 |  | 
|  | 5784 | mLock.lock(); | 
|  | 5785 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5786 | // Cancel the fallback key. | 
|  | 5787 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5788 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5789 | "application handled the original non-fallback key " | 
|  | 5790 | "or is no longer a foreground target, " | 
|  | 5791 | "canceling previously dispatched fallback key"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5792 | options.keyCode = fallbackKeyCode; | 
|  | 5793 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5794 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5795 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 5796 | } | 
|  | 5797 | } else { | 
|  | 5798 | // If the application did not handle a non-fallback key, first check | 
|  | 5799 | // that we are in a good state to perform unhandled key event processing | 
|  | 5800 | // Then ask the policy what to do with it. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5801 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5802 | if (fallbackKeyCode == -1 && !initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5803 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5804 | ALOGD("Unhandled key event: Skipping unhandled key event processing " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5805 | "since this is not an initial down.  " | 
|  | 5806 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5807 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5808 | #endif | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5809 | return false; | 
|  | 5810 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5811 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5812 | // Dispatch the unhandled key to the policy. | 
|  | 5813 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5814 | ALOGD("Unhandled key event: Asking policy to perform fallback action.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5815 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5816 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5817 | #endif | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5818 | KeyEvent event = createKeyEvent(keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5819 |  | 
|  | 5820 | mLock.unlock(); | 
|  | 5821 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5822 | bool fallback = | 
|  | 5823 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5824 | &event, keyEntry.policyFlags, &event); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5825 |  | 
|  | 5826 | mLock.lock(); | 
|  | 5827 |  | 
|  | 5828 | if (connection->status != Connection::STATUS_NORMAL) { | 
|  | 5829 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 5830 | return false; | 
|  | 5831 | } | 
|  | 5832 |  | 
|  | 5833 | // Latch the fallback keycode for this key on an initial down. | 
|  | 5834 | // The fallback keycode cannot change at any other point in the lifecycle. | 
|  | 5835 | if (initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5836 | if (fallback) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5837 | fallbackKeyCode = event.getKeyCode(); | 
|  | 5838 | } else { | 
|  | 5839 | fallbackKeyCode = AKEYCODE_UNKNOWN; | 
|  | 5840 | } | 
|  | 5841 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
|  | 5842 | } | 
|  | 5843 |  | 
|  | 5844 | ALOG_ASSERT(fallbackKeyCode != -1); | 
|  | 5845 |  | 
|  | 5846 | // Cancel the fallback key if the policy decides not to send it anymore. | 
|  | 5847 | // We will continue to dispatch the key to the policy but we will no | 
|  | 5848 | // longer dispatch a fallback key to the application. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5849 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && | 
|  | 5850 | (!fallback || fallbackKeyCode != event.getKeyCode())) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5851 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5852 | if (fallback) { | 
|  | 5853 | ALOGD("Unhandled key event: Policy requested to send key %d" | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5854 | "as a fallback for %d, but on the DOWN it had requested " | 
|  | 5855 | "to send %d instead.  Fallback canceled.", | 
|  | 5856 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5857 | } else { | 
|  | 5858 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5859 | "but on the DOWN it had requested to send %d.  " | 
|  | 5860 | "Fallback canceled.", | 
|  | 5861 | originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5862 | } | 
|  | 5863 | #endif | 
|  | 5864 |  | 
|  | 5865 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
|  | 5866 | "canceling fallback, policy no longer desires it"); | 
|  | 5867 | options.keyCode = fallbackKeyCode; | 
|  | 5868 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
|  | 5869 |  | 
|  | 5870 | fallback = false; | 
|  | 5871 | fallbackKeyCode = AKEYCODE_UNKNOWN; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5872 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5873 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5874 | } | 
|  | 5875 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5876 |  | 
|  | 5877 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5878 | { | 
|  | 5879 | std::string msg; | 
|  | 5880 | const KeyedVector<int32_t, int32_t>& fallbackKeys = | 
|  | 5881 | connection->inputState.getFallbackKeys(); | 
|  | 5882 | for (size_t i = 0; i < fallbackKeys.size(); i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5883 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5884 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5885 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5886 | fallbackKeys.size(), msg.c_str()); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5887 | } | 
|  | 5888 | #endif | 
|  | 5889 |  | 
|  | 5890 | if (fallback) { | 
|  | 5891 | // Restart the dispatch cycle using the fallback key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5892 | keyEntry.eventTime = event.getEventTime(); | 
|  | 5893 | keyEntry.deviceId = event.getDeviceId(); | 
|  | 5894 | keyEntry.source = event.getSource(); | 
|  | 5895 | keyEntry.displayId = event.getDisplayId(); | 
|  | 5896 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; | 
|  | 5897 | keyEntry.keyCode = fallbackKeyCode; | 
|  | 5898 | keyEntry.scanCode = event.getScanCode(); | 
|  | 5899 | keyEntry.metaState = event.getMetaState(); | 
|  | 5900 | keyEntry.repeatCount = event.getRepeatCount(); | 
|  | 5901 | keyEntry.downTime = event.getDownTime(); | 
|  | 5902 | keyEntry.syntheticRepeat = false; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5903 |  | 
|  | 5904 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5905 | ALOGD("Unhandled key event: Dispatching fallback key.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5906 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5907 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5908 | #endif | 
|  | 5909 | return true; // restart the event | 
|  | 5910 | } else { | 
|  | 5911 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5912 | ALOGD("Unhandled key event: No fallback key."); | 
|  | 5913 | #endif | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5914 |  | 
|  | 5915 | // Report the key as unhandled, since there is no fallback key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5916 | mReporter->reportUnhandledKey(keyEntry.id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5917 | } | 
|  | 5918 | } | 
|  | 5919 | return false; | 
|  | 5920 | } | 
|  | 5921 |  | 
|  | 5922 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5923 | DispatchEntry* dispatchEntry, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5924 | MotionEntry& motionEntry, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5925 | return false; | 
|  | 5926 | } | 
|  | 5927 |  | 
|  | 5928 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 5929 | mLock.unlock(); | 
|  | 5930 |  | 
| Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 5931 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType, | 
|  | 5932 | commandEntry->displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5933 |  | 
|  | 5934 | mLock.lock(); | 
|  | 5935 | } | 
|  | 5936 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5937 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, | 
|  | 5938 | const Connection& connection, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5939 | // TODO Write some statistics about how long we spend waiting. | 
|  | 5940 | } | 
|  | 5941 |  | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5942 | /** | 
|  | 5943 | * Report the touch event latency to the statsd server. | 
|  | 5944 | * Input events are reported for statistics if: | 
|  | 5945 | * - This is a touchscreen event | 
|  | 5946 | * - InputFilter is not enabled | 
|  | 5947 | * - Event is not injected or synthesized | 
|  | 5948 | * | 
|  | 5949 | * Statistics should be reported before calling addValue, to prevent a fresh new sample | 
|  | 5950 | * from getting aggregated with the "old" data. | 
|  | 5951 | */ | 
|  | 5952 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) | 
|  | 5953 | REQUIRES(mLock) { | 
|  | 5954 | const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && | 
|  | 5955 | !(motionEntry.isSynthesized()) && !mInputFilterEnabled; | 
|  | 5956 | if (!reportForStatistics) { | 
|  | 5957 | return; | 
|  | 5958 | } | 
|  | 5959 |  | 
|  | 5960 | if (mTouchStatistics.shouldReport()) { | 
|  | 5961 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), | 
|  | 5962 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), | 
|  | 5963 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); | 
|  | 5964 | mTouchStatistics.reset(); | 
|  | 5965 | } | 
|  | 5966 | const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); | 
|  | 5967 | mTouchStatistics.addValue(latencyMicros); | 
|  | 5968 | } | 
|  | 5969 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5970 | void InputDispatcher::traceInboundQueueLengthLocked() { | 
|  | 5971 | if (ATRACE_ENABLED()) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5972 | ATRACE_INT("iq", mInboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5973 | } | 
|  | 5974 | } | 
|  | 5975 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5976 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5977 | if (ATRACE_ENABLED()) { | 
|  | 5978 | char counterName[40]; | 
| Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5979 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5980 | ATRACE_INT(counterName, connection->outboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5981 | } | 
|  | 5982 | } | 
|  | 5983 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5984 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5985 | if (ATRACE_ENABLED()) { | 
|  | 5986 | char counterName[40]; | 
| Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5987 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5988 | ATRACE_INT(counterName, connection->waitQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5989 | } | 
|  | 5990 | } | 
|  | 5991 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5992 | void InputDispatcher::dump(std::string& dump) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5993 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5994 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5995 | dump += "Input Dispatcher State:\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5996 | dumpDispatchStateLocked(dump); | 
|  | 5997 |  | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5998 | if (!mLastAnrState.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5999 | dump += "\nInput Dispatcher State at time of last ANR:\n"; | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6000 | dump += mLastAnrState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6001 | } | 
|  | 6002 | } | 
|  | 6003 |  | 
|  | 6004 | void InputDispatcher::monitor() { | 
|  | 6005 | // 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] | 6006 | std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6007 | mLooper->wake(); | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6008 | mDispatcherIsAlive.wait(_l); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6009 | } | 
|  | 6010 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6011 | /** | 
|  | 6012 | * Wake up the dispatcher and wait until it processes all events and commands. | 
|  | 6013 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so | 
|  | 6014 | * this method can be safely called from any thread, as long as you've ensured that | 
|  | 6015 | * the work you are interested in completing has already been queued. | 
|  | 6016 | */ | 
|  | 6017 | bool InputDispatcher::waitForIdle() { | 
|  | 6018 | /** | 
|  | 6019 | * Timeout should represent the longest possible time that a device might spend processing | 
|  | 6020 | * events and commands. | 
|  | 6021 | */ | 
|  | 6022 | constexpr std::chrono::duration TIMEOUT = 100ms; | 
|  | 6023 | std::unique_lock lock(mLock); | 
|  | 6024 | mLooper->wake(); | 
|  | 6025 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); | 
|  | 6026 | return result == std::cv_status::no_timeout; | 
|  | 6027 | } | 
|  | 6028 |  | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6029 | /** | 
|  | 6030 | * Sets focus to the window identified by the token. This must be called | 
|  | 6031 | * after updating any input window handles. | 
|  | 6032 | * | 
|  | 6033 | * Params: | 
|  | 6034 | *  request.token - input channel token used to identify the window that should gain focus. | 
|  | 6035 | *  request.focusedToken - the token that the caller expects currently to be focused. If the | 
|  | 6036 | *  specified token does not match the currently focused window, this request will be dropped. | 
|  | 6037 | *  If the specified focused token matches the currently focused window, the call will succeed. | 
|  | 6038 | *  Set this to "null" if this call should succeed no matter what the currently focused token is. | 
|  | 6039 | *  request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) | 
|  | 6040 | *  when requesting the focus change. This determines which request gets | 
|  | 6041 | *  precedence if there is a focus change request from another source such as pointer down. | 
|  | 6042 | */ | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6043 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { | 
|  | 6044 | { // acquire lock | 
|  | 6045 | std::scoped_lock _l(mLock); | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6046 | std::optional<FocusResolver::FocusChanges> changes = | 
|  | 6047 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); | 
|  | 6048 | if (changes) { | 
|  | 6049 | onFocusChangedLocked(*changes); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6050 | } | 
|  | 6051 | } // release lock | 
|  | 6052 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 6053 | mLooper->wake(); | 
|  | 6054 | } | 
|  | 6055 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6056 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { | 
|  | 6057 | if (changes.oldFocus) { | 
|  | 6058 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6059 | if (focusedInputChannel) { | 
|  | 6060 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
|  | 6061 | "focus left window"); | 
|  | 6062 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6063 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6064 | } | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6065 | } | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6066 | if (changes.newFocus) { | 
|  | 6067 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6068 | } | 
|  | 6069 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6070 | // If a window has pointer capture, then it must have focus. We need to ensure that this | 
|  | 6071 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. | 
|  | 6072 | // If the window loses focus before it loses pointer capture, then the window can be in a state | 
|  | 6073 | // where it has pointer capture but not focus, violating the contract. Therefore we must | 
|  | 6074 | // dispatch the pointer capture event before the focus event. Since focus events are added to | 
|  | 6075 | // the front of the queue (above), we add the pointer capture event to the front of the queue | 
|  | 6076 | // after the focus events are added. This ensures the pointer capture event ends up at the | 
|  | 6077 | // front. | 
|  | 6078 | disablePointerCaptureForcedLocked(); | 
|  | 6079 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6080 | if (mFocusedDisplayId == changes.displayId) { | 
|  | 6081 | notifyFocusChangedLocked(changes.oldFocus, changes.newFocus); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6082 | } | 
|  | 6083 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6084 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6085 | void InputDispatcher::disablePointerCaptureForcedLocked() { | 
|  | 6086 | if (!mFocusedWindowRequestedPointerCapture && !mWindowTokenWithPointerCapture) { | 
|  | 6087 | return; | 
|  | 6088 | } | 
|  | 6089 |  | 
|  | 6090 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); | 
|  | 6091 |  | 
|  | 6092 | if (mFocusedWindowRequestedPointerCapture) { | 
|  | 6093 | mFocusedWindowRequestedPointerCapture = false; | 
|  | 6094 | setPointerCaptureLocked(false); | 
|  | 6095 | } | 
|  | 6096 |  | 
|  | 6097 | if (!mWindowTokenWithPointerCapture) { | 
|  | 6098 | // No need to send capture changes because no window has capture. | 
|  | 6099 | return; | 
|  | 6100 | } | 
|  | 6101 |  | 
|  | 6102 | if (mPendingEvent != nullptr) { | 
|  | 6103 | // Move the pending event to the front of the queue. This will give the chance | 
|  | 6104 | // for the pending event to be dropped if it is a captured event. | 
|  | 6105 | mInboundQueue.push_front(mPendingEvent); | 
|  | 6106 | mPendingEvent = nullptr; | 
|  | 6107 | } | 
|  | 6108 |  | 
|  | 6109 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), | 
|  | 6110 | false /* hasCapture */); | 
|  | 6111 | mInboundQueue.push_front(std::move(entry)); | 
|  | 6112 | } | 
|  | 6113 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6114 | void InputDispatcher::setPointerCaptureLocked(bool enabled) { | 
|  | 6115 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 6116 | &InputDispatcher::doSetPointerCaptureLockedInterruptible); | 
|  | 6117 | commandEntry->enabled = enabled; | 
|  | 6118 | postCommandLocked(std::move(commandEntry)); | 
|  | 6119 | } | 
|  | 6120 |  | 
|  | 6121 | void InputDispatcher::doSetPointerCaptureLockedInterruptible( | 
|  | 6122 | android::inputdispatcher::CommandEntry* commandEntry) { | 
|  | 6123 | mLock.unlock(); | 
|  | 6124 |  | 
|  | 6125 | mPolicy->setPointerCapture(commandEntry->enabled); | 
|  | 6126 |  | 
|  | 6127 | mLock.lock(); | 
|  | 6128 | } | 
|  | 6129 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6130 | } // namespace android::inputdispatcher |