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