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