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 | |
| 31 | // Log debug messages about registrations. |
| 32 | #define DEBUG_REGISTRATION 0 |
| 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 | |
| 40 | // Log debug messages about the app switch latency optimization. |
| 41 | #define DEBUG_APP_SWITCH 0 |
| 42 | |
| 43 | // Log debug messages about hover events. |
| 44 | #define DEBUG_HOVER 0 |
| 45 | |
| 46 | #include "InputDispatcher.h" |
| 47 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 48 | #include "Connection.h" |
| 49 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 50 | #include <errno.h> |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 51 | #include <inttypes.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 52 | #include <limits.h> |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 53 | #include <statslog.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 54 | #include <stddef.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 55 | #include <time.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 56 | #include <unistd.h> |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 57 | #include <queue> |
| 58 | #include <sstream> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 59 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 60 | #include <android-base/chrono_utils.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 61 | #include <android-base/stringprintf.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 62 | #include <binder/Binder.h> |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 63 | #include <input/InputDevice.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 64 | #include <log/log.h> |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 65 | #include <openssl/hmac.h> |
| 66 | #include <openssl/rand.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 67 | #include <powermanager/PowerManager.h> |
| 68 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 69 | |
| 70 | #define INDENT " " |
| 71 | #define INDENT2 " " |
| 72 | #define INDENT3 " " |
| 73 | #define INDENT4 " " |
| 74 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 75 | using android::base::StringPrintf; |
| 76 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 77 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 78 | |
| 79 | // Default input dispatching timeout if there is no focused application or paused window |
| 80 | // from which to determine an appropriate dispatching timeout. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 81 | constexpr nsecs_t DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5000 * 1000000LL; // 5 sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 82 | |
| 83 | // Amount of time to allow for all pending events to be processed when an app switch |
| 84 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 85 | // 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] | 86 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 87 | |
| 88 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 89 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 90 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 | |
| 92 | // Amount of time to allow touch events to be streamed out to a connection before requiring |
| 93 | // that the first event be finished. This value extends the ANR timeout by the specified |
| 94 | // amount. For example, if streaming is allowed to get ahead by one second relative to the |
| 95 | // queue of waiting unfinished events, then ANRs will similarly be delayed by one second. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 96 | constexpr nsecs_t STREAM_AHEAD_EVENT_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 97 | |
| 98 | // 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] | 99 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 100 | |
| 101 | // Log a warning when an interception call takes longer than this to process. |
| 102 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 103 | |
| 104 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 105 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 106 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 | static inline nsecs_t now() { |
| 108 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 109 | } |
| 110 | |
| 111 | static inline const char* toString(bool value) { |
| 112 | return value ? "true" : "false"; |
| 113 | } |
| 114 | |
| 115 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 116 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 117 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static bool isValidKeyAction(int32_t action) { |
| 121 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 122 | case AKEY_EVENT_ACTION_DOWN: |
| 123 | case AKEY_EVENT_ACTION_UP: |
| 124 | return true; |
| 125 | default: |
| 126 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | static bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 131 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 132 | ALOGE("Key event has invalid action code 0x%x", action); |
| 133 | return false; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 138 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 139 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 140 | case AMOTION_EVENT_ACTION_DOWN: |
| 141 | case AMOTION_EVENT_ACTION_UP: |
| 142 | case AMOTION_EVENT_ACTION_CANCEL: |
| 143 | case AMOTION_EVENT_ACTION_MOVE: |
| 144 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 145 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 146 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 147 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 148 | case AMOTION_EVENT_ACTION_SCROLL: |
| 149 | return true; |
| 150 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 151 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 152 | int32_t index = getMotionEventActionPointerIndex(action); |
| 153 | return index >= 0 && index < pointerCount; |
| 154 | } |
| 155 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 156 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 157 | return actionButton != 0; |
| 158 | default: |
| 159 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 163 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 164 | const PointerProperties* pointerProperties) { |
| 165 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 166 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 167 | return false; |
| 168 | } |
| 169 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 170 | 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] | 171 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | BitSet32 pointerIdBits; |
| 175 | for (size_t i = 0; i < pointerCount; i++) { |
| 176 | int32_t id = pointerProperties[i].id; |
| 177 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 178 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 179 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | if (pointerIdBits.hasBit(id)) { |
| 183 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 184 | return false; |
| 185 | } |
| 186 | pointerIdBits.markBit(id); |
| 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 191 | static void dumpRegion(std::string& dump, const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 192 | if (region.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 193 | dump += "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 194 | return; |
| 195 | } |
| 196 | |
| 197 | bool first = true; |
| 198 | Region::const_iterator cur = region.begin(); |
| 199 | Region::const_iterator const tail = region.end(); |
| 200 | while (cur != tail) { |
| 201 | if (first) { |
| 202 | first = false; |
| 203 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 204 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 205 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 206 | 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] | 207 | cur++; |
| 208 | } |
| 209 | } |
| 210 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 211 | /** |
| 212 | * Find the entry in std::unordered_map by key, and return it. |
| 213 | * If the entry is not found, return a default constructed entry. |
| 214 | * |
| 215 | * Useful when the entries are vectors, since an empty vector will be returned |
| 216 | * if the entry is not found. |
| 217 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 218 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 219 | template <typename K, typename V> |
| 220 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 221 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 222 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 223 | } |
| 224 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 225 | /** |
| 226 | * Find the entry in std::unordered_map by value, and remove it. |
| 227 | * If more than one entry has the same value, then all matching |
| 228 | * key-value pairs will be removed. |
| 229 | * |
| 230 | * Return true if at least one value has been removed. |
| 231 | */ |
| 232 | template <typename K, typename V> |
| 233 | static bool removeByValue(std::unordered_map<K, V>& map, const V& value) { |
| 234 | bool removed = false; |
| 235 | for (auto it = map.begin(); it != map.end();) { |
| 236 | if (it->second == value) { |
| 237 | it = map.erase(it); |
| 238 | removed = true; |
| 239 | } else { |
| 240 | it++; |
| 241 | } |
| 242 | } |
| 243 | return removed; |
| 244 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 245 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 246 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { |
| 247 | if (first == second) { |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | if (first == nullptr || second == nullptr) { |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | return first->getToken() == second->getToken(); |
| 256 | } |
| 257 | |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 258 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 259 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 260 | } |
| 261 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 262 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 263 | EventEntry* eventEntry, |
| 264 | int32_t inputTargetFlags) { |
| 265 | if (inputTarget.useDefaultPointerInfo()) { |
| 266 | const PointerInfo& pointerInfo = inputTarget.getDefaultPointerInfo(); |
| 267 | return std::make_unique<DispatchEntry>(eventEntry, // increments ref |
| 268 | inputTargetFlags, pointerInfo.xOffset, |
| 269 | pointerInfo.yOffset, inputTarget.globalScaleFactor, |
| 270 | pointerInfo.windowXScale, pointerInfo.windowYScale); |
| 271 | } |
| 272 | |
| 273 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 274 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 275 | |
| 276 | PointerCoords pointerCoords[motionEntry.pointerCount]; |
| 277 | |
| 278 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 279 | // as long as all other pointers are normalized to the same value and the final DispatchEntry |
| 280 | // uses the offset and scale for the normalized pointer. |
| 281 | const PointerInfo& firstPointerInfo = |
| 282 | inputTarget.pointerInfos[inputTarget.pointerIds.firstMarkedBit()]; |
| 283 | |
| 284 | // Iterate through all pointers in the event to normalize against the first. |
| 285 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 286 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 287 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 288 | const PointerInfo& currPointerInfo = inputTarget.pointerInfos[pointerId]; |
| 289 | |
| 290 | // The scale factor is the ratio of the current pointers scale to the normalized scale. |
| 291 | float scaleXDiff = currPointerInfo.windowXScale / firstPointerInfo.windowXScale; |
| 292 | float scaleYDiff = currPointerInfo.windowYScale / firstPointerInfo.windowYScale; |
| 293 | |
| 294 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
| 295 | // First apply the current pointers offset to set the window at 0,0 |
| 296 | pointerCoords[pointerIndex].applyOffset(currPointerInfo.xOffset, currPointerInfo.yOffset); |
| 297 | // Next scale the coordinates. |
| 298 | pointerCoords[pointerIndex].scale(1, scaleXDiff, scaleYDiff); |
| 299 | // Lastly, offset the coordinates so they're in the normalized pointer's frame. |
| 300 | pointerCoords[pointerIndex].applyOffset(-firstPointerInfo.xOffset, |
| 301 | -firstPointerInfo.yOffset); |
| 302 | } |
| 303 | |
| 304 | MotionEntry* combinedMotionEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 305 | new MotionEntry(motionEntry.id, motionEntry.eventTime, motionEntry.deviceId, |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 306 | motionEntry.source, motionEntry.displayId, motionEntry.policyFlags, |
| 307 | motionEntry.action, motionEntry.actionButton, motionEntry.flags, |
| 308 | motionEntry.metaState, motionEntry.buttonState, |
| 309 | motionEntry.classification, motionEntry.edgeFlags, |
| 310 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 311 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 312 | motionEntry.downTime, motionEntry.pointerCount, |
| 313 | motionEntry.pointerProperties, pointerCoords, 0 /* xOffset */, |
| 314 | 0 /* yOffset */); |
| 315 | |
| 316 | if (motionEntry.injectionState) { |
| 317 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 318 | combinedMotionEntry->injectionState->refCount += 1; |
| 319 | } |
| 320 | |
| 321 | std::unique_ptr<DispatchEntry> dispatchEntry = |
| 322 | std::make_unique<DispatchEntry>(combinedMotionEntry, // increments ref |
| 323 | inputTargetFlags, firstPointerInfo.xOffset, |
| 324 | firstPointerInfo.yOffset, inputTarget.globalScaleFactor, |
| 325 | firstPointerInfo.windowXScale, |
| 326 | firstPointerInfo.windowYScale); |
| 327 | combinedMotionEntry->release(); |
| 328 | return dispatchEntry; |
| 329 | } |
| 330 | |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 331 | static void addGestureMonitors(const std::vector<Monitor>& monitors, |
| 332 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, |
| 333 | float yOffset = 0) { |
| 334 | if (monitors.empty()) { |
| 335 | return; |
| 336 | } |
| 337 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); |
| 338 | for (const Monitor& monitor : monitors) { |
| 339 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); |
| 340 | } |
| 341 | } |
| 342 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 343 | static std::array<uint8_t, 128> getRandomKey() { |
| 344 | std::array<uint8_t, 128> key; |
| 345 | if (RAND_bytes(key.data(), key.size()) != 1) { |
| 346 | LOG_ALWAYS_FATAL("Can't generate HMAC key"); |
| 347 | } |
| 348 | return key; |
| 349 | } |
| 350 | |
| 351 | // --- HmacKeyManager --- |
| 352 | |
| 353 | HmacKeyManager::HmacKeyManager() : mHmacKey(getRandomKey()) {} |
| 354 | |
| 355 | std::array<uint8_t, 32> HmacKeyManager::sign(const VerifiedInputEvent& event) const { |
| 356 | size_t size; |
| 357 | switch (event.type) { |
| 358 | case VerifiedInputEvent::Type::KEY: { |
| 359 | size = sizeof(VerifiedKeyEvent); |
| 360 | break; |
| 361 | } |
| 362 | case VerifiedInputEvent::Type::MOTION: { |
| 363 | size = sizeof(VerifiedMotionEvent); |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | std::vector<uint8_t> data; |
| 368 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 369 | data.assign(start, start + size); |
| 370 | return sign(data); |
| 371 | } |
| 372 | |
| 373 | std::array<uint8_t, 32> HmacKeyManager::sign(const std::vector<uint8_t>& data) const { |
| 374 | // SHA256 always generates 32-bytes result |
| 375 | std::array<uint8_t, 32> hash; |
| 376 | unsigned int hashLen = 0; |
| 377 | uint8_t* result = HMAC(EVP_sha256(), mHmacKey.data(), mHmacKey.size(), data.data(), data.size(), |
| 378 | hash.data(), &hashLen); |
| 379 | if (result == nullptr) { |
| 380 | ALOGE("Could not sign the data using HMAC"); |
| 381 | return INVALID_HMAC; |
| 382 | } |
| 383 | |
| 384 | if (hashLen != hash.size()) { |
| 385 | ALOGE("HMAC-SHA256 has unexpected length"); |
| 386 | return INVALID_HMAC; |
| 387 | } |
| 388 | |
| 389 | return hash; |
| 390 | } |
| 391 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 392 | // --- InputDispatcher --- |
| 393 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 394 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 395 | : mPolicy(policy), |
| 396 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 397 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 398 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 399 | mAppSwitchSawKeyDown(false), |
| 400 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 401 | mNextUnblockedEvent(nullptr), |
| 402 | mDispatchEnabled(false), |
| 403 | mDispatchFrozen(false), |
| 404 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 405 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 406 | // To avoid leaking stack in case that call never comes, and for tests, |
| 407 | // initialize it here anyways. |
| 408 | mInTouchMode(true), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 409 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
| 410 | mInputTargetWaitCause(INPUT_TARGET_WAIT_CAUSE_NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 411 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 412 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 413 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 414 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 415 | |
| 416 | policy->getDispatcherConfiguration(&mConfig); |
| 417 | } |
| 418 | |
| 419 | InputDispatcher::~InputDispatcher() { |
| 420 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 421 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 422 | |
| 423 | resetKeyRepeatLocked(); |
| 424 | releasePendingEventLocked(); |
| 425 | drainInboundQueueLocked(); |
| 426 | } |
| 427 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 428 | while (!mConnectionsByFd.empty()) { |
| 429 | sp<Connection> connection = mConnectionsByFd.begin()->second; |
| 430 | unregisterInputChannel(connection->inputChannel); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 434 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 435 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 436 | return ALREADY_EXISTS; |
| 437 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 438 | mThread = std::make_unique<InputThread>( |
| 439 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 440 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 444 | if (mThread && mThread->isCallingThread()) { |
| 445 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 446 | return INVALID_OPERATION; |
| 447 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 448 | mThread.reset(); |
| 449 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 452 | void InputDispatcher::dispatchOnce() { |
| 453 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 454 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 455 | std::scoped_lock _l(mLock); |
| 456 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 457 | |
| 458 | // Run a dispatch loop if there are no pending commands. |
| 459 | // The dispatch loop might enqueue commands to run afterwards. |
| 460 | if (!haveCommandsLocked()) { |
| 461 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 462 | } |
| 463 | |
| 464 | // Run all pending commands if there are any. |
| 465 | // If any commands were run then force the next poll to wake up immediately. |
| 466 | if (runCommandsLockedInterruptible()) { |
| 467 | nextWakeupTime = LONG_LONG_MIN; |
| 468 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 469 | |
| 470 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 471 | // pending or queued events |
| 472 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 473 | mDispatcherEnteredIdle.notify_all(); |
| 474 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 475 | } // release lock |
| 476 | |
| 477 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 478 | nsecs_t currentTime = now(); |
| 479 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 480 | mLooper->pollOnce(timeoutMillis); |
| 481 | } |
| 482 | |
| 483 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 484 | nsecs_t currentTime = now(); |
| 485 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 486 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 487 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 488 | // repeat if the device is just coming out of sleep. |
| 489 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 490 | resetKeyRepeatLocked(); |
| 491 | } |
| 492 | |
| 493 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 494 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 495 | if (DEBUG_FOCUS) { |
| 496 | ALOGD("Dispatch frozen. Waiting some more."); |
| 497 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 498 | return; |
| 499 | } |
| 500 | |
| 501 | // Optimize latency of app switches. |
| 502 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 503 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 504 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 505 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 506 | *nextWakeupTime = mAppSwitchDueTime; |
| 507 | } |
| 508 | |
| 509 | // Ready to start a new event. |
| 510 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 511 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 512 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 513 | if (isAppSwitchDue) { |
| 514 | // The inbound queue is empty so the app switch key we were waiting |
| 515 | // for will never arrive. Stop waiting for it. |
| 516 | resetPendingAppSwitchLocked(false); |
| 517 | isAppSwitchDue = false; |
| 518 | } |
| 519 | |
| 520 | // Synthesize a key repeat if appropriate. |
| 521 | if (mKeyRepeatState.lastKeyEntry) { |
| 522 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 523 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 524 | } else { |
| 525 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 526 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // Nothing to do if there is no pending event. |
| 532 | if (!mPendingEvent) { |
| 533 | return; |
| 534 | } |
| 535 | } else { |
| 536 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 537 | mPendingEvent = mInboundQueue.front(); |
| 538 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 539 | traceInboundQueueLengthLocked(); |
| 540 | } |
| 541 | |
| 542 | // Poke user activity for this event. |
| 543 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 544 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | // Get ready to dispatch the event. |
| 548 | resetANRTimeoutsLocked(); |
| 549 | } |
| 550 | |
| 551 | // Now we have an event to dispatch. |
| 552 | // 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] | 553 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 554 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 555 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 556 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 557 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 558 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 559 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 563 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 567 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 568 | ConfigurationChangedEntry* typedEntry = |
| 569 | static_cast<ConfigurationChangedEntry*>(mPendingEvent); |
| 570 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 571 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 572 | break; |
| 573 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 574 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 575 | case EventEntry::Type::DEVICE_RESET: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 576 | DeviceResetEntry* typedEntry = static_cast<DeviceResetEntry*>(mPendingEvent); |
| 577 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 578 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 579 | break; |
| 580 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 581 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 582 | case EventEntry::Type::FOCUS: { |
| 583 | FocusEntry* typedEntry = static_cast<FocusEntry*>(mPendingEvent); |
| 584 | dispatchFocusLocked(currentTime, typedEntry); |
| 585 | done = true; |
| 586 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 587 | break; |
| 588 | } |
| 589 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 590 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 591 | KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); |
| 592 | if (isAppSwitchDue) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 593 | if (isAppSwitchKeyEvent(*typedEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 594 | resetPendingAppSwitchLocked(true); |
| 595 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 596 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 597 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 598 | } |
| 599 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 600 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 601 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 602 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 603 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 604 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 605 | } |
| 606 | done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 607 | break; |
| 608 | } |
| 609 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 610 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 611 | MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 612 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 613 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 614 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 615 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 616 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 617 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 618 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 619 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 620 | } |
| 621 | done = dispatchMotionLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 622 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 627 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 628 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 629 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 630 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 631 | |
| 632 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 633 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
| 637 | bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 638 | bool needWake = mInboundQueue.empty(); |
| 639 | mInboundQueue.push_back(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 640 | traceInboundQueueLengthLocked(); |
| 641 | |
| 642 | switch (entry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 643 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 644 | // Optimize app switch latency. |
| 645 | // If the application takes too long to catch up then we drop all events preceding |
| 646 | // the app switch key. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 647 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 648 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 649 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 650 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 651 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 652 | if (mAppSwitchSawKeyDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 653 | #if DEBUG_APP_SWITCH |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 654 | ALOGD("App switch is pending!"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 655 | #endif |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 656 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 657 | mAppSwitchSawKeyDown = false; |
| 658 | needWake = true; |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | break; |
| 663 | } |
| 664 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 665 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 666 | // Optimize case where the current application is unresponsive and the user |
| 667 | // decides to touch a window in a different application. |
| 668 | // If the application takes too long to catch up then we drop all events preceding |
| 669 | // the touch into the other window. |
| 670 | MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); |
| 671 | if (motionEntry->action == AMOTION_EVENT_ACTION_DOWN && |
| 672 | (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) && |
| 673 | mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY && |
| 674 | mInputTargetWaitApplicationToken != nullptr) { |
| 675 | int32_t displayId = motionEntry->displayId; |
| 676 | int32_t x = |
| 677 | int32_t(motionEntry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 678 | int32_t y = |
| 679 | int32_t(motionEntry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 680 | sp<InputWindowHandle> touchedWindowHandle = |
| 681 | findTouchedWindowAtLocked(displayId, x, y); |
| 682 | if (touchedWindowHandle != nullptr && |
| 683 | touchedWindowHandle->getApplicationToken() != |
| 684 | mInputTargetWaitApplicationToken) { |
| 685 | // User touched a different application than the one we are waiting on. |
| 686 | // Flag the event, and start pruning the input queue. |
| 687 | mNextUnblockedEvent = motionEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 688 | needWake = true; |
| 689 | } |
| 690 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 691 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 692 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 693 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 694 | case EventEntry::Type::DEVICE_RESET: |
| 695 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 696 | // nothing to do |
| 697 | break; |
| 698 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | return needWake; |
| 702 | } |
| 703 | |
| 704 | void InputDispatcher::addRecentEventLocked(EventEntry* entry) { |
| 705 | entry->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 706 | mRecentQueue.push_back(entry); |
| 707 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
| 708 | mRecentQueue.front()->release(); |
| 709 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 713 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 714 | int32_t y, bool addOutsideTargets, |
| 715 | bool addPortalWindows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 716 | // Traverse windows from front to back to find touched window. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 717 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| 718 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 719 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 720 | if (windowInfo->displayId == displayId) { |
| 721 | int32_t flags = windowInfo->layoutParamsFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 722 | |
| 723 | if (windowInfo->visible) { |
| 724 | if (!(flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 725 | bool isTouchModal = (flags & |
| 726 | (InputWindowInfo::FLAG_NOT_FOCUSABLE | |
| 727 | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 728 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 729 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 730 | if (portalToDisplayId != ADISPLAY_ID_NONE && |
| 731 | portalToDisplayId != displayId) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 732 | if (addPortalWindows) { |
| 733 | // For the monitoring channels of the display. |
| 734 | mTempTouchState.addPortalWindow(windowHandle); |
| 735 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 736 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, |
| 737 | addOutsideTargets, addPortalWindows); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 738 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 739 | // Found window. |
| 740 | return windowHandle; |
| 741 | } |
| 742 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 743 | |
| 744 | if (addOutsideTargets && (flags & InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 745 | mTempTouchState.addOrUpdateWindow(windowHandle, |
| 746 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 747 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 748 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 749 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 750 | } |
| 751 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 752 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 753 | } |
| 754 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 755 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 756 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 757 | std::vector<TouchedMonitor> touchedMonitors; |
| 758 | |
| 759 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| 760 | addGestureMonitors(monitors, touchedMonitors); |
| 761 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { |
| 762 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); |
| 763 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 764 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, |
| 765 | -windowInfo->frameTop); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 766 | } |
| 767 | return touchedMonitors; |
| 768 | } |
| 769 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 770 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 771 | const char* reason; |
| 772 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 773 | case DropReason::POLICY: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 774 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 775 | ALOGD("Dropped event because policy consumed it."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 776 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 777 | reason = "inbound event was dropped because the policy consumed it"; |
| 778 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 779 | case DropReason::DISABLED: |
| 780 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 781 | ALOGI("Dropped event because input dispatch is disabled."); |
| 782 | } |
| 783 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 784 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 785 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 786 | ALOGI("Dropped event because of pending overdue app switch."); |
| 787 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 788 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 789 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 790 | ALOGI("Dropped event because the current application is not responding and the user " |
| 791 | "has started interacting with a different application."); |
| 792 | reason = "inbound event was dropped because the current application is not responding " |
| 793 | "and the user has started interacting with a different application"; |
| 794 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 795 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 796 | ALOGI("Dropped event because it is stale."); |
| 797 | reason = "inbound event was dropped because it is stale"; |
| 798 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 799 | case DropReason::NOT_DROPPED: { |
| 800 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 801 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 802 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 803 | } |
| 804 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 805 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 806 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 807 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 808 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 809 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 810 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 811 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 812 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 813 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 814 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 815 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 816 | } else { |
| 817 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 818 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 819 | } |
| 820 | break; |
| 821 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 822 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 823 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 824 | case EventEntry::Type::DEVICE_RESET: { |
| 825 | LOG_ALWAYS_FATAL("Should not drop %s events", EventEntry::typeToString(entry.type)); |
| 826 | break; |
| 827 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 831 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 832 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 833 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 834 | } |
| 835 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 836 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 837 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 838 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 839 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 843 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 844 | } |
| 845 | |
| 846 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 847 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 848 | |
| 849 | #if DEBUG_APP_SWITCH |
| 850 | if (handled) { |
| 851 | ALOGD("App switch has arrived."); |
| 852 | } else { |
| 853 | ALOGD("App switch was abandoned."); |
| 854 | } |
| 855 | #endif |
| 856 | } |
| 857 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 858 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 859 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | bool InputDispatcher::runCommandsLockedInterruptible() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 863 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 864 | return false; |
| 865 | } |
| 866 | |
| 867 | do { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 868 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 869 | mCommandQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 870 | Command command = commandEntry->command; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 871 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 872 | |
| 873 | commandEntry->connection.clear(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 874 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 875 | return true; |
| 876 | } |
| 877 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 878 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { |
| 879 | mCommandQueue.push_back(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 883 | while (!mInboundQueue.empty()) { |
| 884 | EventEntry* entry = mInboundQueue.front(); |
| 885 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 886 | releaseInboundEventLocked(entry); |
| 887 | } |
| 888 | traceInboundQueueLengthLocked(); |
| 889 | } |
| 890 | |
| 891 | void InputDispatcher::releasePendingEventLocked() { |
| 892 | if (mPendingEvent) { |
| 893 | resetANRTimeoutsLocked(); |
| 894 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 895 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
| 899 | void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { |
| 900 | InjectionState* injectionState = entry->injectionState; |
| 901 | if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 902 | #if DEBUG_DISPATCH_CYCLE |
| 903 | ALOGD("Injected inbound event was dropped."); |
| 904 | #endif |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 905 | setInjectionResult(entry, INPUT_EVENT_INJECTION_FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 906 | } |
| 907 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 908 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 909 | } |
| 910 | addRecentEventLocked(entry); |
| 911 | entry->release(); |
| 912 | } |
| 913 | |
| 914 | void InputDispatcher::resetKeyRepeatLocked() { |
| 915 | if (mKeyRepeatState.lastKeyEntry) { |
| 916 | mKeyRepeatState.lastKeyEntry->release(); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 917 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 921 | KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 922 | KeyEntry* entry = mKeyRepeatState.lastKeyEntry; |
| 923 | |
| 924 | // Reuse the repeated key entry if it is otherwise unreferenced. |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 925 | uint32_t policyFlags = entry->policyFlags & |
| 926 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 927 | if (entry->refCount == 1) { |
| 928 | entry->recycle(); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 929 | entry->id = mIdGenerator.nextId(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 930 | entry->eventTime = currentTime; |
| 931 | entry->policyFlags = policyFlags; |
| 932 | entry->repeatCount += 1; |
| 933 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 934 | KeyEntry* newEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 935 | new KeyEntry(mIdGenerator.nextId(), currentTime, entry->deviceId, entry->source, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 936 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 937 | entry->keyCode, entry->scanCode, entry->metaState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 938 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 939 | |
| 940 | mKeyRepeatState.lastKeyEntry = newEntry; |
| 941 | entry->release(); |
| 942 | |
| 943 | entry = newEntry; |
| 944 | } |
| 945 | entry->syntheticRepeat = true; |
| 946 | |
| 947 | // Increment reference count since we keep a reference to the event in |
| 948 | // mKeyRepeatState.lastKeyEntry in addition to the one we return. |
| 949 | entry->refCount += 1; |
| 950 | |
| 951 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
| 952 | return entry; |
| 953 | } |
| 954 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 955 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
| 956 | ConfigurationChangedEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 957 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 958 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 959 | #endif |
| 960 | |
| 961 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 962 | resetKeyRepeatLocked(); |
| 963 | |
| 964 | // 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] | 965 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 966 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 967 | commandEntry->eventTime = entry->eventTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 968 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 969 | return true; |
| 970 | } |
| 971 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 972 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 973 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 974 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry->eventTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 975 | entry->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 976 | #endif |
| 977 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 978 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 979 | options.deviceId = entry->deviceId; |
| 980 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 981 | return true; |
| 982 | } |
| 983 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 984 | void InputDispatcher::enqueueFocusEventLocked(const InputWindowHandle& window, bool hasFocus) { |
| 985 | FocusEntry* focusEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 986 | new FocusEntry(mIdGenerator.nextId(), now(), window.getToken(), hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 987 | enqueueInboundEventLocked(focusEntry); |
| 988 | } |
| 989 | |
| 990 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) { |
| 991 | sp<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 992 | if (channel == nullptr) { |
| 993 | return; // Window has gone away |
| 994 | } |
| 995 | InputTarget target; |
| 996 | target.inputChannel = channel; |
| 997 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 998 | entry->dispatchInProgress = true; |
| 999 | |
| 1000 | dispatchEventLocked(currentTime, entry, {target}); |
| 1001 | } |
| 1002 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1003 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1004 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1005 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1006 | if (!entry->dispatchInProgress) { |
| 1007 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1008 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1009 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1010 | if (mKeyRepeatState.lastKeyEntry && |
| 1011 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1012 | // We have seen two identical key downs in a row which indicates that the device |
| 1013 | // driver is automatically generating key repeats itself. We take note of the |
| 1014 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1015 | // we will not need to synthesize key repeats ourselves. |
| 1016 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1017 | resetKeyRepeatLocked(); |
| 1018 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1019 | } else { |
| 1020 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1021 | resetKeyRepeatLocked(); |
| 1022 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1023 | } |
| 1024 | mKeyRepeatState.lastKeyEntry = entry; |
| 1025 | entry->refCount += 1; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1026 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1027 | resetKeyRepeatLocked(); |
| 1028 | } |
| 1029 | |
| 1030 | if (entry->repeatCount == 1) { |
| 1031 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1032 | } else { |
| 1033 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1034 | } |
| 1035 | |
| 1036 | entry->dispatchInProgress = true; |
| 1037 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1038 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | // Handle case where the policy asked us to try again later last time. |
| 1042 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1043 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1044 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1045 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1046 | } |
| 1047 | return false; // wait until next wakeup |
| 1048 | } |
| 1049 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1050 | entry->interceptKeyWakeupTime = 0; |
| 1051 | } |
| 1052 | |
| 1053 | // Give the policy a chance to intercept the key. |
| 1054 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1055 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1056 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1057 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1058 | sp<InputWindowHandle> focusedWindowHandle = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1059 | getValueByKey(mFocusedWindowHandlesByDisplay, getTargetDisplayId(*entry)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1060 | if (focusedWindowHandle != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1061 | commandEntry->inputChannel = getInputChannelLocked(focusedWindowHandle->getToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1062 | } |
| 1063 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1064 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1065 | entry->refCount += 1; |
| 1066 | return false; // wait for the command to run |
| 1067 | } else { |
| 1068 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1069 | } |
| 1070 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1071 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1072 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1077 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1078 | setInjectionResult(entry, |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1079 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1080 | : INPUT_EVENT_INJECTION_FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1081 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1082 | return true; |
| 1083 | } |
| 1084 | |
| 1085 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1086 | std::vector<InputTarget> inputTargets; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1087 | int32_t injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1088 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1089 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1090 | return false; |
| 1091 | } |
| 1092 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1093 | setInjectionResult(entry, injectionResult); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1094 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
| 1095 | return true; |
| 1096 | } |
| 1097 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1098 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1099 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1100 | |
| 1101 | // Dispatch the key. |
| 1102 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1103 | return true; |
| 1104 | } |
| 1105 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1106 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1107 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1108 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1109 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1110 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1111 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1112 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1113 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1114 | #endif |
| 1115 | } |
| 1116 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1117 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, |
| 1118 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1119 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1120 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1121 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1122 | entry->dispatchInProgress = true; |
| 1123 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1124 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1128 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1129 | setInjectionResult(entry, |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1130 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1131 | : INPUT_EVENT_INJECTION_FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1132 | return true; |
| 1133 | } |
| 1134 | |
| 1135 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1136 | |
| 1137 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1138 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1139 | |
| 1140 | bool conflictingPointerActions = false; |
| 1141 | int32_t injectionResult; |
| 1142 | if (isPointerEvent) { |
| 1143 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1144 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1145 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1146 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1147 | } else { |
| 1148 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1149 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1150 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1151 | } |
| 1152 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1153 | return false; |
| 1154 | } |
| 1155 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1156 | setInjectionResult(entry, injectionResult); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1157 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 1158 | if (injectionResult != INPUT_EVENT_INJECTION_PERMISSION_DENIED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1159 | CancelationOptions::Mode mode(isPointerEvent |
| 1160 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1161 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 1162 | CancelationOptions options(mode, "input event injection failed"); |
| 1163 | synthesizeCancelationEventsForMonitorsLocked(options); |
| 1164 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1165 | return true; |
| 1166 | } |
| 1167 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1168 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1169 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1170 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1171 | if (isPointerEvent) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 1172 | std::unordered_map<int32_t, TouchState>::iterator it = |
| 1173 | mTouchStatesByDisplay.find(entry->displayId); |
| 1174 | if (it != mTouchStatesByDisplay.end()) { |
| 1175 | const TouchState& state = it->second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1176 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1177 | // The event has gone through these portal windows, so we add monitoring targets of |
| 1178 | // the corresponding displays as well. |
| 1179 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1180 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1181 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1182 | -windowInfo->frameLeft, -windowInfo->frameTop); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1188 | // Dispatch the motion. |
| 1189 | if (conflictingPointerActions) { |
| 1190 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1191 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1192 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1193 | } |
| 1194 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1195 | return true; |
| 1196 | } |
| 1197 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1198 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1199 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1200 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1201 | ", policyFlags=0x%x, " |
| 1202 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1203 | "metaState=0x%x, buttonState=0x%x," |
| 1204 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1205 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1206 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1207 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1208 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1209 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1210 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1211 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1212 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1213 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1214 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1215 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1216 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1217 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1218 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1219 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1220 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1221 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1222 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1223 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1224 | } |
| 1225 | #endif |
| 1226 | } |
| 1227 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1228 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, EventEntry* eventEntry, |
| 1229 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1230 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1231 | #if DEBUG_DISPATCH_CYCLE |
| 1232 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1233 | #endif |
| 1234 | |
| 1235 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1236 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1237 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1238 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1239 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1240 | sp<Connection> connection = |
| 1241 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1242 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1243 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1244 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1245 | if (DEBUG_FOCUS) { |
| 1246 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1247 | "is no longer registered with the input dispatcher.", |
| 1248 | inputTarget.inputChannel->getName().c_str()); |
| 1249 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | } |
| 1253 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1254 | int32_t InputDispatcher::handleTargetsNotReadyLocked( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1255 | nsecs_t currentTime, const EventEntry& entry, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1256 | const sp<InputApplicationHandle>& applicationHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1257 | const sp<InputWindowHandle>& windowHandle, nsecs_t* nextWakeupTime, const char* reason) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1258 | if (applicationHandle == nullptr && windowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1259 | if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1260 | if (DEBUG_FOCUS) { |
| 1261 | ALOGD("Waiting for system to become ready for input. Reason: %s", reason); |
| 1262 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1263 | mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY; |
| 1264 | mInputTargetWaitStartTime = currentTime; |
| 1265 | mInputTargetWaitTimeoutTime = LONG_LONG_MAX; |
| 1266 | mInputTargetWaitTimeoutExpired = false; |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 1267 | mInputTargetWaitApplicationToken.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1268 | } |
| 1269 | } else { |
| 1270 | if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1271 | ALOGI("Waiting for application to become ready for input: %s. Reason: %s", |
| 1272 | getApplicationWindowLabel(applicationHandle, windowHandle).c_str(), reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1273 | nsecs_t timeout; |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1274 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1275 | timeout = windowHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1276 | } else if (applicationHandle != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1277 | timeout = |
| 1278 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1279 | } else { |
| 1280 | timeout = DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
| 1281 | } |
| 1282 | |
| 1283 | mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY; |
| 1284 | mInputTargetWaitStartTime = currentTime; |
| 1285 | mInputTargetWaitTimeoutTime = currentTime + timeout; |
| 1286 | mInputTargetWaitTimeoutExpired = false; |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 1287 | mInputTargetWaitApplicationToken.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1288 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1289 | if (windowHandle != nullptr) { |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 1290 | mInputTargetWaitApplicationToken = windowHandle->getApplicationToken(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1291 | } |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 1292 | if (mInputTargetWaitApplicationToken == nullptr && applicationHandle != nullptr) { |
| 1293 | mInputTargetWaitApplicationToken = applicationHandle->getApplicationToken(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | if (mInputTargetWaitTimeoutExpired) { |
| 1299 | return INPUT_EVENT_INJECTION_TIMED_OUT; |
| 1300 | } |
| 1301 | |
| 1302 | if (currentTime >= mInputTargetWaitTimeoutTime) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1303 | onANRLocked(currentTime, applicationHandle, windowHandle, entry.eventTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1304 | mInputTargetWaitStartTime, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1305 | |
| 1306 | // Force poll loop to wake up immediately on next iteration once we get the |
| 1307 | // ANR response back from the policy. |
| 1308 | *nextWakeupTime = LONG_LONG_MIN; |
| 1309 | return INPUT_EVENT_INJECTION_PENDING; |
| 1310 | } else { |
| 1311 | // Force poll loop to wake up when timeout is due. |
| 1312 | if (mInputTargetWaitTimeoutTime < *nextWakeupTime) { |
| 1313 | *nextWakeupTime = mInputTargetWaitTimeoutTime; |
| 1314 | } |
| 1315 | return INPUT_EVENT_INJECTION_PENDING; |
| 1316 | } |
| 1317 | } |
| 1318 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 1319 | void InputDispatcher::removeWindowByTokenLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 1320 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 1321 | TouchState& state = pair.second; |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 1322 | state.removeWindowByToken(token); |
| 1323 | } |
| 1324 | } |
| 1325 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1326 | void InputDispatcher::resumeAfterTargetsNotReadyTimeoutLocked( |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 1327 | nsecs_t newTimeout, const sp<IBinder>& inputConnectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1328 | if (newTimeout > 0) { |
| 1329 | // Extend the timeout. |
| 1330 | mInputTargetWaitTimeoutTime = now() + newTimeout; |
| 1331 | } else { |
| 1332 | // Give up. |
| 1333 | mInputTargetWaitTimeoutExpired = true; |
| 1334 | |
| 1335 | // Input state will not be realistic. Mark it out of sync. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 1336 | sp<Connection> connection = getConnectionLocked(inputConnectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1337 | if (connection != nullptr) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 1338 | removeWindowByTokenLocked(inputConnectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1339 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1340 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1341 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1342 | "application not responding"); |
| 1343 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1349 | nsecs_t InputDispatcher::getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1350 | if (mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { |
| 1351 | return currentTime - mInputTargetWaitStartTime; |
| 1352 | } |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | void InputDispatcher::resetANRTimeoutsLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1357 | if (DEBUG_FOCUS) { |
| 1358 | ALOGD("Resetting ANR timeouts."); |
| 1359 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1360 | |
| 1361 | // Reset input target wait timeout. |
| 1362 | mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_NONE; |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 1363 | mInputTargetWaitApplicationToken.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1366 | /** |
| 1367 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1368 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1369 | * Focused display is the display that the user most recently interacted with. |
| 1370 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1371 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1372 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1373 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1374 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1375 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1376 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1377 | break; |
| 1378 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1379 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1380 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1381 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1382 | break; |
| 1383 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1384 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1385 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1386 | case EventEntry::Type::DEVICE_RESET: { |
| 1387 | ALOGE("%s events do not have a target display", EventEntry::typeToString(entry.type)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1388 | return ADISPLAY_ID_NONE; |
| 1389 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1390 | } |
| 1391 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1392 | } |
| 1393 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1394 | int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1395 | const EventEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1396 | std::vector<InputTarget>& inputTargets, |
| 1397 | nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1398 | int32_t injectionResult; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1399 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1400 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1401 | int32_t displayId = getTargetDisplayId(entry); |
| 1402 | sp<InputWindowHandle> focusedWindowHandle = |
| 1403 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 1404 | sp<InputApplicationHandle> focusedApplicationHandle = |
| 1405 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1406 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1407 | // If there is no currently focused window and no focused application |
| 1408 | // then drop the event. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1409 | if (focusedWindowHandle == nullptr) { |
| 1410 | if (focusedApplicationHandle != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1411 | injectionResult = |
| 1412 | handleTargetsNotReadyLocked(currentTime, entry, focusedApplicationHandle, |
| 1413 | nullptr, nextWakeupTime, |
| 1414 | "Waiting because no window has focus but there is " |
| 1415 | "a focused application that may eventually add a " |
| 1416 | "window when it finishes starting up."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1417 | goto Unresponsive; |
| 1418 | } |
| 1419 | |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 1420 | ALOGI("Dropping event because there is no focused window or focused application in display " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1421 | "%" PRId32 ".", |
| 1422 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1423 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1424 | goto Failed; |
| 1425 | } |
| 1426 | |
| 1427 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1428 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1429 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1430 | goto Failed; |
| 1431 | } |
| 1432 | |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1433 | // Check whether the window is ready for more input. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1434 | reason = checkWindowReadyForMoreInputLocked(currentTime, focusedWindowHandle, entry, "focused"); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1435 | if (!reason.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1436 | injectionResult = |
| 1437 | handleTargetsNotReadyLocked(currentTime, entry, focusedApplicationHandle, |
| 1438 | focusedWindowHandle, nextWakeupTime, reason.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1439 | goto Unresponsive; |
| 1440 | } |
| 1441 | |
| 1442 | // Success! Output targets. |
| 1443 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1444 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1445 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1446 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1447 | |
| 1448 | // Done. |
| 1449 | Failed: |
| 1450 | Unresponsive: |
| 1451 | nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1452 | updateDispatchStatistics(currentTime, entry, injectionResult, timeSpentWaitingForApplication); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1453 | if (DEBUG_FOCUS) { |
| 1454 | ALOGD("findFocusedWindow finished: injectionResult=%d, " |
| 1455 | "timeSpentWaitingForApplication=%0.1fms", |
| 1456 | injectionResult, timeSpentWaitingForApplication / 1000000.0); |
| 1457 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1458 | return injectionResult; |
| 1459 | } |
| 1460 | |
| 1461 | int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1462 | const MotionEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1463 | std::vector<InputTarget>& inputTargets, |
| 1464 | nsecs_t* nextWakeupTime, |
| 1465 | bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1466 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1467 | enum InjectionPermission { |
| 1468 | INJECTION_PERMISSION_UNKNOWN, |
| 1469 | INJECTION_PERMISSION_GRANTED, |
| 1470 | INJECTION_PERMISSION_DENIED |
| 1471 | }; |
| 1472 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1473 | // For security reasons, we defer updating the touch state until we are sure that |
| 1474 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1475 | int32_t displayId = entry.displayId; |
| 1476 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1477 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1478 | |
| 1479 | // Update the touch state as needed based on the properties of the touch event. |
| 1480 | int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; |
| 1481 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
| 1482 | sp<InputWindowHandle> newHoverWindowHandle; |
| 1483 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1484 | // Copy current touch state into mTempTouchState. |
| 1485 | // This state is always reset at the end of this function, so if we don't find state |
| 1486 | // for the specified display then our initial state will be empty. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1487 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 1488 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1489 | mTouchStatesByDisplay.find(displayId); |
| 1490 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1491 | oldState = &(oldStateIt->second); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1492 | mTempTouchState.copyFrom(*oldState); |
| 1493 | } |
| 1494 | |
| 1495 | bool isSplit = mTempTouchState.split; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1496 | bool switchedDevice = mTempTouchState.deviceId >= 0 && mTempTouchState.displayId >= 0 && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1497 | (mTempTouchState.deviceId != entry.deviceId || mTempTouchState.source != entry.source || |
| 1498 | mTempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1499 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1500 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1501 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1502 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1503 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1504 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1505 | bool wrongDevice = false; |
| 1506 | if (newGesture) { |
| 1507 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1508 | if (switchedDevice && mTempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1509 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1510 | "in display %" PRId32, |
| 1511 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1512 | // TODO: test multiple simultaneous input streams. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1513 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1514 | switchedDevice = false; |
| 1515 | wrongDevice = true; |
| 1516 | goto Failed; |
| 1517 | } |
| 1518 | mTempTouchState.reset(); |
| 1519 | mTempTouchState.down = down; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1520 | mTempTouchState.deviceId = entry.deviceId; |
| 1521 | mTempTouchState.source = entry.source; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1522 | mTempTouchState.displayId = displayId; |
| 1523 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1524 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1525 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1526 | "in display %" PRId32, |
| 1527 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1528 | // TODO: test multiple simultaneous input streams. |
| 1529 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1530 | switchedDevice = false; |
| 1531 | wrongDevice = true; |
| 1532 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1536 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1537 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1538 | int32_t x; |
| 1539 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1540 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1541 | // Always dispatch mouse events to cursor position. |
| 1542 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1543 | x = int32_t(entry.xCursorPosition); |
| 1544 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1545 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1546 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1547 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1548 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1549 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1550 | sp<InputWindowHandle> newTouchedWindowHandle = |
| 1551 | findTouchedWindowAtLocked(displayId, x, y, isDown /*addOutsideTargets*/, |
| 1552 | true /*addPortalWindows*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1553 | |
| 1554 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
| 1555 | ? findTouchedGestureMonitorsLocked(displayId, mTempTouchState.portalWindows) |
| 1556 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1557 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1558 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1559 | if (newTouchedWindowHandle != nullptr && |
| 1560 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1561 | // New window supports splitting, but we should never split mouse events. |
| 1562 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1563 | } else if (isSplit) { |
| 1564 | // New window does not support splitting but we have already split events. |
| 1565 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1566 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1570 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1571 | // Try to assign the pointer to the first foreground window we find, if there is one. |
| 1572 | newTouchedWindowHandle = mTempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 1576 | 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] | 1577 | "(%d, %d) in display %" PRId32 ".", |
| 1578 | x, y, displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1579 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1580 | goto Failed; |
| 1581 | } |
| 1582 | |
| 1583 | if (newTouchedWindowHandle != nullptr) { |
| 1584 | // Set target flags. |
| 1585 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 1586 | if (isSplit) { |
| 1587 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1588 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1589 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1590 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1591 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 1592 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1593 | } |
| 1594 | |
| 1595 | // Update hover state. |
| 1596 | if (isHoverAction) { |
| 1597 | newHoverWindowHandle = newTouchedWindowHandle; |
| 1598 | } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
| 1599 | newHoverWindowHandle = mLastHoverWindowHandle; |
| 1600 | } |
| 1601 | |
| 1602 | // Update the temporary touch state. |
| 1603 | BitSet32 pointerIds; |
| 1604 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1605 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1606 | pointerIds.markBit(pointerId); |
| 1607 | } |
| 1608 | mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1609 | } |
| 1610 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1611 | mTempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1612 | } else { |
| 1613 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 1614 | |
| 1615 | // If the pointer is not currently down, then ignore the event. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1616 | if (!mTempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1617 | if (DEBUG_FOCUS) { |
| 1618 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 1619 | "dropped the pointer down event in display %" PRId32, |
| 1620 | displayId); |
| 1621 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1622 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1623 | goto Failed; |
| 1624 | } |
| 1625 | |
| 1626 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1627 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1628 | mTempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1629 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1630 | 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] | 1631 | |
| 1632 | sp<InputWindowHandle> oldTouchedWindowHandle = |
| 1633 | mTempTouchState.getFirstForegroundWindowHandle(); |
| 1634 | sp<InputWindowHandle> newTouchedWindowHandle = |
| 1635 | findTouchedWindowAtLocked(displayId, x, y); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1636 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 1637 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1638 | if (DEBUG_FOCUS) { |
| 1639 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 1640 | oldTouchedWindowHandle->getName().c_str(), |
| 1641 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 1642 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1643 | // Make a slippery exit from the old window. |
| 1644 | mTempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1645 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 1646 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1647 | |
| 1648 | // Make a slippery entrance into the new window. |
| 1649 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 1650 | isSplit = true; |
| 1651 | } |
| 1652 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1653 | int32_t targetFlags = |
| 1654 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1655 | if (isSplit) { |
| 1656 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 1657 | } |
| 1658 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1659 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1660 | } |
| 1661 | |
| 1662 | BitSet32 pointerIds; |
| 1663 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1664 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1665 | } |
| 1666 | mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
| 1667 | } |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
| 1672 | // Let the previous window know that the hover sequence is over. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1673 | if (mLastHoverWindowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1674 | #if DEBUG_HOVER |
| 1675 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1676 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1677 | #endif |
| 1678 | mTempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1679 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, |
| 1680 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | // Let the new window know that the hover sequence is starting. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1684 | if (newHoverWindowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1685 | #if DEBUG_HOVER |
| 1686 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1687 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1688 | #endif |
| 1689 | mTempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1690 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 1691 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | // Check permission to inject into all touched foreground windows and ensure there |
| 1696 | // is at least one touched foreground window. |
| 1697 | { |
| 1698 | bool haveForegroundWindow = false; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1699 | for (const TouchedWindow& touchedWindow : mTempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1700 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 1701 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1702 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1703 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1704 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1705 | goto Failed; |
| 1706 | } |
| 1707 | } |
| 1708 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1709 | bool hasGestureMonitor = !mTempTouchState.gestureMonitors.empty(); |
| 1710 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1711 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 1712 | "%" PRId32 " or gesture monitor to receive it.", |
| 1713 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1714 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1715 | goto Failed; |
| 1716 | } |
| 1717 | |
| 1718 | // Permission granted to injection into all touched foreground windows. |
| 1719 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1720 | } |
| 1721 | |
| 1722 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 1723 | // set the policy flag that we will not reveal coordinate information to this window. |
| 1724 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1725 | sp<InputWindowHandle> foregroundWindowHandle = |
| 1726 | mTempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1727 | if (foregroundWindowHandle) { |
| 1728 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
| 1729 | for (const TouchedWindow& touchedWindow : mTempTouchState.windows) { |
| 1730 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 1731 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 1732 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 1733 | mTempTouchState.addOrUpdateWindow(inputWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1734 | InputTarget::FLAG_ZERO_COORDS, |
| 1735 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1736 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1737 | } |
| 1738 | } |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | // Ensure all touched foreground windows are ready for new input. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1743 | for (const TouchedWindow& touchedWindow : mTempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1744 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1745 | // Check whether the window is ready for more input. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1746 | std::string reason = |
| 1747 | checkWindowReadyForMoreInputLocked(currentTime, touchedWindow.windowHandle, |
| 1748 | entry, "touched"); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1749 | if (!reason.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1750 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, nullptr, |
| 1751 | touchedWindow.windowHandle, |
| 1752 | nextWakeupTime, reason.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1753 | goto Unresponsive; |
| 1754 | } |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | // If this is the first pointer going down and the touched window has a wallpaper |
| 1759 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 1760 | // of the touch gesture. |
| 1761 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 1762 | // engine only supports touch events. We would need to add a mechanism similar |
| 1763 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 1764 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1765 | sp<InputWindowHandle> foregroundWindowHandle = |
| 1766 | mTempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1767 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1768 | const std::vector<sp<InputWindowHandle>> windowHandles = |
| 1769 | getWindowHandlesLocked(displayId); |
| 1770 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1771 | const InputWindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1772 | if (info->displayId == displayId && |
| 1773 | windowHandle->getInfo()->layoutParamsType == InputWindowInfo::TYPE_WALLPAPER) { |
| 1774 | mTempTouchState |
| 1775 | .addOrUpdateWindow(windowHandle, |
| 1776 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 1777 | InputTarget:: |
| 1778 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 1779 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1780 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1781 | } |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | // Success! Output targets. |
| 1787 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 1788 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1789 | for (const TouchedWindow& touchedWindow : mTempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1790 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1791 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1792 | } |
| 1793 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1794 | for (const TouchedMonitor& touchedMonitor : mTempTouchState.gestureMonitors) { |
| 1795 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1796 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1799 | // Drop the outside or hover touch windows since we will not care about them |
| 1800 | // in the next iteration. |
| 1801 | mTempTouchState.filterNonAsIsTouchWindows(); |
| 1802 | |
| 1803 | Failed: |
| 1804 | // Check injection permission once and for all. |
| 1805 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1806 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1807 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1808 | } else { |
| 1809 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | // Update final pieces of touch state if the injector had permission. |
| 1814 | if (injectionPermission == INJECTION_PERMISSION_GRANTED) { |
| 1815 | if (!wrongDevice) { |
| 1816 | if (switchedDevice) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1817 | if (DEBUG_FOCUS) { |
| 1818 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 1819 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1820 | *outConflictingPointerActions = true; |
| 1821 | } |
| 1822 | |
| 1823 | if (isHoverAction) { |
| 1824 | // Started hovering, therefore no longer down. |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1825 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1826 | if (DEBUG_FOCUS) { |
| 1827 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 1828 | "down."); |
| 1829 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1830 | *outConflictingPointerActions = true; |
| 1831 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1832 | mTempTouchState.reset(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1833 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1834 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1835 | mTempTouchState.deviceId = entry.deviceId; |
| 1836 | mTempTouchState.source = entry.source; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1837 | mTempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1838 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1839 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 1840 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1841 | // All pointers up or canceled. |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1842 | mTempTouchState.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1843 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1844 | // First pointer went down. |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1845 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1846 | if (DEBUG_FOCUS) { |
| 1847 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 1848 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1849 | *outConflictingPointerActions = true; |
| 1850 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1851 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 1852 | // One pointer went up. |
| 1853 | if (isSplit) { |
| 1854 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1855 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1856 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1857 | for (size_t i = 0; i < mTempTouchState.windows.size();) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1858 | TouchedWindow& touchedWindow = mTempTouchState.windows[i]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1859 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 1860 | touchedWindow.pointerIds.clearBit(pointerId); |
| 1861 | if (touchedWindow.pointerIds.isEmpty()) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1862 | mTempTouchState.windows.erase(mTempTouchState.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1863 | continue; |
| 1864 | } |
| 1865 | } |
| 1866 | i += 1; |
| 1867 | } |
| 1868 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | // Save changes unless the action was scroll in which case the temporary touch |
| 1872 | // state was only valid for this one action. |
| 1873 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 1874 | if (mTempTouchState.displayId >= 0) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 1875 | mTouchStatesByDisplay[displayId] = mTempTouchState; |
| 1876 | } else { |
| 1877 | mTouchStatesByDisplay.erase(displayId); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1878 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | // Update hover state. |
| 1882 | mLastHoverWindowHandle = newHoverWindowHandle; |
| 1883 | } |
| 1884 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1885 | if (DEBUG_FOCUS) { |
| 1886 | ALOGD("Not updating touch focus because injection was denied."); |
| 1887 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | Unresponsive: |
| 1891 | // Reset temporary touch state to ensure we release unnecessary references to input channels. |
| 1892 | mTempTouchState.reset(); |
| 1893 | |
| 1894 | nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1895 | updateDispatchStatistics(currentTime, entry, injectionResult, timeSpentWaitingForApplication); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1896 | if (DEBUG_FOCUS) { |
| 1897 | ALOGD("findTouchedWindow finished: injectionResult=%d, injectionPermission=%d, " |
| 1898 | "timeSpentWaitingForApplication=%0.1fms", |
| 1899 | injectionResult, injectionPermission, timeSpentWaitingForApplication / 1000000.0); |
| 1900 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1901 | return injectionResult; |
| 1902 | } |
| 1903 | |
| 1904 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1905 | int32_t targetFlags, BitSet32 pointerIds, |
| 1906 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 1907 | std::vector<InputTarget>::iterator it = |
| 1908 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 1909 | [&windowHandle](const InputTarget& inputTarget) { |
| 1910 | return inputTarget.inputChannel->getConnectionToken() == |
| 1911 | windowHandle->getToken(); |
| 1912 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 1913 | |
Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 1914 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 1915 | |
| 1916 | if (it == inputTargets.end()) { |
| 1917 | InputTarget inputTarget; |
| 1918 | sp<InputChannel> inputChannel = getInputChannelLocked(windowHandle->getToken()); |
| 1919 | if (inputChannel == nullptr) { |
| 1920 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 1921 | return; |
| 1922 | } |
| 1923 | inputTarget.inputChannel = inputChannel; |
| 1924 | inputTarget.flags = targetFlags; |
| 1925 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
| 1926 | inputTargets.push_back(inputTarget); |
| 1927 | it = inputTargets.end() - 1; |
| 1928 | } |
| 1929 | |
| 1930 | ALOG_ASSERT(it->flags == targetFlags); |
| 1931 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 1932 | |
| 1933 | it->addPointers(pointerIds, -windowInfo->frameLeft, -windowInfo->frameTop, |
| 1934 | windowInfo->windowXScale, windowInfo->windowYScale); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1935 | } |
| 1936 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1937 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1938 | int32_t displayId, float xOffset, |
| 1939 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1940 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 1941 | mGlobalMonitorsByDisplay.find(displayId); |
| 1942 | |
| 1943 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 1944 | const std::vector<Monitor>& monitors = it->second; |
| 1945 | for (const Monitor& monitor : monitors) { |
| 1946 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1947 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1948 | } |
| 1949 | } |
| 1950 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1951 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 1952 | float yOffset, |
| 1953 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1954 | InputTarget target; |
| 1955 | target.inputChannel = monitor.inputChannel; |
| 1956 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 1957 | target.setDefaultPointerInfo(xOffset, yOffset, 1 /* windowXScale */, 1 /* windowYScale */); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1958 | inputTargets.push_back(target); |
| 1959 | } |
| 1960 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1961 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1962 | const InjectionState* injectionState) { |
| 1963 | if (injectionState && |
| 1964 | (windowHandle == nullptr || |
| 1965 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 1966 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1967 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1968 | 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] | 1969 | "owned by uid %d", |
| 1970 | injectionState->injectorPid, injectionState->injectorUid, |
| 1971 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1972 | } else { |
| 1973 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1974 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1975 | } |
| 1976 | return false; |
| 1977 | } |
| 1978 | return true; |
| 1979 | } |
| 1980 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1981 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, |
| 1982 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1983 | int32_t displayId = windowHandle->getInfo()->displayId; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1984 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| 1985 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 1986 | if (haveSameToken(otherHandle, windowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1987 | break; |
| 1988 | } |
| 1989 | |
| 1990 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 1991 | if (otherInfo->visible && !otherInfo->isTrustedOverlay() && |
| 1992 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1993 | return true; |
| 1994 | } |
| 1995 | } |
| 1996 | return false; |
| 1997 | } |
| 1998 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 1999 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 2000 | int32_t displayId = windowHandle->getInfo()->displayId; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2001 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2002 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2003 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2004 | if (haveSameToken(otherHandle, windowHandle)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2005 | break; |
| 2006 | } |
| 2007 | |
| 2008 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2009 | if (otherInfo->visible && !otherInfo->isTrustedOverlay() && |
| 2010 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2011 | return true; |
| 2012 | } |
| 2013 | } |
| 2014 | return false; |
| 2015 | } |
| 2016 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2017 | std::string InputDispatcher::checkWindowReadyForMoreInputLocked( |
| 2018 | nsecs_t currentTime, const sp<InputWindowHandle>& windowHandle, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2019 | const EventEntry& eventEntry, const char* targetType) { |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 2020 | // If the window is paused then keep waiting. |
| 2021 | if (windowHandle->getInfo()->paused) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2022 | return StringPrintf("Waiting because the %s window is paused.", targetType); |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | // If the window's connection is not registered then keep waiting. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 2026 | sp<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2027 | if (connection == nullptr) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2028 | return StringPrintf("Waiting because the %s window's input channel is not " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2029 | "registered with the input dispatcher. The window may be in the " |
| 2030 | "process of being removed.", |
| 2031 | targetType); |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | // If the connection is dead then keep waiting. |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 2035 | if (connection->status != Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2036 | return StringPrintf("Waiting because the %s window's input connection is %s." |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2037 | "The window may be in the process of being removed.", |
| 2038 | targetType, connection->getStatusLabel()); |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | // If the connection is backed up then keep waiting. |
| 2042 | if (connection->inputPublisherBlocked) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2043 | return StringPrintf("Waiting because the %s window's input channel is full. " |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2044 | "Outbound queue length: %zu. Wait queue length: %zu.", |
| 2045 | targetType, connection->outboundQueue.size(), |
| 2046 | connection->waitQueue.size()); |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | // Ensure that the dispatch queues aren't too far backed up for this event. |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2050 | if (eventEntry.type == EventEntry::Type::KEY) { |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 2051 | // If the event is a key event, then we must wait for all previous events to |
| 2052 | // complete before delivering it because previous events may have the |
| 2053 | // side-effect of transferring focus to a different window and we want to |
| 2054 | // ensure that the following keys are sent to the new window. |
| 2055 | // |
| 2056 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2057 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2058 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2059 | // often anticipate pending UI changes when typing on a keyboard. |
| 2060 | // To obtain this behavior, we must serialize key events with respect to all |
| 2061 | // prior input events. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2062 | if (!connection->outboundQueue.empty() || !connection->waitQueue.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2063 | return StringPrintf("Waiting to send key event because the %s window has not " |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2064 | "finished processing all of the input events that were previously " |
| 2065 | "delivered to it. Outbound queue length: %zu. Wait queue length: " |
| 2066 | "%zu.", |
| 2067 | targetType, connection->outboundQueue.size(), |
| 2068 | connection->waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2069 | } |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 2070 | } else { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2071 | // Touch events can always be sent to a window immediately because the user intended |
| 2072 | // to touch whatever was visible at the time. Even if focus changes or a new |
| 2073 | // window appears moments later, the touch event was meant to be delivered to |
| 2074 | // whatever window happened to be on screen at the time. |
| 2075 | // |
| 2076 | // Generic motion events, such as trackball or joystick events are a little trickier. |
| 2077 | // Like key events, generic motion events are delivered to the focused window. |
| 2078 | // Unlike key events, generic motion events don't tend to transfer focus to other |
| 2079 | // windows and it is not important for them to be serialized. So we prefer to deliver |
| 2080 | // generic motion events as soon as possible to improve efficiency and reduce lag |
| 2081 | // through batching. |
| 2082 | // |
| 2083 | // The one case where we pause input event delivery is when the wait queue is piling |
| 2084 | // up with lots of events because the application is not responding. |
| 2085 | // This condition ensures that ANRs are detected reliably. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2086 | if (!connection->waitQueue.empty() && |
| 2087 | currentTime >= |
| 2088 | connection->waitQueue.front()->deliveryTime + STREAM_AHEAD_EVENT_TIMEOUT) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2089 | return StringPrintf("Waiting to send non-key event because the %s window has not " |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2090 | "finished processing certain input events that were delivered to " |
| 2091 | "it over " |
| 2092 | "%0.1fms ago. Wait queue length: %zu. Wait queue head age: " |
| 2093 | "%0.1fms.", |
| 2094 | targetType, STREAM_AHEAD_EVENT_TIMEOUT * 0.000001f, |
| 2095 | connection->waitQueue.size(), |
| 2096 | (currentTime - connection->waitQueue.front()->deliveryTime) * |
| 2097 | 0.000001f); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2098 | } |
| 2099 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2100 | return ""; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2101 | } |
| 2102 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2103 | std::string InputDispatcher::getApplicationWindowLabel( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2104 | const sp<InputApplicationHandle>& applicationHandle, |
| 2105 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2106 | if (applicationHandle != nullptr) { |
| 2107 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2108 | std::string label(applicationHandle->getName()); |
| 2109 | label += " - "; |
| 2110 | label += windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2111 | return label; |
| 2112 | } else { |
| 2113 | return applicationHandle->getName(); |
| 2114 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2115 | } else if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2116 | return windowHandle->getName(); |
| 2117 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2118 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2122 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2123 | if (eventEntry.type == EventEntry::Type::FOCUS) { |
| 2124 | // Focus events are passed to apps, but do not represent user activity. |
| 2125 | return; |
| 2126 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2127 | int32_t displayId = getTargetDisplayId(eventEntry); |
| 2128 | sp<InputWindowHandle> focusedWindowHandle = |
| 2129 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 2130 | if (focusedWindowHandle != nullptr) { |
| 2131 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2132 | if (info->inputFeatures & InputWindowInfo::INPUT_FEATURE_DISABLE_USER_ACTIVITY) { |
| 2133 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2134 | 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] | 2135 | #endif |
| 2136 | return; |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2141 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2142 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2143 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2144 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2145 | return; |
| 2146 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2147 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2148 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2149 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2150 | } |
| 2151 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2152 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2153 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2154 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2155 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2156 | return; |
| 2157 | } |
| 2158 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2159 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2160 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2161 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2162 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2163 | case EventEntry::Type::DEVICE_RESET: { |
| 2164 | LOG_ALWAYS_FATAL("%s events are not user activity", |
| 2165 | EventEntry::typeToString(eventEntry.type)); |
| 2166 | break; |
| 2167 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2168 | } |
| 2169 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2170 | std::unique_ptr<CommandEntry> commandEntry = |
| 2171 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2172 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2173 | commandEntry->userActivityEventType = eventType; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2174 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2178 | const sp<Connection>& connection, |
| 2179 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2180 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2181 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2182 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2183 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2184 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2185 | ATRACE_NAME(message.c_str()); |
| 2186 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2187 | #if DEBUG_DISPATCH_CYCLE |
| 2188 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2189 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2190 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2191 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2192 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2193 | #endif |
| 2194 | |
| 2195 | // Skip this event if the connection status is not normal. |
| 2196 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2197 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2198 | #if DEBUG_DISPATCH_CYCLE |
| 2199 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2200 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2201 | #endif |
| 2202 | return; |
| 2203 | } |
| 2204 | |
| 2205 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2206 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2207 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2208 | "Entry type %s should not have FLAG_SPLIT", |
| 2209 | EventEntry::typeToString(eventEntry->type)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2210 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2211 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2212 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2213 | MotionEntry* splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2214 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2215 | if (!splitMotionEntry) { |
| 2216 | return; // split event was dropped |
| 2217 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2218 | if (DEBUG_FOCUS) { |
| 2219 | ALOGD("channel '%s' ~ Split motion event.", |
| 2220 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2221 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2222 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2223 | enqueueDispatchEntriesLocked(currentTime, connection, splitMotionEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2224 | splitMotionEntry->release(); |
| 2225 | return; |
| 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2230 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2231 | } |
| 2232 | |
| 2233 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2234 | const sp<Connection>& connection, |
| 2235 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2236 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2237 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2238 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2239 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2240 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2241 | ATRACE_NAME(message.c_str()); |
| 2242 | } |
| 2243 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2244 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2245 | |
| 2246 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2247 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2248 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2249 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2250 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2251 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2252 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2253 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2254 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2255 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2256 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2257 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2258 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2259 | |
| 2260 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2261 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2262 | startDispatchCycleLocked(currentTime, connection); |
| 2263 | } |
| 2264 | } |
| 2265 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2266 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
| 2267 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2268 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2269 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2270 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2271 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2272 | connection->getInputChannelName().c_str(), |
| 2273 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2274 | ATRACE_NAME(message.c_str()); |
| 2275 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2276 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2277 | if (!(inputTargetFlags & dispatchMode)) { |
| 2278 | return; |
| 2279 | } |
| 2280 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2281 | |
| 2282 | // This is a new event. |
| 2283 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2284 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2285 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2286 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2287 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2288 | // different EventEntry than what was passed in. |
| 2289 | EventEntry* newEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2290 | // Apply target flags and update the connection's input state. |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2291 | switch (newEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2292 | case EventEntry::Type::KEY: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2293 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2294 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2295 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2296 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2297 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2298 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2299 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2300 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2301 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2302 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2303 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2304 | return; // skip the inconsistent event |
| 2305 | } |
| 2306 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2307 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2308 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2309 | case EventEntry::Type::MOTION: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2310 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2311 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2312 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2313 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2314 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2315 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2316 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2317 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2318 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2319 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2320 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2321 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2322 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2323 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2324 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2325 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2326 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2327 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2328 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2329 | } |
| 2330 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2331 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2332 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2333 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2334 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2335 | "event", |
| 2336 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2337 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2338 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2339 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2340 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2341 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2342 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2343 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2344 | } |
| 2345 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2346 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2347 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2348 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2349 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2350 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2351 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2352 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2353 | "event", |
| 2354 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2355 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2356 | return; // skip the inconsistent event |
| 2357 | } |
| 2358 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2359 | dispatchEntry->resolvedEventId = |
| 2360 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2361 | ? mIdGenerator.nextId() |
| 2362 | : motionEntry.id; |
| 2363 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2364 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2365 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2366 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2367 | ATRACE_NAME(message.c_str()); |
| 2368 | } |
| 2369 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2370 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2371 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2372 | |
| 2373 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2374 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2375 | case EventEntry::Type::FOCUS: { |
| 2376 | break; |
| 2377 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2378 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2379 | case EventEntry::Type::DEVICE_RESET: { |
| 2380 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2381 | EventEntry::typeToString(newEntry->type)); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2382 | break; |
| 2383 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | // Remember that we are waiting for this dispatch to complete. |
| 2387 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2388 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2392 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2393 | traceOutboundQueueLength(connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2394 | } |
| 2395 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2396 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2397 | const sp<IBinder>& newToken) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2398 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2399 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 2400 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2401 | return; |
| 2402 | } |
| 2403 | |
| 2404 | sp<InputWindowHandle> inputWindowHandle = getWindowHandleLocked(newToken); |
| 2405 | if (inputWindowHandle == nullptr) { |
| 2406 | return; |
| 2407 | } |
| 2408 | |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2409 | sp<InputWindowHandle> focusedWindowHandle = |
Tiger Huang | 0683fe7 | 2019-06-03 21:50:55 +0800 | [diff] [blame] | 2410 | getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2411 | |
| 2412 | bool hasFocusChanged = !focusedWindowHandle || focusedWindowHandle->getToken() != newToken; |
| 2413 | |
| 2414 | if (!hasFocusChanged) { |
| 2415 | return; |
| 2416 | } |
| 2417 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2418 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 2419 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2420 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2421 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2425 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2426 | if (ATRACE_ENABLED()) { |
| 2427 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2428 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2429 | ATRACE_NAME(message.c_str()); |
| 2430 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2431 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2432 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2433 | #endif |
| 2434 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2435 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 2436 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2437 | dispatchEntry->deliveryTime = currentTime; |
| 2438 | |
| 2439 | // Publish the event. |
| 2440 | status_t status; |
| 2441 | EventEntry* eventEntry = dispatchEntry->eventEntry; |
| 2442 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2443 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2444 | KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2445 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(*keyEntry); |
| 2446 | verifiedEvent.flags = dispatchEntry->resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 2447 | verifiedEvent.action = dispatchEntry->resolvedAction; |
| 2448 | std::array<uint8_t, 32> hmac = mHmacKeyManager.sign(verifiedEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2449 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2450 | // Publish the key event. |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2451 | status = |
| 2452 | connection->inputPublisher |
| 2453 | .publishKeyEvent(dispatchEntry->seq, dispatchEntry->resolvedEventId, |
| 2454 | keyEntry->deviceId, keyEntry->source, |
| 2455 | keyEntry->displayId, std::move(hmac), |
| 2456 | dispatchEntry->resolvedAction, |
| 2457 | dispatchEntry->resolvedFlags, keyEntry->keyCode, |
| 2458 | keyEntry->scanCode, keyEntry->metaState, |
| 2459 | keyEntry->repeatCount, keyEntry->downTime, |
| 2460 | keyEntry->eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2461 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2462 | } |
| 2463 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2464 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2465 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2466 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2467 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 2468 | const PointerCoords* usingCoords = motionEntry->pointerCoords; |
| 2469 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2470 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 2471 | float xOffset = 0.0f, yOffset = 0.0f; |
| 2472 | float xScale = 1.0f, yScale = 1.0f; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2473 | if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) && |
| 2474 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 2475 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2476 | xScale = dispatchEntry->windowXScale; |
| 2477 | yScale = dispatchEntry->windowYScale; |
| 2478 | xOffset = dispatchEntry->xOffset * xScale; |
| 2479 | yOffset = dispatchEntry->yOffset * yScale; |
| 2480 | if (globalScaleFactor != 1.0f) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2481 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2482 | scaledCoords[i] = motionEntry->pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2483 | // Don't apply window scale here since we don't want scale to affect raw |
| 2484 | // coordinates. The scale will be sent back to the client and applied |
| 2485 | // later when requesting relative coordinates. |
| 2486 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 2487 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2488 | } |
| 2489 | usingCoords = scaledCoords; |
| 2490 | } |
| 2491 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2492 | // We don't want the dispatch target to know. |
| 2493 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
| 2494 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2495 | scaledCoords[i].clear(); |
| 2496 | } |
| 2497 | usingCoords = scaledCoords; |
| 2498 | } |
| 2499 | } |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2500 | VerifiedMotionEvent verifiedEvent = |
| 2501 | verifiedMotionEventFromMotionEntry(*motionEntry); |
| 2502 | verifiedEvent.actionMasked = |
| 2503 | dispatchEntry->resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 2504 | verifiedEvent.flags = dispatchEntry->resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 2505 | std::array<uint8_t, 32> hmac = mHmacKeyManager.sign(verifiedEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2506 | |
| 2507 | // Publish the motion event. |
| 2508 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2509 | .publishMotionEvent(dispatchEntry->seq, |
| 2510 | dispatchEntry->resolvedEventId, |
| 2511 | motionEntry->deviceId, motionEntry->source, |
| 2512 | motionEntry->displayId, std::move(hmac), |
| 2513 | dispatchEntry->resolvedAction, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2514 | motionEntry->actionButton, |
| 2515 | dispatchEntry->resolvedFlags, |
| 2516 | motionEntry->edgeFlags, motionEntry->metaState, |
| 2517 | motionEntry->buttonState, |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2518 | motionEntry->classification, xScale, yScale, |
| 2519 | xOffset, yOffset, motionEntry->xPrecision, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2520 | motionEntry->yPrecision, |
| 2521 | motionEntry->xCursorPosition, |
| 2522 | motionEntry->yCursorPosition, |
| 2523 | motionEntry->downTime, motionEntry->eventTime, |
| 2524 | motionEntry->pointerCount, |
| 2525 | motionEntry->pointerProperties, usingCoords); |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 2526 | reportTouchEventForStatistics(*motionEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2527 | break; |
| 2528 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2529 | case EventEntry::Type::FOCUS: { |
| 2530 | FocusEntry* focusEntry = static_cast<FocusEntry*>(eventEntry); |
| 2531 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2532 | focusEntry->id, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2533 | focusEntry->hasFocus, |
| 2534 | mInTouchMode); |
| 2535 | break; |
| 2536 | } |
| 2537 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2538 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2539 | case EventEntry::Type::DEVICE_RESET: { |
| 2540 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
| 2541 | EventEntry::typeToString(eventEntry->type)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2542 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2543 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | // Check the result. |
| 2547 | if (status) { |
| 2548 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2549 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2550 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2551 | "This is unexpected because the wait queue is empty, so the pipe " |
| 2552 | "should be empty and we shouldn't have any problems writing an " |
| 2553 | "event to it, status=%d", |
| 2554 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2555 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2556 | } else { |
| 2557 | // Pipe is full and we are waiting for the app to finish process some events |
| 2558 | // before sending more events to it. |
| 2559 | #if DEBUG_DISPATCH_CYCLE |
| 2560 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2561 | "waiting for the application to catch up", |
| 2562 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2563 | #endif |
| 2564 | connection->inputPublisherBlocked = true; |
| 2565 | } |
| 2566 | } else { |
| 2567 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2568 | "status=%d", |
| 2569 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2570 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2571 | } |
| 2572 | return; |
| 2573 | } |
| 2574 | |
| 2575 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2576 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 2577 | connection->outboundQueue.end(), |
| 2578 | dispatchEntry)); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2579 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2580 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2581 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | |
| 2585 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2586 | const sp<Connection>& connection, uint32_t seq, |
| 2587 | bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2588 | #if DEBUG_DISPATCH_CYCLE |
| 2589 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2590 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2591 | #endif |
| 2592 | |
| 2593 | connection->inputPublisherBlocked = false; |
| 2594 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2595 | if (connection->status == Connection::STATUS_BROKEN || |
| 2596 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2597 | return; |
| 2598 | } |
| 2599 | |
| 2600 | // Notify other system components and prepare to start the next dispatch cycle. |
| 2601 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); |
| 2602 | } |
| 2603 | |
| 2604 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2605 | const sp<Connection>& connection, |
| 2606 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2607 | #if DEBUG_DISPATCH_CYCLE |
| 2608 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2609 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2610 | #endif |
| 2611 | |
| 2612 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2613 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2614 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2615 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2616 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2617 | |
| 2618 | // The connection appears to be unrecoverably broken. |
| 2619 | // Ignore already broken or zombie connections. |
| 2620 | if (connection->status == Connection::STATUS_NORMAL) { |
| 2621 | connection->status = Connection::STATUS_BROKEN; |
| 2622 | |
| 2623 | if (notify) { |
| 2624 | // Notify other system components. |
| 2625 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2630 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 2631 | while (!queue.empty()) { |
| 2632 | DispatchEntry* dispatchEntry = queue.front(); |
| 2633 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2634 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2635 | } |
| 2636 | } |
| 2637 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2638 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2639 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2640 | decrementPendingForegroundDispatches(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2641 | } |
| 2642 | delete dispatchEntry; |
| 2643 | } |
| 2644 | |
| 2645 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { |
| 2646 | InputDispatcher* d = static_cast<InputDispatcher*>(data); |
| 2647 | |
| 2648 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 2649 | std::scoped_lock _l(d->mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2650 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2651 | if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2652 | ALOGE("Received spurious receive callback for unknown input channel. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2653 | "fd=%d, events=0x%x", |
| 2654 | fd, events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2655 | return 0; // remove the callback |
| 2656 | } |
| 2657 | |
| 2658 | bool notify; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2659 | sp<Connection> connection = d->mConnectionsByFd[fd]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2660 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 2661 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 2662 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2663 | "events=0x%x", |
| 2664 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2665 | return 1; |
| 2666 | } |
| 2667 | |
| 2668 | nsecs_t currentTime = now(); |
| 2669 | bool gotOne = false; |
| 2670 | status_t status; |
| 2671 | for (;;) { |
| 2672 | uint32_t seq; |
| 2673 | bool handled; |
| 2674 | status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); |
| 2675 | if (status) { |
| 2676 | break; |
| 2677 | } |
| 2678 | d->finishDispatchCycleLocked(currentTime, connection, seq, handled); |
| 2679 | gotOne = true; |
| 2680 | } |
| 2681 | if (gotOne) { |
| 2682 | d->runCommandsLockedInterruptible(); |
| 2683 | if (status == WOULD_BLOCK) { |
| 2684 | return 1; |
| 2685 | } |
| 2686 | } |
| 2687 | |
| 2688 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 2689 | if (notify) { |
| 2690 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2691 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2692 | } |
| 2693 | } else { |
| 2694 | // Monitor channels are never explicitly unregistered. |
| 2695 | // We do it automatically when the remote endpoint is closed so don't warn |
| 2696 | // about them. |
| 2697 | notify = !connection->monitor; |
| 2698 | if (notify) { |
| 2699 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2700 | "events=0x%x", |
| 2701 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | // Unregister the channel. |
| 2706 | d->unregisterInputChannelLocked(connection->inputChannel, notify); |
| 2707 | return 0; // remove the callback |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2708 | } // release lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2709 | } |
| 2710 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2711 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2712 | const CancelationOptions& options) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2713 | for (const auto& pair : mConnectionsByFd) { |
| 2714 | synthesizeCancelationEventsForConnectionLocked(pair.second, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2715 | } |
| 2716 | } |
| 2717 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2718 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2719 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2720 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 2721 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 2722 | } |
| 2723 | |
| 2724 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 2725 | const CancelationOptions& options, |
| 2726 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 2727 | for (const auto& it : monitorsByDisplay) { |
| 2728 | const std::vector<Monitor>& monitors = it.second; |
| 2729 | for (const Monitor& monitor : monitors) { |
| 2730 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2731 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2732 | } |
| 2733 | } |
| 2734 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2735 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
| 2736 | const sp<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 2737 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2738 | if (connection == nullptr) { |
| 2739 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2740 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2741 | |
| 2742 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 2746 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 2747 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2748 | return; |
| 2749 | } |
| 2750 | |
| 2751 | nsecs_t currentTime = now(); |
| 2752 | |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 2753 | std::vector<EventEntry*> cancelationEvents = |
| 2754 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2755 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2756 | if (cancelationEvents.empty()) { |
| 2757 | return; |
| 2758 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2759 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2760 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 2761 | "with reality: %s, mode=%d.", |
| 2762 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 2763 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2764 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2765 | |
| 2766 | InputTarget target; |
| 2767 | sp<InputWindowHandle> windowHandle = |
| 2768 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 2769 | if (windowHandle != nullptr) { |
| 2770 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2771 | target.setDefaultPointerInfo(-windowInfo->frameLeft, -windowInfo->frameTop, |
| 2772 | windowInfo->windowXScale, windowInfo->windowYScale); |
| 2773 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2774 | } |
| 2775 | target.inputChannel = connection->inputChannel; |
| 2776 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2777 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2778 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
| 2779 | EventEntry* cancelationEventEntry = cancelationEvents[i]; |
| 2780 | switch (cancelationEventEntry->type) { |
| 2781 | case EventEntry::Type::KEY: { |
| 2782 | logOutboundKeyDetails("cancel - ", |
| 2783 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 2784 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2785 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2786 | case EventEntry::Type::MOTION: { |
| 2787 | logOutboundMotionDetails("cancel - ", |
| 2788 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 2789 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2790 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2791 | case EventEntry::Type::FOCUS: { |
| 2792 | LOG_ALWAYS_FATAL("Canceling focus events is not supported"); |
| 2793 | break; |
| 2794 | } |
| 2795 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2796 | case EventEntry::Type::DEVICE_RESET: { |
| 2797 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 2798 | EventEntry::typeToString(cancelationEventEntry->type)); |
| 2799 | break; |
| 2800 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2801 | } |
| 2802 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2803 | enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref |
| 2804 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2805 | |
| 2806 | cancelationEventEntry->release(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2807 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2808 | |
| 2809 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2810 | } |
| 2811 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2812 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 2813 | const sp<Connection>& connection) { |
| 2814 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2815 | return; |
| 2816 | } |
| 2817 | |
| 2818 | nsecs_t currentTime = now(); |
| 2819 | |
| 2820 | std::vector<EventEntry*> downEvents = |
| 2821 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 2822 | |
| 2823 | if (downEvents.empty()) { |
| 2824 | return; |
| 2825 | } |
| 2826 | |
| 2827 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 2828 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 2829 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 2830 | #endif |
| 2831 | |
| 2832 | InputTarget target; |
| 2833 | sp<InputWindowHandle> windowHandle = |
| 2834 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 2835 | if (windowHandle != nullptr) { |
| 2836 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2837 | target.setDefaultPointerInfo(-windowInfo->frameLeft, -windowInfo->frameTop, |
| 2838 | windowInfo->windowXScale, windowInfo->windowYScale); |
| 2839 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2840 | } |
| 2841 | target.inputChannel = connection->inputChannel; |
| 2842 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2843 | |
| 2844 | for (EventEntry* downEventEntry : downEvents) { |
| 2845 | switch (downEventEntry->type) { |
| 2846 | case EventEntry::Type::MOTION: { |
| 2847 | logOutboundMotionDetails("down - ", |
| 2848 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 2849 | break; |
| 2850 | } |
| 2851 | |
| 2852 | case EventEntry::Type::KEY: |
| 2853 | case EventEntry::Type::FOCUS: |
| 2854 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2855 | case EventEntry::Type::DEVICE_RESET: { |
| 2856 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 2857 | EventEntry::typeToString(downEventEntry->type)); |
| 2858 | break; |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | enqueueDispatchEntryLocked(connection, downEventEntry, // increments ref |
| 2863 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2864 | |
| 2865 | downEventEntry->release(); |
| 2866 | } |
| 2867 | |
| 2868 | startDispatchCycleLocked(currentTime, connection); |
| 2869 | } |
| 2870 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2871 | MotionEntry* InputDispatcher::splitMotionEvent(const MotionEntry& originalMotionEntry, |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 2872 | BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2873 | ALOG_ASSERT(pointerIds.value != 0); |
| 2874 | |
| 2875 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 2876 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 2877 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 2878 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2879 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2880 | uint32_t splitPointerCount = 0; |
| 2881 | |
| 2882 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2883 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2884 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2885 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2886 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 2887 | if (pointerIds.hasBit(pointerId)) { |
| 2888 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 2889 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 2890 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2891 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2892 | splitPointerCount += 1; |
| 2893 | } |
| 2894 | } |
| 2895 | |
| 2896 | if (splitPointerCount != pointerIds.count()) { |
| 2897 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 2898 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 2899 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 2900 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 2901 | // in this way. |
| 2902 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2903 | "we expected there to be %d pointers. This probably means we received " |
| 2904 | "a broken sequence of pointer ids from the input device.", |
| 2905 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2906 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2907 | } |
| 2908 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2909 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2910 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2911 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 2912 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2913 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 2914 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2915 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2916 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 2917 | if (pointerIds.hasBit(pointerId)) { |
| 2918 | if (pointerIds.count() == 1) { |
| 2919 | // The first/last pointer went down/up. |
| 2920 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2921 | ? AMOTION_EVENT_ACTION_DOWN |
| 2922 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2923 | } else { |
| 2924 | // A secondary pointer went down/up. |
| 2925 | uint32_t splitPointerIndex = 0; |
| 2926 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 2927 | splitPointerIndex += 1; |
| 2928 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2929 | action = maskedAction | |
| 2930 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2931 | } |
| 2932 | } else { |
| 2933 | // An unrelated pointer changed. |
| 2934 | action = AMOTION_EVENT_ACTION_MOVE; |
| 2935 | } |
| 2936 | } |
| 2937 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2938 | int32_t newId = mIdGenerator.nextId(); |
| 2939 | if (ATRACE_ENABLED()) { |
| 2940 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 2941 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2942 | originalMotionEntry.id, newId); |
| 2943 | ATRACE_NAME(message.c_str()); |
| 2944 | } |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2945 | MotionEntry* splitMotionEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2946 | new MotionEntry(newId, originalMotionEntry.eventTime, originalMotionEntry.deviceId, |
| 2947 | originalMotionEntry.source, originalMotionEntry.displayId, |
| 2948 | originalMotionEntry.policyFlags, action, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2949 | originalMotionEntry.actionButton, originalMotionEntry.flags, |
| 2950 | originalMotionEntry.metaState, originalMotionEntry.buttonState, |
| 2951 | originalMotionEntry.classification, originalMotionEntry.edgeFlags, |
| 2952 | originalMotionEntry.xPrecision, originalMotionEntry.yPrecision, |
| 2953 | originalMotionEntry.xCursorPosition, |
| 2954 | originalMotionEntry.yCursorPosition, originalMotionEntry.downTime, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2955 | splitPointerCount, splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2956 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2957 | if (originalMotionEntry.injectionState) { |
| 2958 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2959 | splitMotionEntry->injectionState->refCount += 1; |
| 2960 | } |
| 2961 | |
| 2962 | return splitMotionEntry; |
| 2963 | } |
| 2964 | |
| 2965 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 2966 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 2967 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2968 | #endif |
| 2969 | |
| 2970 | bool needWake; |
| 2971 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 2972 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2973 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 2974 | ConfigurationChangedEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2975 | new ConfigurationChangedEntry(args->id, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2976 | needWake = enqueueInboundEventLocked(newEntry); |
| 2977 | } // release lock |
| 2978 | |
| 2979 | if (needWake) { |
| 2980 | mLooper->wake(); |
| 2981 | } |
| 2982 | } |
| 2983 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 2984 | /** |
| 2985 | * If one of the meta shortcuts is detected, process them here: |
| 2986 | * Meta + Backspace -> generate BACK |
| 2987 | * Meta + Enter -> generate HOME |
| 2988 | * This will potentially overwrite keyCode and metaState. |
| 2989 | */ |
| 2990 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2991 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 2992 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 2993 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 2994 | if (keyCode == AKEYCODE_DEL) { |
| 2995 | newKeyCode = AKEYCODE_BACK; |
| 2996 | } else if (keyCode == AKEYCODE_ENTER) { |
| 2997 | newKeyCode = AKEYCODE_HOME; |
| 2998 | } |
| 2999 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3000 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3001 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 3002 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3003 | keyCode = newKeyCode; |
| 3004 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3005 | } |
| 3006 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3007 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3008 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3009 | // 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] | 3010 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3011 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 3012 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3013 | if (replacementIt != mReplacedKeys.end()) { |
| 3014 | keyCode = replacementIt->second; |
| 3015 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3016 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3017 | } |
| 3018 | } |
| 3019 | } |
| 3020 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3021 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3022 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3023 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3024 | "policyFlags=0x%x, action=0x%x, " |
| 3025 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3026 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3027 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3028 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3029 | #endif |
| 3030 | if (!validateKeyEvent(args->action)) { |
| 3031 | return; |
| 3032 | } |
| 3033 | |
| 3034 | uint32_t policyFlags = args->policyFlags; |
| 3035 | int32_t flags = args->flags; |
| 3036 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3037 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3038 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3039 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3040 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3041 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3042 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3043 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3044 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3045 | metaState |= AMETA_FUNCTION_ON; |
| 3046 | } |
| 3047 | |
| 3048 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3049 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3050 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3051 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3052 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3053 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3054 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3055 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3056 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3057 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3058 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3059 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3060 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3061 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3062 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3063 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3064 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3065 | bool needWake; |
| 3066 | { // acquire lock |
| 3067 | mLock.lock(); |
| 3068 | |
| 3069 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3070 | mLock.unlock(); |
| 3071 | |
| 3072 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3073 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3074 | return; // event was consumed by the filter |
| 3075 | } |
| 3076 | |
| 3077 | mLock.lock(); |
| 3078 | } |
| 3079 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3080 | KeyEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3081 | new KeyEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3082 | args->displayId, policyFlags, args->action, flags, keyCode, |
| 3083 | args->scanCode, metaState, repeatCount, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3084 | |
| 3085 | needWake = enqueueInboundEventLocked(newEntry); |
| 3086 | mLock.unlock(); |
| 3087 | } // release lock |
| 3088 | |
| 3089 | if (needWake) { |
| 3090 | mLooper->wake(); |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3095 | return mInputFilterEnabled; |
| 3096 | } |
| 3097 | |
| 3098 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3099 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3100 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3101 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3102 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3103 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3104 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3105 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3106 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3107 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3108 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3109 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3110 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3111 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3112 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3113 | "orientation=%f", |
| 3114 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3115 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3116 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3117 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3118 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3119 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3120 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3121 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3122 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3123 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3124 | } |
| 3125 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3126 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3127 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3128 | return; |
| 3129 | } |
| 3130 | |
| 3131 | uint32_t policyFlags = args->policyFlags; |
| 3132 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3133 | |
| 3134 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3135 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3136 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3137 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3138 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3139 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3140 | |
| 3141 | bool needWake; |
| 3142 | { // acquire lock |
| 3143 | mLock.lock(); |
| 3144 | |
| 3145 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3146 | mLock.unlock(); |
| 3147 | |
| 3148 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3149 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3150 | args->action, args->actionButton, args->flags, args->edgeFlags, |
| 3151 | args->metaState, args->buttonState, args->classification, 1 /*xScale*/, |
| 3152 | 1 /*yScale*/, 0 /* xOffset */, 0 /* yOffset */, args->xPrecision, |
| 3153 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 3154 | args->downTime, args->eventTime, args->pointerCount, |
| 3155 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3156 | |
| 3157 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3158 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3159 | return; // event was consumed by the filter |
| 3160 | } |
| 3161 | |
| 3162 | mLock.lock(); |
| 3163 | } |
| 3164 | |
| 3165 | // Just enqueue a new motion event. |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3166 | MotionEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3167 | new MotionEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3168 | args->displayId, policyFlags, args->action, args->actionButton, |
| 3169 | args->flags, args->metaState, args->buttonState, |
| 3170 | args->classification, args->edgeFlags, args->xPrecision, |
| 3171 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 3172 | args->downTime, args->pointerCount, args->pointerProperties, |
| 3173 | args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3174 | |
| 3175 | needWake = enqueueInboundEventLocked(newEntry); |
| 3176 | mLock.unlock(); |
| 3177 | } // release lock |
| 3178 | |
| 3179 | if (needWake) { |
| 3180 | mLooper->wake(); |
| 3181 | } |
| 3182 | } |
| 3183 | |
| 3184 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3185 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3189 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3190 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3191 | "switchMask=0x%08x", |
| 3192 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3193 | #endif |
| 3194 | |
| 3195 | uint32_t policyFlags = args->policyFlags; |
| 3196 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3197 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3201 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3202 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3203 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3204 | #endif |
| 3205 | |
| 3206 | bool needWake; |
| 3207 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3208 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3209 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3210 | DeviceResetEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3211 | new DeviceResetEntry(args->id, args->eventTime, args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3212 | needWake = enqueueInboundEventLocked(newEntry); |
| 3213 | } // release lock |
| 3214 | |
| 3215 | if (needWake) { |
| 3216 | mLooper->wake(); |
| 3217 | } |
| 3218 | } |
| 3219 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3220 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, int32_t injectorPid, |
| 3221 | int32_t injectorUid, int32_t syncMode, |
| 3222 | int32_t timeoutMillis, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3223 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3224 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3225 | "syncMode=%d, timeoutMillis=%d, policyFlags=0x%08x", |
| 3226 | event->getType(), injectorPid, injectorUid, syncMode, timeoutMillis, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3227 | #endif |
| 3228 | |
| 3229 | nsecs_t endTime = now() + milliseconds_to_nanoseconds(timeoutMillis); |
| 3230 | |
| 3231 | policyFlags |= POLICY_FLAG_INJECTED; |
| 3232 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 3233 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3234 | } |
| 3235 | |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3236 | std::queue<EventEntry*> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3237 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3238 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3239 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 3240 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3241 | if (!validateKeyEvent(action)) { |
| 3242 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3243 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3244 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3245 | int32_t flags = incomingKey.getFlags(); |
| 3246 | int32_t keyCode = incomingKey.getKeyCode(); |
| 3247 | int32_t metaState = incomingKey.getMetaState(); |
| 3248 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3249 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3250 | KeyEvent keyEvent; |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3251 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3252 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 3253 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 3254 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3255 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3256 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 3257 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3258 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3259 | |
| 3260 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3261 | android::base::Timer t; |
| 3262 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 3263 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3264 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 3265 | std::to_string(t.duration().count()).c_str()); |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | mLock.lock(); |
| 3270 | KeyEntry* injectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3271 | new KeyEntry(incomingKey.getId(), incomingKey.getEventTime(), |
| 3272 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
| 3273 | incomingKey.getDisplayId(), policyFlags, action, flags, |
| 3274 | incomingKey.getKeyCode(), incomingKey.getScanCode(), |
| 3275 | incomingKey.getMetaState(), incomingKey.getRepeatCount(), |
| 3276 | incomingKey.getDownTime()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3277 | injectedEntries.push(injectedEntry); |
| 3278 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3279 | } |
| 3280 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3281 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3282 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
| 3283 | int32_t action = motionEvent->getAction(); |
| 3284 | size_t pointerCount = motionEvent->getPointerCount(); |
| 3285 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
| 3286 | int32_t actionButton = motionEvent->getActionButton(); |
| 3287 | int32_t displayId = motionEvent->getDisplayId(); |
| 3288 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
| 3289 | return INPUT_EVENT_INJECTION_FAILED; |
| 3290 | } |
| 3291 | |
| 3292 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3293 | nsecs_t eventTime = motionEvent->getEventTime(); |
| 3294 | android::base::Timer t; |
| 3295 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 3296 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3297 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 3298 | std::to_string(t.duration().count()).c_str()); |
| 3299 | } |
| 3300 | } |
| 3301 | |
| 3302 | mLock.lock(); |
| 3303 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 3304 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
| 3305 | MotionEntry* injectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3306 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, VIRTUAL_KEYBOARD_ID, |
| 3307 | motionEvent->getSource(), motionEvent->getDisplayId(), |
| 3308 | policyFlags, action, actionButton, motionEvent->getFlags(), |
| 3309 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3310 | motionEvent->getClassification(), motionEvent->getEdgeFlags(), |
| 3311 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3312 | motionEvent->getRawXCursorPosition(), |
| 3313 | motionEvent->getRawYCursorPosition(), |
| 3314 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3315 | pointerProperties, samplePointerCoords, |
| 3316 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3317 | injectedEntries.push(injectedEntry); |
| 3318 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 3319 | sampleEventTimes += 1; |
| 3320 | samplePointerCoords += pointerCount; |
| 3321 | MotionEntry* nextInjectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3322 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3323 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3324 | motionEvent->getDisplayId(), policyFlags, action, |
| 3325 | actionButton, motionEvent->getFlags(), |
| 3326 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3327 | motionEvent->getClassification(), |
| 3328 | motionEvent->getEdgeFlags(), motionEvent->getXPrecision(), |
| 3329 | motionEvent->getYPrecision(), |
| 3330 | motionEvent->getRawXCursorPosition(), |
| 3331 | motionEvent->getRawYCursorPosition(), |
| 3332 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3333 | pointerProperties, samplePointerCoords, |
| 3334 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
| 3335 | injectedEntries.push(nextInjectedEntry); |
| 3336 | } |
| 3337 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3338 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3339 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3340 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 3341 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3342 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
| 3346 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3347 | injectionState->injectionIsAsync = true; |
| 3348 | } |
| 3349 | |
| 3350 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3351 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3352 | |
| 3353 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3354 | while (!injectedEntries.empty()) { |
| 3355 | needWake |= enqueueInboundEventLocked(injectedEntries.front()); |
| 3356 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | mLock.unlock(); |
| 3360 | |
| 3361 | if (needWake) { |
| 3362 | mLooper->wake(); |
| 3363 | } |
| 3364 | |
| 3365 | int32_t injectionResult; |
| 3366 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3367 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3368 | |
| 3369 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3370 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 3371 | } else { |
| 3372 | for (;;) { |
| 3373 | injectionResult = injectionState->injectionResult; |
| 3374 | if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { |
| 3375 | break; |
| 3376 | } |
| 3377 | |
| 3378 | nsecs_t remainingTimeout = endTime - now(); |
| 3379 | if (remainingTimeout <= 0) { |
| 3380 | #if DEBUG_INJECTION |
| 3381 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3382 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3383 | #endif |
| 3384 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3385 | break; |
| 3386 | } |
| 3387 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3388 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3389 | } |
| 3390 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3391 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED && |
| 3392 | syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3393 | while (injectionState->pendingForegroundDispatches != 0) { |
| 3394 | #if DEBUG_INJECTION |
| 3395 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3396 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3397 | #endif |
| 3398 | nsecs_t remainingTimeout = endTime - now(); |
| 3399 | if (remainingTimeout <= 0) { |
| 3400 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3401 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 3402 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3403 | #endif |
| 3404 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3405 | break; |
| 3406 | } |
| 3407 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3408 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3409 | } |
| 3410 | } |
| 3411 | } |
| 3412 | |
| 3413 | injectionState->release(); |
| 3414 | } // release lock |
| 3415 | |
| 3416 | #if DEBUG_INJECTION |
| 3417 | ALOGD("injectInputEvent - Finished with result %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3418 | "injectorPid=%d, injectorUid=%d", |
| 3419 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3420 | #endif |
| 3421 | |
| 3422 | return injectionResult; |
| 3423 | } |
| 3424 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3425 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3426 | std::array<uint8_t, 32> calculatedHmac; |
| 3427 | std::unique_ptr<VerifiedInputEvent> result; |
| 3428 | switch (event.getType()) { |
| 3429 | case AINPUT_EVENT_TYPE_KEY: { |
| 3430 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 3431 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 3432 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
| 3433 | calculatedHmac = mHmacKeyManager.sign(verifiedKeyEvent); |
| 3434 | break; |
| 3435 | } |
| 3436 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3437 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 3438 | VerifiedMotionEvent verifiedMotionEvent = |
| 3439 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 3440 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
| 3441 | calculatedHmac = mHmacKeyManager.sign(verifiedMotionEvent); |
| 3442 | break; |
| 3443 | } |
| 3444 | default: { |
| 3445 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 3446 | return nullptr; |
| 3447 | } |
| 3448 | } |
| 3449 | if (calculatedHmac == INVALID_HMAC) { |
| 3450 | return nullptr; |
| 3451 | } |
| 3452 | if (calculatedHmac != event.getHmac()) { |
| 3453 | return nullptr; |
| 3454 | } |
| 3455 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3456 | } |
| 3457 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3458 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3459 | return injectorUid == 0 || |
| 3460 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3461 | } |
| 3462 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3463 | void InputDispatcher::setInjectionResult(EventEntry* entry, int32_t injectionResult) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3464 | InjectionState* injectionState = entry->injectionState; |
| 3465 | if (injectionState) { |
| 3466 | #if DEBUG_INJECTION |
| 3467 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3468 | "injectorPid=%d, injectorUid=%d", |
| 3469 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3470 | #endif |
| 3471 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3472 | if (injectionState->injectionIsAsync && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3473 | // Log the outcome since the injector did not wait for the injection result. |
| 3474 | switch (injectionResult) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3475 | case INPUT_EVENT_INJECTION_SUCCEEDED: |
| 3476 | ALOGV("Asynchronous input event injection succeeded."); |
| 3477 | break; |
| 3478 | case INPUT_EVENT_INJECTION_FAILED: |
| 3479 | ALOGW("Asynchronous input event injection failed."); |
| 3480 | break; |
| 3481 | case INPUT_EVENT_INJECTION_PERMISSION_DENIED: |
| 3482 | ALOGW("Asynchronous input event injection permission denied."); |
| 3483 | break; |
| 3484 | case INPUT_EVENT_INJECTION_TIMED_OUT: |
| 3485 | ALOGW("Asynchronous input event injection timed out."); |
| 3486 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3491 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3492 | } |
| 3493 | } |
| 3494 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3495 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3496 | InjectionState* injectionState = entry->injectionState; |
| 3497 | if (injectionState) { |
| 3498 | injectionState->pendingForegroundDispatches += 1; |
| 3499 | } |
| 3500 | } |
| 3501 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3502 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3503 | InjectionState* injectionState = entry->injectionState; |
| 3504 | if (injectionState) { |
| 3505 | injectionState->pendingForegroundDispatches -= 1; |
| 3506 | |
| 3507 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3508 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3509 | } |
| 3510 | } |
| 3511 | } |
| 3512 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3513 | std::vector<sp<InputWindowHandle>> InputDispatcher::getWindowHandlesLocked( |
| 3514 | int32_t displayId) const { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3515 | return getValueByKey(mWindowHandlesByDisplay, displayId); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3516 | } |
| 3517 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3518 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3519 | const sp<IBinder>& windowHandleToken) const { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3520 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3521 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
| 3522 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3523 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3524 | return windowHandle; |
| 3525 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3526 | } |
| 3527 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3528 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3529 | } |
| 3530 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3531 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3532 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3533 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
| 3534 | for (const sp<InputWindowHandle>& handle : windowHandles) { |
| 3535 | if (handle->getToken() == windowHandle->getToken()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3536 | if (windowHandle->getInfo()->displayId != it.first) { |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 3537 | ALOGE("Found window %s in display %" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3538 | ", but it should belong to display %" PRId32, |
| 3539 | windowHandle->getName().c_str(), it.first, |
| 3540 | windowHandle->getInfo()->displayId); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3541 | } |
| 3542 | return true; |
| 3543 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3544 | } |
| 3545 | } |
| 3546 | return false; |
| 3547 | } |
| 3548 | |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3549 | sp<InputChannel> InputDispatcher::getInputChannelLocked(const sp<IBinder>& token) const { |
| 3550 | size_t count = mInputChannelsByToken.count(token); |
| 3551 | if (count == 0) { |
| 3552 | return nullptr; |
| 3553 | } |
| 3554 | return mInputChannelsByToken.at(token); |
| 3555 | } |
| 3556 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3557 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
| 3558 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
| 3559 | if (inputWindowHandles.empty()) { |
| 3560 | // Remove all handles on a display if there are no windows left. |
| 3561 | mWindowHandlesByDisplay.erase(displayId); |
| 3562 | return; |
| 3563 | } |
| 3564 | |
| 3565 | // Since we compare the pointer of input window handles across window updates, we need |
| 3566 | // to make sure the handle object for the same window stays unchanged across updates. |
| 3567 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3568 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3569 | for (const sp<InputWindowHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3570 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | std::vector<sp<InputWindowHandle>> newHandles; |
| 3574 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { |
| 3575 | if (!handle->updateInfo()) { |
| 3576 | // handle no longer valid |
| 3577 | continue; |
| 3578 | } |
| 3579 | |
| 3580 | const InputWindowInfo* info = handle->getInfo(); |
| 3581 | if ((getInputChannelLocked(handle->getToken()) == nullptr && |
| 3582 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { |
| 3583 | const bool noInputChannel = |
| 3584 | info->inputFeatures & InputWindowInfo::INPUT_FEATURE_NO_INPUT_CHANNEL; |
| 3585 | const bool canReceiveInput = |
| 3586 | !(info->layoutParamsFlags & InputWindowInfo::FLAG_NOT_TOUCHABLE) || |
| 3587 | !(info->layoutParamsFlags & InputWindowInfo::FLAG_NOT_FOCUSABLE); |
| 3588 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 3589 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3590 | handle->getName().c_str()); |
| 3591 | } |
| 3592 | continue; |
| 3593 | } |
| 3594 | |
| 3595 | if (info->displayId != displayId) { |
| 3596 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 3597 | handle->getName().c_str(), displayId, info->displayId); |
| 3598 | continue; |
| 3599 | } |
| 3600 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3601 | if (oldHandlesById.find(handle->getId()) != oldHandlesById.end()) { |
| 3602 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3603 | oldHandle->updateFrom(handle); |
| 3604 | newHandles.push_back(oldHandle); |
| 3605 | } else { |
| 3606 | newHandles.push_back(handle); |
| 3607 | } |
| 3608 | } |
| 3609 | |
| 3610 | // Insert or replace |
| 3611 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 3612 | } |
| 3613 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3614 | void InputDispatcher::setInputWindows( |
| 3615 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { |
| 3616 | { // acquire lock |
| 3617 | std::scoped_lock _l(mLock); |
| 3618 | for (auto const& i : handlesPerDisplay) { |
| 3619 | setInputWindowsLocked(i.second, i.first); |
| 3620 | } |
| 3621 | } |
| 3622 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3623 | mLooper->wake(); |
| 3624 | } |
| 3625 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3626 | /** |
| 3627 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 3628 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 3629 | * If set an empty list, remove all handles from the specific display. |
| 3630 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 3631 | * For removed handle, check if need to send a cancel event if already in touch. |
| 3632 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3633 | void InputDispatcher::setInputWindowsLocked( |
| 3634 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3635 | if (DEBUG_FOCUS) { |
| 3636 | std::string windowList; |
| 3637 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { |
| 3638 | windowList += iwh->getName() + " "; |
| 3639 | } |
| 3640 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 3641 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3642 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3643 | // Copy old handles for release if they are no longer present. |
| 3644 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3645 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3646 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3647 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3648 | sp<InputWindowHandle> newFocusedWindowHandle = nullptr; |
| 3649 | bool foundHoveredWindow = false; |
| 3650 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
| 3651 | // Set newFocusedWindowHandle to the top most focused window instead of the last one |
| 3652 | if (!newFocusedWindowHandle && windowHandle->getInfo()->hasFocus && |
| 3653 | windowHandle->getInfo()->visible) { |
| 3654 | newFocusedWindowHandle = windowHandle; |
| 3655 | } |
| 3656 | if (windowHandle == mLastHoverWindowHandle) { |
| 3657 | foundHoveredWindow = true; |
| 3658 | } |
| 3659 | } |
| 3660 | |
| 3661 | if (!foundHoveredWindow) { |
| 3662 | mLastHoverWindowHandle = nullptr; |
| 3663 | } |
| 3664 | |
| 3665 | sp<InputWindowHandle> oldFocusedWindowHandle = |
| 3666 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 3667 | |
| 3668 | if (!haveSameToken(oldFocusedWindowHandle, newFocusedWindowHandle)) { |
| 3669 | if (oldFocusedWindowHandle != nullptr) { |
| 3670 | if (DEBUG_FOCUS) { |
| 3671 | ALOGD("Focus left window: %s in display %" PRId32, |
| 3672 | oldFocusedWindowHandle->getName().c_str(), displayId); |
Garfield Tan | bd0fbcd | 2018-11-30 12:45:03 -0800 | [diff] [blame] | 3673 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3674 | sp<InputChannel> focusedInputChannel = |
| 3675 | getInputChannelLocked(oldFocusedWindowHandle->getToken()); |
| 3676 | if (focusedInputChannel != nullptr) { |
| 3677 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 3678 | "focus left window"); |
| 3679 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
| 3680 | enqueueFocusEventLocked(*oldFocusedWindowHandle, false /*hasFocus*/); |
Garfield Tan | bd0fbcd | 2018-11-30 12:45:03 -0800 | [diff] [blame] | 3681 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3682 | mFocusedWindowHandlesByDisplay.erase(displayId); |
| 3683 | } |
| 3684 | if (newFocusedWindowHandle != nullptr) { |
| 3685 | if (DEBUG_FOCUS) { |
| 3686 | ALOGD("Focus entered window: %s in display %" PRId32, |
| 3687 | newFocusedWindowHandle->getName().c_str(), displayId); |
| 3688 | } |
| 3689 | mFocusedWindowHandlesByDisplay[displayId] = newFocusedWindowHandle; |
| 3690 | enqueueFocusEventLocked(*newFocusedWindowHandle, true /*hasFocus*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3691 | } |
| 3692 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3693 | if (mFocusedDisplayId == displayId) { |
| 3694 | onFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3695 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3696 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3697 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 3698 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 3699 | mTouchStatesByDisplay.find(displayId); |
| 3700 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 3701 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3702 | for (size_t i = 0; i < state.windows.size();) { |
| 3703 | TouchedWindow& touchedWindow = state.windows[i]; |
| 3704 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3705 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3706 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 3707 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3708 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3709 | sp<InputChannel> touchedInputChannel = |
| 3710 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 3711 | if (touchedInputChannel != nullptr) { |
| 3712 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 3713 | "touched window was removed"); |
| 3714 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3715 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3716 | state.windows.erase(state.windows.begin() + i); |
| 3717 | } else { |
| 3718 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3719 | } |
| 3720 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3721 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3722 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3723 | // Release information for windows that are no longer present. |
| 3724 | // This ensures that unused input channels are released promptly. |
| 3725 | // Otherwise, they might stick around until the window handle is destroyed |
| 3726 | // which might not happen until the next GC. |
| 3727 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
| 3728 | if (!hasWindowHandleLocked(oldWindowHandle)) { |
| 3729 | if (DEBUG_FOCUS) { |
| 3730 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3731 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3732 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3733 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 3734 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | void InputDispatcher::setFocusedApplication( |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3738 | int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3739 | if (DEBUG_FOCUS) { |
| 3740 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 3741 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 3742 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3743 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3744 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3745 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3746 | sp<InputApplicationHandle> oldFocusedApplicationHandle = |
| 3747 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3748 | if (inputApplicationHandle != nullptr && inputApplicationHandle->updateInfo()) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3749 | if (oldFocusedApplicationHandle != inputApplicationHandle) { |
| 3750 | if (oldFocusedApplicationHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3751 | resetANRTimeoutsLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3752 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3753 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3754 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3755 | } else if (oldFocusedApplicationHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3756 | resetANRTimeoutsLocked(); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3757 | oldFocusedApplicationHandle.clear(); |
| 3758 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3759 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3760 | } // release lock |
| 3761 | |
| 3762 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3763 | mLooper->wake(); |
| 3764 | } |
| 3765 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3766 | /** |
| 3767 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 3768 | * the display not specified. |
| 3769 | * |
| 3770 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 3771 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 3772 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 3773 | * display. The display-specified events won't be affected. |
| 3774 | */ |
| 3775 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3776 | if (DEBUG_FOCUS) { |
| 3777 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 3778 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3779 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3780 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3781 | |
| 3782 | if (mFocusedDisplayId != displayId) { |
| 3783 | sp<InputWindowHandle> oldFocusedWindowHandle = |
| 3784 | getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); |
| 3785 | if (oldFocusedWindowHandle != nullptr) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3786 | sp<InputChannel> inputChannel = |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3787 | getInputChannelLocked(oldFocusedWindowHandle->getToken()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3788 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3789 | CancelationOptions |
| 3790 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 3791 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3792 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3793 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 3794 | } |
| 3795 | } |
| 3796 | mFocusedDisplayId = displayId; |
| 3797 | |
| 3798 | // Sanity check |
| 3799 | sp<InputWindowHandle> newFocusedWindowHandle = |
| 3800 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 3801 | onFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 3802 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3803 | if (newFocusedWindowHandle == nullptr) { |
| 3804 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
| 3805 | if (!mFocusedWindowHandlesByDisplay.empty()) { |
| 3806 | ALOGE("But another display has a focused window:"); |
| 3807 | for (auto& it : mFocusedWindowHandlesByDisplay) { |
| 3808 | const int32_t displayId = it.first; |
| 3809 | const sp<InputWindowHandle>& windowHandle = it.second; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3810 | ALOGE("Display #%" PRId32 " has focused window: '%s'\n", displayId, |
| 3811 | windowHandle->getName().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3812 | } |
| 3813 | } |
| 3814 | } |
| 3815 | } |
| 3816 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3817 | if (DEBUG_FOCUS) { |
| 3818 | logDispatchStateLocked(); |
| 3819 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3820 | } // release lock |
| 3821 | |
| 3822 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3823 | mLooper->wake(); |
| 3824 | } |
| 3825 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3826 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3827 | if (DEBUG_FOCUS) { |
| 3828 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 3829 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3830 | |
| 3831 | bool changed; |
| 3832 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3833 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3834 | |
| 3835 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 3836 | if (mDispatchFrozen && !frozen) { |
| 3837 | resetANRTimeoutsLocked(); |
| 3838 | } |
| 3839 | |
| 3840 | if (mDispatchEnabled && !enabled) { |
| 3841 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 3842 | } |
| 3843 | |
| 3844 | mDispatchEnabled = enabled; |
| 3845 | mDispatchFrozen = frozen; |
| 3846 | changed = true; |
| 3847 | } else { |
| 3848 | changed = false; |
| 3849 | } |
| 3850 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3851 | if (DEBUG_FOCUS) { |
| 3852 | logDispatchStateLocked(); |
| 3853 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3854 | } // release lock |
| 3855 | |
| 3856 | if (changed) { |
| 3857 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3858 | mLooper->wake(); |
| 3859 | } |
| 3860 | } |
| 3861 | |
| 3862 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3863 | if (DEBUG_FOCUS) { |
| 3864 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 3865 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3866 | |
| 3867 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3868 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3869 | |
| 3870 | if (mInputFilterEnabled == enabled) { |
| 3871 | return; |
| 3872 | } |
| 3873 | |
| 3874 | mInputFilterEnabled = enabled; |
| 3875 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 3876 | } // release lock |
| 3877 | |
| 3878 | // Wake up poll loop since there might be work to do to drop everything. |
| 3879 | mLooper->wake(); |
| 3880 | } |
| 3881 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 3882 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 3883 | std::scoped_lock lock(mLock); |
| 3884 | mInTouchMode = inTouchMode; |
| 3885 | } |
| 3886 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3887 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { |
| 3888 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3889 | if (DEBUG_FOCUS) { |
| 3890 | ALOGD("Trivial transfer to same window."); |
| 3891 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3892 | return true; |
| 3893 | } |
| 3894 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3895 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3896 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3897 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3898 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 3899 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3900 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3901 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3902 | return false; |
| 3903 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3904 | if (DEBUG_FOCUS) { |
| 3905 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 3906 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 3907 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3908 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3909 | if (DEBUG_FOCUS) { |
| 3910 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 3911 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3912 | return false; |
| 3913 | } |
| 3914 | |
| 3915 | bool found = false; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 3916 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 3917 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3918 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 3919 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 3920 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 3921 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 3922 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3923 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3924 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3925 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3926 | int32_t newTargetFlags = oldTargetFlags & |
| 3927 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 3928 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3929 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3930 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3931 | found = true; |
| 3932 | goto Found; |
| 3933 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3934 | } |
| 3935 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3936 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3937 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3938 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3939 | if (DEBUG_FOCUS) { |
| 3940 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 3941 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3942 | return false; |
| 3943 | } |
| 3944 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 3945 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 3946 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3947 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3948 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3949 | CancelationOptions |
| 3950 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 3951 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3952 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3953 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3954 | } |
| 3955 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3956 | if (DEBUG_FOCUS) { |
| 3957 | logDispatchStateLocked(); |
| 3958 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3959 | } // release lock |
| 3960 | |
| 3961 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3962 | mLooper->wake(); |
| 3963 | return true; |
| 3964 | } |
| 3965 | |
| 3966 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3967 | if (DEBUG_FOCUS) { |
| 3968 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 3969 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3970 | |
| 3971 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 3972 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 3973 | |
| 3974 | resetKeyRepeatLocked(); |
| 3975 | releasePendingEventLocked(); |
| 3976 | drainInboundQueueLocked(); |
| 3977 | resetANRTimeoutsLocked(); |
| 3978 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3979 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3980 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3981 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3985 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3986 | dumpDispatchStateLocked(dump); |
| 3987 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3988 | std::istringstream stream(dump); |
| 3989 | std::string line; |
| 3990 | |
| 3991 | while (std::getline(stream, line, '\n')) { |
| 3992 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3993 | } |
| 3994 | } |
| 3995 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3996 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 3997 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 3998 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 3999 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4000 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4001 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4002 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4003 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4004 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4005 | const int32_t displayId = it.first; |
| 4006 | const sp<InputApplicationHandle>& applicationHandle = it.second; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4007 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
| 4008 | ", name='%s', dispatchingTimeout=%0.3fms\n", |
| 4009 | displayId, applicationHandle->getName().c_str(), |
| 4010 | applicationHandle->getDispatchingTimeout( |
| 4011 | DEFAULT_INPUT_DISPATCHING_TIMEOUT) / |
| 4012 | 1000000.0); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4013 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4014 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4015 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4016 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4017 | |
| 4018 | if (!mFocusedWindowHandlesByDisplay.empty()) { |
| 4019 | dump += StringPrintf(INDENT "FocusedWindows:\n"); |
| 4020 | for (auto& it : mFocusedWindowHandlesByDisplay) { |
| 4021 | const int32_t displayId = it.first; |
| 4022 | const sp<InputWindowHandle>& windowHandle = it.second; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4023 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", displayId, |
| 4024 | windowHandle->getName().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4025 | } |
| 4026 | } else { |
| 4027 | dump += StringPrintf(INDENT "FocusedWindows: <none>\n"); |
| 4028 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4029 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 4030 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4031 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 4032 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4033 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4034 | 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] | 4035 | state.displayId, toString(state.down), toString(state.split), |
| 4036 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4037 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4038 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4039 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4040 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4041 | dump += StringPrintf(INDENT4 |
| 4042 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 4043 | i, touchedWindow.windowHandle->getName().c_str(), |
| 4044 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4045 | } |
| 4046 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4047 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4048 | } |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4049 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4050 | dump += INDENT3 "Portal windows:\n"; |
| 4051 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4052 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4053 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, |
| 4054 | portalWindowHandle->getName().c_str()); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4055 | } |
| 4056 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4057 | } |
| 4058 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4059 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4060 | } |
| 4061 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4062 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4063 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4064 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4065 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4066 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4067 | dump += INDENT2 "Windows:\n"; |
| 4068 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4069 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4070 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4071 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4072 | dump += StringPrintf(INDENT3 "%zu: name='%s', displayId=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4073 | "portalToDisplayId=%d, paused=%s, hasFocus=%s, " |
chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4074 | "hasWallpaper=%s, visible=%s, canReceiveKeys=%s, " |
| 4075 | "flags=0x%08x, type=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4076 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4077 | "windowScale=(%f,%f), touchableRegion=", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4078 | i, windowInfo->name.c_str(), windowInfo->displayId, |
| 4079 | windowInfo->portalToDisplayId, |
| 4080 | toString(windowInfo->paused), |
| 4081 | toString(windowInfo->hasFocus), |
| 4082 | toString(windowInfo->hasWallpaper), |
| 4083 | toString(windowInfo->visible), |
| 4084 | toString(windowInfo->canReceiveKeys), |
| 4085 | windowInfo->layoutParamsFlags, |
chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4086 | windowInfo->layoutParamsType, windowInfo->frameLeft, |
| 4087 | windowInfo->frameTop, windowInfo->frameRight, |
| 4088 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
| 4089 | windowInfo->windowXScale, windowInfo->windowYScale); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4090 | dumpRegion(dump, windowInfo->touchableRegion); |
| 4091 | dump += StringPrintf(", inputFeatures=0x%08x", windowInfo->inputFeatures); |
| 4092 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%0.3fms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4093 | windowInfo->ownerPid, windowInfo->ownerUid, |
| 4094 | windowInfo->dispatchingTimeout / 1000000.0); |
Siarhei Vishniakou | 67d4450 | 2020-04-09 11:09:29 -0700 | [diff] [blame] | 4095 | dump += StringPrintf(INDENT4 " flags: %s\n", |
| 4096 | inputWindowFlagsToString(windowInfo->layoutParamsFlags) |
| 4097 | .c_str()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4098 | } |
| 4099 | } else { |
| 4100 | dump += INDENT2 "Windows: <none>\n"; |
| 4101 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4102 | } |
| 4103 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4104 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4105 | } |
| 4106 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4107 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4108 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4109 | const std::vector<Monitor>& monitors = it.second; |
| 4110 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 4111 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4112 | } |
| 4113 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4114 | const std::vector<Monitor>& monitors = it.second; |
| 4115 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 4116 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4117 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4118 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4119 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4120 | } |
| 4121 | |
| 4122 | nsecs_t currentTime = now(); |
| 4123 | |
| 4124 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4125 | if (!mRecentQueue.empty()) { |
| 4126 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
| 4127 | for (EventEntry* entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4128 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4129 | entry->appendDescription(dump); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4130 | dump += StringPrintf(", age=%0.1fms\n", (currentTime - entry->eventTime) * 0.000001f); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4131 | } |
| 4132 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4133 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | // Dump event currently being dispatched. |
| 4137 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4138 | dump += INDENT "PendingEvent:\n"; |
| 4139 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4140 | mPendingEvent->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4141 | dump += StringPrintf(", age=%0.1fms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4142 | (currentTime - mPendingEvent->eventTime) * 0.000001f); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4143 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4144 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4148 | if (!mInboundQueue.empty()) { |
| 4149 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
| 4150 | for (EventEntry* entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4151 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4152 | entry->appendDescription(dump); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4153 | dump += StringPrintf(", age=%0.1fms\n", (currentTime - entry->eventTime) * 0.000001f); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4154 | } |
| 4155 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4156 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4157 | } |
| 4158 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 4159 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4160 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 4161 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 4162 | const KeyReplacement& replacement = pair.first; |
| 4163 | int32_t newKeyCode = pair.second; |
| 4164 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4165 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4166 | } |
| 4167 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4168 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4169 | } |
| 4170 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4171 | if (!mConnectionsByFd.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4172 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4173 | for (const auto& pair : mConnectionsByFd) { |
| 4174 | const sp<Connection>& connection = pair.second; |
| 4175 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
| 4176 | "status=%s, monitor=%s, inputPublisherBlocked=%s\n", |
| 4177 | pair.first, connection->getInputChannelName().c_str(), |
| 4178 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
| 4179 | toString(connection->monitor), |
| 4180 | toString(connection->inputPublisherBlocked)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4181 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4182 | if (!connection->outboundQueue.empty()) { |
| 4183 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 4184 | connection->outboundQueue.size()); |
| 4185 | for (DispatchEntry* entry : connection->outboundQueue) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4186 | dump.append(INDENT4); |
| 4187 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4188 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, age=%0.1fms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4189 | entry->targetFlags, entry->resolvedAction, |
| 4190 | (currentTime - entry->eventEntry->eventTime) * 0.000001f); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4191 | } |
| 4192 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4193 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4194 | } |
| 4195 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4196 | if (!connection->waitQueue.empty()) { |
| 4197 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 4198 | connection->waitQueue.size()); |
| 4199 | for (DispatchEntry* entry : connection->waitQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4200 | dump += INDENT4; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4201 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4202 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4203 | "age=%0.1fms, wait=%0.1fms\n", |
| 4204 | entry->targetFlags, entry->resolvedAction, |
| 4205 | (currentTime - entry->eventEntry->eventTime) * 0.000001f, |
| 4206 | (currentTime - entry->deliveryTime) * 0.000001f); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4207 | } |
| 4208 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4209 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4210 | } |
| 4211 | } |
| 4212 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4213 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4214 | } |
| 4215 | |
| 4216 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4217 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %0.1fms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4218 | (mAppSwitchDueTime - now()) / 1000000.0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4219 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4220 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4221 | } |
| 4222 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4223 | dump += INDENT "Configuration:\n"; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4224 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %0.1fms\n", mConfig.keyRepeatDelay * 0.000001f); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4225 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %0.1fms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4226 | mConfig.keyRepeatTimeout * 0.000001f); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4227 | } |
| 4228 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4229 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 4230 | const size_t numMonitors = monitors.size(); |
| 4231 | for (size_t i = 0; i < numMonitors; i++) { |
| 4232 | const Monitor& monitor = monitors[i]; |
| 4233 | const sp<InputChannel>& channel = monitor.inputChannel; |
| 4234 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 4235 | dump += "\n"; |
| 4236 | } |
| 4237 | } |
| 4238 | |
Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 4239 | status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4240 | #if DEBUG_REGISTRATION |
Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 4241 | ALOGD("channel '%s' ~ registerInputChannel", inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4242 | #endif |
| 4243 | |
| 4244 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4245 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4246 | sp<Connection> existingConnection = getConnectionLocked(inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4247 | if (existingConnection != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4248 | ALOGW("Attempted to register already registered input channel '%s'", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4249 | inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4250 | return BAD_VALUE; |
| 4251 | } |
| 4252 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4253 | sp<Connection> connection = new Connection(inputChannel, false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4254 | |
| 4255 | int fd = inputChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4256 | mConnectionsByFd[fd] = connection; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4257 | mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4258 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4259 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
| 4260 | } // release lock |
| 4261 | |
| 4262 | // Wake the looper because some connections have changed. |
| 4263 | mLooper->wake(); |
| 4264 | return OK; |
| 4265 | } |
| 4266 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4267 | status_t InputDispatcher::registerInputMonitor(const sp<InputChannel>& inputChannel, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4268 | int32_t displayId, bool isGestureMonitor) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4269 | { // acquire lock |
| 4270 | std::scoped_lock _l(mLock); |
| 4271 | |
| 4272 | if (displayId < 0) { |
| 4273 | ALOGW("Attempted to register input monitor without a specified display."); |
| 4274 | return BAD_VALUE; |
| 4275 | } |
| 4276 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4277 | if (inputChannel->getConnectionToken() == nullptr) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4278 | ALOGW("Attempted to register input monitor without an identifying token."); |
| 4279 | return BAD_VALUE; |
| 4280 | } |
| 4281 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4282 | sp<Connection> connection = new Connection(inputChannel, true /*monitor*/, mIdGenerator); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4283 | |
| 4284 | const int fd = inputChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4285 | mConnectionsByFd[fd] = connection; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4286 | mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4287 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4288 | auto& monitorsByDisplay = |
| 4289 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4290 | monitorsByDisplay[displayId].emplace_back(inputChannel); |
| 4291 | |
| 4292 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4293 | } |
| 4294 | // Wake the looper because some connections have changed. |
| 4295 | mLooper->wake(); |
| 4296 | return OK; |
| 4297 | } |
| 4298 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4299 | status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) { |
| 4300 | #if DEBUG_REGISTRATION |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4301 | ALOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4302 | #endif |
| 4303 | |
| 4304 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4305 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4306 | |
| 4307 | status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/); |
| 4308 | if (status) { |
| 4309 | return status; |
| 4310 | } |
| 4311 | } // release lock |
| 4312 | |
| 4313 | // Wake the poll loop because removing the connection may have changed the current |
| 4314 | // synchronization state. |
| 4315 | mLooper->wake(); |
| 4316 | return OK; |
| 4317 | } |
| 4318 | |
| 4319 | status_t InputDispatcher::unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4320 | bool notify) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4321 | sp<Connection> connection = getConnectionLocked(inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4322 | if (connection == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4323 | ALOGW("Attempted to unregister already unregistered input channel '%s'", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4324 | inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4325 | return BAD_VALUE; |
| 4326 | } |
| 4327 | |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4328 | [[maybe_unused]] const bool removed = removeByValue(mConnectionsByFd, connection); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4329 | ALOG_ASSERT(removed); |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4330 | mInputChannelsByToken.erase(inputChannel->getConnectionToken()); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4331 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4332 | if (connection->monitor) { |
| 4333 | removeMonitorChannelLocked(inputChannel); |
| 4334 | } |
| 4335 | |
| 4336 | mLooper->removeFd(inputChannel->getFd()); |
| 4337 | |
| 4338 | nsecs_t currentTime = now(); |
| 4339 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 4340 | |
| 4341 | connection->status = Connection::STATUS_ZOMBIE; |
| 4342 | return OK; |
| 4343 | } |
| 4344 | |
| 4345 | void InputDispatcher::removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4346 | removeMonitorChannelLocked(inputChannel, mGlobalMonitorsByDisplay); |
| 4347 | removeMonitorChannelLocked(inputChannel, mGestureMonitorsByDisplay); |
| 4348 | } |
| 4349 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4350 | void InputDispatcher::removeMonitorChannelLocked( |
| 4351 | const sp<InputChannel>& inputChannel, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4352 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4353 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4354 | std::vector<Monitor>& monitors = it->second; |
| 4355 | const size_t numMonitors = monitors.size(); |
| 4356 | for (size_t i = 0; i < numMonitors; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4357 | if (monitors[i].inputChannel == inputChannel) { |
| 4358 | monitors.erase(monitors.begin() + i); |
| 4359 | break; |
| 4360 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4361 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4362 | if (monitors.empty()) { |
| 4363 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4364 | } else { |
| 4365 | ++it; |
| 4366 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4367 | } |
| 4368 | } |
| 4369 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4370 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 4371 | { // acquire lock |
| 4372 | std::scoped_lock _l(mLock); |
| 4373 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 4374 | |
| 4375 | if (!foundDisplayId) { |
| 4376 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 4377 | return BAD_VALUE; |
| 4378 | } |
| 4379 | int32_t displayId = foundDisplayId.value(); |
| 4380 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 4381 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4382 | mTouchStatesByDisplay.find(displayId); |
| 4383 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4384 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 4385 | return BAD_VALUE; |
| 4386 | } |
| 4387 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame^] | 4388 | TouchState& state = stateIt->second; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4389 | std::optional<int32_t> foundDeviceId; |
| 4390 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4391 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4392 | foundDeviceId = state.deviceId; |
| 4393 | } |
| 4394 | } |
| 4395 | if (!foundDeviceId || !state.down) { |
| 4396 | 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] | 4397 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4398 | return BAD_VALUE; |
| 4399 | } |
| 4400 | int32_t deviceId = foundDeviceId.value(); |
| 4401 | |
| 4402 | // Send cancel events to all the input channels we're stealing from. |
| 4403 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4404 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4405 | options.deviceId = deviceId; |
| 4406 | options.displayId = displayId; |
| 4407 | for (const TouchedWindow& window : state.windows) { |
| 4408 | sp<InputChannel> channel = getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4409 | if (channel != nullptr) { |
| 4410 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 4411 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4412 | } |
| 4413 | // Then clear the current touch state so we stop dispatching to them as well. |
| 4414 | state.filterNonMonitors(); |
| 4415 | } |
| 4416 | return OK; |
| 4417 | } |
| 4418 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4419 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 4420 | const sp<IBinder>& token) { |
| 4421 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 4422 | const std::vector<Monitor>& monitors = it.second; |
| 4423 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4424 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4425 | return it.first; |
| 4426 | } |
| 4427 | } |
| 4428 | } |
| 4429 | return std::nullopt; |
| 4430 | } |
| 4431 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4432 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) { |
| 4433 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4434 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4435 | } |
| 4436 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4437 | for (const auto& pair : mConnectionsByFd) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4438 | const sp<Connection>& connection = pair.second; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4439 | if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4440 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4441 | } |
| 4442 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 4443 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4444 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4445 | } |
| 4446 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4447 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 4448 | const sp<Connection>& connection, uint32_t seq, |
| 4449 | bool handled) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4450 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4451 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4452 | commandEntry->connection = connection; |
| 4453 | commandEntry->eventTime = currentTime; |
| 4454 | commandEntry->seq = seq; |
| 4455 | commandEntry->handled = handled; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4456 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4457 | } |
| 4458 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4459 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 4460 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4461 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4462 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4463 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4464 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4465 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4466 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4467 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4468 | } |
| 4469 | |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4470 | void InputDispatcher::onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4471 | const sp<InputWindowHandle>& newFocus) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4472 | sp<IBinder> oldToken = oldFocus != nullptr ? oldFocus->getToken() : nullptr; |
| 4473 | sp<IBinder> newToken = newFocus != nullptr ? newFocus->getToken() : nullptr; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4474 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4475 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4476 | commandEntry->oldToken = oldToken; |
| 4477 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4478 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4479 | } |
| 4480 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4481 | void InputDispatcher::onANRLocked(nsecs_t currentTime, |
| 4482 | const sp<InputApplicationHandle>& applicationHandle, |
| 4483 | const sp<InputWindowHandle>& windowHandle, nsecs_t eventTime, |
| 4484 | nsecs_t waitStartTime, const char* reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4485 | float dispatchLatency = (currentTime - eventTime) * 0.000001f; |
| 4486 | float waitDuration = (currentTime - waitStartTime) * 0.000001f; |
| 4487 | ALOGI("Application is not responding: %s. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4488 | "It has been %0.1fms since event, %0.1fms since wait started. Reason: %s", |
| 4489 | getApplicationWindowLabel(applicationHandle, windowHandle).c_str(), dispatchLatency, |
| 4490 | waitDuration, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4491 | |
| 4492 | // 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] | 4493 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4494 | struct tm tm; |
| 4495 | localtime_r(&t, &tm); |
| 4496 | char timestr[64]; |
| 4497 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
| 4498 | mLastANRState.clear(); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4499 | mLastANRState += INDENT "ANR:\n"; |
| 4500 | mLastANRState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4501 | mLastANRState += |
| 4502 | StringPrintf(INDENT2 "Window: %s\n", |
| 4503 | getApplicationWindowLabel(applicationHandle, windowHandle).c_str()); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4504 | mLastANRState += StringPrintf(INDENT2 "DispatchLatency: %0.1fms\n", dispatchLatency); |
| 4505 | mLastANRState += StringPrintf(INDENT2 "WaitDuration: %0.1fms\n", waitDuration); |
| 4506 | mLastANRState += StringPrintf(INDENT2 "Reason: %s\n", reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4507 | dumpDispatchStateLocked(mLastANRState); |
| 4508 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4509 | std::unique_ptr<CommandEntry> commandEntry = |
| 4510 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyANRLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4511 | commandEntry->inputApplicationHandle = applicationHandle; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4512 | commandEntry->inputChannel = |
| 4513 | windowHandle != nullptr ? getInputChannelLocked(windowHandle->getToken()) : nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4514 | commandEntry->reason = reason; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4515 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4516 | } |
| 4517 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4518 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4519 | mLock.unlock(); |
| 4520 | |
| 4521 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 4522 | |
| 4523 | mLock.lock(); |
| 4524 | } |
| 4525 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4526 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4527 | sp<Connection> connection = commandEntry->connection; |
| 4528 | |
| 4529 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 4530 | mLock.unlock(); |
| 4531 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4532 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4533 | |
| 4534 | mLock.lock(); |
| 4535 | } |
| 4536 | } |
| 4537 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4538 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4539 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 4540 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4541 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4542 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4543 | mLock.lock(); |
| 4544 | } |
| 4545 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4546 | void InputDispatcher::doNotifyANRLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4547 | sp<IBinder> token = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4548 | commandEntry->inputChannel ? commandEntry->inputChannel->getConnectionToken() : nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4549 | mLock.unlock(); |
| 4550 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4551 | nsecs_t newTimeout = |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4552 | mPolicy->notifyANR(commandEntry->inputApplicationHandle, token, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4553 | |
| 4554 | mLock.lock(); |
| 4555 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4556 | resumeAfterTargetsNotReadyTimeoutLocked(newTimeout, token); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4557 | } |
| 4558 | |
| 4559 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 4560 | CommandEntry* commandEntry) { |
| 4561 | KeyEntry* entry = commandEntry->keyEntry; |
| 4562 | |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4563 | KeyEvent event = createKeyEvent(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4564 | |
| 4565 | mLock.unlock(); |
| 4566 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4567 | android::base::Timer t; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4568 | sp<IBinder> token = commandEntry->inputChannel != nullptr |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4569 | ? commandEntry->inputChannel->getConnectionToken() |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4570 | : nullptr; |
| 4571 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry->policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4572 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4573 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4574 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4575 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4576 | |
| 4577 | mLock.lock(); |
| 4578 | |
| 4579 | if (delay < 0) { |
| 4580 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
| 4581 | } else if (!delay) { |
| 4582 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 4583 | } else { |
| 4584 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 4585 | entry->interceptKeyWakeupTime = now() + delay; |
| 4586 | } |
| 4587 | entry->release(); |
| 4588 | } |
| 4589 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4590 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 4591 | mLock.unlock(); |
| 4592 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 4593 | mLock.lock(); |
| 4594 | } |
| 4595 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4596 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4597 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4598 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4599 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4600 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4601 | |
| 4602 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4603 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4604 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4605 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4606 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4607 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4608 | |
| 4609 | nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 4610 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 4611 | std::string msg = |
| 4612 | StringPrintf("Window '%s' spent %0.1fms processing the last input event: ", |
| 4613 | connection->getWindowName().c_str(), eventDuration * 0.000001f); |
| 4614 | dispatchEntry->eventEntry->appendDescription(msg); |
| 4615 | ALOGI("%s", msg.c_str()); |
| 4616 | } |
| 4617 | |
| 4618 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4619 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4620 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); |
| 4621 | restartEvent = |
| 4622 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4623 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4624 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); |
| 4625 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 4626 | handled); |
| 4627 | } else { |
| 4628 | restartEvent = false; |
| 4629 | } |
| 4630 | |
| 4631 | // Dequeue the event and start the next cycle. |
| 4632 | // Note that because the lock might have been released, it is possible that the |
| 4633 | // contents of the wait queue to have been drained, so we need to double-check |
| 4634 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4635 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 4636 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 4637 | dispatchEntry = *dispatchEntryIt; |
| 4638 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4639 | traceWaitQueueLength(connection); |
| 4640 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4641 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4642 | traceOutboundQueueLength(connection); |
| 4643 | } else { |
| 4644 | releaseDispatchEntry(dispatchEntry); |
| 4645 | } |
| 4646 | } |
| 4647 | |
| 4648 | // Start the next dispatch cycle for this connection. |
| 4649 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4653 | DispatchEntry* dispatchEntry, |
| 4654 | KeyEntry* keyEntry, bool handled) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4655 | if (keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4656 | if (!handled) { |
| 4657 | // Report the key as unhandled, since the fallback was not handled. |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4658 | mReporter->reportUnhandledKey(keyEntry->id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4659 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4660 | return false; |
| 4661 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4662 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4663 | // Get the fallback key state. |
| 4664 | // Clear it out after dispatching the UP. |
| 4665 | int32_t originalKeyCode = keyEntry->keyCode; |
| 4666 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
| 4667 | if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
| 4668 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4669 | } |
| 4670 | |
| 4671 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 4672 | // If the application handles the original key for which we previously |
| 4673 | // generated a fallback or if the window is not a foreground window, |
| 4674 | // then cancel the associated fallback key, if any. |
| 4675 | if (fallbackKeyCode != -1) { |
| 4676 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4677 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4678 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4679 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4680 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, |
| 4681 | keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4682 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4683 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4684 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4685 | |
| 4686 | mLock.unlock(); |
| 4687 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4688 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4689 | keyEntry->policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4690 | |
| 4691 | mLock.lock(); |
| 4692 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4693 | // Cancel the fallback key. |
| 4694 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4695 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4696 | "application handled the original non-fallback key " |
| 4697 | "or is no longer a foreground target, " |
| 4698 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4699 | options.keyCode = fallbackKeyCode; |
| 4700 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4701 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4702 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4703 | } |
| 4704 | } else { |
| 4705 | // If the application did not handle a non-fallback key, first check |
| 4706 | // that we are in a good state to perform unhandled key event processing |
| 4707 | // Then ask the policy what to do with it. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4708 | bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN && keyEntry->repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4709 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4710 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4711 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4712 | "since this is not an initial down. " |
| 4713 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4714 | originalKeyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4715 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4716 | return false; |
| 4717 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4718 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4719 | // Dispatch the unhandled key to the policy. |
| 4720 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4721 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4722 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4723 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4724 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4725 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4726 | |
| 4727 | mLock.unlock(); |
| 4728 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4729 | bool fallback = |
| 4730 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 4731 | &event, keyEntry->policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4732 | |
| 4733 | mLock.lock(); |
| 4734 | |
| 4735 | if (connection->status != Connection::STATUS_NORMAL) { |
| 4736 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4737 | return false; |
| 4738 | } |
| 4739 | |
| 4740 | // Latch the fallback keycode for this key on an initial down. |
| 4741 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 4742 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4743 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4744 | fallbackKeyCode = event.getKeyCode(); |
| 4745 | } else { |
| 4746 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 4747 | } |
| 4748 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 4749 | } |
| 4750 | |
| 4751 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 4752 | |
| 4753 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 4754 | // We will continue to dispatch the key to the policy but we will no |
| 4755 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4756 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 4757 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4758 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4759 | if (fallback) { |
| 4760 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4761 | "as a fallback for %d, but on the DOWN it had requested " |
| 4762 | "to send %d instead. Fallback canceled.", |
| 4763 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4764 | } else { |
| 4765 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4766 | "but on the DOWN it had requested to send %d. " |
| 4767 | "Fallback canceled.", |
| 4768 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4769 | } |
| 4770 | #endif |
| 4771 | |
| 4772 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 4773 | "canceling fallback, policy no longer desires it"); |
| 4774 | options.keyCode = fallbackKeyCode; |
| 4775 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 4776 | |
| 4777 | fallback = false; |
| 4778 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 4779 | if (keyEntry->action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4780 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4781 | } |
| 4782 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4783 | |
| 4784 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4785 | { |
| 4786 | std::string msg; |
| 4787 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 4788 | connection->inputState.getFallbackKeys(); |
| 4789 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4790 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4791 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4792 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4793 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4794 | } |
| 4795 | #endif |
| 4796 | |
| 4797 | if (fallback) { |
| 4798 | // Restart the dispatch cycle using the fallback key. |
| 4799 | keyEntry->eventTime = event.getEventTime(); |
| 4800 | keyEntry->deviceId = event.getDeviceId(); |
| 4801 | keyEntry->source = event.getSource(); |
| 4802 | keyEntry->displayId = event.getDisplayId(); |
| 4803 | keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 4804 | keyEntry->keyCode = fallbackKeyCode; |
| 4805 | keyEntry->scanCode = event.getScanCode(); |
| 4806 | keyEntry->metaState = event.getMetaState(); |
| 4807 | keyEntry->repeatCount = event.getRepeatCount(); |
| 4808 | keyEntry->downTime = event.getDownTime(); |
| 4809 | keyEntry->syntheticRepeat = false; |
| 4810 | |
| 4811 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4812 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4813 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 4814 | originalKeyCode, fallbackKeyCode, keyEntry->metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4815 | #endif |
| 4816 | return true; // restart the event |
| 4817 | } else { |
| 4818 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4819 | ALOGD("Unhandled key event: No fallback key."); |
| 4820 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4821 | |
| 4822 | // Report the key as unhandled, since there is no fallback key. |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4823 | mReporter->reportUnhandledKey(keyEntry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4824 | } |
| 4825 | } |
| 4826 | return false; |
| 4827 | } |
| 4828 | |
| 4829 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4830 | DispatchEntry* dispatchEntry, |
| 4831 | MotionEntry* motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4832 | return false; |
| 4833 | } |
| 4834 | |
| 4835 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 4836 | mLock.unlock(); |
| 4837 | |
| 4838 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); |
| 4839 | |
| 4840 | mLock.lock(); |
| 4841 | } |
| 4842 | |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4843 | KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) { |
| 4844 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4845 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 4846 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 4847 | entry.repeatCount, entry.downTime, entry.eventTime); |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4848 | return event; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4849 | } |
| 4850 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4851 | void InputDispatcher::updateDispatchStatistics(nsecs_t currentTime, const EventEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4852 | int32_t injectionResult, |
| 4853 | nsecs_t timeSpentWaitingForApplication) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4854 | // TODO Write some statistics about how long we spend waiting. |
| 4855 | } |
| 4856 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 4857 | /** |
| 4858 | * Report the touch event latency to the statsd server. |
| 4859 | * Input events are reported for statistics if: |
| 4860 | * - This is a touchscreen event |
| 4861 | * - InputFilter is not enabled |
| 4862 | * - Event is not injected or synthesized |
| 4863 | * |
| 4864 | * Statistics should be reported before calling addValue, to prevent a fresh new sample |
| 4865 | * from getting aggregated with the "old" data. |
| 4866 | */ |
| 4867 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) |
| 4868 | REQUIRES(mLock) { |
| 4869 | const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && |
| 4870 | !(motionEntry.isSynthesized()) && !mInputFilterEnabled; |
| 4871 | if (!reportForStatistics) { |
| 4872 | return; |
| 4873 | } |
| 4874 | |
| 4875 | if (mTouchStatistics.shouldReport()) { |
| 4876 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), |
| 4877 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), |
| 4878 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); |
| 4879 | mTouchStatistics.reset(); |
| 4880 | } |
| 4881 | const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); |
| 4882 | mTouchStatistics.addValue(latencyMicros); |
| 4883 | } |
| 4884 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4885 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 4886 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4887 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4888 | } |
| 4889 | } |
| 4890 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 4891 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4892 | if (ATRACE_ENABLED()) { |
| 4893 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 4894 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4895 | ATRACE_INT(counterName, connection->outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4896 | } |
| 4897 | } |
| 4898 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 4899 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4900 | if (ATRACE_ENABLED()) { |
| 4901 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 4902 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4903 | ATRACE_INT(counterName, connection->waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4904 | } |
| 4905 | } |
| 4906 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4907 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4908 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4909 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4910 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4911 | dumpDispatchStateLocked(dump); |
| 4912 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4913 | if (!mLastANRState.empty()) { |
| 4914 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
| 4915 | dump += mLastANRState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4916 | } |
| 4917 | } |
| 4918 | |
| 4919 | void InputDispatcher::monitor() { |
| 4920 | // 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] | 4921 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4922 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4923 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4924 | } |
| 4925 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4926 | /** |
| 4927 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 4928 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 4929 | * this method can be safely called from any thread, as long as you've ensured that |
| 4930 | * the work you are interested in completing has already been queued. |
| 4931 | */ |
| 4932 | bool InputDispatcher::waitForIdle() { |
| 4933 | /** |
| 4934 | * Timeout should represent the longest possible time that a device might spend processing |
| 4935 | * events and commands. |
| 4936 | */ |
| 4937 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 4938 | std::unique_lock lock(mLock); |
| 4939 | mLooper->wake(); |
| 4940 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 4941 | return result == std::cv_status::no_timeout; |
| 4942 | } |
| 4943 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4944 | } // namespace android::inputdispatcher |