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> |
Siarhei Vishniakou | 7394eaa | 2020-04-09 11:16:18 -0700 | [diff] [blame] | 65 | #include <log/log_event_list.h> |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 66 | #include <openssl/hmac.h> |
| 67 | #include <openssl/rand.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 68 | #include <powermanager/PowerManager.h> |
| 69 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 70 | |
| 71 | #define INDENT " " |
| 72 | #define INDENT2 " " |
| 73 | #define INDENT3 " " |
| 74 | #define INDENT4 " " |
| 75 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 76 | using android::base::StringPrintf; |
| 77 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 78 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 | |
| 80 | // Default input dispatching timeout if there is no focused application or paused window |
| 81 | // from which to determine an appropriate dispatching timeout. |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 82 | constexpr std::chrono::nanoseconds DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5s; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 83 | |
| 84 | // Amount of time to allow for all pending events to be processed when an app switch |
| 85 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 86 | // 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] | 87 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 88 | |
| 89 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 90 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 91 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 93 | // 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] | 94 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 95 | |
| 96 | // Log a warning when an interception call takes longer than this to process. |
| 97 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 98 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 99 | // Additional key latency in case a connection is still processing some motion events. |
| 100 | // This will help with the case when a user touched a button that opens a new window, |
| 101 | // and gives us the chance to dispatch the key to this new window. |
| 102 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 103 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 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 | |
Siarhei Vishniakou | 7394eaa | 2020-04-09 11:16:18 -0700 | [diff] [blame] | 107 | // Event log tags. See EventLogTags.logtags for reference |
| 108 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 109 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
| 110 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 111 | static inline nsecs_t now() { |
| 112 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 113 | } |
| 114 | |
| 115 | static inline const char* toString(bool value) { |
| 116 | return value ? "true" : "false"; |
| 117 | } |
| 118 | |
| 119 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 120 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 121 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static bool isValidKeyAction(int32_t action) { |
| 125 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 126 | case AKEY_EVENT_ACTION_DOWN: |
| 127 | case AKEY_EVENT_ACTION_UP: |
| 128 | return true; |
| 129 | default: |
| 130 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | static bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 135 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 136 | ALOGE("Key event has invalid action code 0x%x", action); |
| 137 | return false; |
| 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 142 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 143 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 144 | case AMOTION_EVENT_ACTION_DOWN: |
| 145 | case AMOTION_EVENT_ACTION_UP: |
| 146 | case AMOTION_EVENT_ACTION_CANCEL: |
| 147 | case AMOTION_EVENT_ACTION_MOVE: |
| 148 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 149 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 150 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 151 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 152 | case AMOTION_EVENT_ACTION_SCROLL: |
| 153 | return true; |
| 154 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 155 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 156 | int32_t index = getMotionEventActionPointerIndex(action); |
| 157 | return index >= 0 && index < pointerCount; |
| 158 | } |
| 159 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 160 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 161 | return actionButton != 0; |
| 162 | default: |
| 163 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 167 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 168 | const PointerProperties* pointerProperties) { |
| 169 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 170 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 171 | return false; |
| 172 | } |
| 173 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 174 | 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] | 175 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 176 | return false; |
| 177 | } |
| 178 | BitSet32 pointerIdBits; |
| 179 | for (size_t i = 0; i < pointerCount; i++) { |
| 180 | int32_t id = pointerProperties[i].id; |
| 181 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 182 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 183 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | if (pointerIdBits.hasBit(id)) { |
| 187 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 188 | return false; |
| 189 | } |
| 190 | pointerIdBits.markBit(id); |
| 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 195 | static void dumpRegion(std::string& dump, const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 196 | if (region.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 197 | dump += "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | |
| 201 | bool first = true; |
| 202 | Region::const_iterator cur = region.begin(); |
| 203 | Region::const_iterator const tail = region.end(); |
| 204 | while (cur != tail) { |
| 205 | if (first) { |
| 206 | first = false; |
| 207 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 208 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 209 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 210 | 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] | 211 | cur++; |
| 212 | } |
| 213 | } |
| 214 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 215 | /** |
| 216 | * Find the entry in std::unordered_map by key, and return it. |
| 217 | * If the entry is not found, return a default constructed entry. |
| 218 | * |
| 219 | * Useful when the entries are vectors, since an empty vector will be returned |
| 220 | * if the entry is not found. |
| 221 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 222 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 223 | template <typename K, typename V> |
| 224 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 225 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 226 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 227 | } |
| 228 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 229 | /** |
| 230 | * Find the entry in std::unordered_map by value, and remove it. |
| 231 | * If more than one entry has the same value, then all matching |
| 232 | * key-value pairs will be removed. |
| 233 | * |
| 234 | * Return true if at least one value has been removed. |
| 235 | */ |
| 236 | template <typename K, typename V> |
| 237 | static bool removeByValue(std::unordered_map<K, V>& map, const V& value) { |
| 238 | bool removed = false; |
| 239 | for (auto it = map.begin(); it != map.end();) { |
| 240 | if (it->second == value) { |
| 241 | it = map.erase(it); |
| 242 | removed = true; |
| 243 | } else { |
| 244 | it++; |
| 245 | } |
| 246 | } |
| 247 | return removed; |
| 248 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 249 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 250 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { |
| 251 | if (first == second) { |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | if (first == nullptr || second == nullptr) { |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | return first->getToken() == second->getToken(); |
| 260 | } |
| 261 | |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 262 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 263 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 264 | } |
| 265 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 266 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 267 | EventEntry* eventEntry, |
| 268 | int32_t inputTargetFlags) { |
| 269 | if (inputTarget.useDefaultPointerInfo()) { |
| 270 | const PointerInfo& pointerInfo = inputTarget.getDefaultPointerInfo(); |
| 271 | return std::make_unique<DispatchEntry>(eventEntry, // increments ref |
| 272 | inputTargetFlags, pointerInfo.xOffset, |
| 273 | pointerInfo.yOffset, inputTarget.globalScaleFactor, |
| 274 | pointerInfo.windowXScale, pointerInfo.windowYScale); |
| 275 | } |
| 276 | |
| 277 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 278 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 279 | |
| 280 | PointerCoords pointerCoords[motionEntry.pointerCount]; |
| 281 | |
| 282 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 283 | // as long as all other pointers are normalized to the same value and the final DispatchEntry |
| 284 | // uses the offset and scale for the normalized pointer. |
| 285 | const PointerInfo& firstPointerInfo = |
| 286 | inputTarget.pointerInfos[inputTarget.pointerIds.firstMarkedBit()]; |
| 287 | |
| 288 | // Iterate through all pointers in the event to normalize against the first. |
| 289 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 290 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 291 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 292 | const PointerInfo& currPointerInfo = inputTarget.pointerInfos[pointerId]; |
| 293 | |
| 294 | // The scale factor is the ratio of the current pointers scale to the normalized scale. |
| 295 | float scaleXDiff = currPointerInfo.windowXScale / firstPointerInfo.windowXScale; |
| 296 | float scaleYDiff = currPointerInfo.windowYScale / firstPointerInfo.windowYScale; |
| 297 | |
| 298 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
| 299 | // First apply the current pointers offset to set the window at 0,0 |
| 300 | pointerCoords[pointerIndex].applyOffset(currPointerInfo.xOffset, currPointerInfo.yOffset); |
| 301 | // Next scale the coordinates. |
| 302 | pointerCoords[pointerIndex].scale(1, scaleXDiff, scaleYDiff); |
| 303 | // Lastly, offset the coordinates so they're in the normalized pointer's frame. |
| 304 | pointerCoords[pointerIndex].applyOffset(-firstPointerInfo.xOffset, |
| 305 | -firstPointerInfo.yOffset); |
| 306 | } |
| 307 | |
| 308 | MotionEntry* combinedMotionEntry = |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 309 | new MotionEntry(motionEntry.id, motionEntry.eventTime, motionEntry.deviceId, |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 310 | motionEntry.source, motionEntry.displayId, motionEntry.policyFlags, |
| 311 | motionEntry.action, motionEntry.actionButton, motionEntry.flags, |
| 312 | motionEntry.metaState, motionEntry.buttonState, |
| 313 | motionEntry.classification, motionEntry.edgeFlags, |
| 314 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 315 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 316 | motionEntry.downTime, motionEntry.pointerCount, |
| 317 | motionEntry.pointerProperties, pointerCoords, 0 /* xOffset */, |
| 318 | 0 /* yOffset */); |
| 319 | |
| 320 | if (motionEntry.injectionState) { |
| 321 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 322 | combinedMotionEntry->injectionState->refCount += 1; |
| 323 | } |
| 324 | |
| 325 | std::unique_ptr<DispatchEntry> dispatchEntry = |
| 326 | std::make_unique<DispatchEntry>(combinedMotionEntry, // increments ref |
| 327 | inputTargetFlags, firstPointerInfo.xOffset, |
| 328 | firstPointerInfo.yOffset, inputTarget.globalScaleFactor, |
| 329 | firstPointerInfo.windowXScale, |
| 330 | firstPointerInfo.windowYScale); |
| 331 | combinedMotionEntry->release(); |
| 332 | return dispatchEntry; |
| 333 | } |
| 334 | |
Siarhei Vishniakou | e0fb6bd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 335 | static void addGestureMonitors(const std::vector<Monitor>& monitors, |
| 336 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, |
| 337 | float yOffset = 0) { |
| 338 | if (monitors.empty()) { |
| 339 | return; |
| 340 | } |
| 341 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); |
| 342 | for (const Monitor& monitor : monitors) { |
| 343 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); |
| 344 | } |
| 345 | } |
| 346 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 347 | static std::array<uint8_t, 128> getRandomKey() { |
| 348 | std::array<uint8_t, 128> key; |
| 349 | if (RAND_bytes(key.data(), key.size()) != 1) { |
| 350 | LOG_ALWAYS_FATAL("Can't generate HMAC key"); |
| 351 | } |
| 352 | return key; |
| 353 | } |
| 354 | |
| 355 | // --- HmacKeyManager --- |
| 356 | |
| 357 | HmacKeyManager::HmacKeyManager() : mHmacKey(getRandomKey()) {} |
| 358 | |
| 359 | std::array<uint8_t, 32> HmacKeyManager::sign(const VerifiedInputEvent& event) const { |
| 360 | size_t size; |
| 361 | switch (event.type) { |
| 362 | case VerifiedInputEvent::Type::KEY: { |
| 363 | size = sizeof(VerifiedKeyEvent); |
| 364 | break; |
| 365 | } |
| 366 | case VerifiedInputEvent::Type::MOTION: { |
| 367 | size = sizeof(VerifiedMotionEvent); |
| 368 | break; |
| 369 | } |
| 370 | } |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 371 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
Edgar Arriaga | 3d61bc1 | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 372 | return sign(start, size); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 373 | } |
| 374 | |
Edgar Arriaga | 3d61bc1 | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 375 | std::array<uint8_t, 32> HmacKeyManager::sign(const uint8_t* data, size_t size) const { |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 376 | // SHA256 always generates 32-bytes result |
| 377 | std::array<uint8_t, 32> hash; |
| 378 | unsigned int hashLen = 0; |
Edgar Arriaga | 3d61bc1 | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 379 | uint8_t* result = |
| 380 | HMAC(EVP_sha256(), mHmacKey.data(), mHmacKey.size(), data, size, hash.data(), &hashLen); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 381 | if (result == nullptr) { |
| 382 | ALOGE("Could not sign the data using HMAC"); |
| 383 | return INVALID_HMAC; |
| 384 | } |
| 385 | |
| 386 | if (hashLen != hash.size()) { |
| 387 | ALOGE("HMAC-SHA256 has unexpected length"); |
| 388 | return INVALID_HMAC; |
| 389 | } |
| 390 | |
| 391 | return hash; |
| 392 | } |
| 393 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 394 | // --- InputDispatcher --- |
| 395 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 396 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 397 | : mPolicy(policy), |
| 398 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 399 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 400 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 401 | mAppSwitchSawKeyDown(false), |
| 402 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 403 | mNextUnblockedEvent(nullptr), |
| 404 | mDispatchEnabled(false), |
| 405 | mDispatchFrozen(false), |
| 406 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 407 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 408 | // To avoid leaking stack in case that call never comes, and for tests, |
| 409 | // initialize it here anyways. |
| 410 | mInTouchMode(true), |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 411 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 412 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 413 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 414 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 415 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 416 | |
| 417 | policy->getDispatcherConfiguration(&mConfig); |
| 418 | } |
| 419 | |
| 420 | InputDispatcher::~InputDispatcher() { |
| 421 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 422 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 423 | |
| 424 | resetKeyRepeatLocked(); |
| 425 | releasePendingEventLocked(); |
| 426 | drainInboundQueueLocked(); |
| 427 | } |
| 428 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 429 | while (!mConnectionsByFd.empty()) { |
| 430 | sp<Connection> connection = mConnectionsByFd.begin()->second; |
| 431 | unregisterInputChannel(connection->inputChannel); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 435 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 436 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 437 | return ALREADY_EXISTS; |
| 438 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 439 | mThread = std::make_unique<InputThread>( |
| 440 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 441 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 445 | if (mThread && mThread->isCallingThread()) { |
| 446 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 447 | return INVALID_OPERATION; |
| 448 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 449 | mThread.reset(); |
| 450 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 453 | void InputDispatcher::dispatchOnce() { |
| 454 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 455 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 456 | std::scoped_lock _l(mLock); |
| 457 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 458 | |
| 459 | // Run a dispatch loop if there are no pending commands. |
| 460 | // The dispatch loop might enqueue commands to run afterwards. |
| 461 | if (!haveCommandsLocked()) { |
| 462 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 463 | } |
| 464 | |
| 465 | // Run all pending commands if there are any. |
| 466 | // If any commands were run then force the next poll to wake up immediately. |
| 467 | if (runCommandsLockedInterruptible()) { |
| 468 | nextWakeupTime = LONG_LONG_MIN; |
| 469 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 470 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 471 | // If we are still waiting for ack on some events, |
| 472 | // we might have to wake up earlier to check if an app is anr'ing. |
| 473 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 474 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 475 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 476 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 477 | // pending or queued events |
| 478 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 479 | mDispatcherEnteredIdle.notify_all(); |
| 480 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 481 | } // release lock |
| 482 | |
| 483 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 484 | nsecs_t currentTime = now(); |
| 485 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 486 | mLooper->pollOnce(timeoutMillis); |
| 487 | } |
| 488 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 489 | /** |
| 490 | * Check if any of the connections' wait queues have events that are too old. |
| 491 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 492 | * Return the time at which we should wake up next. |
| 493 | */ |
| 494 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 495 | const nsecs_t currentTime = now(); |
| 496 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 497 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 498 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 499 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
| 500 | onAnrLocked(mAwaitedFocusedApplication); |
| 501 | mAwaitedFocusedApplication.clear(); |
| 502 | return LONG_LONG_MIN; |
| 503 | } else { |
| 504 | // Keep waiting |
| 505 | const nsecs_t millisRemaining = ns2ms(*mNoFocusedWindowTimeoutTime - currentTime); |
| 506 | ALOGW("Still no focused window. Will drop the event in %" PRId64 "ms", millisRemaining); |
| 507 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // Check if any connection ANRs are due |
| 512 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 513 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 514 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 515 | } |
| 516 | |
| 517 | // If we reached here, we have an unresponsive connection. |
| 518 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 519 | if (connection == nullptr) { |
| 520 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 521 | return nextAnrCheck; |
| 522 | } |
| 523 | connection->responsive = false; |
| 524 | // Stop waking up for this unresponsive connection |
| 525 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
| 526 | onAnrLocked(connection); |
| 527 | return LONG_LONG_MIN; |
| 528 | } |
| 529 | |
| 530 | nsecs_t InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
| 531 | sp<InputWindowHandle> window = getWindowHandleLocked(token); |
| 532 | if (window != nullptr) { |
| 533 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT).count(); |
| 534 | } |
| 535 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT.count(); |
| 536 | } |
| 537 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 538 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 539 | nsecs_t currentTime = now(); |
| 540 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 541 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 542 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 543 | // repeat if the device is just coming out of sleep. |
| 544 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 545 | resetKeyRepeatLocked(); |
| 546 | } |
| 547 | |
| 548 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 549 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 550 | if (DEBUG_FOCUS) { |
| 551 | ALOGD("Dispatch frozen. Waiting some more."); |
| 552 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 553 | return; |
| 554 | } |
| 555 | |
| 556 | // Optimize latency of app switches. |
| 557 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 558 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 559 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 560 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 561 | *nextWakeupTime = mAppSwitchDueTime; |
| 562 | } |
| 563 | |
| 564 | // Ready to start a new event. |
| 565 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 566 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 567 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 568 | if (isAppSwitchDue) { |
| 569 | // The inbound queue is empty so the app switch key we were waiting |
| 570 | // for will never arrive. Stop waiting for it. |
| 571 | resetPendingAppSwitchLocked(false); |
| 572 | isAppSwitchDue = false; |
| 573 | } |
| 574 | |
| 575 | // Synthesize a key repeat if appropriate. |
| 576 | if (mKeyRepeatState.lastKeyEntry) { |
| 577 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 578 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 579 | } else { |
| 580 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 581 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // Nothing to do if there is no pending event. |
| 587 | if (!mPendingEvent) { |
| 588 | return; |
| 589 | } |
| 590 | } else { |
| 591 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 592 | mPendingEvent = mInboundQueue.front(); |
| 593 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 594 | traceInboundQueueLengthLocked(); |
| 595 | } |
| 596 | |
| 597 | // Poke user activity for this event. |
| 598 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 599 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 600 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | // Now we have an event to dispatch. |
| 604 | // 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] | 605 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 606 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 607 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 608 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 609 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 610 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 611 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 615 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 619 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 620 | ConfigurationChangedEntry* typedEntry = |
| 621 | static_cast<ConfigurationChangedEntry*>(mPendingEvent); |
| 622 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 623 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 624 | break; |
| 625 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 626 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 627 | case EventEntry::Type::DEVICE_RESET: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 628 | DeviceResetEntry* typedEntry = static_cast<DeviceResetEntry*>(mPendingEvent); |
| 629 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 630 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 631 | break; |
| 632 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 633 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 634 | case EventEntry::Type::FOCUS: { |
| 635 | FocusEntry* typedEntry = static_cast<FocusEntry*>(mPendingEvent); |
| 636 | dispatchFocusLocked(currentTime, typedEntry); |
| 637 | done = true; |
| 638 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 639 | break; |
| 640 | } |
| 641 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 642 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 643 | KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); |
| 644 | if (isAppSwitchDue) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 645 | if (isAppSwitchKeyEvent(*typedEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 646 | resetPendingAppSwitchLocked(true); |
| 647 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 648 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 649 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 650 | } |
| 651 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 652 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 653 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 654 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 655 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 656 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 657 | } |
| 658 | done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 659 | break; |
| 660 | } |
| 661 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 662 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 663 | MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 664 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 665 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 666 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 667 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 668 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 669 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 670 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 671 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 672 | } |
| 673 | done = dispatchMotionLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 674 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 675 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 679 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 680 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 681 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 682 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 683 | |
| 684 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 685 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 689 | /** |
| 690 | * Return true if the events preceding this incoming motion event should be dropped |
| 691 | * Return false otherwise (the default behaviour) |
| 692 | */ |
| 693 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 694 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 695 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 696 | |
| 697 | // Optimize case where the current application is unresponsive and the user |
| 698 | // decides to touch a window in a different application. |
| 699 | // If the application takes too long to catch up then we drop all events preceding |
| 700 | // the touch into the other window. |
| 701 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 702 | int32_t displayId = motionEntry.displayId; |
| 703 | int32_t x = static_cast<int32_t>( |
| 704 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 705 | int32_t y = static_cast<int32_t>( |
| 706 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 707 | sp<InputWindowHandle> touchedWindowHandle = |
| 708 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 709 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 710 | touchedWindowHandle->getApplicationToken() != |
| 711 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 712 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 713 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 714 | "for %s", |
| 715 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 716 | return true; |
| 717 | } |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 718 | |
| 719 | // Alternatively, maybe there's a gesture monitor that could handle this event |
| 720 | std::vector<TouchedMonitor> gestureMonitors = |
| 721 | findTouchedGestureMonitorsLocked(displayId, {}); |
| 722 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { |
| 723 | sp<Connection> connection = |
| 724 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); |
| 725 | if (connection->responsive) { |
| 726 | // This monitor could take more input. Drop all events preceding this |
| 727 | // event, so that gesture monitor could get a chance to receive the stream |
| 728 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 729 | "responsive gesture monitor that may handle the event", |
| 730 | mAwaitedFocusedApplication->getName().c_str()); |
| 731 | return true; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 737 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 738 | // events to be processed before dispatching the key event. This is because these motion events |
| 739 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 740 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 741 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 742 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 743 | "just send the pending key event to the focused window."); |
| 744 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 745 | } |
| 746 | return false; |
| 747 | } |
| 748 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 749 | bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 750 | bool needWake = mInboundQueue.empty(); |
| 751 | mInboundQueue.push_back(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 752 | traceInboundQueueLengthLocked(); |
| 753 | |
| 754 | switch (entry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 755 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 756 | // Optimize app switch latency. |
| 757 | // If the application takes too long to catch up then we drop all events preceding |
| 758 | // the app switch key. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 759 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 760 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 761 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 762 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 763 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 764 | if (mAppSwitchSawKeyDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 765 | #if DEBUG_APP_SWITCH |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 766 | ALOGD("App switch is pending!"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 767 | #endif |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 768 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 769 | mAppSwitchSawKeyDown = false; |
| 770 | needWake = true; |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | break; |
| 775 | } |
| 776 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 777 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 778 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(*entry))) { |
| 779 | mNextUnblockedEvent = entry; |
| 780 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 781 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 782 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 783 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 784 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 785 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 786 | break; |
| 787 | } |
| 788 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 789 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 790 | // nothing to do |
| 791 | break; |
| 792 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | return needWake; |
| 796 | } |
| 797 | |
| 798 | void InputDispatcher::addRecentEventLocked(EventEntry* entry) { |
| 799 | entry->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 800 | mRecentQueue.push_back(entry); |
| 801 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
| 802 | mRecentQueue.front()->release(); |
| 803 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 807 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 808 | int32_t y, TouchState* touchState, |
| 809 | bool addOutsideTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 810 | bool addPortalWindows) { |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 811 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { |
| 812 | LOG_ALWAYS_FATAL( |
| 813 | "Must provide a valid touch state if adding portal windows or outside targets"); |
| 814 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 815 | // Traverse windows from front to back to find touched window. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 816 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| 817 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 818 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 819 | if (windowInfo->displayId == displayId) { |
| 820 | int32_t flags = windowInfo->layoutParamsFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 821 | |
| 822 | if (windowInfo->visible) { |
| 823 | if (!(flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 824 | bool isTouchModal = (flags & |
| 825 | (InputWindowInfo::FLAG_NOT_FOCUSABLE | |
| 826 | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 827 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 828 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 829 | if (portalToDisplayId != ADISPLAY_ID_NONE && |
| 830 | portalToDisplayId != displayId) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 831 | if (addPortalWindows) { |
| 832 | // For the monitoring channels of the display. |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 833 | touchState->addPortalWindow(windowHandle); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 834 | } |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 835 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 836 | addOutsideTargets, addPortalWindows); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 837 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 838 | // Found window. |
| 839 | return windowHandle; |
| 840 | } |
| 841 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 842 | |
| 843 | if (addOutsideTargets && (flags & InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 844 | touchState->addOrUpdateWindow(windowHandle, |
| 845 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 846 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 847 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 848 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 849 | } |
| 850 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 851 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 854 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
Siarhei Vishniakou | e0fb6bd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 855 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 856 | std::vector<TouchedMonitor> touchedMonitors; |
| 857 | |
| 858 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| 859 | addGestureMonitors(monitors, touchedMonitors); |
| 860 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { |
| 861 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); |
| 862 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 863 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, |
| 864 | -windowInfo->frameTop); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 865 | } |
| 866 | return touchedMonitors; |
| 867 | } |
| 868 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 869 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 870 | const char* reason; |
| 871 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 872 | case DropReason::POLICY: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 873 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 874 | ALOGD("Dropped event because policy consumed it."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 875 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 876 | reason = "inbound event was dropped because the policy consumed it"; |
| 877 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 878 | case DropReason::DISABLED: |
| 879 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 880 | ALOGI("Dropped event because input dispatch is disabled."); |
| 881 | } |
| 882 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 883 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 884 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 885 | ALOGI("Dropped event because of pending overdue app switch."); |
| 886 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 887 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 888 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 889 | ALOGI("Dropped event because the current application is not responding and the user " |
| 890 | "has started interacting with a different application."); |
| 891 | reason = "inbound event was dropped because the current application is not responding " |
| 892 | "and the user has started interacting with a different application"; |
| 893 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 894 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 895 | ALOGI("Dropped event because it is stale."); |
| 896 | reason = "inbound event was dropped because it is stale"; |
| 897 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 898 | case DropReason::NOT_DROPPED: { |
| 899 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 900 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 901 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 902 | } |
| 903 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 904 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 905 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 906 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 907 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 908 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 909 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 910 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 911 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 912 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 913 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 914 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 915 | } else { |
| 916 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 917 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 918 | } |
| 919 | break; |
| 920 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 921 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 922 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 923 | case EventEntry::Type::DEVICE_RESET: { |
| 924 | LOG_ALWAYS_FATAL("Should not drop %s events", EventEntry::typeToString(entry.type)); |
| 925 | break; |
| 926 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 930 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 931 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 932 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 933 | } |
| 934 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 935 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 936 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 937 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 938 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 942 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 943 | } |
| 944 | |
| 945 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 946 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 947 | |
| 948 | #if DEBUG_APP_SWITCH |
| 949 | if (handled) { |
| 950 | ALOGD("App switch has arrived."); |
| 951 | } else { |
| 952 | ALOGD("App switch was abandoned."); |
| 953 | } |
| 954 | #endif |
| 955 | } |
| 956 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 957 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 958 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | bool InputDispatcher::runCommandsLockedInterruptible() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 962 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 963 | return false; |
| 964 | } |
| 965 | |
| 966 | do { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 967 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 968 | mCommandQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 969 | Command command = commandEntry->command; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 970 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 971 | |
| 972 | commandEntry->connection.clear(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 973 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 974 | return true; |
| 975 | } |
| 976 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 977 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { |
| 978 | mCommandQueue.push_back(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 982 | while (!mInboundQueue.empty()) { |
| 983 | EventEntry* entry = mInboundQueue.front(); |
| 984 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 985 | releaseInboundEventLocked(entry); |
| 986 | } |
| 987 | traceInboundQueueLengthLocked(); |
| 988 | } |
| 989 | |
| 990 | void InputDispatcher::releasePendingEventLocked() { |
| 991 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 992 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 993 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
| 997 | void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { |
| 998 | InjectionState* injectionState = entry->injectionState; |
| 999 | if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1000 | #if DEBUG_DISPATCH_CYCLE |
| 1001 | ALOGD("Injected inbound event was dropped."); |
| 1002 | #endif |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1003 | setInjectionResult(entry, INPUT_EVENT_INJECTION_FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1004 | } |
| 1005 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1006 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1007 | } |
| 1008 | addRecentEventLocked(entry); |
| 1009 | entry->release(); |
| 1010 | } |
| 1011 | |
| 1012 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1013 | if (mKeyRepeatState.lastKeyEntry) { |
| 1014 | mKeyRepeatState.lastKeyEntry->release(); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1015 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1019 | KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1020 | KeyEntry* entry = mKeyRepeatState.lastKeyEntry; |
| 1021 | |
| 1022 | // Reuse the repeated key entry if it is otherwise unreferenced. |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1023 | uint32_t policyFlags = entry->policyFlags & |
| 1024 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1025 | if (entry->refCount == 1) { |
| 1026 | entry->recycle(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1027 | entry->id = mIdGenerator.nextId(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1028 | entry->eventTime = currentTime; |
| 1029 | entry->policyFlags = policyFlags; |
| 1030 | entry->repeatCount += 1; |
| 1031 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1032 | KeyEntry* newEntry = |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1033 | new KeyEntry(mIdGenerator.nextId(), currentTime, entry->deviceId, entry->source, |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1034 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 1035 | entry->keyCode, entry->scanCode, entry->metaState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1036 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1037 | |
| 1038 | mKeyRepeatState.lastKeyEntry = newEntry; |
| 1039 | entry->release(); |
| 1040 | |
| 1041 | entry = newEntry; |
| 1042 | } |
| 1043 | entry->syntheticRepeat = true; |
| 1044 | |
| 1045 | // Increment reference count since we keep a reference to the event in |
| 1046 | // mKeyRepeatState.lastKeyEntry in addition to the one we return. |
| 1047 | entry->refCount += 1; |
| 1048 | |
| 1049 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
| 1050 | return entry; |
| 1051 | } |
| 1052 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1053 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
| 1054 | ConfigurationChangedEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1055 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1056 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1057 | #endif |
| 1058 | |
| 1059 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1060 | resetKeyRepeatLocked(); |
| 1061 | |
| 1062 | // 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] | 1063 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 1064 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1065 | commandEntry->eventTime = entry->eventTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1066 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1067 | return true; |
| 1068 | } |
| 1069 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1070 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1071 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1072 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry->eventTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1073 | entry->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1074 | #endif |
| 1075 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1076 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1077 | options.deviceId = entry->deviceId; |
| 1078 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1079 | return true; |
| 1080 | } |
| 1081 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1082 | void InputDispatcher::enqueueFocusEventLocked(const InputWindowHandle& window, bool hasFocus) { |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1083 | if (mPendingEvent != nullptr) { |
| 1084 | // Move the pending event to the front of the queue. This will give the chance |
| 1085 | // for the pending event to get dispatched to the newly focused window |
| 1086 | mInboundQueue.push_front(mPendingEvent); |
| 1087 | mPendingEvent = nullptr; |
| 1088 | } |
| 1089 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1090 | FocusEntry* focusEntry = |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1091 | new FocusEntry(mIdGenerator.nextId(), now(), window.getToken(), hasFocus); |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1092 | |
| 1093 | // This event should go to the front of the queue, but behind all other focus events |
| 1094 | // Find the last focus event, and insert right after it |
| 1095 | std::deque<EventEntry*>::reverse_iterator it = |
| 1096 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1097 | [](EventEntry* event) { return event->type == EventEntry::Type::FOCUS; }); |
| 1098 | |
| 1099 | // Maintain the order of focus events. Insert the entry after all other focus events. |
| 1100 | mInboundQueue.insert(it.base(), focusEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) { |
| 1104 | sp<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1105 | if (channel == nullptr) { |
| 1106 | return; // Window has gone away |
| 1107 | } |
| 1108 | InputTarget target; |
| 1109 | target.inputChannel = channel; |
| 1110 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1111 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 7394eaa | 2020-04-09 11:16:18 -0700 | [diff] [blame] | 1112 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1113 | channel->getName(); |
| 1114 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1115 | dispatchEventLocked(currentTime, entry, {target}); |
| 1116 | } |
| 1117 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1118 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1119 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
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) { |
| 1122 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1123 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1124 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1125 | if (mKeyRepeatState.lastKeyEntry && |
| 1126 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1127 | // We have seen two identical key downs in a row which indicates that the device |
| 1128 | // driver is automatically generating key repeats itself. We take note of the |
| 1129 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1130 | // we will not need to synthesize key repeats ourselves. |
| 1131 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1132 | resetKeyRepeatLocked(); |
| 1133 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1134 | } else { |
| 1135 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1136 | resetKeyRepeatLocked(); |
| 1137 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1138 | } |
| 1139 | mKeyRepeatState.lastKeyEntry = entry; |
| 1140 | entry->refCount += 1; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1141 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1142 | resetKeyRepeatLocked(); |
| 1143 | } |
| 1144 | |
| 1145 | if (entry->repeatCount == 1) { |
| 1146 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1147 | } else { |
| 1148 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1149 | } |
| 1150 | |
| 1151 | entry->dispatchInProgress = true; |
| 1152 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1153 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | // Handle case where the policy asked us to try again later last time. |
| 1157 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1158 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1159 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1160 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1161 | } |
| 1162 | return false; // wait until next wakeup |
| 1163 | } |
| 1164 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1165 | entry->interceptKeyWakeupTime = 0; |
| 1166 | } |
| 1167 | |
| 1168 | // Give the policy a chance to intercept the key. |
| 1169 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1170 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1171 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1172 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1173 | sp<InputWindowHandle> focusedWindowHandle = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1174 | getValueByKey(mFocusedWindowHandlesByDisplay, getTargetDisplayId(*entry)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1175 | if (focusedWindowHandle != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1176 | commandEntry->inputChannel = getInputChannelLocked(focusedWindowHandle->getToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1177 | } |
| 1178 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1179 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1180 | entry->refCount += 1; |
| 1181 | return false; // wait for the command to run |
| 1182 | } else { |
| 1183 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1184 | } |
| 1185 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1186 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1187 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1192 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1193 | setInjectionResult(entry, |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1194 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1195 | : INPUT_EVENT_INJECTION_FAILED); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1196 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1197 | return true; |
| 1198 | } |
| 1199 | |
| 1200 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1201 | std::vector<InputTarget> inputTargets; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1202 | int32_t injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1203 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1204 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1205 | return false; |
| 1206 | } |
| 1207 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1208 | setInjectionResult(entry, injectionResult); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1209 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
| 1210 | return true; |
| 1211 | } |
| 1212 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1213 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1214 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1215 | |
| 1216 | // Dispatch the key. |
| 1217 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1218 | return true; |
| 1219 | } |
| 1220 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1221 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1222 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1223 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1224 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1225 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1226 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1227 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1228 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1229 | #endif |
| 1230 | } |
| 1231 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1232 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, |
| 1233 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1234 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1235 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1236 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1237 | entry->dispatchInProgress = true; |
| 1238 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1239 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1243 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1244 | setInjectionResult(entry, |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1245 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1246 | : INPUT_EVENT_INJECTION_FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1247 | return true; |
| 1248 | } |
| 1249 | |
| 1250 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1251 | |
| 1252 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1253 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | |
| 1255 | bool conflictingPointerActions = false; |
| 1256 | int32_t injectionResult; |
| 1257 | if (isPointerEvent) { |
| 1258 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1259 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1260 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1261 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1262 | } else { |
| 1263 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1264 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1265 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1266 | } |
| 1267 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1268 | return false; |
| 1269 | } |
| 1270 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1271 | setInjectionResult(entry, injectionResult); |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1272 | if (injectionResult == INPUT_EVENT_INJECTION_PERMISSION_DENIED) { |
| 1273 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1274 | return true; |
| 1275 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1276 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1277 | CancelationOptions::Mode mode(isPointerEvent |
| 1278 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1279 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1280 | CancelationOptions options(mode, "input event injection failed"); |
| 1281 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1282 | return true; |
| 1283 | } |
| 1284 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1285 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1286 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1287 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1288 | if (isPointerEvent) { |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1289 | std::unordered_map<int32_t, TouchState>::iterator it = |
| 1290 | mTouchStatesByDisplay.find(entry->displayId); |
| 1291 | if (it != mTouchStatesByDisplay.end()) { |
| 1292 | const TouchState& state = it->second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1293 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1294 | // The event has gone through these portal windows, so we add monitoring targets of |
| 1295 | // the corresponding displays as well. |
| 1296 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1297 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1298 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1299 | -windowInfo->frameLeft, -windowInfo->frameTop); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1305 | // Dispatch the motion. |
| 1306 | if (conflictingPointerActions) { |
| 1307 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1308 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1309 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1310 | } |
| 1311 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1312 | return true; |
| 1313 | } |
| 1314 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1315 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1316 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1317 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1318 | ", policyFlags=0x%x, " |
| 1319 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1320 | "metaState=0x%x, buttonState=0x%x," |
| 1321 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1322 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1323 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1324 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1325 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1326 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1327 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1328 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1329 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1330 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1331 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1332 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1333 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1334 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1335 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1336 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1337 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1338 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1339 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1340 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1341 | } |
| 1342 | #endif |
| 1343 | } |
| 1344 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1345 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, EventEntry* eventEntry, |
| 1346 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1347 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1348 | #if DEBUG_DISPATCH_CYCLE |
| 1349 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1350 | #endif |
| 1351 | |
Siarhei Vishniakou | 7394eaa | 2020-04-09 11:16:18 -0700 | [diff] [blame] | 1352 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1353 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1354 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1355 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1356 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1357 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1358 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1359 | sp<Connection> connection = |
| 1360 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1361 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1362 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1363 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1364 | if (DEBUG_FOCUS) { |
| 1365 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1366 | "is no longer registered with the input dispatcher.", |
| 1367 | inputTarget.inputChannel->getName().c_str()); |
| 1368 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | } |
| 1372 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 1373 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1374 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1375 | // If the policy decides to close the app, we will get a channel removal event via |
| 1376 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1377 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1378 | // pile up. |
| 1379 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1380 | connection->inputChannel->getName().c_str()); |
| 1381 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1382 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1383 | "application not responding"); |
| 1384 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 1388 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1389 | if (DEBUG_FOCUS) { |
| 1390 | ALOGD("Resetting ANR timeouts."); |
| 1391 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1392 | |
| 1393 | // Reset input target wait timeout. |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 1394 | mNoFocusedWindowTimeoutTime = std::nullopt; |
| 1395 | mAwaitedFocusedApplication.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1396 | } |
| 1397 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1398 | /** |
| 1399 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1400 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1401 | * Focused display is the display that the user most recently interacted with. |
| 1402 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1403 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1404 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1405 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1406 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1407 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1408 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1409 | break; |
| 1410 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1411 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1412 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1413 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1414 | break; |
| 1415 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1416 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1417 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1418 | case EventEntry::Type::DEVICE_RESET: { |
| 1419 | 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] | 1420 | return ADISPLAY_ID_NONE; |
| 1421 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1422 | } |
| 1423 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1424 | } |
| 1425 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 1426 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1427 | const char* focusedWindowName) { |
| 1428 | if (mAnrTracker.empty()) { |
| 1429 | // already processed all events that we waited for |
| 1430 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1431 | return false; |
| 1432 | } |
| 1433 | |
| 1434 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1435 | // Start the timer |
| 1436 | ALOGD("Waiting to send key to %s because there are unprocessed events that may cause " |
| 1437 | "focus to change", |
| 1438 | focusedWindowName); |
| 1439 | mKeyIsWaitingForEventsTimeout = currentTime + KEY_WAITING_FOR_EVENTS_TIMEOUT.count(); |
| 1440 | return true; |
| 1441 | } |
| 1442 | |
| 1443 | // We still have pending events, and already started the timer |
| 1444 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1445 | return true; // Still waiting |
| 1446 | } |
| 1447 | |
| 1448 | // Waited too long, and some connection still hasn't processed all motions |
| 1449 | // Just send the key to the focused window |
| 1450 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1451 | focusedWindowName); |
| 1452 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1453 | return false; |
| 1454 | } |
| 1455 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1456 | int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1457 | const EventEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1458 | std::vector<InputTarget>& inputTargets, |
| 1459 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1460 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1461 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1462 | int32_t displayId = getTargetDisplayId(entry); |
| 1463 | sp<InputWindowHandle> focusedWindowHandle = |
| 1464 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 1465 | sp<InputApplicationHandle> focusedApplicationHandle = |
| 1466 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1467 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1468 | // If there is no currently focused window and no focused application |
| 1469 | // then drop the event. |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 1470 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1471 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1472 | "display %" PRId32 ".", |
| 1473 | EventEntry::typeToString(entry.type), displayId); |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1474 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1475 | } |
| 1476 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 1477 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1478 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1479 | // start interacting with another application via touch (app switch). This code can be removed |
| 1480 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1481 | // an app is expected to have a focused window. |
| 1482 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1483 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1484 | // We just discovered that there's no focused window. Start the ANR timer |
| 1485 | const nsecs_t timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1486 | DEFAULT_INPUT_DISPATCHING_TIMEOUT.count()); |
| 1487 | mNoFocusedWindowTimeoutTime = currentTime + timeout; |
| 1488 | mAwaitedFocusedApplication = focusedApplicationHandle; |
| 1489 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1490 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
| 1491 | mAwaitedFocusedApplication->getName().c_str(), ns2ms(timeout)); |
| 1492 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
| 1493 | return INPUT_EVENT_INJECTION_PENDING; |
| 1494 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1495 | // Already raised ANR. Drop the event |
| 1496 | ALOGE("Dropping %s event because there is no focused window", |
| 1497 | EventEntry::typeToString(entry.type)); |
| 1498 | return INPUT_EVENT_INJECTION_FAILED; |
| 1499 | } else { |
| 1500 | // Still waiting for the focused window |
| 1501 | return INPUT_EVENT_INJECTION_PENDING; |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | // we have a valid, non-null focused window |
| 1506 | resetNoFocusedWindowTimeoutLocked(); |
| 1507 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1508 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1509 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1510 | return INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1511 | } |
| 1512 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 1513 | if (focusedWindowHandle->getInfo()->paused) { |
| 1514 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
| 1515 | return INPUT_EVENT_INJECTION_PENDING; |
| 1516 | } |
| 1517 | |
| 1518 | // If the event is a key event, then we must wait for all previous events to |
| 1519 | // complete before delivering it because previous events may have the |
| 1520 | // side-effect of transferring focus to a different window and we want to |
| 1521 | // ensure that the following keys are sent to the new window. |
| 1522 | // |
| 1523 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1524 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1525 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1526 | // often anticipate pending UI changes when typing on a keyboard. |
| 1527 | // To obtain this behavior, we must serialize key events with respect to all |
| 1528 | // prior input events. |
| 1529 | if (entry.type == EventEntry::Type::KEY) { |
| 1530 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1531 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
| 1532 | return INPUT_EVENT_INJECTION_PENDING; |
| 1533 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1537 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1538 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1539 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1540 | |
| 1541 | // Done. |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1542 | return INPUT_EVENT_INJECTION_SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1543 | } |
| 1544 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 1545 | /** |
| 1546 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1547 | * that are currently unresponsive. |
| 1548 | */ |
| 1549 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1550 | const std::vector<TouchedMonitor>& monitors) const { |
| 1551 | std::vector<TouchedMonitor> responsiveMonitors; |
| 1552 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
| 1553 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { |
| 1554 | sp<Connection> connection = getConnectionLocked( |
| 1555 | monitor.monitor.inputChannel->getConnectionToken()); |
| 1556 | if (connection == nullptr) { |
| 1557 | ALOGE("Could not find connection for monitor %s", |
| 1558 | monitor.monitor.inputChannel->getName().c_str()); |
| 1559 | return false; |
| 1560 | } |
| 1561 | if (!connection->responsive) { |
| 1562 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1563 | connection->inputChannel->getName().c_str()); |
| 1564 | return false; |
| 1565 | } |
| 1566 | return true; |
| 1567 | }); |
| 1568 | return responsiveMonitors; |
| 1569 | } |
| 1570 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1571 | int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1572 | const MotionEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1573 | std::vector<InputTarget>& inputTargets, |
| 1574 | nsecs_t* nextWakeupTime, |
| 1575 | bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1576 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1577 | enum InjectionPermission { |
| 1578 | INJECTION_PERMISSION_UNKNOWN, |
| 1579 | INJECTION_PERMISSION_GRANTED, |
| 1580 | INJECTION_PERMISSION_DENIED |
| 1581 | }; |
| 1582 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1583 | // For security reasons, we defer updating the touch state until we are sure that |
| 1584 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1585 | int32_t displayId = entry.displayId; |
| 1586 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1587 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1588 | |
| 1589 | // Update the touch state as needed based on the properties of the touch event. |
| 1590 | int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; |
| 1591 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
| 1592 | sp<InputWindowHandle> newHoverWindowHandle; |
| 1593 | |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1594 | // Copy current touch state into tempTouchState. |
| 1595 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1596 | // If no state for the specified display exists, then our initial state will be empty. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1597 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1598 | TouchState tempTouchState; |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1599 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1600 | mTouchStatesByDisplay.find(displayId); |
| 1601 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1602 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1603 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1606 | bool isSplit = tempTouchState.split; |
| 1607 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1608 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1609 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1610 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1611 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1612 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1613 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1614 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1615 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1616 | bool wrongDevice = false; |
| 1617 | if (newGesture) { |
| 1618 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1619 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | e0fb6bd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1620 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1621 | "in display %" PRId32, |
| 1622 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1623 | // TODO: test multiple simultaneous input streams. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1624 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1625 | switchedDevice = false; |
| 1626 | wrongDevice = true; |
| 1627 | goto Failed; |
| 1628 | } |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1629 | tempTouchState.reset(); |
| 1630 | tempTouchState.down = down; |
| 1631 | tempTouchState.deviceId = entry.deviceId; |
| 1632 | tempTouchState.source = entry.source; |
| 1633 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1634 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1635 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | e0fb6bd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1636 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1637 | "in display %" PRId32, |
| 1638 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1639 | // TODO: test multiple simultaneous input streams. |
| 1640 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1641 | switchedDevice = false; |
| 1642 | wrongDevice = true; |
| 1643 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1647 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1648 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1649 | int32_t x; |
| 1650 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1651 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1652 | // Always dispatch mouse events to cursor position. |
| 1653 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1654 | x = int32_t(entry.xCursorPosition); |
| 1655 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1656 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1657 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1658 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1659 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1660 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1661 | sp<InputWindowHandle> newTouchedWindowHandle = |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1662 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 1663 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1664 | |
| 1665 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1666 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1667 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1668 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1669 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1670 | if (newTouchedWindowHandle != nullptr && |
| 1671 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1672 | // New window supports splitting, but we should never split mouse events. |
| 1673 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1674 | } else if (isSplit) { |
| 1675 | // New window does not support splitting but we have already split events. |
| 1676 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1677 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1681 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1682 | // Try to assign the pointer to the first foreground window we find, if there is one. |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1683 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 1686 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 1687 | ALOGI("Not sending touch event to %s because it is paused", |
| 1688 | newTouchedWindowHandle->getName().c_str()); |
| 1689 | newTouchedWindowHandle = nullptr; |
| 1690 | } |
| 1691 | |
| 1692 | if (newTouchedWindowHandle != nullptr) { |
| 1693 | sp<Connection> connection = getConnectionLocked(newTouchedWindowHandle->getToken()); |
| 1694 | if (connection == nullptr) { |
| 1695 | ALOGI("Could not find connection for %s", |
| 1696 | newTouchedWindowHandle->getName().c_str()); |
| 1697 | newTouchedWindowHandle = nullptr; |
| 1698 | } else if (!connection->responsive) { |
| 1699 | // don't send the new touch to an unresponsive window |
| 1700 | ALOGW("Unresponsive window %s will not get the new gesture at %" PRIu64, |
| 1701 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 1702 | newTouchedWindowHandle = nullptr; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | // Also don't send the new touch event to unresponsive gesture monitors |
| 1707 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); |
| 1708 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1709 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 1710 | 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] | 1711 | "(%d, %d) in display %" PRId32 ".", |
| 1712 | x, y, displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1713 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1714 | goto Failed; |
| 1715 | } |
| 1716 | |
| 1717 | if (newTouchedWindowHandle != nullptr) { |
| 1718 | // Set target flags. |
| 1719 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 1720 | if (isSplit) { |
| 1721 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1722 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1723 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1724 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1725 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 1726 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1727 | } |
| 1728 | |
| 1729 | // Update hover state. |
| 1730 | if (isHoverAction) { |
| 1731 | newHoverWindowHandle = newTouchedWindowHandle; |
| 1732 | } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
| 1733 | newHoverWindowHandle = mLastHoverWindowHandle; |
| 1734 | } |
| 1735 | |
| 1736 | // Update the temporary touch state. |
| 1737 | BitSet32 pointerIds; |
| 1738 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1739 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1740 | pointerIds.markBit(pointerId); |
| 1741 | } |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1742 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1743 | } |
| 1744 | |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1745 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1746 | } else { |
| 1747 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 1748 | |
| 1749 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1750 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1751 | if (DEBUG_FOCUS) { |
| 1752 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 1753 | "dropped the pointer down event in display %" PRId32, |
| 1754 | displayId); |
| 1755 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1756 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1757 | goto Failed; |
| 1758 | } |
| 1759 | |
| 1760 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1761 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1762 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1763 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1764 | 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] | 1765 | |
| 1766 | sp<InputWindowHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1767 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1768 | sp<InputWindowHandle> newTouchedWindowHandle = |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1769 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1770 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 1771 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1772 | if (DEBUG_FOCUS) { |
| 1773 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 1774 | oldTouchedWindowHandle->getName().c_str(), |
| 1775 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 1776 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1777 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1778 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 1779 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 1780 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1781 | |
| 1782 | // Make a slippery entrance into the new window. |
| 1783 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 1784 | isSplit = true; |
| 1785 | } |
| 1786 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1787 | int32_t targetFlags = |
| 1788 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1789 | if (isSplit) { |
| 1790 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 1791 | } |
| 1792 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1793 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1794 | } |
| 1795 | |
| 1796 | BitSet32 pointerIds; |
| 1797 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1798 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1799 | } |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1800 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
| 1806 | // Let the previous window know that the hover sequence is over. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1807 | if (mLastHoverWindowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1808 | #if DEBUG_HOVER |
| 1809 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1810 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1811 | #endif |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1812 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 1813 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | // Let the new window know that the hover sequence is starting. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1817 | if (newHoverWindowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1818 | #if DEBUG_HOVER |
| 1819 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1820 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1821 | #endif |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1822 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 1823 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 1824 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | // Check permission to inject into all touched foreground windows and ensure there |
| 1829 | // is at least one touched foreground window. |
| 1830 | { |
| 1831 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1832 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1833 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 1834 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1835 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1836 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1837 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1838 | goto Failed; |
| 1839 | } |
| 1840 | } |
| 1841 | } |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1842 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1843 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | e0fb6bd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1844 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 1845 | "%" PRId32 " or gesture monitor to receive it.", |
| 1846 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1847 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1848 | goto Failed; |
| 1849 | } |
| 1850 | |
| 1851 | // Permission granted to injection into all touched foreground windows. |
| 1852 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1853 | } |
| 1854 | |
| 1855 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 1856 | // set the policy flag that we will not reveal coordinate information to this window. |
| 1857 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1858 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1859 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1860 | if (foregroundWindowHandle) { |
| 1861 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1862 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1863 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 1864 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 1865 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1866 | tempTouchState.addOrUpdateWindow(inputWindowHandle, |
| 1867 | InputTarget::FLAG_ZERO_COORDS, |
| 1868 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1869 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1875 | // If this is the first pointer going down and the touched window has a wallpaper |
| 1876 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 1877 | // of the touch gesture. |
| 1878 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 1879 | // engine only supports touch events. We would need to add a mechanism similar |
| 1880 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 1881 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1882 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1883 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1884 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1885 | const std::vector<sp<InputWindowHandle>> windowHandles = |
| 1886 | getWindowHandlesLocked(displayId); |
| 1887 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1888 | const InputWindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1889 | if (info->displayId == displayId && |
| 1890 | windowHandle->getInfo()->layoutParamsType == InputWindowInfo::TYPE_WALLPAPER) { |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1891 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1892 | .addOrUpdateWindow(windowHandle, |
| 1893 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 1894 | InputTarget:: |
| 1895 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 1896 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1897 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | // Success! Output targets. |
| 1904 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 1905 | |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1906 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1907 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1908 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1909 | } |
| 1910 | |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1911 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1912 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1913 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1916 | // Drop the outside or hover touch windows since we will not care about them |
| 1917 | // in the next iteration. |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1918 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1919 | |
| 1920 | Failed: |
| 1921 | // Check injection permission once and for all. |
| 1922 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1923 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1924 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1925 | } else { |
| 1926 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1927 | } |
| 1928 | } |
| 1929 | |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1930 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 1931 | return injectionResult; |
| 1932 | } |
| 1933 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1934 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1935 | if (!wrongDevice) { |
| 1936 | if (switchedDevice) { |
| 1937 | if (DEBUG_FOCUS) { |
| 1938 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 1939 | } |
| 1940 | *outConflictingPointerActions = true; |
| 1941 | } |
| 1942 | |
| 1943 | if (isHoverAction) { |
| 1944 | // Started hovering, therefore no longer down. |
| 1945 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1946 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1947 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 1948 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1949 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1950 | *outConflictingPointerActions = true; |
| 1951 | } |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1952 | tempTouchState.reset(); |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1953 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1954 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1955 | tempTouchState.deviceId = entry.deviceId; |
| 1956 | tempTouchState.source = entry.source; |
| 1957 | tempTouchState.displayId = displayId; |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1958 | } |
| 1959 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 1960 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 1961 | // All pointers up or canceled. |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1962 | tempTouchState.reset(); |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1963 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1964 | // First pointer went down. |
| 1965 | if (oldState && oldState->down) { |
| 1966 | if (DEBUG_FOCUS) { |
| 1967 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 1968 | } |
| 1969 | *outConflictingPointerActions = true; |
| 1970 | } |
| 1971 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 1972 | // One pointer went up. |
| 1973 | if (isSplit) { |
| 1974 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 1975 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1976 | |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1977 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 1978 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1979 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 1980 | touchedWindow.pointerIds.clearBit(pointerId); |
| 1981 | if (touchedWindow.pointerIds.isEmpty()) { |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1982 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1983 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1984 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1985 | } |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1986 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1987 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1988 | } |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1989 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1990 | |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1991 | // Save changes unless the action was scroll in which case the temporary touch |
| 1992 | // state was only valid for this one action. |
| 1993 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1994 | if (tempTouchState.displayId >= 0) { |
| 1995 | mTouchStatesByDisplay[displayId] = tempTouchState; |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1996 | } else { |
| 1997 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1998 | } |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1999 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2000 | |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2001 | // Update hover state. |
| 2002 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2003 | } |
| 2004 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2005 | return injectionResult; |
| 2006 | } |
| 2007 | |
| 2008 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2009 | int32_t targetFlags, BitSet32 pointerIds, |
| 2010 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2011 | std::vector<InputTarget>::iterator it = |
| 2012 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2013 | [&windowHandle](const InputTarget& inputTarget) { |
| 2014 | return inputTarget.inputChannel->getConnectionToken() == |
| 2015 | windowHandle->getToken(); |
| 2016 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2017 | |
Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2018 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2019 | |
| 2020 | if (it == inputTargets.end()) { |
| 2021 | InputTarget inputTarget; |
| 2022 | sp<InputChannel> inputChannel = getInputChannelLocked(windowHandle->getToken()); |
| 2023 | if (inputChannel == nullptr) { |
| 2024 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2025 | return; |
| 2026 | } |
| 2027 | inputTarget.inputChannel = inputChannel; |
| 2028 | inputTarget.flags = targetFlags; |
| 2029 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2030 | inputTargets.push_back(inputTarget); |
| 2031 | it = inputTargets.end() - 1; |
| 2032 | } |
| 2033 | |
| 2034 | ALOG_ASSERT(it->flags == targetFlags); |
| 2035 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2036 | |
| 2037 | it->addPointers(pointerIds, -windowInfo->frameLeft, -windowInfo->frameTop, |
| 2038 | windowInfo->windowXScale, windowInfo->windowYScale); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2039 | } |
| 2040 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2041 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2042 | int32_t displayId, float xOffset, |
| 2043 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2044 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2045 | mGlobalMonitorsByDisplay.find(displayId); |
| 2046 | |
| 2047 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2048 | const std::vector<Monitor>& monitors = it->second; |
| 2049 | for (const Monitor& monitor : monitors) { |
| 2050 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2051 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2052 | } |
| 2053 | } |
| 2054 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2055 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 2056 | float yOffset, |
| 2057 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2058 | InputTarget target; |
| 2059 | target.inputChannel = monitor.inputChannel; |
| 2060 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2061 | target.setDefaultPointerInfo(xOffset, yOffset, 1 /* windowXScale */, 1 /* windowYScale */); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2062 | inputTargets.push_back(target); |
| 2063 | } |
| 2064 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2065 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2066 | const InjectionState* injectionState) { |
| 2067 | if (injectionState && |
| 2068 | (windowHandle == nullptr || |
| 2069 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2070 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2071 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2072 | 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] | 2073 | "owned by uid %d", |
| 2074 | injectionState->injectorPid, injectionState->injectorUid, |
| 2075 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2076 | } else { |
| 2077 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2078 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2079 | } |
| 2080 | return false; |
| 2081 | } |
| 2082 | return true; |
| 2083 | } |
| 2084 | |
Robert Carr | 9cada03 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2085 | /** |
| 2086 | * Indicate whether one window handle should be considered as obscuring |
| 2087 | * another window handle. We only check a few preconditions. Actually |
| 2088 | * checking the bounds is left to the caller. |
| 2089 | */ |
| 2090 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, |
| 2091 | const sp<InputWindowHandle>& otherHandle) { |
| 2092 | // Compare by token so cloned layers aren't counted |
| 2093 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2094 | return false; |
| 2095 | } |
| 2096 | auto info = windowHandle->getInfo(); |
| 2097 | auto otherInfo = otherHandle->getInfo(); |
| 2098 | if (!otherInfo->visible) { |
| 2099 | return false; |
| 2100 | } else if (info->ownerPid == otherInfo->ownerPid && otherHandle->getToken() == nullptr) { |
| 2101 | // In general, if ownerPid is the same we don't want to generate occlusion |
| 2102 | // events. This line is now necessary since we are including all Surfaces |
| 2103 | // in occlusion calculation, so if we didn't check PID like this SurfaceView |
| 2104 | // would occlude their parents. On the other hand before we started including |
| 2105 | // all surfaces in occlusion calculation and had this line, we would count |
| 2106 | // windows with an input channel from the same PID as occluding, and so we |
| 2107 | // preserve this behavior with the getToken() == null check. |
| 2108 | return false; |
| 2109 | } else if (otherInfo->isTrustedOverlay()) { |
| 2110 | return false; |
| 2111 | } else if (otherInfo->displayId != info->displayId) { |
| 2112 | return false; |
| 2113 | } |
| 2114 | return true; |
| 2115 | } |
| 2116 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2117 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, |
| 2118 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2119 | int32_t displayId = windowHandle->getInfo()->displayId; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2120 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| 2121 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | 9cada03 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2122 | if (windowHandle == otherHandle) { |
| 2123 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2124 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2125 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | 9cada03 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2126 | if (canBeObscuredBy(windowHandle, otherHandle) && |
| 2127 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2128 | return true; |
| 2129 | } |
| 2130 | } |
| 2131 | return false; |
| 2132 | } |
| 2133 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2134 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 2135 | int32_t displayId = windowHandle->getInfo()->displayId; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2136 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2137 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2138 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | 9cada03 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2139 | if (windowHandle == otherHandle) { |
| 2140 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | 9cada03 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2144 | if (canBeObscuredBy(windowHandle, otherHandle) && |
| 2145 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2146 | return true; |
| 2147 | } |
| 2148 | } |
| 2149 | return false; |
| 2150 | } |
| 2151 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2152 | std::string InputDispatcher::getApplicationWindowLabel( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2153 | const sp<InputApplicationHandle>& applicationHandle, |
| 2154 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2155 | if (applicationHandle != nullptr) { |
| 2156 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2157 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2158 | } else { |
| 2159 | return applicationHandle->getName(); |
| 2160 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2161 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2162 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2163 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2164 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2165 | } |
| 2166 | } |
| 2167 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2168 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2169 | if (eventEntry.type == EventEntry::Type::FOCUS) { |
| 2170 | // Focus events are passed to apps, but do not represent user activity. |
| 2171 | return; |
| 2172 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2173 | int32_t displayId = getTargetDisplayId(eventEntry); |
| 2174 | sp<InputWindowHandle> focusedWindowHandle = |
| 2175 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 2176 | if (focusedWindowHandle != nullptr) { |
| 2177 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2178 | if (info->inputFeatures & InputWindowInfo::INPUT_FEATURE_DISABLE_USER_ACTIVITY) { |
| 2179 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2180 | 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] | 2181 | #endif |
| 2182 | return; |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2187 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2188 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2189 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2190 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2191 | return; |
| 2192 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2193 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2194 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2195 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2196 | } |
| 2197 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2198 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2199 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2200 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2201 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2202 | return; |
| 2203 | } |
| 2204 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2205 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2206 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2207 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2208 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2209 | case EventEntry::Type::DEVICE_RESET: { |
| 2210 | LOG_ALWAYS_FATAL("%s events are not user activity", |
| 2211 | EventEntry::typeToString(eventEntry.type)); |
| 2212 | break; |
| 2213 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2214 | } |
| 2215 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2216 | std::unique_ptr<CommandEntry> commandEntry = |
| 2217 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2218 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2219 | commandEntry->userActivityEventType = eventType; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2220 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2224 | const sp<Connection>& connection, |
| 2225 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2226 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2227 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2228 | std::string message = |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2229 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2230 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2231 | ATRACE_NAME(message.c_str()); |
| 2232 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2233 | #if DEBUG_DISPATCH_CYCLE |
| 2234 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2235 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2236 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2237 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2238 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2239 | #endif |
| 2240 | |
| 2241 | // Skip this event if the connection status is not normal. |
| 2242 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2243 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2244 | #if DEBUG_DISPATCH_CYCLE |
| 2245 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2246 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2247 | #endif |
| 2248 | return; |
| 2249 | } |
| 2250 | |
| 2251 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2252 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2253 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2254 | "Entry type %s should not have FLAG_SPLIT", |
| 2255 | EventEntry::typeToString(eventEntry->type)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2256 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2257 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2258 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2259 | MotionEntry* splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2260 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2261 | if (!splitMotionEntry) { |
| 2262 | return; // split event was dropped |
| 2263 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2264 | if (DEBUG_FOCUS) { |
| 2265 | ALOGD("channel '%s' ~ Split motion event.", |
| 2266 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2267 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2268 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2269 | enqueueDispatchEntriesLocked(currentTime, connection, splitMotionEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2270 | splitMotionEntry->release(); |
| 2271 | return; |
| 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2276 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2277 | } |
| 2278 | |
| 2279 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2280 | const sp<Connection>& connection, |
| 2281 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2282 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2283 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2284 | std::string message = |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2285 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2286 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2287 | ATRACE_NAME(message.c_str()); |
| 2288 | } |
| 2289 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2290 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2291 | |
| 2292 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2293 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2294 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2295 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2296 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2297 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2298 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2299 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2300 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2301 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2302 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2303 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2304 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2305 | |
| 2306 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2307 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2308 | startDispatchCycleLocked(currentTime, connection); |
| 2309 | } |
| 2310 | } |
| 2311 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2312 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
| 2313 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2314 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2315 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2316 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2317 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2318 | connection->getInputChannelName().c_str(), |
| 2319 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2320 | ATRACE_NAME(message.c_str()); |
| 2321 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2322 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2323 | if (!(inputTargetFlags & dispatchMode)) { |
| 2324 | return; |
| 2325 | } |
| 2326 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2327 | |
| 2328 | // This is a new event. |
| 2329 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2330 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2331 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2332 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2333 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2334 | // different EventEntry than what was passed in. |
| 2335 | EventEntry* newEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2336 | // Apply target flags and update the connection's input state. |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2337 | switch (newEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2338 | case EventEntry::Type::KEY: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2339 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*newEntry); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2340 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2341 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2342 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2343 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2344 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2345 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2346 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2347 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2348 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2349 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2350 | return; // skip the inconsistent event |
| 2351 | } |
| 2352 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2353 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2354 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2355 | case EventEntry::Type::MOTION: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2356 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*newEntry); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2357 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2358 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2359 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2360 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2361 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2362 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2363 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2364 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2365 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2366 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2367 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2368 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2369 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2370 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2371 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2372 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2373 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2374 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2375 | } |
| 2376 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2377 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2378 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2379 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2380 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2381 | "event", |
| 2382 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2383 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2384 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2385 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2386 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2387 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2388 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2389 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2390 | } |
| 2391 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2392 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2393 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2394 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2395 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2396 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2397 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2398 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2399 | "event", |
| 2400 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2401 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2402 | return; // skip the inconsistent event |
| 2403 | } |
| 2404 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2405 | dispatchEntry->resolvedEventId = |
| 2406 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2407 | ? mIdGenerator.nextId() |
| 2408 | : motionEntry.id; |
| 2409 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2410 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2411 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2412 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2413 | ATRACE_NAME(message.c_str()); |
| 2414 | } |
| 2415 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2416 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2417 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2418 | |
| 2419 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2420 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2421 | case EventEntry::Type::FOCUS: { |
| 2422 | break; |
| 2423 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2424 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2425 | case EventEntry::Type::DEVICE_RESET: { |
| 2426 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2427 | EventEntry::typeToString(newEntry->type)); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2428 | break; |
| 2429 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | // Remember that we are waiting for this dispatch to complete. |
| 2433 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2434 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2438 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2439 | traceOutboundQueueLength(connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2440 | } |
| 2441 | |
Siarhei Vishniakou | 7394eaa | 2020-04-09 11:16:18 -0700 | [diff] [blame] | 2442 | /** |
| 2443 | * This function is purely for debugging. It helps us understand where the user interaction |
| 2444 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 2445 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 2446 | * We will see both launcher and wallpaper in that list. |
| 2447 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 2448 | * until the set of interacted connections changes. |
| 2449 | * |
| 2450 | * The following items are skipped, to reduce the logspam: |
| 2451 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 2452 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 2453 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 2454 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 2455 | * Both of those ACTION_UP events would not be logged |
| 2456 | * Monitors (both gesture and global): any gesture monitors or global monitors receiving events |
| 2457 | * will not be logged. This is omitted to reduce the amount of data printed. |
| 2458 | * If you see <none>, it's likely that one of the gesture monitors pilfered the event, and therefore |
| 2459 | * gesture monitor is the only connection receiving the remainder of the gesture. |
| 2460 | */ |
| 2461 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 2462 | const std::vector<InputTarget>& targets) { |
| 2463 | // Skip ACTION_UP events, and all events other than keys and motions |
| 2464 | if (entry.type == EventEntry::Type::KEY) { |
| 2465 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2466 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 2467 | return; |
| 2468 | } |
| 2469 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 2470 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2471 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP) { |
| 2472 | return; |
| 2473 | } |
| 2474 | } else { |
| 2475 | return; // Not a key or a motion |
| 2476 | } |
| 2477 | |
| 2478 | std::unordered_set<sp<IBinder>, IBinderHash> newConnections; |
| 2479 | for (const InputTarget& target : targets) { |
| 2480 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 2481 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2482 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 2483 | } |
| 2484 | |
| 2485 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 2486 | sp<Connection> connection = getConnectionLocked(token); // get connection |
| 2487 | if (connection->monitor) { |
| 2488 | continue; // We only need to keep track of the non-monitor connections. |
| 2489 | } |
| 2490 | |
| 2491 | newConnections.insert(std::move(token)); |
| 2492 | } |
| 2493 | if (newConnections == mInteractionConnections) { |
| 2494 | return; // no change |
| 2495 | } |
| 2496 | mInteractionConnections = newConnections; |
| 2497 | std::string windowList; |
| 2498 | for (const sp<IBinder>& token : newConnections) { |
| 2499 | sp<Connection> connection = getConnectionLocked(token); |
| 2500 | windowList += connection->getWindowName() + ", "; |
| 2501 | } |
| 2502 | std::string message = "Interaction with windows: " + windowList; |
| 2503 | if (windowList.empty()) { |
| 2504 | message += "<none>"; |
| 2505 | } |
| 2506 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 2507 | } |
| 2508 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2509 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2510 | const sp<IBinder>& newToken) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2511 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2512 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 2513 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2514 | return; |
| 2515 | } |
| 2516 | |
| 2517 | sp<InputWindowHandle> inputWindowHandle = getWindowHandleLocked(newToken); |
| 2518 | if (inputWindowHandle == nullptr) { |
| 2519 | return; |
| 2520 | } |
| 2521 | |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2522 | sp<InputWindowHandle> focusedWindowHandle = |
Tiger Huang | 0683fe7 | 2019-06-03 21:50:55 +0800 | [diff] [blame] | 2523 | getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2524 | |
| 2525 | bool hasFocusChanged = !focusedWindowHandle || focusedWindowHandle->getToken() != newToken; |
| 2526 | |
| 2527 | if (!hasFocusChanged) { |
| 2528 | return; |
| 2529 | } |
| 2530 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2531 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 2532 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2533 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2534 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2538 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2539 | if (ATRACE_ENABLED()) { |
| 2540 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2541 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2542 | ATRACE_NAME(message.c_str()); |
| 2543 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2544 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2545 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2546 | #endif |
| 2547 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2548 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 2549 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2550 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 2551 | const nsecs_t timeout = |
| 2552 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
| 2553 | dispatchEntry->timeoutTime = currentTime + timeout; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2554 | |
| 2555 | // Publish the event. |
| 2556 | status_t status; |
| 2557 | EventEntry* eventEntry = dispatchEntry->eventEntry; |
| 2558 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2559 | case EventEntry::Type::KEY: { |
Edgar Arriaga | 3d61bc1 | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2560 | const KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
| 2561 | std::array<uint8_t, 32> hmac = getSignature(*keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2562 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2563 | // Publish the key event. |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2564 | status = |
| 2565 | connection->inputPublisher |
| 2566 | .publishKeyEvent(dispatchEntry->seq, dispatchEntry->resolvedEventId, |
| 2567 | keyEntry->deviceId, keyEntry->source, |
| 2568 | keyEntry->displayId, std::move(hmac), |
| 2569 | dispatchEntry->resolvedAction, |
| 2570 | dispatchEntry->resolvedFlags, keyEntry->keyCode, |
| 2571 | keyEntry->scanCode, keyEntry->metaState, |
| 2572 | keyEntry->repeatCount, keyEntry->downTime, |
| 2573 | keyEntry->eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2574 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2575 | } |
| 2576 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2577 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2578 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2579 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2580 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 2581 | const PointerCoords* usingCoords = motionEntry->pointerCoords; |
| 2582 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2583 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 2584 | float xOffset = 0.0f, yOffset = 0.0f; |
| 2585 | float xScale = 1.0f, yScale = 1.0f; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2586 | if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) && |
| 2587 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 2588 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2589 | xScale = dispatchEntry->windowXScale; |
| 2590 | yScale = dispatchEntry->windowYScale; |
| 2591 | xOffset = dispatchEntry->xOffset * xScale; |
| 2592 | yOffset = dispatchEntry->yOffset * yScale; |
| 2593 | if (globalScaleFactor != 1.0f) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2594 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2595 | scaledCoords[i] = motionEntry->pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2596 | // Don't apply window scale here since we don't want scale to affect raw |
| 2597 | // coordinates. The scale will be sent back to the client and applied |
| 2598 | // later when requesting relative coordinates. |
| 2599 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 2600 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2601 | } |
| 2602 | usingCoords = scaledCoords; |
| 2603 | } |
| 2604 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2605 | // We don't want the dispatch target to know. |
| 2606 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
| 2607 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2608 | scaledCoords[i].clear(); |
| 2609 | } |
| 2610 | usingCoords = scaledCoords; |
| 2611 | } |
| 2612 | } |
Edgar Arriaga | 3d61bc1 | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2613 | |
| 2614 | std::array<uint8_t, 32> hmac = getSignature(*motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2615 | |
| 2616 | // Publish the motion event. |
| 2617 | status = connection->inputPublisher |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2618 | .publishMotionEvent(dispatchEntry->seq, |
| 2619 | dispatchEntry->resolvedEventId, |
| 2620 | motionEntry->deviceId, motionEntry->source, |
| 2621 | motionEntry->displayId, std::move(hmac), |
| 2622 | dispatchEntry->resolvedAction, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2623 | motionEntry->actionButton, |
| 2624 | dispatchEntry->resolvedFlags, |
| 2625 | motionEntry->edgeFlags, motionEntry->metaState, |
| 2626 | motionEntry->buttonState, |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2627 | motionEntry->classification, xScale, yScale, |
| 2628 | xOffset, yOffset, motionEntry->xPrecision, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2629 | motionEntry->yPrecision, |
| 2630 | motionEntry->xCursorPosition, |
| 2631 | motionEntry->yCursorPosition, |
| 2632 | motionEntry->downTime, motionEntry->eventTime, |
| 2633 | motionEntry->pointerCount, |
| 2634 | motionEntry->pointerProperties, usingCoords); |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 2635 | reportTouchEventForStatistics(*motionEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2636 | break; |
| 2637 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2638 | case EventEntry::Type::FOCUS: { |
| 2639 | FocusEntry* focusEntry = static_cast<FocusEntry*>(eventEntry); |
| 2640 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2641 | focusEntry->id, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2642 | focusEntry->hasFocus, |
| 2643 | mInTouchMode); |
| 2644 | break; |
| 2645 | } |
| 2646 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2647 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2648 | case EventEntry::Type::DEVICE_RESET: { |
| 2649 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
| 2650 | EventEntry::typeToString(eventEntry->type)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2651 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2652 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | // Check the result. |
| 2656 | if (status) { |
| 2657 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2658 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2659 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2660 | "This is unexpected because the wait queue is empty, so the pipe " |
| 2661 | "should be empty and we shouldn't have any problems writing an " |
| 2662 | "event to it, status=%d", |
| 2663 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2664 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2665 | } else { |
| 2666 | // Pipe is full and we are waiting for the app to finish process some events |
| 2667 | // before sending more events to it. |
| 2668 | #if DEBUG_DISPATCH_CYCLE |
| 2669 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2670 | "waiting for the application to catch up", |
| 2671 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2672 | #endif |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2673 | } |
| 2674 | } else { |
| 2675 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2676 | "status=%d", |
| 2677 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2678 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2679 | } |
| 2680 | return; |
| 2681 | } |
| 2682 | |
| 2683 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2684 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 2685 | connection->outboundQueue.end(), |
| 2686 | dispatchEntry)); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2687 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2688 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 2689 | if (connection->responsive) { |
| 2690 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 2691 | connection->inputChannel->getConnectionToken()); |
| 2692 | } |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2693 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2694 | } |
| 2695 | } |
| 2696 | |
Edgar Arriaga | 3d61bc1 | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2697 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 2698 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 2699 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 2700 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 2701 | // Only sign events up and down events as the purely move events |
| 2702 | // are tied to their up/down counterparts so signing would be redundant. |
| 2703 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 2704 | verifiedEvent.actionMasked = actionMasked; |
| 2705 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 2706 | return mHmacKeyManager.sign(verifiedEvent); |
| 2707 | } |
| 2708 | return INVALID_HMAC; |
| 2709 | } |
| 2710 | |
| 2711 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 2712 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 2713 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 2714 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 2715 | verifiedEvent.action = dispatchEntry.resolvedAction; |
| 2716 | return mHmacKeyManager.sign(verifiedEvent); |
| 2717 | } |
| 2718 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2719 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2720 | const sp<Connection>& connection, uint32_t seq, |
| 2721 | bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2722 | #if DEBUG_DISPATCH_CYCLE |
| 2723 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2724 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2725 | #endif |
| 2726 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2727 | if (connection->status == Connection::STATUS_BROKEN || |
| 2728 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2729 | return; |
| 2730 | } |
| 2731 | |
| 2732 | // Notify other system components and prepare to start the next dispatch cycle. |
| 2733 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); |
| 2734 | } |
| 2735 | |
| 2736 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2737 | const sp<Connection>& connection, |
| 2738 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2739 | #if DEBUG_DISPATCH_CYCLE |
| 2740 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2741 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2742 | #endif |
| 2743 | |
| 2744 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2745 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2746 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2747 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2748 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2749 | |
| 2750 | // The connection appears to be unrecoverably broken. |
| 2751 | // Ignore already broken or zombie connections. |
| 2752 | if (connection->status == Connection::STATUS_NORMAL) { |
| 2753 | connection->status = Connection::STATUS_BROKEN; |
| 2754 | |
| 2755 | if (notify) { |
| 2756 | // Notify other system components. |
| 2757 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 2758 | } |
| 2759 | } |
| 2760 | } |
| 2761 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2762 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 2763 | while (!queue.empty()) { |
| 2764 | DispatchEntry* dispatchEntry = queue.front(); |
| 2765 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2766 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2767 | } |
| 2768 | } |
| 2769 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2770 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2771 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2772 | decrementPendingForegroundDispatches(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2773 | } |
| 2774 | delete dispatchEntry; |
| 2775 | } |
| 2776 | |
| 2777 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { |
| 2778 | InputDispatcher* d = static_cast<InputDispatcher*>(data); |
| 2779 | |
| 2780 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 2781 | std::scoped_lock _l(d->mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2782 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2783 | if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2784 | ALOGE("Received spurious receive callback for unknown input channel. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2785 | "fd=%d, events=0x%x", |
| 2786 | fd, events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2787 | return 0; // remove the callback |
| 2788 | } |
| 2789 | |
| 2790 | bool notify; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2791 | sp<Connection> connection = d->mConnectionsByFd[fd]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2792 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 2793 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 2794 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2795 | "events=0x%x", |
| 2796 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2797 | return 1; |
| 2798 | } |
| 2799 | |
| 2800 | nsecs_t currentTime = now(); |
| 2801 | bool gotOne = false; |
| 2802 | status_t status; |
| 2803 | for (;;) { |
| 2804 | uint32_t seq; |
| 2805 | bool handled; |
| 2806 | status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); |
| 2807 | if (status) { |
| 2808 | break; |
| 2809 | } |
| 2810 | d->finishDispatchCycleLocked(currentTime, connection, seq, handled); |
| 2811 | gotOne = true; |
| 2812 | } |
| 2813 | if (gotOne) { |
| 2814 | d->runCommandsLockedInterruptible(); |
| 2815 | if (status == WOULD_BLOCK) { |
| 2816 | return 1; |
| 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 2821 | if (notify) { |
| 2822 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2823 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2824 | } |
| 2825 | } else { |
| 2826 | // Monitor channels are never explicitly unregistered. |
| 2827 | // We do it automatically when the remote endpoint is closed so don't warn |
| 2828 | // about them. |
arthurhung | d352cb3 | 2020-04-28 17:09:28 +0800 | [diff] [blame] | 2829 | const bool stillHaveWindowHandle = |
| 2830 | d->getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != |
| 2831 | nullptr; |
| 2832 | notify = !connection->monitor && stillHaveWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2833 | if (notify) { |
| 2834 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2835 | "events=0x%x", |
| 2836 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | |
| 2840 | // Unregister the channel. |
| 2841 | d->unregisterInputChannelLocked(connection->inputChannel, notify); |
| 2842 | return 0; // remove the callback |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2843 | } // release lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2844 | } |
| 2845 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2846 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2847 | const CancelationOptions& options) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2848 | for (const auto& pair : mConnectionsByFd) { |
| 2849 | synthesizeCancelationEventsForConnectionLocked(pair.second, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2850 | } |
| 2851 | } |
| 2852 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2853 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2854 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2855 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 2856 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 2857 | } |
| 2858 | |
| 2859 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 2860 | const CancelationOptions& options, |
| 2861 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 2862 | for (const auto& it : monitorsByDisplay) { |
| 2863 | const std::vector<Monitor>& monitors = it.second; |
| 2864 | for (const Monitor& monitor : monitors) { |
| 2865 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2866 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2867 | } |
| 2868 | } |
| 2869 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2870 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
| 2871 | const sp<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 2872 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2873 | if (connection == nullptr) { |
| 2874 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2875 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2876 | |
| 2877 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2878 | } |
| 2879 | |
| 2880 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 2881 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 2882 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2883 | return; |
| 2884 | } |
| 2885 | |
| 2886 | nsecs_t currentTime = now(); |
| 2887 | |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 2888 | std::vector<EventEntry*> cancelationEvents = |
| 2889 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2890 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2891 | if (cancelationEvents.empty()) { |
| 2892 | return; |
| 2893 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2894 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2895 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 2896 | "with reality: %s, mode=%d.", |
| 2897 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 2898 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2899 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2900 | |
| 2901 | InputTarget target; |
| 2902 | sp<InputWindowHandle> windowHandle = |
| 2903 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 2904 | if (windowHandle != nullptr) { |
| 2905 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2906 | target.setDefaultPointerInfo(-windowInfo->frameLeft, -windowInfo->frameTop, |
| 2907 | windowInfo->windowXScale, windowInfo->windowYScale); |
| 2908 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2909 | } |
| 2910 | target.inputChannel = connection->inputChannel; |
| 2911 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2912 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2913 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
| 2914 | EventEntry* cancelationEventEntry = cancelationEvents[i]; |
| 2915 | switch (cancelationEventEntry->type) { |
| 2916 | case EventEntry::Type::KEY: { |
| 2917 | logOutboundKeyDetails("cancel - ", |
| 2918 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 2919 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2920 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2921 | case EventEntry::Type::MOTION: { |
| 2922 | logOutboundMotionDetails("cancel - ", |
| 2923 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 2924 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2925 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2926 | case EventEntry::Type::FOCUS: { |
| 2927 | LOG_ALWAYS_FATAL("Canceling focus events is not supported"); |
| 2928 | break; |
| 2929 | } |
| 2930 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2931 | case EventEntry::Type::DEVICE_RESET: { |
| 2932 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 2933 | EventEntry::typeToString(cancelationEventEntry->type)); |
| 2934 | break; |
| 2935 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2936 | } |
| 2937 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2938 | enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref |
| 2939 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2940 | |
| 2941 | cancelationEventEntry->release(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2942 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2943 | |
| 2944 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2945 | } |
| 2946 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2947 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 2948 | const sp<Connection>& connection) { |
| 2949 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2950 | return; |
| 2951 | } |
| 2952 | |
| 2953 | nsecs_t currentTime = now(); |
| 2954 | |
| 2955 | std::vector<EventEntry*> downEvents = |
| 2956 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 2957 | |
| 2958 | if (downEvents.empty()) { |
| 2959 | return; |
| 2960 | } |
| 2961 | |
| 2962 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 2963 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 2964 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 2965 | #endif |
| 2966 | |
| 2967 | InputTarget target; |
| 2968 | sp<InputWindowHandle> windowHandle = |
| 2969 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 2970 | if (windowHandle != nullptr) { |
| 2971 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2972 | target.setDefaultPointerInfo(-windowInfo->frameLeft, -windowInfo->frameTop, |
| 2973 | windowInfo->windowXScale, windowInfo->windowYScale); |
| 2974 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2975 | } |
| 2976 | target.inputChannel = connection->inputChannel; |
| 2977 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2978 | |
| 2979 | for (EventEntry* downEventEntry : downEvents) { |
| 2980 | switch (downEventEntry->type) { |
| 2981 | case EventEntry::Type::MOTION: { |
| 2982 | logOutboundMotionDetails("down - ", |
| 2983 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 2984 | break; |
| 2985 | } |
| 2986 | |
| 2987 | case EventEntry::Type::KEY: |
| 2988 | case EventEntry::Type::FOCUS: |
| 2989 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2990 | case EventEntry::Type::DEVICE_RESET: { |
| 2991 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 2992 | EventEntry::typeToString(downEventEntry->type)); |
| 2993 | break; |
| 2994 | } |
| 2995 | } |
| 2996 | |
| 2997 | enqueueDispatchEntryLocked(connection, downEventEntry, // increments ref |
| 2998 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2999 | |
| 3000 | downEventEntry->release(); |
| 3001 | } |
| 3002 | |
| 3003 | startDispatchCycleLocked(currentTime, connection); |
| 3004 | } |
| 3005 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3006 | MotionEntry* InputDispatcher::splitMotionEvent(const MotionEntry& originalMotionEntry, |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 3007 | BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3008 | ALOG_ASSERT(pointerIds.value != 0); |
| 3009 | |
| 3010 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3011 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3012 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3013 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3014 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3015 | uint32_t splitPointerCount = 0; |
| 3016 | |
| 3017 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3018 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3019 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3020 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3021 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3022 | if (pointerIds.hasBit(pointerId)) { |
| 3023 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3024 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3025 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3026 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3027 | splitPointerCount += 1; |
| 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | if (splitPointerCount != pointerIds.count()) { |
| 3032 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3033 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3034 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3035 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3036 | // in this way. |
| 3037 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3038 | "we expected there to be %d pointers. This probably means we received " |
| 3039 | "a broken sequence of pointer ids from the input device.", |
| 3040 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3041 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3042 | } |
| 3043 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3044 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3045 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3046 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3047 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3048 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3049 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3050 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3051 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3052 | if (pointerIds.hasBit(pointerId)) { |
| 3053 | if (pointerIds.count() == 1) { |
| 3054 | // The first/last pointer went down/up. |
| 3055 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3056 | ? AMOTION_EVENT_ACTION_DOWN |
| 3057 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3058 | } else { |
| 3059 | // A secondary pointer went down/up. |
| 3060 | uint32_t splitPointerIndex = 0; |
| 3061 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3062 | splitPointerIndex += 1; |
| 3063 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3064 | action = maskedAction | |
| 3065 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3066 | } |
| 3067 | } else { |
| 3068 | // An unrelated pointer changed. |
| 3069 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3070 | } |
| 3071 | } |
| 3072 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3073 | int32_t newId = mIdGenerator.nextId(); |
| 3074 | if (ATRACE_ENABLED()) { |
| 3075 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3076 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3077 | originalMotionEntry.id, newId); |
| 3078 | ATRACE_NAME(message.c_str()); |
| 3079 | } |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3080 | MotionEntry* splitMotionEntry = |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3081 | new MotionEntry(newId, originalMotionEntry.eventTime, originalMotionEntry.deviceId, |
| 3082 | originalMotionEntry.source, originalMotionEntry.displayId, |
| 3083 | originalMotionEntry.policyFlags, action, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3084 | originalMotionEntry.actionButton, originalMotionEntry.flags, |
| 3085 | originalMotionEntry.metaState, originalMotionEntry.buttonState, |
| 3086 | originalMotionEntry.classification, originalMotionEntry.edgeFlags, |
| 3087 | originalMotionEntry.xPrecision, originalMotionEntry.yPrecision, |
| 3088 | originalMotionEntry.xCursorPosition, |
| 3089 | originalMotionEntry.yCursorPosition, originalMotionEntry.downTime, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3090 | splitPointerCount, splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3091 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3092 | if (originalMotionEntry.injectionState) { |
| 3093 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3094 | splitMotionEntry->injectionState->refCount += 1; |
| 3095 | } |
| 3096 | |
| 3097 | return splitMotionEntry; |
| 3098 | } |
| 3099 | |
| 3100 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 3101 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3102 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3103 | #endif |
| 3104 | |
| 3105 | bool needWake; |
| 3106 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3107 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3108 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3109 | ConfigurationChangedEntry* newEntry = |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3110 | new ConfigurationChangedEntry(args->id, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3111 | needWake = enqueueInboundEventLocked(newEntry); |
| 3112 | } // release lock |
| 3113 | |
| 3114 | if (needWake) { |
| 3115 | mLooper->wake(); |
| 3116 | } |
| 3117 | } |
| 3118 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3119 | /** |
| 3120 | * If one of the meta shortcuts is detected, process them here: |
| 3121 | * Meta + Backspace -> generate BACK |
| 3122 | * Meta + Enter -> generate HOME |
| 3123 | * This will potentially overwrite keyCode and metaState. |
| 3124 | */ |
| 3125 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3126 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3127 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3128 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3129 | if (keyCode == AKEYCODE_DEL) { |
| 3130 | newKeyCode = AKEYCODE_BACK; |
| 3131 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3132 | newKeyCode = AKEYCODE_HOME; |
| 3133 | } |
| 3134 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3135 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3136 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3137 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3138 | keyCode = newKeyCode; |
| 3139 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3140 | } |
| 3141 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3142 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3143 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3144 | // 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] | 3145 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3146 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3147 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3148 | if (replacementIt != mReplacedKeys.end()) { |
| 3149 | keyCode = replacementIt->second; |
| 3150 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3151 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3152 | } |
| 3153 | } |
| 3154 | } |
| 3155 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3156 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3157 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3158 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3159 | "policyFlags=0x%x, action=0x%x, " |
| 3160 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3161 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3162 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3163 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3164 | #endif |
| 3165 | if (!validateKeyEvent(args->action)) { |
| 3166 | return; |
| 3167 | } |
| 3168 | |
| 3169 | uint32_t policyFlags = args->policyFlags; |
| 3170 | int32_t flags = args->flags; |
| 3171 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3172 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3173 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3174 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3175 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3176 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3177 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3178 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3179 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3180 | metaState |= AMETA_FUNCTION_ON; |
| 3181 | } |
| 3182 | |
| 3183 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3184 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3185 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3186 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3187 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3188 | KeyEvent event; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3189 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3190 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3191 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3192 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3193 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3194 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3195 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3196 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3197 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3198 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3199 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3200 | bool needWake; |
| 3201 | { // acquire lock |
| 3202 | mLock.lock(); |
| 3203 | |
| 3204 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3205 | mLock.unlock(); |
| 3206 | |
| 3207 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3208 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3209 | return; // event was consumed by the filter |
| 3210 | } |
| 3211 | |
| 3212 | mLock.lock(); |
| 3213 | } |
| 3214 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3215 | KeyEntry* newEntry = |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3216 | new KeyEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3217 | args->displayId, policyFlags, args->action, flags, keyCode, |
| 3218 | args->scanCode, metaState, repeatCount, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3219 | |
| 3220 | needWake = enqueueInboundEventLocked(newEntry); |
| 3221 | mLock.unlock(); |
| 3222 | } // release lock |
| 3223 | |
| 3224 | if (needWake) { |
| 3225 | mLooper->wake(); |
| 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3230 | return mInputFilterEnabled; |
| 3231 | } |
| 3232 | |
| 3233 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3234 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3235 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3236 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3237 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3238 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3239 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3240 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3241 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3242 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3243 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3244 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3245 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3246 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3247 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3248 | "orientation=%f", |
| 3249 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3250 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3251 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3252 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3253 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3254 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3255 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3256 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3257 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3258 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3259 | } |
| 3260 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3261 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3262 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3263 | return; |
| 3264 | } |
| 3265 | |
| 3266 | uint32_t policyFlags = args->policyFlags; |
| 3267 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3268 | |
| 3269 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3270 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3271 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3272 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3273 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3274 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3275 | |
| 3276 | bool needWake; |
| 3277 | { // acquire lock |
| 3278 | mLock.lock(); |
| 3279 | |
| 3280 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3281 | mLock.unlock(); |
| 3282 | |
| 3283 | MotionEvent event; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3284 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3285 | args->action, args->actionButton, args->flags, args->edgeFlags, |
| 3286 | args->metaState, args->buttonState, args->classification, 1 /*xScale*/, |
| 3287 | 1 /*yScale*/, 0 /* xOffset */, 0 /* yOffset */, args->xPrecision, |
| 3288 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 3289 | args->downTime, args->eventTime, args->pointerCount, |
| 3290 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3291 | |
| 3292 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3293 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3294 | return; // event was consumed by the filter |
| 3295 | } |
| 3296 | |
| 3297 | mLock.lock(); |
| 3298 | } |
| 3299 | |
| 3300 | // Just enqueue a new motion event. |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3301 | MotionEntry* newEntry = |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3302 | new MotionEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3303 | args->displayId, policyFlags, args->action, args->actionButton, |
| 3304 | args->flags, args->metaState, args->buttonState, |
| 3305 | args->classification, args->edgeFlags, args->xPrecision, |
| 3306 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 3307 | args->downTime, args->pointerCount, args->pointerProperties, |
| 3308 | args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3309 | |
| 3310 | needWake = enqueueInboundEventLocked(newEntry); |
| 3311 | mLock.unlock(); |
| 3312 | } // release lock |
| 3313 | |
| 3314 | if (needWake) { |
| 3315 | mLooper->wake(); |
| 3316 | } |
| 3317 | } |
| 3318 | |
| 3319 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3320 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3324 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3325 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3326 | "switchMask=0x%08x", |
| 3327 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3328 | #endif |
| 3329 | |
| 3330 | uint32_t policyFlags = args->policyFlags; |
| 3331 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3332 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3336 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3337 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3338 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3339 | #endif |
| 3340 | |
| 3341 | bool needWake; |
| 3342 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3343 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3344 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3345 | DeviceResetEntry* newEntry = |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3346 | new DeviceResetEntry(args->id, args->eventTime, args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3347 | needWake = enqueueInboundEventLocked(newEntry); |
| 3348 | } // release lock |
| 3349 | |
| 3350 | if (needWake) { |
| 3351 | mLooper->wake(); |
| 3352 | } |
| 3353 | } |
| 3354 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3355 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, int32_t injectorPid, |
| 3356 | int32_t injectorUid, int32_t syncMode, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3357 | std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3358 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3359 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3360 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 3361 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3362 | #endif |
| 3363 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3364 | nsecs_t endTime = now() + std::chrono::duration_cast<std::chrono::nanoseconds>(timeout).count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3365 | |
| 3366 | policyFlags |= POLICY_FLAG_INJECTED; |
| 3367 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 3368 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3369 | } |
| 3370 | |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3371 | std::queue<EventEntry*> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3372 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3373 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3374 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 3375 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3376 | if (!validateKeyEvent(action)) { |
| 3377 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3378 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3379 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3380 | int32_t flags = incomingKey.getFlags(); |
| 3381 | int32_t keyCode = incomingKey.getKeyCode(); |
| 3382 | int32_t metaState = incomingKey.getMetaState(); |
| 3383 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3384 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3385 | KeyEvent keyEvent; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3386 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3387 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 3388 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 3389 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3390 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3391 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 3392 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3393 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3394 | |
| 3395 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3396 | android::base::Timer t; |
| 3397 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 3398 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3399 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 3400 | std::to_string(t.duration().count()).c_str()); |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | mLock.lock(); |
| 3405 | KeyEntry* injectedEntry = |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3406 | new KeyEntry(incomingKey.getId(), incomingKey.getEventTime(), |
| 3407 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
arthurhung | b1462ec | 2020-04-20 17:18:37 +0800 | [diff] [blame] | 3408 | incomingKey.getDisplayId(), policyFlags, action, flags, keyCode, |
| 3409 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3410 | incomingKey.getDownTime()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3411 | injectedEntries.push(injectedEntry); |
| 3412 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3413 | } |
| 3414 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3415 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3416 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
| 3417 | int32_t action = motionEvent->getAction(); |
| 3418 | size_t pointerCount = motionEvent->getPointerCount(); |
| 3419 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
| 3420 | int32_t actionButton = motionEvent->getActionButton(); |
| 3421 | int32_t displayId = motionEvent->getDisplayId(); |
| 3422 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
| 3423 | return INPUT_EVENT_INJECTION_FAILED; |
| 3424 | } |
| 3425 | |
| 3426 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3427 | nsecs_t eventTime = motionEvent->getEventTime(); |
| 3428 | android::base::Timer t; |
| 3429 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 3430 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3431 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 3432 | std::to_string(t.duration().count()).c_str()); |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | mLock.lock(); |
| 3437 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 3438 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
| 3439 | MotionEntry* injectedEntry = |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3440 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, VIRTUAL_KEYBOARD_ID, |
| 3441 | motionEvent->getSource(), motionEvent->getDisplayId(), |
| 3442 | policyFlags, action, actionButton, motionEvent->getFlags(), |
| 3443 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3444 | motionEvent->getClassification(), motionEvent->getEdgeFlags(), |
| 3445 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3446 | motionEvent->getRawXCursorPosition(), |
| 3447 | motionEvent->getRawYCursorPosition(), |
| 3448 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3449 | pointerProperties, samplePointerCoords, |
| 3450 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3451 | injectedEntries.push(injectedEntry); |
| 3452 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 3453 | sampleEventTimes += 1; |
| 3454 | samplePointerCoords += pointerCount; |
| 3455 | MotionEntry* nextInjectedEntry = |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3456 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3457 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3458 | motionEvent->getDisplayId(), policyFlags, action, |
| 3459 | actionButton, motionEvent->getFlags(), |
| 3460 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3461 | motionEvent->getClassification(), |
| 3462 | motionEvent->getEdgeFlags(), motionEvent->getXPrecision(), |
| 3463 | motionEvent->getYPrecision(), |
| 3464 | motionEvent->getRawXCursorPosition(), |
| 3465 | motionEvent->getRawYCursorPosition(), |
| 3466 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3467 | pointerProperties, samplePointerCoords, |
| 3468 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
| 3469 | injectedEntries.push(nextInjectedEntry); |
| 3470 | } |
| 3471 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3472 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3473 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3474 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 3475 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3476 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
| 3480 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3481 | injectionState->injectionIsAsync = true; |
| 3482 | } |
| 3483 | |
| 3484 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3485 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3486 | |
| 3487 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3488 | while (!injectedEntries.empty()) { |
| 3489 | needWake |= enqueueInboundEventLocked(injectedEntries.front()); |
| 3490 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | mLock.unlock(); |
| 3494 | |
| 3495 | if (needWake) { |
| 3496 | mLooper->wake(); |
| 3497 | } |
| 3498 | |
| 3499 | int32_t injectionResult; |
| 3500 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3501 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3502 | |
| 3503 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3504 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 3505 | } else { |
| 3506 | for (;;) { |
| 3507 | injectionResult = injectionState->injectionResult; |
| 3508 | if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { |
| 3509 | break; |
| 3510 | } |
| 3511 | |
| 3512 | nsecs_t remainingTimeout = endTime - now(); |
| 3513 | if (remainingTimeout <= 0) { |
| 3514 | #if DEBUG_INJECTION |
| 3515 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3516 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3517 | #endif |
| 3518 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3519 | break; |
| 3520 | } |
| 3521 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3522 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3523 | } |
| 3524 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3525 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED && |
| 3526 | syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3527 | while (injectionState->pendingForegroundDispatches != 0) { |
| 3528 | #if DEBUG_INJECTION |
| 3529 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3530 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3531 | #endif |
| 3532 | nsecs_t remainingTimeout = endTime - now(); |
| 3533 | if (remainingTimeout <= 0) { |
| 3534 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3535 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 3536 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3537 | #endif |
| 3538 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3539 | break; |
| 3540 | } |
| 3541 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3542 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3543 | } |
| 3544 | } |
| 3545 | } |
| 3546 | |
| 3547 | injectionState->release(); |
| 3548 | } // release lock |
| 3549 | |
| 3550 | #if DEBUG_INJECTION |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3551 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3552 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3553 | #endif |
| 3554 | |
| 3555 | return injectionResult; |
| 3556 | } |
| 3557 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3558 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3559 | std::array<uint8_t, 32> calculatedHmac; |
| 3560 | std::unique_ptr<VerifiedInputEvent> result; |
| 3561 | switch (event.getType()) { |
| 3562 | case AINPUT_EVENT_TYPE_KEY: { |
| 3563 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 3564 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 3565 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
| 3566 | calculatedHmac = mHmacKeyManager.sign(verifiedKeyEvent); |
| 3567 | break; |
| 3568 | } |
| 3569 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3570 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 3571 | VerifiedMotionEvent verifiedMotionEvent = |
| 3572 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 3573 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
| 3574 | calculatedHmac = mHmacKeyManager.sign(verifiedMotionEvent); |
| 3575 | break; |
| 3576 | } |
| 3577 | default: { |
| 3578 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 3579 | return nullptr; |
| 3580 | } |
| 3581 | } |
| 3582 | if (calculatedHmac == INVALID_HMAC) { |
| 3583 | return nullptr; |
| 3584 | } |
| 3585 | if (calculatedHmac != event.getHmac()) { |
| 3586 | return nullptr; |
| 3587 | } |
| 3588 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3589 | } |
| 3590 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3591 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3592 | return injectorUid == 0 || |
| 3593 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3594 | } |
| 3595 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3596 | void InputDispatcher::setInjectionResult(EventEntry* entry, int32_t injectionResult) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3597 | InjectionState* injectionState = entry->injectionState; |
| 3598 | if (injectionState) { |
| 3599 | #if DEBUG_INJECTION |
| 3600 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3601 | "injectorPid=%d, injectorUid=%d", |
| 3602 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3603 | #endif |
| 3604 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3605 | if (injectionState->injectionIsAsync && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3606 | // Log the outcome since the injector did not wait for the injection result. |
| 3607 | switch (injectionResult) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3608 | case INPUT_EVENT_INJECTION_SUCCEEDED: |
| 3609 | ALOGV("Asynchronous input event injection succeeded."); |
| 3610 | break; |
| 3611 | case INPUT_EVENT_INJECTION_FAILED: |
| 3612 | ALOGW("Asynchronous input event injection failed."); |
| 3613 | break; |
| 3614 | case INPUT_EVENT_INJECTION_PERMISSION_DENIED: |
| 3615 | ALOGW("Asynchronous input event injection permission denied."); |
| 3616 | break; |
| 3617 | case INPUT_EVENT_INJECTION_TIMED_OUT: |
| 3618 | ALOGW("Asynchronous input event injection timed out."); |
| 3619 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3620 | } |
| 3621 | } |
| 3622 | |
| 3623 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3624 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3625 | } |
| 3626 | } |
| 3627 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3628 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3629 | InjectionState* injectionState = entry->injectionState; |
| 3630 | if (injectionState) { |
| 3631 | injectionState->pendingForegroundDispatches += 1; |
| 3632 | } |
| 3633 | } |
| 3634 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3635 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3636 | InjectionState* injectionState = entry->injectionState; |
| 3637 | if (injectionState) { |
| 3638 | injectionState->pendingForegroundDispatches -= 1; |
| 3639 | |
| 3640 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3641 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3642 | } |
| 3643 | } |
| 3644 | } |
| 3645 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3646 | std::vector<sp<InputWindowHandle>> InputDispatcher::getWindowHandlesLocked( |
| 3647 | int32_t displayId) const { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3648 | return getValueByKey(mWindowHandlesByDisplay, displayId); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3649 | } |
| 3650 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3651 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3652 | const sp<IBinder>& windowHandleToken) const { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3653 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3654 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
| 3655 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3656 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3657 | return windowHandle; |
| 3658 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3659 | } |
| 3660 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3661 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3662 | } |
| 3663 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3664 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3665 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3666 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
| 3667 | for (const sp<InputWindowHandle>& handle : windowHandles) { |
| 3668 | if (handle->getToken() == windowHandle->getToken()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3669 | if (windowHandle->getInfo()->displayId != it.first) { |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 3670 | ALOGE("Found window %s in display %" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3671 | ", but it should belong to display %" PRId32, |
| 3672 | windowHandle->getName().c_str(), it.first, |
| 3673 | windowHandle->getInfo()->displayId); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3674 | } |
| 3675 | return true; |
| 3676 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3677 | } |
| 3678 | } |
| 3679 | return false; |
| 3680 | } |
| 3681 | |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3682 | sp<InputChannel> InputDispatcher::getInputChannelLocked(const sp<IBinder>& token) const { |
| 3683 | size_t count = mInputChannelsByToken.count(token); |
| 3684 | if (count == 0) { |
| 3685 | return nullptr; |
| 3686 | } |
| 3687 | return mInputChannelsByToken.at(token); |
| 3688 | } |
| 3689 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3690 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
| 3691 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
| 3692 | if (inputWindowHandles.empty()) { |
| 3693 | // Remove all handles on a display if there are no windows left. |
| 3694 | mWindowHandlesByDisplay.erase(displayId); |
| 3695 | return; |
| 3696 | } |
| 3697 | |
| 3698 | // Since we compare the pointer of input window handles across window updates, we need |
| 3699 | // to make sure the handle object for the same window stays unchanged across updates. |
| 3700 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3701 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3702 | for (const sp<InputWindowHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3703 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | std::vector<sp<InputWindowHandle>> newHandles; |
| 3707 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { |
| 3708 | if (!handle->updateInfo()) { |
| 3709 | // handle no longer valid |
| 3710 | continue; |
| 3711 | } |
| 3712 | |
| 3713 | const InputWindowInfo* info = handle->getInfo(); |
| 3714 | if ((getInputChannelLocked(handle->getToken()) == nullptr && |
| 3715 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { |
| 3716 | const bool noInputChannel = |
| 3717 | info->inputFeatures & InputWindowInfo::INPUT_FEATURE_NO_INPUT_CHANNEL; |
| 3718 | const bool canReceiveInput = |
| 3719 | !(info->layoutParamsFlags & InputWindowInfo::FLAG_NOT_TOUCHABLE) || |
| 3720 | !(info->layoutParamsFlags & InputWindowInfo::FLAG_NOT_FOCUSABLE); |
| 3721 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 3722 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3723 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 3724 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3725 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | if (info->displayId != displayId) { |
| 3729 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 3730 | handle->getName().c_str(), displayId, info->displayId); |
| 3731 | continue; |
| 3732 | } |
| 3733 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 3734 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 3735 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3736 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3737 | oldHandle->updateFrom(handle); |
| 3738 | newHandles.push_back(oldHandle); |
| 3739 | } else { |
| 3740 | newHandles.push_back(handle); |
| 3741 | } |
| 3742 | } |
| 3743 | |
| 3744 | // Insert or replace |
| 3745 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 3746 | } |
| 3747 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3748 | void InputDispatcher::setInputWindows( |
| 3749 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { |
| 3750 | { // acquire lock |
| 3751 | std::scoped_lock _l(mLock); |
| 3752 | for (auto const& i : handlesPerDisplay) { |
| 3753 | setInputWindowsLocked(i.second, i.first); |
| 3754 | } |
| 3755 | } |
| 3756 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3757 | mLooper->wake(); |
| 3758 | } |
| 3759 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3760 | /** |
| 3761 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 3762 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 3763 | * If set an empty list, remove all handles from the specific display. |
| 3764 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 3765 | * For removed handle, check if need to send a cancel event if already in touch. |
| 3766 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3767 | void InputDispatcher::setInputWindowsLocked( |
| 3768 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3769 | if (DEBUG_FOCUS) { |
| 3770 | std::string windowList; |
| 3771 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { |
| 3772 | windowList += iwh->getName() + " "; |
| 3773 | } |
| 3774 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 3775 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3776 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3777 | // Copy old handles for release if they are no longer present. |
| 3778 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3779 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3780 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3781 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3782 | sp<InputWindowHandle> newFocusedWindowHandle = nullptr; |
| 3783 | bool foundHoveredWindow = false; |
| 3784 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
| 3785 | // Set newFocusedWindowHandle to the top most focused window instead of the last one |
| 3786 | if (!newFocusedWindowHandle && windowHandle->getInfo()->hasFocus && |
| 3787 | windowHandle->getInfo()->visible) { |
| 3788 | newFocusedWindowHandle = windowHandle; |
| 3789 | } |
| 3790 | if (windowHandle == mLastHoverWindowHandle) { |
| 3791 | foundHoveredWindow = true; |
| 3792 | } |
| 3793 | } |
| 3794 | |
| 3795 | if (!foundHoveredWindow) { |
| 3796 | mLastHoverWindowHandle = nullptr; |
| 3797 | } |
| 3798 | |
| 3799 | sp<InputWindowHandle> oldFocusedWindowHandle = |
| 3800 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 3801 | |
| 3802 | if (!haveSameToken(oldFocusedWindowHandle, newFocusedWindowHandle)) { |
| 3803 | if (oldFocusedWindowHandle != nullptr) { |
| 3804 | if (DEBUG_FOCUS) { |
| 3805 | ALOGD("Focus left window: %s in display %" PRId32, |
| 3806 | oldFocusedWindowHandle->getName().c_str(), displayId); |
Garfield Tan | bd0fbcd | 2018-11-30 12:45:03 -0800 | [diff] [blame] | 3807 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3808 | sp<InputChannel> focusedInputChannel = |
| 3809 | getInputChannelLocked(oldFocusedWindowHandle->getToken()); |
| 3810 | if (focusedInputChannel != nullptr) { |
| 3811 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 3812 | "focus left window"); |
| 3813 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
| 3814 | enqueueFocusEventLocked(*oldFocusedWindowHandle, false /*hasFocus*/); |
Garfield Tan | bd0fbcd | 2018-11-30 12:45:03 -0800 | [diff] [blame] | 3815 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3816 | mFocusedWindowHandlesByDisplay.erase(displayId); |
| 3817 | } |
| 3818 | if (newFocusedWindowHandle != nullptr) { |
| 3819 | if (DEBUG_FOCUS) { |
| 3820 | ALOGD("Focus entered window: %s in display %" PRId32, |
| 3821 | newFocusedWindowHandle->getName().c_str(), displayId); |
| 3822 | } |
| 3823 | mFocusedWindowHandlesByDisplay[displayId] = newFocusedWindowHandle; |
| 3824 | enqueueFocusEventLocked(*newFocusedWindowHandle, true /*hasFocus*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3825 | } |
| 3826 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3827 | if (mFocusedDisplayId == displayId) { |
| 3828 | onFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3829 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3830 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3831 | |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3832 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 3833 | mTouchStatesByDisplay.find(displayId); |
| 3834 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 3835 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3836 | for (size_t i = 0; i < state.windows.size();) { |
| 3837 | TouchedWindow& touchedWindow = state.windows[i]; |
| 3838 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3839 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3840 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 3841 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3842 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3843 | sp<InputChannel> touchedInputChannel = |
| 3844 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 3845 | if (touchedInputChannel != nullptr) { |
| 3846 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 3847 | "touched window was removed"); |
| 3848 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3849 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3850 | state.windows.erase(state.windows.begin() + i); |
| 3851 | } else { |
| 3852 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3853 | } |
| 3854 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3855 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3856 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3857 | // Release information for windows that are no longer present. |
| 3858 | // This ensures that unused input channels are released promptly. |
| 3859 | // Otherwise, they might stick around until the window handle is destroyed |
| 3860 | // which might not happen until the next GC. |
| 3861 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
| 3862 | if (!hasWindowHandleLocked(oldWindowHandle)) { |
| 3863 | if (DEBUG_FOCUS) { |
| 3864 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3865 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3866 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3867 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 3868 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | void InputDispatcher::setFocusedApplication( |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3872 | int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3873 | if (DEBUG_FOCUS) { |
| 3874 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 3875 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 3876 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3877 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3878 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3879 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3880 | sp<InputApplicationHandle> oldFocusedApplicationHandle = |
| 3881 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 3882 | |
| 3883 | if (oldFocusedApplicationHandle == mAwaitedFocusedApplication && |
| 3884 | inputApplicationHandle != oldFocusedApplicationHandle) { |
| 3885 | resetNoFocusedWindowTimeoutLocked(); |
| 3886 | } |
| 3887 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3888 | if (inputApplicationHandle != nullptr && inputApplicationHandle->updateInfo()) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3889 | if (oldFocusedApplicationHandle != inputApplicationHandle) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3890 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3891 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3892 | } else if (oldFocusedApplicationHandle != nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3893 | oldFocusedApplicationHandle.clear(); |
| 3894 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3895 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3896 | } // release lock |
| 3897 | |
| 3898 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3899 | mLooper->wake(); |
| 3900 | } |
| 3901 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3902 | /** |
| 3903 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 3904 | * the display not specified. |
| 3905 | * |
| 3906 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 3907 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 3908 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 3909 | * display. The display-specified events won't be affected. |
| 3910 | */ |
| 3911 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3912 | if (DEBUG_FOCUS) { |
| 3913 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 3914 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3915 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3916 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3917 | |
| 3918 | if (mFocusedDisplayId != displayId) { |
| 3919 | sp<InputWindowHandle> oldFocusedWindowHandle = |
| 3920 | getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); |
| 3921 | if (oldFocusedWindowHandle != nullptr) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3922 | sp<InputChannel> inputChannel = |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3923 | getInputChannelLocked(oldFocusedWindowHandle->getToken()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3924 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3925 | CancelationOptions |
| 3926 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 3927 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3928 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3929 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 3930 | } |
| 3931 | } |
| 3932 | mFocusedDisplayId = displayId; |
| 3933 | |
| 3934 | // Sanity check |
| 3935 | sp<InputWindowHandle> newFocusedWindowHandle = |
| 3936 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 3937 | onFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 3938 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3939 | if (newFocusedWindowHandle == nullptr) { |
| 3940 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
| 3941 | if (!mFocusedWindowHandlesByDisplay.empty()) { |
| 3942 | ALOGE("But another display has a focused window:"); |
| 3943 | for (auto& it : mFocusedWindowHandlesByDisplay) { |
| 3944 | const int32_t displayId = it.first; |
| 3945 | const sp<InputWindowHandle>& windowHandle = it.second; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3946 | ALOGE("Display #%" PRId32 " has focused window: '%s'\n", displayId, |
| 3947 | windowHandle->getName().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3948 | } |
| 3949 | } |
| 3950 | } |
| 3951 | } |
| 3952 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3953 | if (DEBUG_FOCUS) { |
| 3954 | logDispatchStateLocked(); |
| 3955 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3956 | } // release lock |
| 3957 | |
| 3958 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3959 | mLooper->wake(); |
| 3960 | } |
| 3961 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3962 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3963 | if (DEBUG_FOCUS) { |
| 3964 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 3965 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3966 | |
| 3967 | bool changed; |
| 3968 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3969 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3970 | |
| 3971 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 3972 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 3973 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | if (mDispatchEnabled && !enabled) { |
| 3977 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 3978 | } |
| 3979 | |
| 3980 | mDispatchEnabled = enabled; |
| 3981 | mDispatchFrozen = frozen; |
| 3982 | changed = true; |
| 3983 | } else { |
| 3984 | changed = false; |
| 3985 | } |
| 3986 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3987 | if (DEBUG_FOCUS) { |
| 3988 | logDispatchStateLocked(); |
| 3989 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3990 | } // release lock |
| 3991 | |
| 3992 | if (changed) { |
| 3993 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3994 | mLooper->wake(); |
| 3995 | } |
| 3996 | } |
| 3997 | |
| 3998 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3999 | if (DEBUG_FOCUS) { |
| 4000 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4001 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4002 | |
| 4003 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4004 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4005 | |
| 4006 | if (mInputFilterEnabled == enabled) { |
| 4007 | return; |
| 4008 | } |
| 4009 | |
| 4010 | mInputFilterEnabled = enabled; |
| 4011 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4012 | } // release lock |
| 4013 | |
| 4014 | // Wake up poll loop since there might be work to do to drop everything. |
| 4015 | mLooper->wake(); |
| 4016 | } |
| 4017 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4018 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4019 | std::scoped_lock lock(mLock); |
| 4020 | mInTouchMode = inTouchMode; |
| 4021 | } |
| 4022 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4023 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { |
| 4024 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4025 | if (DEBUG_FOCUS) { |
| 4026 | ALOGD("Trivial transfer to same window."); |
| 4027 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4028 | return true; |
| 4029 | } |
| 4030 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4031 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4032 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4033 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4034 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 4035 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4036 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4037 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4038 | return false; |
| 4039 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4040 | if (DEBUG_FOCUS) { |
| 4041 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 4042 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 4043 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4044 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4045 | if (DEBUG_FOCUS) { |
| 4046 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 4047 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4048 | return false; |
| 4049 | } |
| 4050 | |
| 4051 | bool found = false; |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4052 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4053 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4054 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4055 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 4056 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 4057 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 4058 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4059 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4060 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4061 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4062 | int32_t newTargetFlags = oldTargetFlags & |
| 4063 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4064 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4065 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4066 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4067 | found = true; |
| 4068 | goto Found; |
| 4069 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4070 | } |
| 4071 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4072 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4073 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4074 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4075 | if (DEBUG_FOCUS) { |
| 4076 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 4077 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4078 | return false; |
| 4079 | } |
| 4080 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4081 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4082 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4083 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4084 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4085 | CancelationOptions |
| 4086 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4087 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4088 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4089 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4090 | } |
| 4091 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4092 | if (DEBUG_FOCUS) { |
| 4093 | logDispatchStateLocked(); |
| 4094 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4095 | } // release lock |
| 4096 | |
| 4097 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4098 | mLooper->wake(); |
| 4099 | return true; |
| 4100 | } |
| 4101 | |
| 4102 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4103 | if (DEBUG_FOCUS) { |
| 4104 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 4105 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4106 | |
| 4107 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 4108 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 4109 | |
| 4110 | resetKeyRepeatLocked(); |
| 4111 | releasePendingEventLocked(); |
| 4112 | drainInboundQueueLocked(); |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 4113 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4114 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 4115 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4116 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4117 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4118 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4122 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4123 | dumpDispatchStateLocked(dump); |
| 4124 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4125 | std::istringstream stream(dump); |
| 4126 | std::string line; |
| 4127 | |
| 4128 | while (std::getline(stream, line, '\n')) { |
| 4129 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4130 | } |
| 4131 | } |
| 4132 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4133 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4134 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 4135 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 4136 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4137 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4138 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4139 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4140 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4141 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4142 | const int32_t displayId = it.first; |
| 4143 | const sp<InputApplicationHandle>& applicationHandle = it.second; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4144 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4145 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4146 | displayId, applicationHandle->getName().c_str(), |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4147 | ns2ms(applicationHandle |
| 4148 | ->getDispatchingTimeout( |
| 4149 | DEFAULT_INPUT_DISPATCHING_TIMEOUT) |
| 4150 | .count())); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4151 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4152 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4153 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4154 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4155 | |
| 4156 | if (!mFocusedWindowHandlesByDisplay.empty()) { |
| 4157 | dump += StringPrintf(INDENT "FocusedWindows:\n"); |
| 4158 | for (auto& it : mFocusedWindowHandlesByDisplay) { |
| 4159 | const int32_t displayId = it.first; |
| 4160 | const sp<InputWindowHandle>& windowHandle = it.second; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4161 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", displayId, |
| 4162 | windowHandle->getName().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4163 | } |
| 4164 | } else { |
| 4165 | dump += StringPrintf(INDENT "FocusedWindows: <none>\n"); |
| 4166 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4167 | |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4168 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4169 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4170 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4171 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4172 | 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] | 4173 | state.displayId, toString(state.down), toString(state.split), |
| 4174 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4175 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4176 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4177 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4178 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4179 | dump += StringPrintf(INDENT4 |
| 4180 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 4181 | i, touchedWindow.windowHandle->getName().c_str(), |
| 4182 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4183 | } |
| 4184 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4185 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4186 | } |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4187 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4188 | dump += INDENT3 "Portal windows:\n"; |
| 4189 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4190 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4191 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, |
| 4192 | portalWindowHandle->getName().c_str()); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4193 | } |
| 4194 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4195 | } |
| 4196 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4197 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4198 | } |
| 4199 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4200 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4201 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4202 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4203 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4204 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4205 | dump += INDENT2 "Windows:\n"; |
| 4206 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4207 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4208 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4209 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4210 | dump += StringPrintf(INDENT3 "%zu: name='%s', displayId=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4211 | "portalToDisplayId=%d, paused=%s, hasFocus=%s, " |
chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4212 | "hasWallpaper=%s, visible=%s, canReceiveKeys=%s, " |
| 4213 | "flags=0x%08x, type=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4214 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4215 | "windowScale=(%f,%f), touchableRegion=", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4216 | i, windowInfo->name.c_str(), windowInfo->displayId, |
| 4217 | windowInfo->portalToDisplayId, |
| 4218 | toString(windowInfo->paused), |
| 4219 | toString(windowInfo->hasFocus), |
| 4220 | toString(windowInfo->hasWallpaper), |
| 4221 | toString(windowInfo->visible), |
| 4222 | toString(windowInfo->canReceiveKeys), |
| 4223 | windowInfo->layoutParamsFlags, |
chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4224 | windowInfo->layoutParamsType, windowInfo->frameLeft, |
| 4225 | windowInfo->frameTop, windowInfo->frameRight, |
| 4226 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
| 4227 | windowInfo->windowXScale, windowInfo->windowYScale); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4228 | dumpRegion(dump, windowInfo->touchableRegion); |
| 4229 | dump += StringPrintf(", inputFeatures=0x%08x", windowInfo->inputFeatures); |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4230 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
| 4231 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4232 | windowInfo->ownerPid, windowInfo->ownerUid, |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4233 | ns2ms(windowInfo->dispatchingTimeout)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4234 | } |
| 4235 | } else { |
| 4236 | dump += INDENT2 "Windows: <none>\n"; |
| 4237 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4238 | } |
| 4239 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4240 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4241 | } |
| 4242 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4243 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4244 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4245 | const std::vector<Monitor>& monitors = it.second; |
| 4246 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 4247 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4248 | } |
| 4249 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4250 | const std::vector<Monitor>& monitors = it.second; |
| 4251 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 4252 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4253 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4254 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4255 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | nsecs_t currentTime = now(); |
| 4259 | |
| 4260 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4261 | if (!mRecentQueue.empty()) { |
| 4262 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
| 4263 | for (EventEntry* entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4264 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4265 | entry->appendDescription(dump); |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4266 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4267 | } |
| 4268 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4269 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4270 | } |
| 4271 | |
| 4272 | // Dump event currently being dispatched. |
| 4273 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4274 | dump += INDENT "PendingEvent:\n"; |
| 4275 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4276 | mPendingEvent->appendDescription(dump); |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4277 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 4278 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4279 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4280 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4284 | if (!mInboundQueue.empty()) { |
| 4285 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
| 4286 | for (EventEntry* entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4287 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4288 | entry->appendDescription(dump); |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4289 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4290 | } |
| 4291 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4292 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4293 | } |
| 4294 | |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4295 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4296 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4297 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 4298 | const KeyReplacement& replacement = pair.first; |
| 4299 | int32_t newKeyCode = pair.second; |
| 4300 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4301 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4302 | } |
| 4303 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4304 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4305 | } |
| 4306 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4307 | if (!mConnectionsByFd.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4308 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4309 | for (const auto& pair : mConnectionsByFd) { |
| 4310 | const sp<Connection>& connection = pair.second; |
| 4311 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 4312 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4313 | pair.first, connection->getInputChannelName().c_str(), |
| 4314 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 4315 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4316 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4317 | if (!connection->outboundQueue.empty()) { |
| 4318 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 4319 | connection->outboundQueue.size()); |
| 4320 | for (DispatchEntry* entry : connection->outboundQueue) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4321 | dump.append(INDENT4); |
| 4322 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4323 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 |
| 4324 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4325 | entry->targetFlags, entry->resolvedAction, |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4326 | ns2ms(currentTime - entry->eventEntry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4327 | } |
| 4328 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4329 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4330 | } |
| 4331 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4332 | if (!connection->waitQueue.empty()) { |
| 4333 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 4334 | connection->waitQueue.size()); |
| 4335 | for (DispatchEntry* entry : connection->waitQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4336 | dump += INDENT4; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4337 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4338 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, " |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4339 | "age=%" PRId64 "ms, wait=%" PRId64 "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4340 | entry->targetFlags, entry->resolvedAction, |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4341 | ns2ms(currentTime - entry->eventEntry->eventTime), |
| 4342 | ns2ms(currentTime - entry->deliveryTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4343 | } |
| 4344 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4345 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4346 | } |
| 4347 | } |
| 4348 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4349 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4353 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 4354 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4355 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4356 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4357 | } |
| 4358 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4359 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4360 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 4361 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 4362 | ns2ms(mConfig.keyRepeatTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4363 | } |
| 4364 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4365 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 4366 | const size_t numMonitors = monitors.size(); |
| 4367 | for (size_t i = 0; i < numMonitors; i++) { |
| 4368 | const Monitor& monitor = monitors[i]; |
| 4369 | const sp<InputChannel>& channel = monitor.inputChannel; |
| 4370 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 4371 | dump += "\n"; |
| 4372 | } |
| 4373 | } |
| 4374 | |
Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 4375 | status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4376 | #if DEBUG_REGISTRATION |
Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 4377 | ALOGD("channel '%s' ~ registerInputChannel", inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4378 | #endif |
| 4379 | |
| 4380 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4381 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4382 | sp<Connection> existingConnection = getConnectionLocked(inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4383 | if (existingConnection != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4384 | ALOGW("Attempted to register already registered input channel '%s'", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4385 | inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4386 | return BAD_VALUE; |
| 4387 | } |
| 4388 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4389 | sp<Connection> connection = new Connection(inputChannel, false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4390 | |
| 4391 | int fd = inputChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4392 | mConnectionsByFd[fd] = connection; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4393 | mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4394 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4395 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
| 4396 | } // release lock |
| 4397 | |
| 4398 | // Wake the looper because some connections have changed. |
| 4399 | mLooper->wake(); |
| 4400 | return OK; |
| 4401 | } |
| 4402 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4403 | status_t InputDispatcher::registerInputMonitor(const sp<InputChannel>& inputChannel, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4404 | int32_t displayId, bool isGestureMonitor) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4405 | { // acquire lock |
| 4406 | std::scoped_lock _l(mLock); |
| 4407 | |
| 4408 | if (displayId < 0) { |
| 4409 | ALOGW("Attempted to register input monitor without a specified display."); |
| 4410 | return BAD_VALUE; |
| 4411 | } |
| 4412 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4413 | if (inputChannel->getConnectionToken() == nullptr) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4414 | ALOGW("Attempted to register input monitor without an identifying token."); |
| 4415 | return BAD_VALUE; |
| 4416 | } |
| 4417 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4418 | sp<Connection> connection = new Connection(inputChannel, true /*monitor*/, mIdGenerator); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4419 | |
| 4420 | const int fd = inputChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4421 | mConnectionsByFd[fd] = connection; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4422 | mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4423 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4424 | auto& monitorsByDisplay = |
| 4425 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4426 | monitorsByDisplay[displayId].emplace_back(inputChannel); |
| 4427 | |
| 4428 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4429 | } |
| 4430 | // Wake the looper because some connections have changed. |
| 4431 | mLooper->wake(); |
| 4432 | return OK; |
| 4433 | } |
| 4434 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4435 | status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) { |
| 4436 | #if DEBUG_REGISTRATION |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4437 | ALOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4438 | #endif |
| 4439 | |
| 4440 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4441 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4442 | |
| 4443 | status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/); |
| 4444 | if (status) { |
| 4445 | return status; |
| 4446 | } |
| 4447 | } // release lock |
| 4448 | |
| 4449 | // Wake the poll loop because removing the connection may have changed the current |
| 4450 | // synchronization state. |
| 4451 | mLooper->wake(); |
| 4452 | return OK; |
| 4453 | } |
| 4454 | |
| 4455 | status_t InputDispatcher::unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4456 | bool notify) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4457 | sp<Connection> connection = getConnectionLocked(inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4458 | if (connection == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4459 | ALOGW("Attempted to unregister already unregistered input channel '%s'", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4460 | inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4461 | return BAD_VALUE; |
| 4462 | } |
| 4463 | |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4464 | removeConnectionLocked(connection); |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4465 | mInputChannelsByToken.erase(inputChannel->getConnectionToken()); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4466 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4467 | if (connection->monitor) { |
| 4468 | removeMonitorChannelLocked(inputChannel); |
| 4469 | } |
| 4470 | |
| 4471 | mLooper->removeFd(inputChannel->getFd()); |
| 4472 | |
| 4473 | nsecs_t currentTime = now(); |
| 4474 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 4475 | |
| 4476 | connection->status = Connection::STATUS_ZOMBIE; |
| 4477 | return OK; |
| 4478 | } |
| 4479 | |
| 4480 | void InputDispatcher::removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4481 | removeMonitorChannelLocked(inputChannel, mGlobalMonitorsByDisplay); |
| 4482 | removeMonitorChannelLocked(inputChannel, mGestureMonitorsByDisplay); |
| 4483 | } |
| 4484 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4485 | void InputDispatcher::removeMonitorChannelLocked( |
| 4486 | const sp<InputChannel>& inputChannel, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4487 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4488 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4489 | std::vector<Monitor>& monitors = it->second; |
| 4490 | const size_t numMonitors = monitors.size(); |
| 4491 | for (size_t i = 0; i < numMonitors; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4492 | if (monitors[i].inputChannel == inputChannel) { |
| 4493 | monitors.erase(monitors.begin() + i); |
| 4494 | break; |
| 4495 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4496 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4497 | if (monitors.empty()) { |
| 4498 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4499 | } else { |
| 4500 | ++it; |
| 4501 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4502 | } |
| 4503 | } |
| 4504 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4505 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 4506 | { // acquire lock |
| 4507 | std::scoped_lock _l(mLock); |
| 4508 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 4509 | |
| 4510 | if (!foundDisplayId) { |
| 4511 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 4512 | return BAD_VALUE; |
| 4513 | } |
| 4514 | int32_t displayId = foundDisplayId.value(); |
| 4515 | |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4516 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4517 | mTouchStatesByDisplay.find(displayId); |
| 4518 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4519 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 4520 | return BAD_VALUE; |
| 4521 | } |
| 4522 | |
Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4523 | TouchState& state = stateIt->second; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4524 | std::optional<int32_t> foundDeviceId; |
| 4525 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4526 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4527 | foundDeviceId = state.deviceId; |
| 4528 | } |
| 4529 | } |
| 4530 | if (!foundDeviceId || !state.down) { |
| 4531 | 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] | 4532 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4533 | return BAD_VALUE; |
| 4534 | } |
| 4535 | int32_t deviceId = foundDeviceId.value(); |
| 4536 | |
| 4537 | // Send cancel events to all the input channels we're stealing from. |
| 4538 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4539 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4540 | options.deviceId = deviceId; |
| 4541 | options.displayId = displayId; |
| 4542 | for (const TouchedWindow& window : state.windows) { |
| 4543 | sp<InputChannel> channel = getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4544 | if (channel != nullptr) { |
| 4545 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 4546 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4547 | } |
| 4548 | // Then clear the current touch state so we stop dispatching to them as well. |
| 4549 | state.filterNonMonitors(); |
| 4550 | } |
| 4551 | return OK; |
| 4552 | } |
| 4553 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4554 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 4555 | const sp<IBinder>& token) { |
| 4556 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 4557 | const std::vector<Monitor>& monitors = it.second; |
| 4558 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4559 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4560 | return it.first; |
| 4561 | } |
| 4562 | } |
| 4563 | } |
| 4564 | return std::nullopt; |
| 4565 | } |
| 4566 | |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4567 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4568 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4569 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4570 | } |
| 4571 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4572 | for (const auto& pair : mConnectionsByFd) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4573 | const sp<Connection>& connection = pair.second; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4574 | if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4575 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4576 | } |
| 4577 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 4578 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4579 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4580 | } |
| 4581 | |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4582 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 4583 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4584 | removeByValue(mConnectionsByFd, connection); |
| 4585 | } |
| 4586 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4587 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 4588 | const sp<Connection>& connection, uint32_t seq, |
| 4589 | bool handled) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4590 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4591 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4592 | commandEntry->connection = connection; |
| 4593 | commandEntry->eventTime = currentTime; |
| 4594 | commandEntry->seq = seq; |
| 4595 | commandEntry->handled = handled; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4596 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4597 | } |
| 4598 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4599 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 4600 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4601 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4602 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4603 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4604 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4605 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4606 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4607 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4608 | } |
| 4609 | |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4610 | void InputDispatcher::onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4611 | const sp<InputWindowHandle>& newFocus) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4612 | sp<IBinder> oldToken = oldFocus != nullptr ? oldFocus->getToken() : nullptr; |
| 4613 | sp<IBinder> newToken = newFocus != nullptr ? newFocus->getToken() : nullptr; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4614 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4615 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4616 | commandEntry->oldToken = oldToken; |
| 4617 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4618 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4619 | } |
| 4620 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 4621 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 4622 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 4623 | // is already healthy again. Don't raise ANR in this situation |
| 4624 | if (connection->waitQueue.empty()) { |
| 4625 | ALOGI("Not raising ANR because the connection %s has recovered", |
| 4626 | connection->inputChannel->getName().c_str()); |
| 4627 | return; |
| 4628 | } |
| 4629 | /** |
| 4630 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 4631 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 4632 | * has changed. This could cause newer entries to time out before the already dispatched |
| 4633 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 4634 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 4635 | * most useful. |
| 4636 | */ |
| 4637 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
| 4638 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 4639 | std::string reason = |
| 4640 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
| 4641 | connection->inputChannel->getName().c_str(), |
| 4642 | ns2ms(currentWait), |
| 4643 | oldestEntry->eventEntry->getDescription().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4644 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 4645 | updateLastAnrStateLocked(getWindowHandleLocked(connection->inputChannel->getConnectionToken()), |
| 4646 | reason); |
| 4647 | |
| 4648 | std::unique_ptr<CommandEntry> commandEntry = |
| 4649 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); |
| 4650 | commandEntry->inputApplicationHandle = nullptr; |
| 4651 | commandEntry->inputChannel = connection->inputChannel; |
| 4652 | commandEntry->reason = std::move(reason); |
| 4653 | postCommandLocked(std::move(commandEntry)); |
| 4654 | } |
| 4655 | |
| 4656 | void InputDispatcher::onAnrLocked(const sp<InputApplicationHandle>& application) { |
| 4657 | std::string reason = android::base::StringPrintf("%s does not have a focused window", |
| 4658 | application->getName().c_str()); |
| 4659 | |
| 4660 | updateLastAnrStateLocked(application, reason); |
| 4661 | |
| 4662 | std::unique_ptr<CommandEntry> commandEntry = |
| 4663 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); |
| 4664 | commandEntry->inputApplicationHandle = application; |
| 4665 | commandEntry->inputChannel = nullptr; |
| 4666 | commandEntry->reason = std::move(reason); |
| 4667 | postCommandLocked(std::move(commandEntry)); |
| 4668 | } |
| 4669 | |
| 4670 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, |
| 4671 | const std::string& reason) { |
| 4672 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 4673 | updateLastAnrStateLocked(windowLabel, reason); |
| 4674 | } |
| 4675 | |
| 4676 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputApplicationHandle>& application, |
| 4677 | const std::string& reason) { |
| 4678 | const std::string windowLabel = getApplicationWindowLabel(application, nullptr); |
| 4679 | updateLastAnrStateLocked(windowLabel, reason); |
| 4680 | } |
| 4681 | |
| 4682 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 4683 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4684 | // 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] | 4685 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4686 | struct tm tm; |
| 4687 | localtime_r(&t, &tm); |
| 4688 | char timestr[64]; |
| 4689 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4690 | mLastAnrState.clear(); |
| 4691 | mLastAnrState += INDENT "ANR:\n"; |
| 4692 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 4693 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 4694 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4695 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4696 | } |
| 4697 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4698 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4699 | mLock.unlock(); |
| 4700 | |
| 4701 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 4702 | |
| 4703 | mLock.lock(); |
| 4704 | } |
| 4705 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4706 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4707 | sp<Connection> connection = commandEntry->connection; |
| 4708 | |
| 4709 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 4710 | mLock.unlock(); |
| 4711 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4712 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4713 | |
| 4714 | mLock.lock(); |
| 4715 | } |
| 4716 | } |
| 4717 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4718 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4719 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 4720 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4721 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4722 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4723 | mLock.lock(); |
| 4724 | } |
| 4725 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4726 | void InputDispatcher::doNotifyAnrLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4727 | sp<IBinder> token = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4728 | commandEntry->inputChannel ? commandEntry->inputChannel->getConnectionToken() : nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4729 | mLock.unlock(); |
| 4730 | |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4731 | const nsecs_t timeoutExtension = |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4732 | mPolicy->notifyAnr(commandEntry->inputApplicationHandle, token, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4733 | |
| 4734 | mLock.lock(); |
| 4735 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 4736 | if (timeoutExtension > 0) { |
| 4737 | extendAnrTimeoutsLocked(commandEntry->inputApplicationHandle, token, timeoutExtension); |
| 4738 | } else { |
| 4739 | // stop waking up for events in this connection, it is already not responding |
| 4740 | sp<Connection> connection = getConnectionLocked(token); |
| 4741 | if (connection == nullptr) { |
| 4742 | return; |
| 4743 | } |
| 4744 | cancelEventsForAnrLocked(connection); |
| 4745 | } |
| 4746 | } |
| 4747 | |
| 4748 | void InputDispatcher::extendAnrTimeoutsLocked(const sp<InputApplicationHandle>& application, |
| 4749 | const sp<IBinder>& connectionToken, |
| 4750 | nsecs_t timeoutExtension) { |
| 4751 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 4752 | if (connection == nullptr) { |
| 4753 | if (mNoFocusedWindowTimeoutTime.has_value() && application != nullptr) { |
| 4754 | // Maybe ANR happened because there's no focused window? |
| 4755 | mNoFocusedWindowTimeoutTime = now() + timeoutExtension; |
| 4756 | mAwaitedFocusedApplication = application; |
| 4757 | } else { |
| 4758 | // It's also possible that the connection already disappeared. No action necessary. |
| 4759 | } |
| 4760 | return; |
| 4761 | } |
| 4762 | |
| 4763 | ALOGI("Raised ANR, but the policy wants to keep waiting on %s for %" PRId64 "ms longer", |
| 4764 | connection->inputChannel->getName().c_str(), ns2ms(timeoutExtension)); |
| 4765 | |
| 4766 | connection->responsive = true; |
| 4767 | const nsecs_t newTimeout = now() + timeoutExtension; |
| 4768 | for (DispatchEntry* entry : connection->waitQueue) { |
| 4769 | if (newTimeout >= entry->timeoutTime) { |
| 4770 | // Already removed old entries when connection was marked unresponsive |
| 4771 | entry->timeoutTime = newTimeout; |
| 4772 | mAnrTracker.insert(entry->timeoutTime, connectionToken); |
| 4773 | } |
| 4774 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4775 | } |
| 4776 | |
| 4777 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 4778 | CommandEntry* commandEntry) { |
| 4779 | KeyEntry* entry = commandEntry->keyEntry; |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4780 | KeyEvent event = createKeyEvent(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4781 | |
| 4782 | mLock.unlock(); |
| 4783 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4784 | android::base::Timer t; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4785 | sp<IBinder> token = commandEntry->inputChannel != nullptr |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4786 | ? commandEntry->inputChannel->getConnectionToken() |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4787 | : nullptr; |
| 4788 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry->policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4789 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4790 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4791 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4792 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4793 | |
| 4794 | mLock.lock(); |
| 4795 | |
| 4796 | if (delay < 0) { |
| 4797 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
| 4798 | } else if (!delay) { |
| 4799 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 4800 | } else { |
| 4801 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 4802 | entry->interceptKeyWakeupTime = now() + delay; |
| 4803 | } |
| 4804 | entry->release(); |
| 4805 | } |
| 4806 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4807 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 4808 | mLock.unlock(); |
| 4809 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 4810 | mLock.lock(); |
| 4811 | } |
| 4812 | |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 4813 | /** |
| 4814 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 4815 | * current time. |
| 4816 | */ |
| 4817 | static bool isConnectionResponsive(const Connection& connection) { |
| 4818 | const nsecs_t currentTime = now(); |
| 4819 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 4820 | if (entry->timeoutTime < currentTime) { |
| 4821 | return false; |
| 4822 | } |
| 4823 | } |
| 4824 | return true; |
| 4825 | } |
| 4826 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4827 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4828 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4829 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4830 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4831 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4832 | |
| 4833 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4834 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4835 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4836 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4837 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4838 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4839 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4840 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4841 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 4842 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4843 | } |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 4844 | reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4845 | |
| 4846 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4847 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4848 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); |
| 4849 | restartEvent = |
| 4850 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4851 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4852 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); |
| 4853 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 4854 | handled); |
| 4855 | } else { |
| 4856 | restartEvent = false; |
| 4857 | } |
| 4858 | |
| 4859 | // Dequeue the event and start the next cycle. |
Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4860 | // Because the lock might have been released, it is possible that the |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4861 | // contents of the wait queue to have been drained, so we need to double-check |
| 4862 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4863 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 4864 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 4865 | dispatchEntry = *dispatchEntryIt; |
| 4866 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame^] | 4867 | mAnrTracker.erase(dispatchEntry->timeoutTime, |
| 4868 | connection->inputChannel->getConnectionToken()); |
| 4869 | if (!connection->responsive) { |
| 4870 | connection->responsive = isConnectionResponsive(*connection); |
| 4871 | } |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4872 | traceWaitQueueLength(connection); |
| 4873 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4874 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4875 | traceOutboundQueueLength(connection); |
| 4876 | } else { |
| 4877 | releaseDispatchEntry(dispatchEntry); |
| 4878 | } |
| 4879 | } |
| 4880 | |
| 4881 | // Start the next dispatch cycle for this connection. |
| 4882 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4883 | } |
| 4884 | |
| 4885 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4886 | DispatchEntry* dispatchEntry, |
| 4887 | KeyEntry* keyEntry, bool handled) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4888 | if (keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4889 | if (!handled) { |
| 4890 | // Report the key as unhandled, since the fallback was not handled. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4891 | mReporter->reportUnhandledKey(keyEntry->id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4892 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4893 | return false; |
| 4894 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4895 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4896 | // Get the fallback key state. |
| 4897 | // Clear it out after dispatching the UP. |
| 4898 | int32_t originalKeyCode = keyEntry->keyCode; |
| 4899 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
| 4900 | if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
| 4901 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4902 | } |
| 4903 | |
| 4904 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 4905 | // If the application handles the original key for which we previously |
| 4906 | // generated a fallback or if the window is not a foreground window, |
| 4907 | // then cancel the associated fallback key, if any. |
| 4908 | if (fallbackKeyCode != -1) { |
| 4909 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4910 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4911 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4912 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4913 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, |
| 4914 | keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4915 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4916 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4917 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4918 | |
| 4919 | mLock.unlock(); |
| 4920 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4921 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4922 | keyEntry->policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4923 | |
| 4924 | mLock.lock(); |
| 4925 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4926 | // Cancel the fallback key. |
| 4927 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4928 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4929 | "application handled the original non-fallback key " |
| 4930 | "or is no longer a foreground target, " |
| 4931 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4932 | options.keyCode = fallbackKeyCode; |
| 4933 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4934 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4935 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4936 | } |
| 4937 | } else { |
| 4938 | // If the application did not handle a non-fallback key, first check |
| 4939 | // that we are in a good state to perform unhandled key event processing |
| 4940 | // Then ask the policy what to do with it. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4941 | bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN && keyEntry->repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4942 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4943 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4944 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4945 | "since this is not an initial down. " |
| 4946 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4947 | originalKeyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4948 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4949 | return false; |
| 4950 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4951 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4952 | // Dispatch the unhandled key to the policy. |
| 4953 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4954 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4955 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4956 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4957 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4958 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4959 | |
| 4960 | mLock.unlock(); |
| 4961 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4962 | bool fallback = |
| 4963 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 4964 | &event, keyEntry->policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4965 | |
| 4966 | mLock.lock(); |
| 4967 | |
| 4968 | if (connection->status != Connection::STATUS_NORMAL) { |
| 4969 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4970 | return false; |
| 4971 | } |
| 4972 | |
| 4973 | // Latch the fallback keycode for this key on an initial down. |
| 4974 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 4975 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4976 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4977 | fallbackKeyCode = event.getKeyCode(); |
| 4978 | } else { |
| 4979 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 4980 | } |
| 4981 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 4982 | } |
| 4983 | |
| 4984 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 4985 | |
| 4986 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 4987 | // We will continue to dispatch the key to the policy but we will no |
| 4988 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4989 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 4990 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4991 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4992 | if (fallback) { |
| 4993 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4994 | "as a fallback for %d, but on the DOWN it had requested " |
| 4995 | "to send %d instead. Fallback canceled.", |
| 4996 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4997 | } else { |
| 4998 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4999 | "but on the DOWN it had requested to send %d. " |
| 5000 | "Fallback canceled.", |
| 5001 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5002 | } |
| 5003 | #endif |
| 5004 | |
| 5005 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 5006 | "canceling fallback, policy no longer desires it"); |
| 5007 | options.keyCode = fallbackKeyCode; |
| 5008 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 5009 | |
| 5010 | fallback = false; |
| 5011 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5012 | if (keyEntry->action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5013 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5014 | } |
| 5015 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5016 | |
| 5017 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5018 | { |
| 5019 | std::string msg; |
| 5020 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 5021 | connection->inputState.getFallbackKeys(); |
| 5022 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5023 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5024 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5025 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5026 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5027 | } |
| 5028 | #endif |
| 5029 | |
| 5030 | if (fallback) { |
| 5031 | // Restart the dispatch cycle using the fallback key. |
| 5032 | keyEntry->eventTime = event.getEventTime(); |
| 5033 | keyEntry->deviceId = event.getDeviceId(); |
| 5034 | keyEntry->source = event.getSource(); |
| 5035 | keyEntry->displayId = event.getDisplayId(); |
| 5036 | keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 5037 | keyEntry->keyCode = fallbackKeyCode; |
| 5038 | keyEntry->scanCode = event.getScanCode(); |
| 5039 | keyEntry->metaState = event.getMetaState(); |
| 5040 | keyEntry->repeatCount = event.getRepeatCount(); |
| 5041 | keyEntry->downTime = event.getDownTime(); |
| 5042 | keyEntry->syntheticRepeat = false; |
| 5043 | |
| 5044 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5045 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5046 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 5047 | originalKeyCode, fallbackKeyCode, keyEntry->metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5048 | #endif |
| 5049 | return true; // restart the event |
| 5050 | } else { |
| 5051 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5052 | ALOGD("Unhandled key event: No fallback key."); |
| 5053 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5054 | |
| 5055 | // Report the key as unhandled, since there is no fallback key. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5056 | mReporter->reportUnhandledKey(keyEntry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5057 | } |
| 5058 | } |
| 5059 | return false; |
| 5060 | } |
| 5061 | |
| 5062 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5063 | DispatchEntry* dispatchEntry, |
| 5064 | MotionEntry* motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5065 | return false; |
| 5066 | } |
| 5067 | |
| 5068 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 5069 | mLock.unlock(); |
| 5070 | |
| 5071 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); |
| 5072 | |
| 5073 | mLock.lock(); |
| 5074 | } |
| 5075 | |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5076 | KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) { |
| 5077 | KeyEvent event; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5078 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 5079 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 5080 | entry.repeatCount, entry.downTime, entry.eventTime); |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5081 | return event; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5082 | } |
| 5083 | |
Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5084 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, |
| 5085 | const Connection& connection, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5086 | // TODO Write some statistics about how long we spend waiting. |
| 5087 | } |
| 5088 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5089 | /** |
| 5090 | * Report the touch event latency to the statsd server. |
| 5091 | * Input events are reported for statistics if: |
| 5092 | * - This is a touchscreen event |
| 5093 | * - InputFilter is not enabled |
| 5094 | * - Event is not injected or synthesized |
| 5095 | * |
| 5096 | * Statistics should be reported before calling addValue, to prevent a fresh new sample |
| 5097 | * from getting aggregated with the "old" data. |
| 5098 | */ |
| 5099 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) |
| 5100 | REQUIRES(mLock) { |
| 5101 | const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && |
| 5102 | !(motionEntry.isSynthesized()) && !mInputFilterEnabled; |
| 5103 | if (!reportForStatistics) { |
| 5104 | return; |
| 5105 | } |
| 5106 | |
| 5107 | if (mTouchStatistics.shouldReport()) { |
| 5108 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), |
| 5109 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), |
| 5110 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); |
| 5111 | mTouchStatistics.reset(); |
| 5112 | } |
| 5113 | const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); |
| 5114 | mTouchStatistics.addValue(latencyMicros); |
| 5115 | } |
| 5116 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5117 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 5118 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5119 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5120 | } |
| 5121 | } |
| 5122 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5123 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5124 | if (ATRACE_ENABLED()) { |
| 5125 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5126 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5127 | ATRACE_INT(counterName, connection->outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5128 | } |
| 5129 | } |
| 5130 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5131 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5132 | if (ATRACE_ENABLED()) { |
| 5133 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5134 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5135 | ATRACE_INT(counterName, connection->waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5136 | } |
| 5137 | } |
| 5138 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5139 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5140 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5141 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5142 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5143 | dumpDispatchStateLocked(dump); |
| 5144 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5145 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5146 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5147 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5148 | } |
| 5149 | } |
| 5150 | |
| 5151 | void InputDispatcher::monitor() { |
| 5152 | // 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] | 5153 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5154 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5155 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5156 | } |
| 5157 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5158 | /** |
| 5159 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 5160 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 5161 | * this method can be safely called from any thread, as long as you've ensured that |
| 5162 | * the work you are interested in completing has already been queued. |
| 5163 | */ |
| 5164 | bool InputDispatcher::waitForIdle() { |
| 5165 | /** |
| 5166 | * Timeout should represent the longest possible time that a device might spend processing |
| 5167 | * events and commands. |
| 5168 | */ |
| 5169 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 5170 | std::unique_lock lock(mLock); |
| 5171 | mLooper->wake(); |
| 5172 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 5173 | return result == std::cv_status::no_timeout; |
| 5174 | } |
| 5175 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5176 | } // namespace android::inputdispatcher |