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