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